2017-05-25 13:09:29 +00:00
|
|
|
|
|
|
|
import BluebirdPromise = require("bluebird");
|
2017-10-16 22:35:34 +00:00
|
|
|
import Sinon = require("sinon");
|
2017-10-22 15:42:05 +00:00
|
|
|
import Assert = require("assert");
|
|
|
|
import Exceptions = require("../../../../../src/lib/Exceptions");
|
|
|
|
import { AuthenticationSessionHandler } from "../../../../../src/lib/AuthenticationSessionHandler";
|
2017-10-17 21:24:02 +00:00
|
|
|
import { AuthenticationSession } from "../../../../../types/AuthenticationSession";
|
2017-10-06 22:09:42 +00:00
|
|
|
import SignPost = require("../../../../../src/lib/routes/secondfactor/totp/sign/post");
|
2017-10-16 22:35:34 +00:00
|
|
|
import { ServerVariables } from "../../../../../src/lib/ServerVariables";
|
2017-05-25 13:09:29 +00:00
|
|
|
|
|
|
|
import ExpressMock = require("../../../../mocks/express");
|
2017-07-19 19:06:12 +00:00
|
|
|
import { UserDataStoreStub } from "../../../../mocks/storage/UserDataStoreStub";
|
2017-10-16 22:35:34 +00:00
|
|
|
import { ServerVariablesMock, ServerVariablesMockBuilder } from "../../../../mocks/ServerVariablesMockBuilder";
|
2017-05-25 13:09:29 +00:00
|
|
|
|
|
|
|
describe("test totp route", function () {
|
|
|
|
let req: ExpressMock.RequestMock;
|
|
|
|
let res: ExpressMock.ResponseMock;
|
2017-10-17 21:24:02 +00:00
|
|
|
let authSession: AuthenticationSession;
|
2017-10-16 22:35:34 +00:00
|
|
|
let vars: ServerVariables;
|
|
|
|
let mocks: ServerVariablesMock;
|
2017-05-25 13:09:29 +00:00
|
|
|
|
|
|
|
beforeEach(function () {
|
2017-10-16 22:35:34 +00:00
|
|
|
const s = ServerVariablesMockBuilder.build();
|
|
|
|
vars = s.variables;
|
|
|
|
mocks = s.mocks;
|
|
|
|
const app_get = Sinon.stub();
|
2017-05-25 13:09:29 +00:00
|
|
|
req = {
|
2017-10-22 15:42:05 +00:00
|
|
|
app: {},
|
2017-05-25 13:09:29 +00:00
|
|
|
body: {
|
|
|
|
token: "abc"
|
|
|
|
},
|
2017-10-16 22:35:34 +00:00
|
|
|
session: {},
|
|
|
|
query: {
|
|
|
|
redirect: "http://redirect"
|
|
|
|
}
|
2017-05-25 13:09:29 +00:00
|
|
|
};
|
|
|
|
res = ExpressMock.ResponseMock();
|
|
|
|
|
|
|
|
const doc = {
|
|
|
|
userid: "user",
|
|
|
|
secret: {
|
|
|
|
base32: "ABCDEF"
|
|
|
|
}
|
|
|
|
};
|
2017-07-19 19:06:12 +00:00
|
|
|
mocks.userDataStore.retrieveTOTPSecretStub.returns(BluebirdPromise.resolve(doc));
|
2017-10-22 15:42:05 +00:00
|
|
|
authSession = AuthenticationSessionHandler.get(req as any, vars.logger);
|
|
|
|
authSession.userid = "user";
|
|
|
|
authSession.first_factor = true;
|
|
|
|
authSession.second_factor = false;
|
2017-05-25 13:09:29 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it("should send status code 200 when totp is valid", function () {
|
2017-10-16 22:35:34 +00:00
|
|
|
mocks.totpHandler.validateStub.returns(true);
|
|
|
|
return SignPost.default(vars)(req as any, res as any)
|
2017-05-25 13:09:29 +00:00
|
|
|
.then(function () {
|
2017-10-22 15:42:05 +00:00
|
|
|
Assert.equal(true, authSession.second_factor);
|
2017-05-25 13:09:29 +00:00
|
|
|
return BluebirdPromise.resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-10-14 23:34:23 +00:00
|
|
|
it("should send error message when totp is not valid", function () {
|
2017-10-16 22:35:34 +00:00
|
|
|
mocks.totpHandler.validateStub.returns(false);
|
|
|
|
return SignPost.default(vars)(req as any, res as any)
|
2017-10-14 23:34:23 +00:00
|
|
|
.then(function () {
|
2017-10-22 15:42:05 +00:00
|
|
|
Assert.equal(false, authSession.second_factor);
|
|
|
|
Assert.equal(res.status.getCall(0).args[0], 200);
|
|
|
|
Assert.deepEqual(res.send.getCall(0).args[0], {
|
2017-10-14 23:34:23 +00:00
|
|
|
error: "Operation failed."
|
|
|
|
});
|
2017-05-25 13:09:29 +00:00
|
|
|
return BluebirdPromise.resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|