2017-05-25 13:09:29 +00:00
|
|
|
|
2017-07-16 12:55:01 +00:00
|
|
|
import PasswordResetFormPost = require("../../../../../src/server/lib/routes/password-reset/form/post");
|
2017-07-16 15:37:13 +00:00
|
|
|
import { PasswordUpdater } from "../../../../../src/server/lib/ldap/PasswordUpdater";
|
2017-07-16 12:55:01 +00:00
|
|
|
import AuthenticationSession = require("../../../../../src/server/lib/AuthenticationSession");
|
2017-07-16 15:37:13 +00:00
|
|
|
import { ServerVariables } from "../../../../../src/server/lib/ServerVariables";
|
2017-05-25 13:09:29 +00:00
|
|
|
import sinon = require("sinon");
|
|
|
|
import winston = require("winston");
|
|
|
|
import assert = require("assert");
|
|
|
|
import BluebirdPromise = require("bluebird");
|
|
|
|
|
|
|
|
import ExpressMock = require("../../mocks/express");
|
|
|
|
import { UserDataStore } from "../../mocks/UserDataStore";
|
|
|
|
import ServerVariablesMock = require("../../mocks/ServerVariablesMock");
|
|
|
|
|
|
|
|
describe("test reset password route", function () {
|
|
|
|
let req: ExpressMock.RequestMock;
|
|
|
|
let res: ExpressMock.ResponseMock;
|
2017-07-16 15:37:13 +00:00
|
|
|
let userDataStore: UserDataStore;
|
2017-05-25 13:09:29 +00:00
|
|
|
let configuration: any;
|
|
|
|
let authSession: AuthenticationSession.AuthenticationSession;
|
2017-07-16 15:37:13 +00:00
|
|
|
let serverVariables: ServerVariables;
|
2017-05-25 13:09:29 +00:00
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
req = {
|
|
|
|
body: {
|
|
|
|
userid: "user"
|
|
|
|
},
|
|
|
|
app: {
|
|
|
|
get: sinon.stub()
|
|
|
|
},
|
|
|
|
session: {},
|
|
|
|
headers: {
|
|
|
|
host: "localhost"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
AuthenticationSession.reset(req as any);
|
|
|
|
authSession = AuthenticationSession.get(req as any);
|
|
|
|
authSession.userid = "user";
|
|
|
|
authSession.email = "user@example.com";
|
|
|
|
authSession.first_factor = true;
|
|
|
|
authSession.second_factor = false;
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
inMemoryOnly: true
|
|
|
|
};
|
|
|
|
|
2017-07-16 15:37:13 +00:00
|
|
|
serverVariables = ServerVariablesMock.mock(req.app);
|
|
|
|
userDataStore = UserDataStore();
|
|
|
|
userDataStore.set_u2f_meta.returns(BluebirdPromise.resolve({}));
|
|
|
|
userDataStore.get_u2f_meta.returns(BluebirdPromise.resolve({}));
|
|
|
|
userDataStore.issue_identity_check_token.returns(BluebirdPromise.resolve({}));
|
|
|
|
userDataStore.consume_identity_check_token.returns(BluebirdPromise.resolve({}));
|
|
|
|
serverVariables.userDataStore = userDataStore as any;
|
2017-05-25 13:09:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
configuration = {
|
|
|
|
ldap: {
|
|
|
|
base_dn: "dc=example,dc=com",
|
|
|
|
user_name_attribute: "cn"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-07-16 15:37:13 +00:00
|
|
|
serverVariables.logger = winston;
|
|
|
|
serverVariables.config = configuration;
|
2017-05-25 13:09:29 +00:00
|
|
|
|
2017-07-16 15:37:13 +00:00
|
|
|
serverVariables.ldapPasswordUpdater = {
|
|
|
|
updatePassword: sinon.stub()
|
|
|
|
} as any;
|
2017-05-25 13:09:29 +00:00
|
|
|
|
|
|
|
res = ExpressMock.ResponseMock();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("test reset password post", () => {
|
|
|
|
it("should update the password and reset auth_session for reauthentication", function () {
|
|
|
|
authSession.identity_check = {
|
|
|
|
userid: "user",
|
|
|
|
challenge: "reset-password"
|
|
|
|
};
|
|
|
|
req.body = {};
|
|
|
|
req.body.password = "new-password";
|
|
|
|
|
2017-07-16 15:37:13 +00:00
|
|
|
(serverVariables.ldapPasswordUpdater.updatePassword as sinon.SinonStub).returns(BluebirdPromise.resolve());
|
2017-05-25 13:09:29 +00:00
|
|
|
return PasswordResetFormPost.default(req as any, res as any)
|
|
|
|
.then(function () {
|
|
|
|
const authSession = AuthenticationSession.get(req as any);
|
|
|
|
assert.equal(res.status.getCall(0).args[0], 204);
|
|
|
|
assert.equal(authSession.first_factor, false);
|
|
|
|
assert.equal(authSession.second_factor, false);
|
|
|
|
return BluebirdPromise.resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should fail if identity_challenge does not exist", function (done) {
|
|
|
|
authSession.identity_check = {
|
|
|
|
userid: "user",
|
|
|
|
challenge: undefined
|
|
|
|
};
|
|
|
|
res.send = sinon.spy(function () {
|
|
|
|
assert.equal(res.status.getCall(0).args[0], 403);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
PasswordResetFormPost.default(req as any, res as any);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should fail when ldap fails", function (done) {
|
|
|
|
authSession.identity_check = {
|
|
|
|
challenge: "reset-password",
|
|
|
|
userid: "user"
|
|
|
|
};
|
|
|
|
req.body = {};
|
|
|
|
req.body.password = "new-password";
|
|
|
|
|
2017-07-16 15:37:13 +00:00
|
|
|
(serverVariables.ldapPasswordUpdater.updatePassword as sinon.SinonStub).returns(BluebirdPromise.reject("Internal error with LDAP"));
|
2017-05-25 13:09:29 +00:00
|
|
|
res.send = sinon.spy(function () {
|
|
|
|
assert.equal(res.status.getCall(0).args[0], 500);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
PasswordResetFormPost.default(req as any, res as any);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|