-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsign_in.1.cy.ts
416 lines (384 loc) · 14.8 KB
/
sign_in.1.cy.ts
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import * as SignIn from '../shared/sign_in.shared';
describe('Sign in flow, Okta enabled', () => {
context('Terms and Conditions links', () => {
it(...SignIn.linksToTheGoogleTermsOfServicePage());
it(...SignIn.linksToTheGooglePrivacyPolicyPage());
it(...SignIn.linksToTheGuardianTermsAndConditionsPage());
it(...SignIn.linksToTheGuardianPrivacyPolicyPage());
it(
...SignIn.linksToTheGuardianJobsTermsAndConditionsPageWhenJobsClientIdSet(),
);
it(...SignIn.linksToTheGuardianJobsPrivacyPolicyPageWhenJobsClientIdSet());
});
it(...SignIn.persistsTheClientIdWhenNavigatingAway());
it(...SignIn.appliesFormValidationToEmailAndPasswordInputFields());
it(...SignIn.showsAMessageWhenCredentialsAreInvalid());
it(...SignIn.correctlySignsInAnExistingUser());
it(...SignIn.navigatesToResetPassword());
it(...SignIn.navigatesToRegistration());
it(...SignIn.respectsTheReturnUrlQueryParam());
it(...SignIn.removesEncryptedEmailParameterFromQueryString());
it(
...SignIn.removesEncryptedEmailParameterAndPreservesAllOtherValidParameters(),
);
it(
...SignIn.showsRecaptchaErrorsWhenTheUserTriesToSignInOfflineAndAllowsSignInWhenBackOnline(),
);
it(...SignIn.hitsAccessTokenRateLimitAndRecoversTokenAfterTimeout());
it(...SignIn.redirectsCorrectlyForSocialSignIn());
it(
...SignIn.showsAnErrorMessageAndInformationParagraphWhenAccountLinkingRequiredErrorParameterIsPresent(),
);
it(
...SignIn.doesNotDisplaySocialButtonsWhenAccountLinkingRequiredErrorParameterIsPresent(),
);
it('sets emailValidated flag on oauth callback', () => {
// this is a specific test case for new user registrations in Okta
// In Okta new social registered users are added to the GuardianUser-EmailValidated group
// by default, but the custom emailValidated field is not defined/set to false
// this causes problems in legacy code, where the emailValidated flag is not set but the group is
// so we need to set the flag to true when the user is added to the group
// we do this on the oauth callback route /oauth/authorization-code/callback
// where we update the user profile with the emailValidated flag if the user is in the GuardianUser-EmailValidated group but the emailValidated is falsy
// This test checks this behaviour by first getting a user into this state
// i.e user.profile.emailValidated = false, and user groups has GuardianUser-EmailValidated
// first we have to get the id of the GuardianUser-EmailValidated group
cy.findEmailValidatedOktaGroupId().then((groupId) => {
// next we create a test user
cy.createTestUser({}).then(({ emailAddress, finalPassword }) => {
// we get the user profile object from Okta
cy.getTestOktaUser(emailAddress).then((user) => {
const { id, profile } = user;
// check the user profile has the emailValidated flag set to false
expect(profile.emailValidated).to.be.false;
// next check the user groups
cy.getOktaUserGroups(id).then((groups) => {
// make sure the user is not in the GuardianUser-EmailValidated group
const group = groups.find((g) => g.id === groupId);
expect(group).not.to.exist;
// and add them to the group if this is the case
cy.addOktaUserToGroup(id, groupId);
// at this point the user is in the correct state
// so we attempt to sign them in
cy.visit(
`/signin?returnUrl=${encodeURIComponent(
`https://${Cypress.env('BASE_URI')}/consents`,
)}`,
);
cy.get('input[name=email]').type(emailAddress);
cy.get('input[name=password]').type(finalPassword);
cy.get('[data-cy="main-form-submit-button"]').click();
cy.url().should('include', '/consents');
// at this point the oauth callback route will have run, so we can recheck the user profile to see if the emailValidated flag has been set
cy.getTestOktaUser(id).then((user) => {
const { profile } = user;
expect(profile.emailValidated).to.be.true;
});
// and the user should also be in the group
cy.getOktaUserGroups(id).then((groups) => {
const group = groups.find((g) => g.id === groupId);
expect(group).to.exist;
});
});
});
});
});
});
context('Okta session refresh', () => {
it('refreshes a valid Okta session', () => {
// Create a validated test user
cy.createTestUser({ isUserEmailValidated: true }).then(
({ emailAddress, finalPassword }) => {
// Sign our new user in
cy.visit(
`/signin?returnUrl=${encodeURIComponent(
`https://${Cypress.env('BASE_URI')}/consents`,
)}`,
);
cy.get('input[name=email]').type(emailAddress);
cy.get('input[name=password]').type(finalPassword);
cy.get('[data-cy="main-form-submit-button"]').click();
cy.url().should('include', '/consents');
// Get the current session data
cy.getCookie('idx').then((orignalIdxCookie) => {
expect(orignalIdxCookie).to.exist;
// we want to check the cookie is being set as a persistent cookie and not a session cookie, hence the expiry check
expect(orignalIdxCookie?.expiry).to.exist;
// Refresh our user session
cy.visit(
`/signin/refresh?returnUrl=${encodeURIComponent(
`https://${Cypress.env('BASE_URI')}/consents`,
)}`,
);
cy.url().should('include', '/consents');
// Get the refreshed session data
cy.getCookie('idx').then((newIdxCookie) => {
expect(newIdxCookie).to.exist;
// `idx` cookie doesn't have same value as original when refreshed, which is different to the Okta Classic `idx` cookie
expect(newIdxCookie?.value).to.not.equal(orignalIdxCookie?.value);
// we want to check the cookie is being set as a persistent cookie and not a session cookie, hence the expiry check
expect(newIdxCookie?.expiry).to.exist;
if (newIdxCookie?.expiry && orignalIdxCookie?.expiry) {
expect(newIdxCookie?.expiry).to.be.greaterThan(
orignalIdxCookie?.expiry,
);
}
});
});
},
);
});
it('sends a client with the Okta cookie and an invalid Okta session to the redirectUrl', () => {
// Create a validated test user
cy.createTestUser({ isUserEmailValidated: true }).then(
({ emailAddress, finalPassword }) => {
// Sign our new user in
cy.visit(
`/signin?returnUrl=${encodeURIComponent(
`https://${Cypress.env('BASE_URI')}/consents`,
)}`,
);
cy.get('input[name=email]').type(emailAddress);
cy.get('input[name=password]').type(finalPassword);
cy.get('[data-cy="main-form-submit-button"]').click();
cy.url().should('include', '/consents');
// Get the current session data
cy.getCookie('idx').then((idxCookie) => {
// Close the user's current session in Okta
cy.closeCurrentOktaSession({
idx: idxCookie?.value,
}).then(() => {
// Refresh our user session
cy.visit(
`/signin/refresh?returnUrl=${encodeURIComponent(
`https://${Cypress.env('BASE_URI')}/reset-password`,
)}`,
);
cy.url().should('include', '/reset-password');
});
});
},
);
});
it('sends a client without the Okta cookie to the redirectUrl', () => {
// Create a validated test user
cy.createTestUser({ isUserEmailValidated: true }).then(
({ emailAddress, finalPassword }) => {
// Sign our new user in
cy.visit(
`/signin?returnUrl=${encodeURIComponent(
`https://${Cypress.env('BASE_URI')}/consents`,
)}`,
);
cy.get('input[name=email]').type(emailAddress);
cy.get('input[name=password]').type(finalPassword);
cy.get('[data-cy="main-form-submit-button"]').click();
cy.url().should('include', '/consents');
// Delete the Okta idx cookie
// strange behaviour form Cypress 12
// where we need to delete cookie from both domains
// to get the test to pass
// cypress 12 seems to have issues with hostOnly cookies not being removed or persisting after clear
// https://github.com/cypress-io/cypress/issues/25174
cy.clearCookie('idx', {
domain: Cypress.env('BASE_URI'),
});
cy.clearCookie('idx', {
domain: `.${Cypress.env('BASE_URI')}`,
});
// Visit the refresh endpoint
cy.visit(
`/signin/refresh?returnUrl=${encodeURIComponent(
`https://${Cypress.env('BASE_URI')}/reset-password`,
)}`,
);
cy.url().should('include', '/reset-password');
cy.getCookie('idx').should('not.exist');
},
);
});
it('sends a client with neither Okta nor Identity cookies to /signin', () => {
// Create a validated test user
cy.createTestUser({ isUserEmailValidated: true }).then(
({ emailAddress, finalPassword }) => {
// Sign our new user in
cy.visit(
`/signin?returnUrl=${encodeURIComponent(
`https://${Cypress.env('BASE_URI')}/consents`,
)}`,
);
cy.get('input[name=email]').type(emailAddress);
cy.get('input[name=password]').type(finalPassword);
cy.get('[data-cy="main-form-submit-button"]').click();
cy.url().should('include', '/consents');
// Delete all cookies (Okta and IDAPI)
cy.clearCookies();
// Visit the refresh endpoint
cy.visit(
`/signin/refresh?returnUrl=${encodeURIComponent(
`https://${Cypress.env('BASE_URI')}/consents`,
)}`,
);
cy.url().should('include', '/signin');
cy.getCookie('idx').should('not.exist');
cy.getCookie('sc_gu_u').should('not.exist');
cy.getCookie('sc_gu_la').should('not.exist');
},
);
});
it('leaves the last access cookie unchanged when refreshing a valid Okta session', () => {
// Create a validated test user
cy.createTestUser({ isUserEmailValidated: true }).then(
({ emailAddress, finalPassword }) => {
// Sign our new user in
cy.visit(
`/signin?returnUrl=${encodeURIComponent(
`https://${Cypress.env('BASE_URI')}/consents`,
)}`,
);
cy.get('input[name=email]').type(emailAddress);
cy.get('input[name=password]').type(finalPassword);
cy.get('[data-cy="main-form-submit-button"]').click();
cy.url().should('include', '/consents');
// Get the current session data
cy.getCookie('SC_GU_LA').then((originalLastAccessCookie) => {
cy.getCookie('SC_GU_U').then((originalSecureIdapiCookie) => {
expect(originalLastAccessCookie).to.exist;
expect(originalSecureIdapiCookie).to.exist;
// Refresh our user session
cy.visit(
`/signin/refresh?returnUrl=${encodeURIComponent(
`https://${Cypress.env('BASE_URI')}/consents`,
)}`,
);
cy.url().should('include', '/consents');
// Expect the last access cookie to be unchanged
cy.getCookie('SC_GU_LA').then((lastAccessCookie) => {
expect(lastAccessCookie).to.exist;
expect(lastAccessCookie?.value).to.equal(
originalLastAccessCookie?.value,
);
expect(lastAccessCookie?.expiry).to.equal(
originalLastAccessCookie?.expiry,
);
});
// Expect other Idapi cookies to have changed
cy.getCookie('SC_GU_U').then((secureIdapiCookie) => {
expect(secureIdapiCookie).to.exist;
expect(secureIdapiCookie?.value).not.to.equal(
originalSecureIdapiCookie?.value,
);
if (
secureIdapiCookie?.expiry &&
originalSecureIdapiCookie?.expiry
) {
expect(secureIdapiCookie?.expiry).to.be.greaterThan(
originalSecureIdapiCookie?.expiry,
);
}
});
});
});
},
);
});
});
context('Okta unvalidated email flow', () => {
it('Sends a user with an unvalidated email a reset password email on sign in', () => {
cy
.createTestUser({
isUserEmailValidated: false,
})
?.then(({ emailAddress, finalPassword }) => {
const timeRequestWasMade = new Date();
cy.visit('/signin');
cy.get('input[name=email]').type(emailAddress);
cy.get('input[name=password]').type(finalPassword);
cy.get('[data-cy="main-form-submit-button"]').click();
cy.url().should('include', '/signin/email-sent');
cy.contains(
'For security reasons we need you to change your password.',
);
cy.contains(emailAddress);
cy.contains('send again');
cy.contains('try another address');
// Ensure the user's authentication cookies are not set
cy.getCookie('idx').then((idxCookie) => {
expect(idxCookie).to.not.exist;
cy.checkForEmailAndGetDetails(
emailAddress,
timeRequestWasMade,
/reset-password\/([^"]*)/,
).then(({ links, body, token }) => {
expect(body).to.have.string(
'Because your security is extremely important to us, we have changed our password policy.',
);
expect(body).to.have.string('Reset password');
expect(links.length).to.eq(2);
const resetPasswordLink = links.find((s) =>
s.text?.includes('Reset password'),
);
expect(resetPasswordLink?.href ?? '').to.have.string(
'reset-password',
);
cy.visit(`/reset-password/${token}`);
cy.contains(emailAddress);
cy.contains('Reset password');
});
});
});
});
});
context('Okta session exists on /signin', () => {
beforeEach(() => {
// Intercept the external redirect page.
// We just want to check that the redirect happens, not that the page loads.
cy.intercept('GET', 'https://m.code.dev-theguardian.com/', (req) => {
req.reply(200);
});
});
it('shows the signed in as page', () => {
// Create a validated test user
cy.createTestUser({ isUserEmailValidated: true }).then(
({ emailAddress, finalPassword }) => {
// Sign our new user in
cy.visit(
`/signin?returnUrl=${encodeURIComponent(
`https://${Cypress.env('BASE_URI')}/consents`,
)}`,
);
cy.get('input[name=email]').type(emailAddress);
cy.get('input[name=password]').type(finalPassword);
cy.get('[data-cy="main-form-submit-button"]').click();
cy.url().should('include', '/consents');
// Get the current session data
cy.getCookie('idx').then((originalIdxCookie) => {
expect(originalIdxCookie).to.exist;
// Visit sign in again
cy.visit(
`/signin?returnUrl=${encodeURIComponent(
`https://${Cypress.env('BASE_URI')}/consents`,
)}`,
);
cy.url().should('include', '/signin');
cy.contains('Sign in to the Guardian');
cy.contains('You are signed in with');
cy.contains(emailAddress);
cy.contains('Continue')
.should('have.attr', 'href')
.and(
'include',
`https://${Cypress.env(
'BASE_URI',
)}/signin/refresh?returnUrl=https%3A%2F%2Fprofile.thegulocal.com%2Fconsents`,
);
cy.contains('a', 'Sign in')
.should('have.attr', 'href')
.and('include', '/signout?returnUrl=');
cy.contains('Sign in with a different email');
});
},
);
});
});
});