-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathbase-integration.test.ts
More file actions
200 lines (177 loc) · 6.7 KB
/
Copy pathbase-integration.test.ts
File metadata and controls
200 lines (177 loc) · 6.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import { expect } from "chai";
import * as cheerio from "cheerio";
import { describe } from "mocha";
import nock from "nock";
import request from "supertest";
import { sinon } from "../../../../../test/utils/test-utils.js";
import { CHANNEL, PATH_NAMES } from "../../../../app.constants.js";
import type { NextFunction, Request, Response } from "express";
import { getPermittedJourneyForPath } from "../../../../../test/helpers/session-helper.js";
import { buildMfaMethods } from "../../../../../test/helpers/mfa-helper.js";
import esmock from "esmock";
describe("Integration:: base page ", () => {
let app: any;
const setupApp = async (channel: string, showTestBanner?: boolean) => {
const maybeMockEnvBannerMiddleware =
showTestBanner != undefined
? {
"../../../../middleware/environment-banner-middleware.js": {
environmentBannerMiddleware: sinon.fake(function (
req: Request,
res: Response,
next: NextFunction
): void {
res.locals.showTestBanner = showTestBanner;
next();
}),
},
}
: {};
const { createApp } = await esmock(
"../../../../app.js",
{},
{
"../../../../middleware/session-middleware.js": {
validateSessionMiddleware: sinon.fake(function (
req: Request,
res: Response,
next: NextFunction
): void {
res.locals.sessionId = "tDy103saszhcxbQq0-mjdzU854";
if (channel === CHANNEL.WEB) {
res.locals.webChannel = true;
} else if (
channel === CHANNEL.STRATEGIC_APP ||
channel === CHANNEL.GENERIC_APP
) {
res.locals.isApp = true;
}
req.session.client = {
serviceType: "MANDATORY",
};
req.session.user = {
email: "test@test.com",
mfaMethods: buildMfaMethods({ phoneNumber: "7867" }),
journey: getPermittedJourneyForPath(PATH_NAMES.SIGN_IN_OR_CREATE),
};
next();
}),
},
...maybeMockEnvBannerMiddleware,
}
);
app = await createApp();
};
beforeEach(() => {
nock.cleanAll();
});
after(() => {
sinon.restore();
app = undefined;
});
it("should return the sign in or create page", async () => {
await setupApp(CHANNEL.WEB);
await request(app).get(PATH_NAMES.SIGN_IN_OR_CREATE).expect(200);
});
describe("Web channel", () => {
let $: any;
before(async () => {
await setupApp(CHANNEL.WEB, true);
const response = await request(app).get(PATH_NAMES.SIGN_IN_OR_CREATE);
expect(response.status).to.equal(200);
$ = cheerio.load(response.text);
});
it("should render the footer", async () => {
expect($(".govuk-footer").length).to.equal(1);
});
describe("when in a non-production environment", () => {
it("should render the test phase banner", async () => {
expect($(".govuk-phase-banner").length).to.equal(1);
expect($(".govuk-phase-banner").hasClass("test-banner")).to.be.true;
expect($(".govuk-phase-banner__content__tag").text().trim()).to.equal(
"test"
);
});
it("should render the test phase header css", async () => {
expect($("a.govuk-header__link").length).to.equal(1);
expect($(".strategic-app-header").length).to.equal(0);
expect($(".govuk-header").hasClass("test-banner")).to.be.true;
});
});
describe("when in a production environment", () => {
let $: any;
before(async () => {
await setupApp(CHANNEL.WEB, false);
const response = await request(app).get(PATH_NAMES.SIGN_IN_OR_CREATE);
expect(response.status).to.equal(200);
$ = cheerio.load(response.text);
});
it("should render the beta phase banner", async () => {
expect($(".govuk-phase-banner").length).to.equal(1);
expect($(".govuk-phase-banner").hasClass("test-banner")).to.be.false;
expect($(".govuk-phase-banner__content__tag").text().trim()).to.equal(
"beta"
);
});
it("should not render the test phase header css", async () => {
expect($(".govuk-header").hasClass("test-banner")).to.be.false;
expect($(".strategic-app-header").length).to.equal(0);
expect($("a.govuk-header__link").length).to.equal(1);
});
});
});
describe("App (strategic app and generic app) channels", () => {
const testCases = [CHANNEL.STRATEGIC_APP, CHANNEL.GENERIC_APP];
let $: any;
testCases.forEach((channel) => {
before(async () => {
await setupApp(channel, true);
const response = await request(app).get(PATH_NAMES.SIGN_IN_OR_CREATE);
expect(response.status).to.equal(200);
$ = cheerio.load(response.text);
});
it("should render the custom header with no links", async () => {
expect($("a.govuk-header__link").length).to.equal(0);
expect($(".strategic-app-header").length).to.equal(1);
});
it("should not render the footer", async () => {
expect($(".govuk-footer").length).to.equal(0);
});
describe("when in a non-production environment", () => {
it("should render the test phase header css", async () => {
expect($(".govuk-header").hasClass("test-banner")).to.be.true;
});
});
describe("when in a production environment", () => {
let $: any;
before(async () => {
await setupApp(CHANNEL.STRATEGIC_APP, false);
const response = await request(app).get(PATH_NAMES.SIGN_IN_OR_CREATE);
expect(response.status).to.equal(200);
$ = cheerio.load(response.text);
});
it("should not render the test phase header css", async () => {
expect($(".govuk-header").hasClass("test-banner")).to.be.false;
});
});
});
});
describe("Undefined channel", () => {
it("should render the default header if no value is provided for channel", async () => {
await setupApp("");
const response = await request(app).get(PATH_NAMES.SIGN_IN_OR_CREATE);
expect(response.status).to.equal(200);
const $ = cheerio.load(response.text);
expect($("a.govuk-header__link").length).to.equal(1);
});
});
describe("Cache-Control", () => {
it("should not be cached", async () => {
await setupApp("");
const response = await request(app).get(PATH_NAMES.SIGN_IN_OR_CREATE);
expect(response.header["cache-control"]).to.equal(
"no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0"
);
});
});
});