mirror of
https://github.com/0rangebananaspy/authelia.git
synced 2024-09-14 22:47:21 +07:00
b9fa786df6
This refactoring aims to ease testability and clean up a lot of soft touchy typings in test code. This is the first step of this refactoring introducing the concept and implementing missing interfaces and stubs. At the end of the day, ServerVariablesHandler should completely disappear and every variable should be injected in the endpoint handler builder itself.
94 lines
3.1 KiB
TypeScript
94 lines
3.1 KiB
TypeScript
|
|
import BluebirdPromise = require("bluebird");
|
|
import Sinon = require("sinon");
|
|
import assert = require("assert");
|
|
import winston = require("winston");
|
|
|
|
import exceptions = require("../../../../../src/lib/Exceptions");
|
|
import AuthenticationSession = require("../../../../../src/lib/AuthenticationSession");
|
|
import SignPost = require("../../../../../src/lib/routes/secondfactor/totp/sign/post");
|
|
import { ServerVariables } from "../../../../../src/lib/ServerVariables";
|
|
|
|
import ExpressMock = require("../../../../mocks/express");
|
|
import { UserDataStoreStub } from "../../../../mocks/storage/UserDataStoreStub";
|
|
import { ServerVariablesMock, ServerVariablesMockBuilder } from "../../../../mocks/ServerVariablesMockBuilder";
|
|
|
|
describe("test totp route", function () {
|
|
let req: ExpressMock.RequestMock;
|
|
let res: ExpressMock.ResponseMock;
|
|
let authSession: AuthenticationSession.AuthenticationSession;
|
|
let vars: ServerVariables;
|
|
let mocks: ServerVariablesMock;
|
|
|
|
beforeEach(function () {
|
|
const s = ServerVariablesMockBuilder.build();
|
|
vars = s.variables;
|
|
mocks = s.mocks;
|
|
const app_get = Sinon.stub();
|
|
req = {
|
|
app: {
|
|
get: Sinon.stub().returns({ logger: winston })
|
|
},
|
|
body: {
|
|
token: "abc"
|
|
},
|
|
session: {},
|
|
query: {
|
|
redirect: "http://redirect"
|
|
}
|
|
};
|
|
res = ExpressMock.ResponseMock();
|
|
AuthenticationSession.reset(req as any);
|
|
|
|
const doc = {
|
|
userid: "user",
|
|
secret: {
|
|
base32: "ABCDEF"
|
|
}
|
|
};
|
|
mocks.userDataStore.retrieveTOTPSecretStub.returns(BluebirdPromise.resolve(doc));
|
|
return AuthenticationSession.get(req as any)
|
|
.then(function (_authSession: AuthenticationSession.AuthenticationSession) {
|
|
authSession = _authSession;
|
|
authSession.userid = "user";
|
|
authSession.first_factor = true;
|
|
authSession.second_factor = false;
|
|
});
|
|
});
|
|
|
|
|
|
it("should send status code 200 when totp is valid", function () {
|
|
mocks.totpHandler.validateStub.returns(true);
|
|
return SignPost.default(vars)(req as any, res as any)
|
|
.then(function () {
|
|
assert.equal(true, authSession.second_factor);
|
|
return BluebirdPromise.resolve();
|
|
});
|
|
});
|
|
|
|
it("should send error message when totp is not valid", function () {
|
|
mocks.totpHandler.validateStub.returns(false);
|
|
return SignPost.default(vars)(req as any, res as any)
|
|
.then(function () {
|
|
assert.equal(false, authSession.second_factor);
|
|
assert.equal(res.status.getCall(0).args[0], 200);
|
|
assert.deepEqual(res.send.getCall(0).args[0], {
|
|
error: "Operation failed."
|
|
});
|
|
return BluebirdPromise.resolve();
|
|
});
|
|
});
|
|
|
|
it("should send status code 401 when session has not been initiated", function () {
|
|
mocks.totpHandler.validateStub.returns(true);
|
|
req.session = {};
|
|
return SignPost.default(vars)(req as any, res as any)
|
|
.then(function () { return BluebirdPromise.reject(new Error("It should fail")); })
|
|
.catch(function () {
|
|
assert.equal(401, res.status.getCall(0).args[0]);
|
|
return BluebirdPromise.resolve();
|
|
});
|
|
});
|
|
});
|
|
|