UNPKG

realm-object-server

Version:

Realm Object Server

229 lines 11.5 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const chai_1 = require("chai"); const faker = require("faker"); const superagent = require("superagent"); const TestServer_1 = require("../../TestServer"); const auth_1 = require("../../auth"); const _1 = require("."); describe("PasswordAuthUIService", function () { let server; let provider; let emailsSent = []; const sendMail = (type) => (email, token) => __awaiter(this, void 0, void 0, function* () { emailsSent.push({ type, email, token }); }); const getLatestEmail = (type, email) => { return emailsSent .reverse() .find(mail => mail.type === type && mail.email === email); }; const expectRedirect = (res, check) => __awaiter(this, void 0, void 0, function* () { try { yield res.redirects(0); return Promise.reject(new Error("Expected a redirect")); } catch (err) { if (err.response && err.response.headers && err.response.headers.location) { const success = check(err.response.headers.location); if (success) { return Promise.resolve(); } else { return Promise.reject(new Error("Redirection check failed")); } } else { return Promise.reject(new Error("Expected a location header")); } } }); before(function () { return __awaiter(this, void 0, void 0, function* () { emailsSent = []; server = new TestServer_1.TestServer(); provider = new auth_1.PasswordAuthProvider({ emailHandler: { confirmEmail: sendMail("confirm-email"), resetPassword: sendMail("reset-password"), } }); const service = new _1.PasswordAuthUIService(); server.addService(service); yield server.start({ authProviders: [provider], }); }); }); after(function () { return __awaiter(this, void 0, void 0, function* () { if (server) { yield server.shutdown(); } }); }); describe("without a valid user", () => { describe("GET /confirm-email", () => { it("should respond with 400 Bad Request", () => __awaiter(this, void 0, void 0, function* () { const url = `${server.url}/confirm-email`; chai_1.expect(superagent.get(url)).to.eventually.be.rejectedWith("Bad Request"); })); }); }); describe("with a valid user", () => { const email = "testing-" + faker.internet.email(); const password = faker.internet.password(); before(() => __awaiter(this, void 0, void 0, function* () { yield provider.authenticateOrCreateUser({ username: email, password, }); })); describe("GET /confirm-email", () => { it("should not be confirmed before confirming", () => __awaiter(this, void 0, void 0, function* () { const user = yield provider.authenticateOrCreateUser({ username: email, password, }); chai_1.expect(user.metadata).to.have.length(0); })); it("should respond with HTML", () => __awaiter(this, void 0, void 0, function* () { const confirmationMail = getLatestEmail("confirm-email", email); const url = `${server.url}/confirm-email?token=${confirmationMail.token}`; const res = yield superagent.get(url); chai_1.expect(res.status).to.equal(200); chai_1.expect(res.type).to.equal("text/html"); chai_1.expect(res.text).to.contain("Your email was confirmed!"); })); it("should not be confirmed before confirming", () => __awaiter(this, void 0, void 0, function* () { const user = yield provider.authenticateOrCreateUser({ username: email, password, }); chai_1.expect(user.metadata).to.have.length(1); const confirmation = user.metadata.find(item => item.key === "isEmailConfirmed"); chai_1.expect(confirmation.value).to.be.equal("true"); })); }); describe("GET /reset-password", () => { it("should respond with HTML", () => __awaiter(this, void 0, void 0, function* () { const url = `${server.url}/reset-password`; const res = yield superagent.get(url); chai_1.expect(res.status).to.equal(200); chai_1.expect(res.type).to.equal("text/html"); chai_1.expect(res.text).to.contain("<h1>Reset password</h1>"); })); }); describe("POST /reset-password", () => { const newPassword = faker.internet.password(); it("should not fail if email is unknown", () => __awaiter(this, void 0, void 0, function* () { const emailsSentBefore = emailsSent.length; const url = `${server.url}/reset-password`; const res = yield superagent.post(url) .type("form") .send({ email: "testing-with-an-unknown-email@realm.io" }); chai_1.expect(res.status).to.equal(200); chai_1.expect(res.type).to.equal("text/html"); chai_1.expect(res.text).to.contain("<h1>Reset password</h1>"); chai_1.expect(res.text).to.contain("Please check your inbox"); chai_1.expect(emailsSent.length).to.equal(emailsSentBefore); })); it("should send an email if email is known", () => __awaiter(this, void 0, void 0, function* () { const emailsSentBefore = emailsSent.length; const url = `${server.url}/reset-password`; const res = yield superagent.post(url) .type("form") .send({ email }); chai_1.expect(res.status).to.equal(200); chai_1.expect(res.type).to.equal("text/html"); chai_1.expect(res.text).to.contain("<h1>Reset password</h1>"); chai_1.expect(res.text).to.contain("Please check your inbox"); chai_1.expect(emailsSent.length).to.equal(emailsSentBefore + 1); })); it("should authenticate using the initial password", () => __awaiter(this, void 0, void 0, function* () { yield provider.authenticateOrCreateUser({ username: email, password, }); })); it("should reset the password", () => __awaiter(this, void 0, void 0, function* () { const resetPasswordMail = getLatestEmail("reset-password", email); const url = `${server.url}/reset-password`; const res = yield superagent.post(url) .type("form") .send({ token: resetPasswordMail.token, password: newPassword, "password-repeated": newPassword, }); chai_1.expect(res.status).to.equal(200); chai_1.expect(res.type).to.equal("text/html"); chai_1.expect(res.text).to.contain("<h1>Password was changed</h1>"); })); it("should no longer authenticate using the initial password", () => __awaiter(this, void 0, void 0, function* () { const result = provider.authenticateOrCreateUser({ username: email, password, }); yield chai_1.assert.isRejected(result); })); it("should now authenticate using the new password", () => __awaiter(this, void 0, void 0, function* () { yield provider.authenticateOrCreateUser({ username: email, password: newPassword, }); })); it("should send an email and redirect if redirectUrl is sat", () => __awaiter(this, void 0, void 0, function* () { const redirectBaseUrl = "http://ros-tests-fake-server.realm.io"; const redirectUrl = `${redirectBaseUrl}/reset-password`; const url = `${server.url}/reset-password?redirectUrl=${redirectUrl}`; const req = superagent.post(url) .type("form") .send({ email, "redirect-url": redirectUrl }); yield expectRedirect(req, url => url === redirectUrl); })); it("should redirect with success=false if failing and redirectUrl is sat", () => __awaiter(this, void 0, void 0, function* () { const redirectBaseUrl = "http://ros-tests-fake-server.realm.io"; const redirectUrl = `${redirectBaseUrl}/reset-password`; const url = `${server.url}/reset-password?redirectUrl=${redirectUrl}`; const req = superagent.post(url) .type("form") .send({ token: "invalid-token", password: "verysecure", "password-repeated": "verysecure", "redirect-url": redirectUrl, }); yield expectRedirect(req, url => { return url.indexOf(redirectUrl) === 0 && url.indexOf("success=false") >= 0; }); })); it("should redirect with success=true if succeding and redirectUrl is sat", () => __awaiter(this, void 0, void 0, function* () { const redirectBaseUrl = "http://ros-tests-fake-server.realm.io"; const redirectUrl = `${redirectBaseUrl}/reset-password`; const url = `${server.url}/reset-password?redirectUrl=${redirectUrl}`; const resetPasswordMail = getLatestEmail("reset-password", email); const req = superagent.post(url) .type("form") .send({ token: resetPasswordMail.token, password: "verysecure", "password-repeated": "verysecure", "redirect-url": redirectUrl, }); yield expectRedirect(req, url => { return url.indexOf(redirectUrl) === 0 && url.indexOf("success=true") >= 0; }); })); }); }); }); //# sourceMappingURL=PasswordAuthUIService.spec.js.map