2017-05-22 03:45:54 +07:00
|
|
|
|
|
|
|
import BluebirdPromise = require("bluebird");
|
|
|
|
import request = require("request");
|
|
|
|
import assert = require("assert");
|
|
|
|
import express = require("express");
|
|
|
|
import nodemailer = require("nodemailer");
|
2017-10-07 05:09:42 +07:00
|
|
|
import Endpoints = require("../../shared/api");
|
2017-05-22 03:45:54 +07:00
|
|
|
|
2017-10-07 05:09:42 +07:00
|
|
|
declare module "request" {
|
|
|
|
export interface RequestAPI<TRequest extends Request,
|
|
|
|
TOptions extends CoreOptions,
|
|
|
|
TUriUrlOptions> {
|
|
|
|
getAsync(uri: string, options?: RequiredUriUrl): BluebirdPromise<RequestResponse>;
|
|
|
|
getAsync(uri: string): BluebirdPromise<RequestResponse>;
|
|
|
|
getAsync(options: RequiredUriUrl & CoreOptions): BluebirdPromise<RequestResponse>;
|
|
|
|
|
|
|
|
postAsync(uri: string, options?: CoreOptions): BluebirdPromise<RequestResponse>;
|
|
|
|
postAsync(uri: string): BluebirdPromise<RequestResponse>;
|
|
|
|
postAsync(options: RequiredUriUrl & CoreOptions): BluebirdPromise<RequestResponse>;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-25 20:09:29 +07:00
|
|
|
const requestAsync: typeof request = BluebirdPromise.promisifyAll(request) as typeof request;
|
2017-05-22 03:45:54 +07:00
|
|
|
|
|
|
|
export = function (port: number) {
|
|
|
|
const PORT = port;
|
|
|
|
const BASE_URL = "http://localhost:" + PORT;
|
|
|
|
|
|
|
|
function execute_totp(jar: request.CookieJar, token: string) {
|
|
|
|
return requestAsync.postAsync({
|
2017-05-25 20:09:29 +07:00
|
|
|
url: BASE_URL + Endpoints.SECOND_FACTOR_TOTP_POST,
|
2017-05-22 03:45:54 +07:00
|
|
|
jar: jar,
|
|
|
|
form: {
|
|
|
|
token: token
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function execute_u2f_authentication(jar: request.CookieJar) {
|
|
|
|
return requestAsync.getAsync({
|
2017-05-25 20:09:29 +07:00
|
|
|
url: BASE_URL + Endpoints.SECOND_FACTOR_U2F_SIGN_REQUEST_GET,
|
2017-05-22 03:45:54 +07:00
|
|
|
jar: jar
|
|
|
|
})
|
|
|
|
.then(function (res: request.RequestResponse) {
|
|
|
|
assert.equal(res.statusCode, 200);
|
|
|
|
return requestAsync.postAsync({
|
2017-05-25 20:09:29 +07:00
|
|
|
url: BASE_URL + Endpoints.SECOND_FACTOR_U2F_SIGN_POST,
|
2017-05-22 03:45:54 +07:00
|
|
|
jar: jar,
|
|
|
|
form: {
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function execute_verification(jar: request.CookieJar) {
|
2017-05-25 20:09:29 +07:00
|
|
|
return requestAsync.getAsync({ url: BASE_URL + Endpoints.VERIFY_GET, jar: jar });
|
2017-05-22 03:45:54 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
function execute_login(jar: request.CookieJar) {
|
2017-05-25 20:09:29 +07:00
|
|
|
return requestAsync.getAsync({ url: BASE_URL + Endpoints.FIRST_FACTOR_GET, jar: jar });
|
2017-05-22 03:45:54 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
function execute_first_factor(jar: request.CookieJar) {
|
|
|
|
return requestAsync.postAsync({
|
2017-05-25 20:09:29 +07:00
|
|
|
url: BASE_URL + Endpoints.FIRST_FACTOR_POST,
|
2017-05-22 03:45:54 +07:00
|
|
|
jar: jar,
|
|
|
|
form: {
|
|
|
|
username: "test_ok",
|
|
|
|
password: "password"
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function execute_failing_first_factor(jar: request.CookieJar) {
|
|
|
|
return requestAsync.postAsync({
|
2017-05-25 20:09:29 +07:00
|
|
|
url: BASE_URL + Endpoints.FIRST_FACTOR_POST,
|
2017-05-22 03:45:54 +07:00
|
|
|
jar: jar,
|
|
|
|
form: {
|
|
|
|
username: "test_nok",
|
|
|
|
password: "password"
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
login: execute_login,
|
|
|
|
verify: execute_verification,
|
|
|
|
u2f_authentication: execute_u2f_authentication,
|
|
|
|
first_factor: execute_first_factor,
|
|
|
|
failing_first_factor: execute_failing_first_factor,
|
|
|
|
totp: execute_totp,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|