|
| 1 | +import request from "supertest"; |
| 2 | +import { describe } from "mocha"; |
| 3 | +import { expect, sinon } from "../../../../test/utils/test-utils"; |
| 4 | +import nock = require("nock"); |
| 5 | +import * as cheerio from "cheerio"; |
| 6 | +import decache from "decache"; |
| 7 | + |
| 8 | +describe("Integration::share info", () => { |
| 9 | + let sandbox: sinon.SinonSandbox; |
| 10 | + let token: string | string[]; |
| 11 | + let cookies: string; |
| 12 | + let app: any; |
| 13 | + let baseApi: string; |
| 14 | + |
| 15 | + before(() => { |
| 16 | + decache("../../../app"); |
| 17 | + decache("../../../middleware/session-middleware"); |
| 18 | + const sessionMiddleware = require("../../../middleware/session-middleware"); |
| 19 | + sandbox = sinon.createSandbox(); |
| 20 | + sandbox |
| 21 | + .stub(sessionMiddleware, "validateSessionMiddleware") |
| 22 | + .callsFake(function (req: any, res: any, next: any): void { |
| 23 | + res.locals.sessionId = "tDy103saszhcxbQq0-mjdzU854"; |
| 24 | + res.locals.clientSessionId = "csy103saszhcxbQq0-mjdzU854"; |
| 25 | + req.session.user = { |
| 26 | + email: "test@test.com", |
| 27 | + }; |
| 28 | + next(); |
| 29 | + }); |
| 30 | + |
| 31 | + app = require("../../../app").createApp(); |
| 32 | + baseApi = process.env.API_BASE_URL; |
| 33 | + |
| 34 | + nock(baseApi) |
| 35 | + .get("/client-info") |
| 36 | + .once() |
| 37 | + .reply(200, { |
| 38 | + clientId: "client_test", |
| 39 | + clientName: "client_test_name", |
| 40 | + scopes: ["email", "phone"], |
| 41 | + }); |
| 42 | + |
| 43 | + request(app) |
| 44 | + .get("/share-info") |
| 45 | + .end((err, res) => { |
| 46 | + const $ = cheerio.load(res.text); |
| 47 | + token = $("[name=_csrf]").val(); |
| 48 | + cookies = res.headers["set-cookie"]; |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + beforeEach(() => { |
| 53 | + nock.cleanAll(); |
| 54 | + }); |
| 55 | + |
| 56 | + after(() => { |
| 57 | + sandbox.restore(); |
| 58 | + app = undefined; |
| 59 | + }); |
| 60 | + |
| 61 | + it("should return share info page", (done) => { |
| 62 | + nock(baseApi) |
| 63 | + .get("/client-info") |
| 64 | + .once() |
| 65 | + .reply(200, { |
| 66 | + clientId: "client_test", |
| 67 | + clientName: "client_test_name", |
| 68 | + scopes: ["email", "phone"], |
| 69 | + }); |
| 70 | + request(app).get("/share-info").expect(200, done); |
| 71 | + }); |
| 72 | + |
| 73 | + it("should return error when csrf not present", (done) => { |
| 74 | + request(app) |
| 75 | + .post("/share-info") |
| 76 | + .type("form") |
| 77 | + .send({ |
| 78 | + consentValue: "true", |
| 79 | + }) |
| 80 | + .expect(500, done); |
| 81 | + }); |
| 82 | + |
| 83 | + it("should return validation error when consentValue not selected", (done) => { |
| 84 | + request(app) |
| 85 | + .post("/share-info") |
| 86 | + .type("form") |
| 87 | + .set("Cookie", cookies) |
| 88 | + .send({ |
| 89 | + _csrf: token, |
| 90 | + consentValue: undefined, |
| 91 | + }) |
| 92 | + .expect(function (res) { |
| 93 | + const $ = cheerio.load(res.text); |
| 94 | + expect($("#share-info-error").text()).to.contains( |
| 95 | + "Select if you want to share your email address and phone number or not" |
| 96 | + ); |
| 97 | + }) |
| 98 | + .expect(400, done); |
| 99 | + }); |
| 100 | + |
| 101 | + it("should redirect to /auth-code page when consentValue valid", (done) => { |
| 102 | + nock(baseApi) |
| 103 | + .post("/update-profile") |
| 104 | + .once() |
| 105 | + .reply(200, { |
| 106 | + sessionState: "ADDED_CONSENT", |
| 107 | + }); |
| 108 | + |
| 109 | + request(app) |
| 110 | + .post("/share-info") |
| 111 | + .type("form") |
| 112 | + .set("Cookie", cookies) |
| 113 | + .send({ |
| 114 | + _csrf: token, |
| 115 | + consentValue: "true", |
| 116 | + }) |
| 117 | + .expect("Location", "/auth-code") |
| 118 | + .expect(302, done); |
| 119 | + }); |
| 120 | + |
| 121 | + it("should return internal server error when /update-profile API call response is 500", (done) => { |
| 122 | + nock(baseApi) |
| 123 | + .post("/update-profile") |
| 124 | + .once() |
| 125 | + .reply(500, {}); |
| 126 | + |
| 127 | + request(app) |
| 128 | + .post("/share-info") |
| 129 | + .type("form") |
| 130 | + .set("Cookie", cookies) |
| 131 | + .send({ |
| 132 | + _csrf: token, |
| 133 | + consentValue: "true", |
| 134 | + }) |
| 135 | + .expect(500, done); |
| 136 | + }); |
| 137 | + |
| 138 | + it("should return internal server error when /client-info API call response is 500", (done) => { |
| 139 | + nock(baseApi) |
| 140 | + .get("/client-info") |
| 141 | + .once() |
| 142 | + .reply(500, {}); |
| 143 | + request(app).get("/share-info").expect(500, done); |
| 144 | + }); |
| 145 | +}); |
0 commit comments