2017-05-25 13:09:29 +00:00
|
|
|
|
2017-10-06 22:09:42 +00:00
|
|
|
import PasswordResetFormPost = require("../../../src/lib/routes/password-reset/form/post");
|
|
|
|
import { PasswordUpdater } from "../../../src/lib/ldap/PasswordUpdater";
|
|
|
|
import AuthenticationSession = require("../../../src/lib/AuthenticationSession");
|
|
|
|
import { ServerVariablesHandler } from "../../../src/lib/ServerVariablesHandler";
|
|
|
|
import { UserDataStore } from "../../../src/lib/storage/UserDataStore";
|
2017-07-19 19:06:12 +00:00
|
|
|
import Sinon = require("sinon");
|
2017-05-25 13:09:29 +00:00
|
|
|
import winston = require("winston");
|
2017-10-10 21:03:30 +00:00
|
|
|
import Assert = require("assert");
|
2017-05-25 13:09:29 +00:00
|
|
|
import BluebirdPromise = require("bluebird");
|
|
|
|
|
|
|
|
import ExpressMock = require("../../mocks/express");
|
|
|
|
import ServerVariablesMock = require("../../mocks/ServerVariablesMock");
|
|
|
|
|
|
|
|
describe("test reset password route", function () {
|
|
|
|
let req: ExpressMock.RequestMock;
|
|
|
|
let res: ExpressMock.ResponseMock;
|
|
|
|
let configuration: any;
|
2017-07-19 19:06:12 +00:00
|
|
|
let serverVariables: ServerVariablesMock.ServerVariablesMock;
|
2017-05-25 13:09:29 +00:00
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
req = {
|
|
|
|
body: {
|
|
|
|
userid: "user"
|
|
|
|
},
|
|
|
|
app: {
|
2017-09-21 20:07:34 +00:00
|
|
|
get: Sinon.stub().returns({ logger: winston })
|
2017-05-25 13:09:29 +00:00
|
|
|
},
|
|
|
|
session: {},
|
|
|
|
headers: {
|
|
|
|
host: "localhost"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
AuthenticationSession.reset(req as any);
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
inMemoryOnly: true
|
|
|
|
};
|
|
|
|
|
2017-07-16 15:37:13 +00:00
|
|
|
serverVariables = ServerVariablesMock.mock(req.app);
|
2017-07-19 19:06:12 +00:00
|
|
|
serverVariables.userDataStore.saveU2FRegistrationStub.returns(BluebirdPromise.resolve({}));
|
|
|
|
serverVariables.userDataStore.retrieveU2FRegistrationStub.returns(BluebirdPromise.resolve({}));
|
|
|
|
serverVariables.userDataStore.produceIdentityValidationTokenStub.returns(BluebirdPromise.resolve({}));
|
|
|
|
serverVariables.userDataStore.consumeIdentityValidationTokenStub.returns(BluebirdPromise.resolve({}));
|
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.config = configuration;
|
2017-05-25 13:09:29 +00:00
|
|
|
|
2017-07-16 15:37:13 +00:00
|
|
|
serverVariables.ldapPasswordUpdater = {
|
2017-07-19 19:06:12 +00:00
|
|
|
updatePassword: Sinon.stub()
|
2017-07-16 15:37:13 +00:00
|
|
|
} as any;
|
2017-05-25 13:09:29 +00:00
|
|
|
|
|
|
|
res = ExpressMock.ResponseMock();
|
2017-09-21 20:07:34 +00:00
|
|
|
AuthenticationSession.get(req as any)
|
|
|
|
.then(function (authSession: AuthenticationSession.AuthenticationSession) {
|
|
|
|
authSession.userid = "user";
|
|
|
|
authSession.email = "user@example.com";
|
|
|
|
authSession.first_factor = true;
|
|
|
|
authSession.second_factor = false;
|
|
|
|
});
|
2017-05-25 13:09:29 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("test reset password post", () => {
|
|
|
|
it("should update the password and reset auth_session for reauthentication", function () {
|
|
|
|
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-09-21 20:07:34 +00:00
|
|
|
|
|
|
|
return AuthenticationSession.get(req as any)
|
|
|
|
.then(function (authSession) {
|
|
|
|
authSession.identity_check = {
|
|
|
|
userid: "user",
|
|
|
|
challenge: "reset-password"
|
|
|
|
};
|
|
|
|
return PasswordResetFormPost.default(req as any, res as any);
|
|
|
|
})
|
2017-05-25 13:09:29 +00:00
|
|
|
.then(function () {
|
2017-09-21 20:07:34 +00:00
|
|
|
return AuthenticationSession.get(req as any);
|
|
|
|
}).then(function (_authSession: AuthenticationSession.AuthenticationSession) {
|
2017-10-10 21:03:30 +00:00
|
|
|
Assert.equal(res.status.getCall(0).args[0], 204);
|
|
|
|
Assert.equal(_authSession.first_factor, false);
|
|
|
|
Assert.equal(_authSession.second_factor, false);
|
2017-05-25 13:09:29 +00:00
|
|
|
return BluebirdPromise.resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-09-21 20:07:34 +00:00
|
|
|
it("should fail if identity_challenge does not exist", function () {
|
|
|
|
return AuthenticationSession.get(req as any)
|
|
|
|
.then(function (authSession) {
|
|
|
|
authSession.identity_check = {
|
|
|
|
userid: "user",
|
|
|
|
challenge: undefined
|
|
|
|
};
|
|
|
|
return PasswordResetFormPost.default(req as any, res as any);
|
|
|
|
})
|
|
|
|
.then(function () {
|
2017-10-10 21:03:30 +00:00
|
|
|
Assert.equal(res.status.getCall(0).args[0], 200);
|
|
|
|
Assert.deepEqual(res.send.getCall(0).args[0], {
|
|
|
|
error: "An error occurred during password reset. Your password has not been changed."
|
|
|
|
});
|
2017-09-21 20:07:34 +00:00
|
|
|
});
|
2017-05-25 13:09:29 +00:00
|
|
|
});
|
|
|
|
|
2017-09-21 20:07:34 +00:00
|
|
|
it("should fail when ldap fails", function () {
|
2017-05-25 13:09:29 +00:00
|
|
|
req.body = {};
|
|
|
|
req.body.password = "new-password";
|
|
|
|
|
2017-09-21 20:07:34 +00:00
|
|
|
(serverVariables.ldapPasswordUpdater.updatePassword as Sinon.SinonStub)
|
|
|
|
.returns(BluebirdPromise.reject("Internal error with LDAP"));
|
|
|
|
|
|
|
|
return AuthenticationSession.get(req as any)
|
|
|
|
.then(function (authSession) {
|
|
|
|
authSession.identity_check = {
|
|
|
|
challenge: "reset-password",
|
|
|
|
userid: "user"
|
|
|
|
};
|
|
|
|
return PasswordResetFormPost.default(req as any, res as any);
|
|
|
|
}).then(function () {
|
2017-10-10 21:03:30 +00:00
|
|
|
Assert.equal(res.status.getCall(0).args[0], 200);
|
|
|
|
Assert.deepEqual(res.send.getCall(0).args[0], {
|
|
|
|
error: "An error occurred during password reset. Your password has not been changed."
|
|
|
|
});
|
2017-09-21 20:07:34 +00:00
|
|
|
return BluebirdPromise.resolve();
|
|
|
|
});
|
2017-05-25 13:09:29 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|