authelia/test/server/requests.ts

173 lines
5.7 KiB
TypeScript
Raw Normal View History

2017-05-21 20:45:54 +00:00
import BluebirdPromise = require("bluebird");
import request = require("request");
import assert = require("assert");
import express = require("express");
import nodemailer = require("nodemailer");
import Endpoints = require("../../src/server/endpoints");
2017-05-21 20:45:54 +00:00
import NodemailerMock = require("./mocks/nodemailer");
const requestAsync: typeof request = BluebirdPromise.promisifyAll(request) as typeof request;
2017-05-21 20:45:54 +00:00
export = function (port: number) {
const PORT = port;
const BASE_URL = "http://localhost:" + PORT;
function execute_reset_password(jar: request.CookieJar, transporter: NodemailerMock.NodemailerTransporterMock, user: string, new_password: string) {
return requestAsync.getAsync({
url: BASE_URL + Endpoints.RESET_PASSWORD_IDENTITY_START_GET,
2017-05-21 20:45:54 +00:00
jar: jar,
qs: { userid: user }
2017-05-21 20:45:54 +00:00
})
.then(function (res: request.RequestResponse) {
assert.equal(res.statusCode, 200);
2017-05-21 20:45:54 +00:00
const html_content = transporter.sendMail.getCall(0).args[0].html;
const regexp = /identity_token=([a-zA-Z0-9]+)/;
const token = regexp.exec(html_content)[1];
// console.log(html_content, token);
return requestAsync.getAsync({
url: BASE_URL + Endpoints.RESET_PASSWORD_IDENTITY_FINISH_GET + "?identity_token=" + token,
2017-05-21 20:45:54 +00:00
jar: jar
});
})
.then(function (res: request.RequestResponse) {
assert.equal(res.statusCode, 200);
return requestAsync.postAsync({
url: BASE_URL + Endpoints.RESET_PASSWORD_FORM_POST,
2017-05-21 20:45:54 +00:00
jar: jar,
form: {
password: new_password
}
});
});
}
function execute_register_totp(jar: request.CookieJar, transporter: NodemailerMock.NodemailerTransporterMock) {
return requestAsync.getAsync({
url: BASE_URL + Endpoints.SECOND_FACTOR_TOTP_IDENTITY_START_GET,
2017-05-21 20:45:54 +00:00
jar: jar
})
.then(function (res: request.RequestResponse) {
assert.equal(res.statusCode, 200);
2017-05-21 20:45:54 +00:00
const html_content = transporter.sendMail.getCall(0).args[0].html;
const regexp = /identity_token=([a-zA-Z0-9]+)/;
const token = regexp.exec(html_content)[1];
return requestAsync.getAsync({
url: BASE_URL + Endpoints.SECOND_FACTOR_TOTP_IDENTITY_FINISH_GET + "?identity_token=" + token,
2017-05-21 20:45:54 +00:00
jar: jar
});
})
.then(function (res: request.RequestResponse) {
assert.equal(res.statusCode, 200);
const regex = /<p id="secret">([A-Z0-9]+)<\/p>/g;
const secret = regex.exec(res.body);
return BluebirdPromise.resolve(secret[1]);
2017-05-21 20:45:54 +00:00
});
}
function execute_totp(jar: request.CookieJar, token: string) {
return requestAsync.postAsync({
url: BASE_URL + Endpoints.SECOND_FACTOR_TOTP_POST,
2017-05-21 20:45:54 +00:00
jar: jar,
form: {
token: token
}
});
}
function execute_u2f_authentication(jar: request.CookieJar) {
return requestAsync.getAsync({
url: BASE_URL + Endpoints.SECOND_FACTOR_U2F_SIGN_REQUEST_GET,
2017-05-21 20:45:54 +00:00
jar: jar
})
.then(function (res: request.RequestResponse) {
assert.equal(res.statusCode, 200);
return requestAsync.postAsync({
url: BASE_URL + Endpoints.SECOND_FACTOR_U2F_SIGN_POST,
2017-05-21 20:45:54 +00:00
jar: jar,
form: {
}
});
});
}
function execute_verification(jar: request.CookieJar) {
return requestAsync.getAsync({ url: BASE_URL + Endpoints.VERIFY_GET, jar: jar });
2017-05-21 20:45:54 +00:00
}
function execute_login(jar: request.CookieJar) {
return requestAsync.getAsync({ url: BASE_URL + Endpoints.FIRST_FACTOR_GET, jar: jar });
2017-05-21 20:45:54 +00:00
}
function execute_u2f_registration(jar: request.CookieJar, transporter: NodemailerMock.NodemailerTransporterMock) {
return requestAsync.getAsync({
url: BASE_URL + Endpoints.SECOND_FACTOR_U2F_IDENTITY_START_GET,
2017-05-21 20:45:54 +00:00
jar: jar
})
.then(function (res: request.RequestResponse) {
assert.equal(res.statusCode, 200);
2017-05-21 20:45:54 +00:00
const html_content = transporter.sendMail.getCall(0).args[0].html;
const regexp = /identity_token=([a-zA-Z0-9]+)/;
const token = regexp.exec(html_content)[1];
// console.log(html_content, token);
return requestAsync.getAsync({
url: BASE_URL + Endpoints.SECOND_FACTOR_U2F_IDENTITY_FINISH_GET + "?identity_token=" + token,
2017-05-21 20:45:54 +00:00
jar: jar
});
})
.then(function (res: request.RequestResponse) {
assert.equal(res.statusCode, 200);
return requestAsync.getAsync({
url: BASE_URL + Endpoints.SECOND_FACTOR_U2F_REGISTER_REQUEST_GET,
2017-05-21 20:45:54 +00:00
jar: jar,
});
})
.then(function (res: request.RequestResponse) {
assert.equal(res.statusCode, 200);
return requestAsync.postAsync({
url: BASE_URL + Endpoints.SECOND_FACTOR_U2F_REGISTER_POST,
2017-05-21 20:45:54 +00:00
jar: jar,
form: {
s: "test"
}
});
});
}
function execute_first_factor(jar: request.CookieJar) {
return requestAsync.postAsync({
url: BASE_URL + Endpoints.FIRST_FACTOR_POST,
2017-05-21 20:45:54 +00:00
jar: jar,
form: {
username: "test_ok",
password: "password"
}
});
}
function execute_failing_first_factor(jar: request.CookieJar) {
return requestAsync.postAsync({
url: BASE_URL + Endpoints.FIRST_FACTOR_POST,
2017-05-21 20:45:54 +00:00
jar: jar,
form: {
username: "test_nok",
password: "password"
}
});
}
return {
login: execute_login,
verify: execute_verification,
reset_password: execute_reset_password,
u2f_authentication: execute_u2f_authentication,
u2f_registration: execute_u2f_registration,
first_factor: execute_first_factor,
failing_first_factor: execute_failing_first_factor,
totp: execute_totp,
register_totp: execute_register_totp,
};
};