2017-05-25 20:09:29 +07:00
|
|
|
import sinon = require("sinon");
|
|
|
|
import winston = require("winston");
|
|
|
|
import assert = require("assert");
|
|
|
|
import BluebirdPromise = require("bluebird");
|
|
|
|
|
2017-10-07 05:09:42 +07:00
|
|
|
import { Identity } from "../../../../../types/Identity";
|
|
|
|
import RegistrationHandler from "../../../../../src/lib/routes/secondfactor/u2f/identity/RegistrationHandler";
|
|
|
|
import AuthenticationSession = require("../../../../../src/lib/AuthenticationSession");
|
2017-05-25 20:09:29 +07:00
|
|
|
|
|
|
|
import ExpressMock = require("../../../../mocks/express");
|
2017-07-20 02:06:12 +07:00
|
|
|
import { UserDataStoreStub } from "../../../../mocks/storage/UserDataStoreStub";
|
2017-05-25 20:09:29 +07:00
|
|
|
import ServerVariablesMock = require("../../../../mocks/ServerVariablesMock");
|
|
|
|
|
|
|
|
describe("test register handler", function () {
|
|
|
|
let req: ExpressMock.RequestMock;
|
|
|
|
let res: ExpressMock.ResponseMock;
|
|
|
|
let authSession: AuthenticationSession.AuthenticationSession;
|
|
|
|
|
|
|
|
beforeEach(function () {
|
2017-09-01 21:06:02 +07:00
|
|
|
req = ExpressMock.RequestMock();
|
2017-05-25 20:09:29 +07:00
|
|
|
req.app = {};
|
|
|
|
const mocks = ServerVariablesMock.mock(req.app);
|
|
|
|
mocks.logger = winston;
|
|
|
|
req.session = {};
|
|
|
|
AuthenticationSession.reset(req as any);
|
|
|
|
req.headers = {};
|
|
|
|
req.headers.host = "localhost";
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
inMemoryOnly: true
|
|
|
|
};
|
|
|
|
|
2017-07-20 02:06:12 +07: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 20:09:29 +07:00
|
|
|
|
|
|
|
res = ExpressMock.ResponseMock();
|
|
|
|
res.send = sinon.spy();
|
|
|
|
res.json = sinon.spy();
|
|
|
|
res.status = sinon.spy();
|
2017-09-22 03:07:34 +07:00
|
|
|
|
|
|
|
return AuthenticationSession.get(req as any)
|
|
|
|
.then(function (_authSession: AuthenticationSession.AuthenticationSession) {
|
|
|
|
authSession = _authSession;
|
|
|
|
authSession.userid = "user";
|
|
|
|
authSession.email = "user@example.com";
|
|
|
|
authSession.first_factor = true;
|
|
|
|
authSession.second_factor = false;
|
|
|
|
});
|
2017-05-25 20:09:29 +07:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("test u2f registration check", test_registration_check);
|
|
|
|
|
|
|
|
function test_registration_check() {
|
|
|
|
it("should fail if first_factor has not been passed", function () {
|
|
|
|
authSession.first_factor = false;
|
|
|
|
return new RegistrationHandler().preValidationInit(req as any)
|
|
|
|
.then(function () { return BluebirdPromise.reject(new Error("It should fail")); })
|
|
|
|
.catch(function (err: Error) {
|
|
|
|
return BluebirdPromise.resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should fail if userid is missing", function (done) {
|
|
|
|
authSession.first_factor = false;
|
|
|
|
authSession.userid = undefined;
|
|
|
|
|
|
|
|
new RegistrationHandler().preValidationInit(req as any)
|
|
|
|
.catch(function (err: Error) {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should fail if email is missing", function (done) {
|
|
|
|
authSession.first_factor = false;
|
|
|
|
authSession.email = undefined;
|
|
|
|
|
|
|
|
new RegistrationHandler().preValidationInit(req as any)
|
|
|
|
.catch(function (err: Error) {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should succeed if first factor passed, userid and email are provided", function (done) {
|
|
|
|
new RegistrationHandler().preValidationInit(req as any)
|
|
|
|
.then(function (identity: Identity) {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|