discourse/test/javascripts/acceptance/password-reset-test.js.es6

146 lines
4.2 KiB
Plaintext
Raw Normal View History

import { acceptance } from "helpers/qunit-helpers";
2018-06-15 23:03:24 +08:00
import PreloadStore from "preload-store";
import { parsePostData } from "helpers/create-pretender";
acceptance("Password Reset", {
2017-06-15 01:57:58 +08:00
beforeEach() {
2018-06-15 23:03:24 +08:00
const response = object => {
return [200, { "Content-Type": "application/json" }, object];
};
2018-06-16 00:18:45 +08:00
// prettier-ignore
server.get("/u/confirm-email-token/myvalidtoken.json", () => { //eslint-disable-line
2018-06-15 23:03:24 +08:00
return response({ success: "OK" });
});
2018-06-16 00:18:45 +08:00
// prettier-ignore
server.put("/u/password-reset/myvalidtoken.json", request => { //eslint-disable-line
const body = parsePostData(request.requestBody);
if (body.password === "jonesyAlienSlayer") {
2018-06-15 23:03:24 +08:00
return response({
success: false,
errors: { password: ["is the name of your cat"] }
});
} else {
2018-06-15 23:03:24 +08:00
return response({
success: "OK",
message: I18n.t("password_reset.success")
});
}
});
2018-06-16 00:18:45 +08:00
// prettier-ignore
server.get("/u/confirm-email-token/requiretwofactor.json", () => { //eslint-disable-line
return response({ success: "OK" });
});
2018-06-16 00:18:45 +08:00
// prettier-ignore
server.put("/u/password-reset/requiretwofactor.json", request => { //eslint-disable-line
const body = parsePostData(request.requestBody);
2018-06-15 23:03:24 +08:00
if (
body.password === "perf3ctly5ecur3" &&
body.second_factor_token === "123123"
) {
return response({
success: "OK",
message: I18n.t("password_reset.success")
});
} else if (body.second_factor_token === "123123") {
return response({ success: false, errors: { password: ["invalid"] } });
} else {
return response({
success: false,
message: "invalid token",
2018-06-28 16:12:32 +08:00
errors: { user_second_factors: ["invalid token"] }
});
}
});
}
});
2017-06-15 01:57:58 +08:00
QUnit.test("Password Reset Page", assert => {
2018-06-15 23:03:24 +08:00
PreloadStore.store("password_reset", { is_developer: false });
2017-03-29 02:27:54 +08:00
visit("/u/password-reset/myvalidtoken");
andThen(() => {
2017-06-15 01:57:58 +08:00
assert.ok(exists(".password-reset input"), "shows the input");
});
2018-06-15 23:03:24 +08:00
fillIn(".password-reset input", "perf3ctly5ecur3");
andThen(() => {
2017-06-15 01:57:58 +08:00
assert.ok(exists(".password-reset .tip.good"), "input looks good");
});
2018-06-15 23:03:24 +08:00
fillIn(".password-reset input", "123");
andThen(() => {
2017-06-15 01:57:58 +08:00
assert.ok(exists(".password-reset .tip.bad"), "input is not valid");
2018-06-15 23:03:24 +08:00
assert.ok(
find(".password-reset .tip.bad")
.html()
.indexOf(I18n.t("user.password.too_short")) > -1,
"password too short"
);
});
2018-06-15 23:03:24 +08:00
fillIn(".password-reset input", "jonesyAlienSlayer");
click(".password-reset form button");
andThen(() => {
2017-06-15 01:57:58 +08:00
assert.ok(exists(".password-reset .tip.bad"), "input is not valid");
2018-06-15 23:03:24 +08:00
assert.ok(
find(".password-reset .tip.bad")
.html()
.indexOf("is the name of your cat") > -1,
"server validation error message shows"
);
});
2018-06-15 23:03:24 +08:00
fillIn(".password-reset input", "perf3ctly5ecur3");
click(".password-reset form button");
andThen(() => {
2017-06-15 01:57:58 +08:00
assert.ok(!exists(".password-reset form"), "form is gone");
});
});
QUnit.test("Password Reset Page With Second Factor", assert => {
2018-06-15 23:03:24 +08:00
PreloadStore.store("password_reset", {
is_developer: false,
second_factor_required: true
});
visit("/u/password-reset/requiretwofactor");
andThen(() => {
assert.notOk(exists("#new-account-password"), "does not show the input");
assert.ok(exists("#second-factor"), "shows the second factor prompt");
});
2018-06-28 16:12:32 +08:00
fillIn("input#second-factor", "0000");
2018-06-15 23:03:24 +08:00
click(".password-reset form button");
andThen(() => {
assert.ok(exists(".alert-error"), "shows 2 factor error");
assert.ok(
2018-06-15 23:03:24 +08:00
find(".alert-error")
.html()
.indexOf("invalid token") > -1,
"shows server validation error message"
);
});
2018-06-28 16:12:32 +08:00
fillIn("input#second-factor", "123123");
2018-06-15 23:03:24 +08:00
click(".password-reset form button");
andThen(() => {
assert.notOk(exists(".alert-error"), "hides error");
assert.ok(exists("#new-account-password"), "shows the input");
});
2018-06-15 23:03:24 +08:00
fillIn(".password-reset input", "perf3ctly5ecur3");
click(".password-reset form button");
andThen(() => {
assert.ok(!exists(".password-reset form"), "form is gone");
});
});