2017-05-25 20:09:29 +07:00
|
|
|
|
|
|
|
import sinon = require("sinon");
|
|
|
|
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 U2FRegisterRequestGet = require("../../../../../src/lib/routes/secondfactor/u2f/register_request/get");
|
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-10-18 04:24:02 +07:00
|
|
|
import { ServerVariablesMockBuilder, ServerVariablesMock } from "../../../../mocks/ServerVariablesMockBuilder";
|
|
|
|
import { ServerVariables } from "../../../../../src/lib/ServerVariables";
|
2017-05-25 20:09:29 +07:00
|
|
|
|
|
|
|
describe("test u2f routes: register_request", function () {
|
2017-09-22 03:07:34 +07:00
|
|
|
let req: ExpressMock.RequestMock;
|
|
|
|
let res: ExpressMock.ResponseMock;
|
2017-10-18 04:24:02 +07:00
|
|
|
let mocks: ServerVariablesMock;
|
|
|
|
let vars: ServerVariables;
|
2017-09-22 03:07:34 +07:00
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
req = ExpressMock.RequestMock();
|
2017-11-01 20:24:18 +07:00
|
|
|
req.originalUrl = "/api/xxxx";
|
2017-09-22 03:07:34 +07:00
|
|
|
req.app = {};
|
2017-10-18 04:24:02 +07:00
|
|
|
req.session = {
|
|
|
|
auth: {
|
|
|
|
userid: "user",
|
|
|
|
first_factor: true,
|
|
|
|
second_factor: false,
|
|
|
|
identity_check: {
|
|
|
|
challenge: "u2f-register",
|
|
|
|
userid: "user"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2017-09-22 03:07:34 +07:00
|
|
|
req.headers = {};
|
|
|
|
req.headers.host = "localhost";
|
|
|
|
|
2017-10-18 04:24:02 +07:00
|
|
|
const s = ServerVariablesMockBuilder.build();
|
|
|
|
mocks = s.mocks;
|
|
|
|
vars = s.variables;
|
|
|
|
|
2017-09-22 03:07:34 +07:00
|
|
|
const options = {
|
|
|
|
inMemoryOnly: true
|
|
|
|
};
|
|
|
|
|
|
|
|
mocks.userDataStore.saveU2FRegistrationStub.returns(BluebirdPromise.resolve({}));
|
|
|
|
mocks.userDataStore.retrieveU2FRegistrationStub.returns(BluebirdPromise.resolve({}));
|
|
|
|
|
|
|
|
res = ExpressMock.ResponseMock();
|
|
|
|
res.send = sinon.spy();
|
|
|
|
res.json = sinon.spy();
|
|
|
|
res.status = sinon.spy();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("test registration request", () => {
|
|
|
|
it("should send back the registration request and save it in the session", function () {
|
|
|
|
const expectedRequest = {
|
|
|
|
test: "abc"
|
|
|
|
};
|
2017-10-18 04:24:02 +07:00
|
|
|
mocks.u2f.requestStub.returns(BluebirdPromise.resolve(expectedRequest));
|
|
|
|
return U2FRegisterRequestGet.default(vars)(req as any, res as any)
|
2017-09-22 03:07:34 +07:00
|
|
|
.then(function () {
|
2017-10-11 04:03:30 +07:00
|
|
|
Assert.deepEqual(expectedRequest, res.json.getCall(0).args[0]);
|
2017-05-25 20:09:29 +07:00
|
|
|
});
|
2017-09-22 03:07:34 +07:00
|
|
|
});
|
2017-05-25 20:09:29 +07:00
|
|
|
|
2017-10-11 04:03:30 +07:00
|
|
|
it("should return internal error on registration request", function () {
|
|
|
|
res.send = sinon.spy();
|
2017-09-22 03:07:34 +07:00
|
|
|
const user_key_container = {};
|
2017-10-18 04:24:02 +07:00
|
|
|
mocks.u2f.requestStub.returns(BluebirdPromise.reject("Internal error"));
|
|
|
|
return U2FRegisterRequestGet.default(vars)(req as any, res as any)
|
|
|
|
.then(function () {
|
|
|
|
Assert.equal(res.status.getCall(0).args[0], 200);
|
|
|
|
Assert.deepEqual(res.send.getCall(0).args[0], {
|
|
|
|
error: "Operation failed."
|
|
|
|
});
|
2017-10-11 04:03:30 +07:00
|
|
|
});
|
2017-09-22 03:07:34 +07:00
|
|
|
});
|
2017-05-25 20:09:29 +07:00
|
|
|
|
2017-09-22 03:07:34 +07:00
|
|
|
it("should return forbidden if identity has not been verified", function () {
|
2017-10-18 04:24:02 +07:00
|
|
|
req.session.auth.identity_check = undefined;
|
|
|
|
return U2FRegisterRequestGet.default(vars)(req as any, res as any)
|
2017-09-22 03:07:34 +07:00
|
|
|
.then(function () {
|
2017-10-11 04:03:30 +07:00
|
|
|
Assert.equal(403, res.status.getCall(0).args[0]);
|
2017-05-25 20:09:29 +07:00
|
|
|
});
|
|
|
|
});
|
2017-09-22 03:07:34 +07:00
|
|
|
});
|
2017-05-25 20:09:29 +07:00
|
|
|
});
|
|
|
|
|