2017-10-22 15:42:05 +00:00
|
|
|
import Sinon = require("sinon");
|
|
|
|
import Assert = require("assert");
|
2017-05-25 13:09:29 +00:00
|
|
|
import BluebirdPromise = require("bluebird");
|
|
|
|
|
2017-10-06 22:09:42 +00:00
|
|
|
import { Identity } from "../../../../../types/Identity";
|
|
|
|
import RegistrationHandler from "../../../../../src/lib/routes/secondfactor/u2f/identity/RegistrationHandler";
|
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-17 21:24:02 +00:00
|
|
|
import { ServerVariablesMock, ServerVariablesMockBuilder } from "../../../../mocks/ServerVariablesMockBuilder";
|
|
|
|
import { ServerVariables } from "../../../../../src/lib/ServerVariables";
|
2017-05-25 13:09:29 +00:00
|
|
|
|
2017-10-22 15:42:05 +00:00
|
|
|
describe("test U2F register handler", function () {
|
2017-05-25 13:09:29 +00:00
|
|
|
let req: ExpressMock.RequestMock;
|
|
|
|
let res: ExpressMock.ResponseMock;
|
2017-10-17 21:24:02 +00:00
|
|
|
let mocks: ServerVariablesMock;
|
|
|
|
let vars: ServerVariables;
|
2017-05-25 13:09:29 +00:00
|
|
|
|
|
|
|
beforeEach(function () {
|
2017-10-17 21:24:02 +00:00
|
|
|
const s = ServerVariablesMockBuilder.build();
|
|
|
|
mocks = s.mocks;
|
|
|
|
vars = s.variables;
|
|
|
|
|
2017-09-01 14:06:02 +00:00
|
|
|
req = ExpressMock.RequestMock();
|
2017-05-25 13:09:29 +00:00
|
|
|
req.app = {};
|
2017-10-17 21:24:02 +00:00
|
|
|
req.session = {
|
|
|
|
auth: {
|
|
|
|
userid: "user",
|
|
|
|
email: "user@example.com",
|
|
|
|
first_factor: true,
|
|
|
|
second_factor: false
|
|
|
|
}
|
|
|
|
};
|
2017-05-25 13:09:29 +00:00
|
|
|
req.headers = {};
|
|
|
|
req.headers.host = "localhost";
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
inMemoryOnly: true
|
|
|
|
};
|
|
|
|
|
2017-07-19 19:06:12 +00:00
|
|
|
mocks.userDataStore.saveU2FRegistrationStub.returns(BluebirdPromise.resolve({}));
|
|
|
|
mocks.userDataStore.retrieveU2FRegistrationStub.returns(BluebirdPromise.resolve({}));
|
|
|
|
mocks.userDataStore.produceIdentityValidationTokenStub.returns(BluebirdPromise.resolve({}));
|
|
|
|
mocks.userDataStore.consumeIdentityValidationTokenStub.returns(BluebirdPromise.resolve({}));
|
2017-05-25 13:09:29 +00:00
|
|
|
|
|
|
|
res = ExpressMock.ResponseMock();
|
2017-10-22 15:42:05 +00:00
|
|
|
res.send = Sinon.spy();
|
|
|
|
res.json = Sinon.spy();
|
|
|
|
res.status = Sinon.spy();
|
2017-05-25 13:09:29 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("test u2f registration check", test_registration_check);
|
|
|
|
|
|
|
|
function test_registration_check() {
|
|
|
|
it("should fail if first_factor has not been passed", function () {
|
2017-10-17 21:24:02 +00:00
|
|
|
req.session.auth.first_factor = false;
|
|
|
|
return new RegistrationHandler(vars.logger).preValidationInit(req as any)
|
2017-05-25 13:09:29 +00:00
|
|
|
.then(function () { return BluebirdPromise.reject(new Error("It should fail")); })
|
|
|
|
.catch(function (err: Error) {
|
|
|
|
return BluebirdPromise.resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-10-22 15:42:05 +00:00
|
|
|
it("should fail if userid is missing", function () {
|
2017-10-17 21:24:02 +00:00
|
|
|
req.session.auth.first_factor = false;
|
|
|
|
req.session.auth.userid = undefined;
|
2017-05-25 13:09:29 +00:00
|
|
|
|
2017-10-22 15:42:05 +00:00
|
|
|
return new RegistrationHandler(vars.logger).preValidationInit(req as any)
|
|
|
|
.then(function () {
|
|
|
|
return BluebirdPromise.reject(new Error("should not be here"));
|
|
|
|
},
|
|
|
|
function (err: Error) {
|
|
|
|
return BluebirdPromise.resolve();
|
2017-05-25 13:09:29 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-10-22 15:42:05 +00:00
|
|
|
it("should fail if email is missing", function () {
|
2017-10-17 21:24:02 +00:00
|
|
|
req.session.auth.first_factor = false;
|
|
|
|
req.session.auth.email = undefined;
|
2017-05-25 13:09:29 +00:00
|
|
|
|
2017-10-22 15:42:05 +00:00
|
|
|
return new RegistrationHandler(vars.logger).preValidationInit(req as any)
|
|
|
|
.then(function () {
|
|
|
|
return BluebirdPromise.reject(new Error("should not be here"));
|
|
|
|
},
|
|
|
|
function (err: Error) {
|
|
|
|
return BluebirdPromise.resolve();
|
2017-05-25 13:09:29 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-10-22 15:42:05 +00:00
|
|
|
it("should succeed if first factor passed, userid and email are provided", function () {
|
|
|
|
req.session.auth.first_factor = true;
|
|
|
|
req.session.auth.email = "admin@example.com";
|
|
|
|
req.session.auth.userid = "user";
|
|
|
|
return new RegistrationHandler(vars.logger).preValidationInit(req as any);
|
2017-05-25 13:09:29 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|