keycloak-testcontainer
Version:
Run a Keycloak testcontainer with node.js
98 lines (97 loc) • 3.87 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { GenericContainer, Wait } from 'testcontainers';
import { Keycloak } from '../keycloak.js';
import { StartedKeycloakContainer } from './started-container.js';
import { Configuration } from './configuration.js';
export class KeycloakContainer extends GenericContainer {
constructor(options) {
var _a, _b;
const registry = (_a = options === null || options === void 0 ? void 0 : options.registry) !== null && _a !== void 0 ? _a : 'quay.io/keycloak/keycloak';
const tag = (_b = options === null || options === void 0 ? void 0 : options.tag) !== null && _b !== void 0 ? _b : 'latest';
const imageName = `${registry}:${tag}`;
super(imageName);
this.configuration = new Configuration();
}
getImageName() {
return this.imageName;
}
withHostname(hostname) {
this.configuration.withHostName(hostname);
return this;
}
withHealth() {
this.configuration.withHealth();
return this;
}
withRealmImport(source) {
this.withCopyDirectoriesToContainer([{
source,
target: Keycloak.IMPORT_PATH
}]);
this.configuration.withRealmImport();
return this;
}
withProviders(source) {
this.withCopyDirectoriesToContainer([{
source,
target: Keycloak.PROVIDERS_PATH
}]);
return this;
}
withDatabase(options) {
this.configuration.withDatabase(options);
return this;
}
withMetrics() {
this.configuration.withMetrics();
return this;
}
withFeatures(features) {
this.configuration.withFeatures(features);
return this;
}
withDisabledFeatures(disabledFeatures) {
this.configuration.withDisabledFeatures(disabledFeatures);
return this;
}
withAdminUser(adminUser) {
this.configuration.withAdminUser(adminUser);
return this;
}
withManagementPort(managementPort) {
this.configuration.withManagementPort(managementPort);
return this;
}
withManagementPath(managementPath) {
this.configuration.withManagementPath(managementPath);
return this;
}
withHostnamePath(hostnamePath) {
this.configuration.withHostnamePath(hostnamePath);
return this;
}
start() {
const _super = Object.create(null, {
start: { get: () => super.start }
});
return __awaiter(this, void 0, void 0, function* () {
const hostNamePath = this.configuration.getHostNamePath();
const endpoint = hostNamePath === '/'
? '/realms/master'
: `${hostNamePath}/realms/master`;
this.withExposedPorts(...this.configuration.getPorts());
this.withWaitStrategy(Wait.forHttp(endpoint, this.configuration.getPorts()[0]));
this.withCommand(this.configuration.getCommands());
this.withEnvironment(this.configuration.getEnvironmentConfiguration());
return new StartedKeycloakContainer(yield _super.start.call(this), this.configuration.getAdminUser());
});
}
}