2017-05-25 20:09:29 +07:00
|
|
|
|
2017-10-07 05:09:42 +07:00
|
|
|
import PasswordResetFormPost = require("../../../src/lib/routes/password-reset/form/post");
|
|
|
|
import { PasswordUpdater } from "../../../src/lib/ldap/PasswordUpdater";
|
2017-10-22 22:42:05 +07:00
|
|
|
import { AuthenticationSessionHandler } from "../../../src/lib/AuthenticationSessionHandler";
|
|
|
|
import { AuthenticationSession } from "../../../types/AuthenticationSession";
|
2017-10-07 05:09:42 +07:00
|
|
|
import { UserDataStore } from "../../../src/lib/storage/UserDataStore";
|
2017-07-20 02:06:12 +07:00
|
|
|
import Sinon = require("sinon");
|
2017-10-11 04:03:30 +07:00
|
|
|
import Assert = require("assert");
|
2017-05-25 20:09:29 +07:00
|
|
|
import BluebirdPromise = require("bluebird");
|
|
|
|
import ExpressMock = require("../../mocks/express");
|
2017-10-18 04:24:02 +07:00
|
|
|
import { ServerVariablesMock, ServerVariablesMockBuilder } from "../../mocks/ServerVariablesMockBuilder";
|
|
|
|
import { ServerVariables } from "../../../src/lib/ServerVariables";
|
2017-05-25 20:09:29 +07:00
|
|
|
|
|
|
|
describe("test reset password route", function () {
|
|
|
|
let req: ExpressMock.RequestMock;
|
|
|
|
let res: ExpressMock.ResponseMock;
|
2017-10-18 04:24:02 +07:00
|
|
|
let vars: ServerVariables;
|
|
|
|
let mocks: ServerVariablesMock;
|
2017-10-22 22:42:05 +07:00
|
|
|
let authSession: AuthenticationSession;
|
2017-05-25 20:09:29 +07:00
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
req = {
|
|
|
|
body: {
|
|
|
|
userid: "user"
|
|
|
|
},
|
|
|
|
session: {},
|
|
|
|
headers: {
|
|
|
|
host: "localhost"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-10-18 04:24:02 +07:00
|
|
|
const s = ServerVariablesMockBuilder.build();
|
|
|
|
mocks = s.mocks;
|
|
|
|
vars = s.variables;
|
2017-05-25 20:09:29 +07:00
|
|
|
|
|
|
|
const options = {
|
|
|
|
inMemoryOnly: true
|
|
|
|
};
|
|
|
|
|
2017-10-18 04:24:02 +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({}));
|
|
|
|
|
|
|
|
mocks.config.ldap = {
|
|
|
|
url: "ldap://ldapjs",
|
|
|
|
mail_attribute: "mail",
|
|
|
|
user: "user",
|
|
|
|
password: "password",
|
|
|
|
users_dn: "ou=users,dc=example,dc=com",
|
|
|
|
groups_dn: "ou=groups,dc=example,dc=com",
|
|
|
|
users_filter: "user",
|
|
|
|
group_name_attribute: "cn",
|
|
|
|
groups_filter: "groups"
|
2017-05-25 20:09:29 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
res = ExpressMock.ResponseMock();
|
2017-10-22 22:42:05 +07:00
|
|
|
authSession = AuthenticationSessionHandler.get(req as any, vars.logger);
|
|
|
|
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 reset password post", () => {
|
|
|
|
it("should update the password and reset auth_session for reauthentication", function () {
|
|
|
|
req.body = {};
|
|
|
|
req.body.password = "new-password";
|
|
|
|
|
2017-10-18 04:24:02 +07:00
|
|
|
mocks.ldapPasswordUpdater.updatePasswordStub.returns(BluebirdPromise.resolve());
|
2017-09-22 03:07:34 +07:00
|
|
|
|
2017-10-22 22:42:05 +07:00
|
|
|
authSession.identity_check = {
|
|
|
|
userid: "user",
|
|
|
|
challenge: "reset-password"
|
|
|
|
};
|
|
|
|
return PasswordResetFormPost.default(vars)(req as any, res as any)
|
2017-05-25 20:09:29 +07:00
|
|
|
.then(function () {
|
2017-10-18 04:24:02 +07:00
|
|
|
return AuthenticationSessionHandler.get(req as any, vars.logger);
|
|
|
|
}).then(function (_authSession) {
|
2017-10-11 04:03:30 +07: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 20:09:29 +07:00
|
|
|
return BluebirdPromise.resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-09-22 03:07:34 +07:00
|
|
|
it("should fail if identity_challenge does not exist", function () {
|
2017-10-22 22:42:05 +07:00
|
|
|
authSession.identity_check = {
|
|
|
|
userid: "user",
|
|
|
|
challenge: undefined
|
|
|
|
};
|
|
|
|
return PasswordResetFormPost.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(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-22 03:07:34 +07:00
|
|
|
});
|
2017-05-25 20:09:29 +07:00
|
|
|
});
|
|
|
|
|
2017-09-22 03:07:34 +07:00
|
|
|
it("should fail when ldap fails", function () {
|
2017-05-25 20:09:29 +07:00
|
|
|
req.body = {};
|
|
|
|
req.body.password = "new-password";
|
|
|
|
|
2017-10-18 04:24:02 +07:00
|
|
|
mocks.ldapPasswordUpdater.updatePasswordStub
|
2017-09-22 03:07:34 +07:00
|
|
|
.returns(BluebirdPromise.reject("Internal error with LDAP"));
|
|
|
|
|
2017-10-22 22:42:05 +07:00
|
|
|
authSession.identity_check = {
|
|
|
|
challenge: "reset-password",
|
|
|
|
userid: "user"
|
|
|
|
};
|
|
|
|
return PasswordResetFormPost.default(vars)(req as any, res as any)
|
|
|
|
.then(function () {
|
2017-10-11 04:03:30 +07: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-22 03:07:34 +07:00
|
|
|
return BluebirdPromise.resolve();
|
|
|
|
});
|
2017-05-25 20:09:29 +07:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|