2017-05-21 03:55:37 +07:00
|
|
|
|
|
2017-10-17 05:35:34 +07:00
|
|
|
|
import Sinon = require("sinon");
|
2017-05-21 03:55:37 +07:00
|
|
|
|
import BluebirdPromise = require("bluebird");
|
2017-10-11 04:03:30 +07:00
|
|
|
|
import Assert = require("assert");
|
2017-10-07 05:09:42 +07:00
|
|
|
|
import FirstFactorPost = require("../../../src/lib/routes/firstfactor/post");
|
|
|
|
|
import exceptions = require("../../../src/lib/Exceptions");
|
2017-10-22 22:42:05 +07:00
|
|
|
|
import { AuthenticationSessionHandler } from "../../../src/lib/AuthenticationSessionHandler";
|
2017-10-18 04:24:02 +07:00
|
|
|
|
import { AuthenticationSession } from "../../../types/AuthenticationSession";
|
2017-10-07 05:09:42 +07:00
|
|
|
|
import Endpoints = require("../../../../shared/api");
|
2017-05-25 20:09:29 +07:00
|
|
|
|
import AuthenticationRegulatorMock = require("../../mocks/AuthenticationRegulator");
|
2017-09-03 20:22:09 +07:00
|
|
|
|
import { AccessControllerStub } from "../../mocks/AccessControllerStub";
|
2017-05-25 20:09:29 +07:00
|
|
|
|
import ExpressMock = require("../../mocks/express");
|
2017-10-17 05:35:34 +07:00
|
|
|
|
import { ServerVariablesMock, ServerVariablesMockBuilder } from "../../mocks/ServerVariablesMockBuilder";
|
2017-10-08 05:46:57 +07:00
|
|
|
|
import { ServerVariables } from "../../../src/lib/ServerVariables";
|
2017-05-21 03:55:37 +07:00
|
|
|
|
|
2017-05-22 04:32:09 +07:00
|
|
|
|
describe("test the first factor validation route", function () {
|
2017-05-21 17:14:59 +07:00
|
|
|
|
let req: ExpressMock.RequestMock;
|
|
|
|
|
let res: ExpressMock.ResponseMock;
|
2017-05-21 03:55:37 +07:00
|
|
|
|
let emails: string[];
|
|
|
|
|
let groups: string[];
|
2017-10-17 05:35:34 +07:00
|
|
|
|
let vars: ServerVariables;
|
|
|
|
|
let mocks: ServerVariablesMock;
|
2017-10-22 22:42:05 +07:00
|
|
|
|
let authSession: AuthenticationSession;
|
2017-05-21 03:55:37 +07:00
|
|
|
|
|
2017-05-22 04:32:09 +07:00
|
|
|
|
beforeEach(function () {
|
|
|
|
|
emails = ["test_ok@example.com"];
|
|
|
|
|
groups = ["group1", "group2" ];
|
2017-10-17 05:35:34 +07:00
|
|
|
|
const s = ServerVariablesMockBuilder.build();
|
|
|
|
|
mocks = s.mocks;
|
|
|
|
|
vars = s.variables;
|
2017-05-21 03:55:37 +07:00
|
|
|
|
|
2017-10-17 05:35:34 +07:00
|
|
|
|
mocks.accessController.isAccessAllowedMock.returns(true);
|
|
|
|
|
mocks.regulator.regulateStub.returns(BluebirdPromise.resolve());
|
|
|
|
|
mocks.regulator.markStub.returns(BluebirdPromise.resolve());
|
2017-05-21 03:55:37 +07:00
|
|
|
|
|
|
|
|
|
req = {
|
2017-11-01 20:24:18 +07:00
|
|
|
|
originalUrl: "/api/firstfactor",
|
2017-05-21 03:55:37 +07:00
|
|
|
|
body: {
|
|
|
|
|
username: "username",
|
|
|
|
|
password: "password"
|
|
|
|
|
},
|
2017-09-25 04:19:03 +07:00
|
|
|
|
query: {
|
|
|
|
|
redirect: "http://redirect.url"
|
|
|
|
|
},
|
2017-05-21 03:55:37 +07:00
|
|
|
|
session: {
|
2017-05-22 04:32:09 +07:00
|
|
|
|
},
|
|
|
|
|
headers: {
|
|
|
|
|
host: "home.example.com"
|
2017-05-21 03:55:37 +07:00
|
|
|
|
}
|
|
|
|
|
};
|
2017-05-25 20:09:29 +07:00
|
|
|
|
|
2017-05-21 06:15:34 +07:00
|
|
|
|
res = ExpressMock.ResponseMock();
|
2017-10-22 22:42:05 +07:00
|
|
|
|
authSession = AuthenticationSessionHandler.get(req as any, vars.logger);
|
2017-05-21 03:55:37 +07:00
|
|
|
|
});
|
|
|
|
|
|
2017-09-22 22:53:18 +07:00
|
|
|
|
it("should reply with 204 if success", function () {
|
2017-10-17 05:35:34 +07:00
|
|
|
|
mocks.ldapAuthenticator.authenticateStub.withArgs("username", "password")
|
2017-07-16 22:37:13 +07:00
|
|
|
|
.returns(BluebirdPromise.resolve({
|
|
|
|
|
emails: emails,
|
|
|
|
|
groups: groups
|
|
|
|
|
}));
|
2017-10-22 22:42:05 +07:00
|
|
|
|
return FirstFactorPost.default(vars)(req as any, res as any)
|
2017-05-25 20:09:29 +07:00
|
|
|
|
.then(function () {
|
2017-10-11 04:03:30 +07:00
|
|
|
|
Assert.equal("username", authSession.userid);
|
|
|
|
|
Assert(res.send.calledOnce);
|
2017-05-21 03:55:37 +07:00
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2017-06-15 05:22:16 +07:00
|
|
|
|
it("should retrieve email from LDAP", function () {
|
2017-10-17 05:35:34 +07:00
|
|
|
|
mocks.ldapAuthenticator.authenticateStub.withArgs("username", "password")
|
2017-07-16 22:37:13 +07:00
|
|
|
|
.returns(BluebirdPromise.resolve([{ mail: ["test@example.com"] }]));
|
2017-10-17 05:35:34 +07:00
|
|
|
|
return FirstFactorPost.default(vars)(req as any, res as any);
|
2017-05-21 03:55:37 +07:00
|
|
|
|
});
|
|
|
|
|
|
2017-06-15 05:22:16 +07:00
|
|
|
|
it("should set first email address as user session variable", function () {
|
2017-05-25 20:09:29 +07:00
|
|
|
|
const emails = ["test_ok@example.com"];
|
2017-10-17 05:35:34 +07:00
|
|
|
|
mocks.ldapAuthenticator.authenticateStub.withArgs("username", "password")
|
2017-07-16 22:37:13 +07:00
|
|
|
|
.returns(BluebirdPromise.resolve({
|
|
|
|
|
emails: emails,
|
|
|
|
|
groups: groups
|
|
|
|
|
}));
|
2017-09-22 03:07:34 +07:00
|
|
|
|
|
2017-10-22 22:42:05 +07:00
|
|
|
|
return FirstFactorPost.default(vars)(req as any, res as any)
|
2017-05-25 20:09:29 +07:00
|
|
|
|
.then(function () {
|
2017-10-11 04:03:30 +07:00
|
|
|
|
Assert.equal("test_ok@example.com", authSession.email);
|
2017-05-21 03:55:37 +07:00
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2017-10-11 04:03:30 +07:00
|
|
|
|
it("should return error message when LDAP authenticator throws", function () {
|
2017-10-17 05:35:34 +07:00
|
|
|
|
mocks.ldapAuthenticator.authenticateStub.withArgs("username", "password")
|
2017-07-16 22:37:13 +07:00
|
|
|
|
.returns(BluebirdPromise.reject(new exceptions.LdapBindError("Bad credentials")));
|
2017-10-15 06:34:23 +07:00
|
|
|
|
|
2017-10-17 05:35:34 +07:00
|
|
|
|
return FirstFactorPost.default(vars)(req as any, res as any)
|
2017-06-15 05:22:16 +07:00
|
|
|
|
.then(function () {
|
2017-10-11 04:03:30 +07:00
|
|
|
|
Assert.equal(res.status.getCall(0).args[0], 200);
|
2017-10-17 05:35:34 +07:00
|
|
|
|
Assert.equal(mocks.regulator.markStub.getCall(0).args[0], "username");
|
2017-10-11 04:03:30 +07:00
|
|
|
|
Assert.deepEqual(res.send.getCall(0).args[0], {
|
|
|
|
|
error: "Operation failed."
|
|
|
|
|
});
|
2017-06-15 05:22:16 +07:00
|
|
|
|
});
|
2017-05-21 03:55:37 +07:00
|
|
|
|
});
|
|
|
|
|
|
2017-10-11 04:03:30 +07:00
|
|
|
|
it("should return error message when regulator rejects authentication", function () {
|
2017-05-21 03:55:37 +07:00
|
|
|
|
const err = new exceptions.AuthenticationRegulationError("Authentication regulation...");
|
2017-10-17 05:35:34 +07:00
|
|
|
|
mocks.regulator.regulateStub.returns(BluebirdPromise.reject(err));
|
|
|
|
|
return FirstFactorPost.default(vars)(req as any, res as any)
|
2017-06-15 05:22:16 +07:00
|
|
|
|
.then(function () {
|
2017-10-11 04:03:30 +07:00
|
|
|
|
Assert.equal(res.status.getCall(0).args[0], 200);
|
|
|
|
|
Assert.deepEqual(res.send.getCall(0).args[0], {
|
|
|
|
|
error: "Operation failed."
|
|
|
|
|
});
|
2017-06-15 05:22:16 +07:00
|
|
|
|
});
|
|
|
|
|
});
|
2017-05-21 03:55:37 +07:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|