authelia/test/features/step_definitions/hooks.ts

174 lines
5.8 KiB
TypeScript
Raw Normal View History

2018-08-10 03:24:02 +07:00
import {setDefaultTimeout, After, Before, BeforeAll, AfterAll} from "cucumber";
import fs = require("fs");
import BluebirdPromise = require("bluebird");
import ChildProcess = require("child_process");
import { UserDataStore } from "../../../server/src/lib/storage/UserDataStore";
import { CollectionFactoryFactory } from "../../../server/src/lib/storage/CollectionFactoryFactory";
import { IMongoClient } from "../../../server/src/lib/connectors/mongo/IMongoClient";
import { TotpHandler } from "../../../server/src/lib/authentication/totp/TotpHandler";
import Speakeasy = require("speakeasy");
2018-03-29 05:04:59 +07:00
import Request = require("request-promise");
2018-05-17 05:06:07 +07:00
import { TOTPSecret } from "../../../server/types/TOTPSecret";
2018-08-10 03:24:02 +07:00
import Environment = require("../../environment");
import { MongoClient } from "../../../server/src/lib/connectors/mongo/MongoClient";
import { GlobalLogger } from "../../../server/src/lib/logging/GlobalLogger";
import { GlobalLoggerStub } from "../../../server/src/lib/logging/GlobalLoggerStub.spec";
2018-08-10 03:24:02 +07:00
setDefaultTimeout(30 * 1000);
2018-03-29 05:04:59 +07:00
const exec = BluebirdPromise.promisify<any, any>(ChildProcess.exec);
2018-08-10 03:24:02 +07:00
const includes = [
"docker-compose.yml",
"example/compose/docker-compose.base.yml",
"example/compose/mongo/docker-compose.yml",
"example/compose/redis/docker-compose.yml",
"example/compose/nginx/backend/docker-compose.yml",
"example/compose/nginx/portal/docker-compose.yml",
"example/compose/smtp/docker-compose.yml",
"example/compose/httpbin/docker-compose.yml",
"example/compose/ldap/docker-compose.yml"
]
const environment = new Environment.Environment(includes);
2018-08-10 03:24:02 +07:00
BeforeAll(function() {
return environment.setup(10000);
2018-08-10 03:24:02 +07:00
});
AfterAll(function() {
return environment.cleanup()
2018-08-10 03:24:02 +07:00
});
2018-03-29 05:04:59 +07:00
Before(function () {
this.jar = Request.jar();
})
2018-03-29 05:04:59 +07:00
After(function () {
return this.driver.quit();
});
2018-03-29 05:04:59 +07:00
function createRegulationConfiguration(): BluebirdPromise<void> {
return exec("\
cat config.template.yml | \
sed 's/find_time: [0-9]\\+/find_time: 15/' | \
sed 's/ban_time: [0-9]\\+/ban_time: 4/' > config.test.yml \
");
}
function createInactivityConfiguration(): BluebirdPromise<void> {
return exec("\
cat config.template.yml | \
sed 's/expiration: [0-9]\\+/expiration: 10000/' | \
sed 's/inactivity: [0-9]\\+/inactivity: 5000/' > config.test.yml \
");
}
function createSingleFactorConfiguration(): BluebirdPromise<void> {
return exec("\
cat config.template.yml | \
sed 's/default_method: two_factor/default_method: single_factor/' > config.test.yml \
");
}
function createCustomTotpIssuerConfiguration(): BluebirdPromise<void> {
return exec("\
cat config.template.yml | \
sed 's/issuer: authelia.com/issuer: custom.com/' > config.test.yml \
2018-03-29 05:04:59 +07:00
");
}
function declareNeedsConfiguration(tag: string, cb: () => BluebirdPromise<void>) {
Before({ tags: "@needs-" + tag + "-config", timeout: 20 * 1000 }, function () {
return cb()
.then(function () {
return exec("./scripts/example-commit/dc-example.sh -f " +
"./example/compose/authelia/docker-compose.test.yml up -d authelia &&" +
" sleep 3");
})
2018-03-29 05:04:59 +07:00
});
After({ tags: "@needs-" + tag + "-config", timeout: 20 * 1000 }, function () {
return exec("rm config.test.yml")
.then(function () {
return exec("./scripts/example-commit/dc-example.sh up -d authelia && sleep 3");
});
2018-03-29 05:04:59 +07:00
});
}
declareNeedsConfiguration("regulation", createRegulationConfiguration);
declareNeedsConfiguration("inactivity", createInactivityConfiguration);
declareNeedsConfiguration("single_factor", createSingleFactorConfiguration);
declareNeedsConfiguration("totp_issuer", createCustomTotpIssuerConfiguration);
function registerUser(context: any, username: string) {
2018-05-17 05:06:07 +07:00
let secret: TOTPSecret;
const mongoClient = new MongoClient({
url: "mongodb://localhost:27017",
database: "authelia",
auth: {
username: "authelia",
password: "authelia"
}
}, new GlobalLoggerStub());
const collectionFactory = CollectionFactoryFactory.createMongo(mongoClient);
const userDataStore = new UserDataStore(collectionFactory);
const generator = new TotpHandler(Speakeasy);
secret = generator.generate("user", "authelia.com");
return userDataStore.saveTOTPSecret(username, secret)
2018-03-29 05:04:59 +07:00
.then(function () {
context.totpSecrets["REGISTERED"] = secret.base32;
return mongoClient.close();
});
2018-03-29 05:04:59 +07:00
}
2018-03-29 05:04:59 +07:00
function declareNeedRegisteredUserHooks(username: string) {
Before({ tags: "@need-registered-user-" + username, timeout: 15 * 1000 }, function () {
return registerUser(this, username);
});
2018-03-29 05:04:59 +07:00
After({ tags: "@need-registered-user-" + username, timeout: 15 * 1000 }, function () {
this.totpSecrets["REGISTERED"] = undefined;
return BluebirdPromise.resolve();
});
}
function needAuthenticatedUser(context: any, username: string): BluebirdPromise<void> {
return context.visit("https://login.example.com:8080/logout")
.then(function () {
return context.visit("https://login.example.com:8080/");
})
.then(function () {
return registerUser(context, username);
})
.then(function () {
return context.loginWithUserPassword(username, "password");
})
.then(function () {
return context.useTotpTokenHandle("REGISTERED");
})
.then(function () {
return context.clickOnButton("Sign in");
});
2018-03-29 05:04:59 +07:00
}
2018-03-29 05:04:59 +07:00
function declareNeedAuthenticatedUserHooks(username: string) {
Before({ tags: "@need-authenticated-user-" + username, timeout: 15 * 1000 }, function () {
return needAuthenticatedUser(this, username);
});
After({ tags: "@need-authenticated-user-" + username, timeout: 15 * 1000 }, function () {
this.totpSecrets["REGISTERED"] = undefined;
return BluebirdPromise.resolve();
});
}
2018-03-29 05:04:59 +07:00
function declareHooksForUser(username: string) {
declareNeedRegisteredUserHooks(username);
declareNeedAuthenticatedUserHooks(username);
}
2018-03-29 05:04:59 +07:00
const users = ["harry", "john", "bob", "blackhat"];
users.forEach(declareHooksForUser);