Skip to content

Commit e14bf71

Browse files
committed
chore: update guide from comments in code review
1 parent dbd35c4 commit e14bf71

File tree

1 file changed

+49
-71
lines changed

1 file changed

+49
-71
lines changed

content/guides/end-to-end-testing/amazon-cognito-authentication.md

Lines changed: 49 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,27 @@ e2eSpecific: true
77

88
## <Icon name="graduation-cap"></Icon> What you'll learn
99

10-
- Authenticate with [`cy.origin()`](/api/commands/origin) or programmatically
11-
(legacy) to [Amazon Cognito](https://aws.amazon.com/cognito) via a custom
12-
Cypress command
13-
- If needed, adapting your [Amazon Cognito](https://aws.amazon.com/cognito)
14-
application for programmatic authentication during testing
15-
16-
</Alert>
10+
- Login to [Amazon Cognito](https://aws.amazon.com/cognito) through the UI with
11+
[`cy.origin()`](/api/commands/origin)
12+
- Programmatically authenticate with
13+
[Amazon Cognito](https://aws.amazon.com/cognito) via a custom command Cypress
14+
command
15+
- Adapting your [Amazon Cognito](https://aws.amazon.com/cognito) application for
16+
programmatic authentication during testing </Alert>
1717

1818
<Alert type="success">
1919

2020
<strong class="alert-header">Authenticate by visiting a different domain with
2121
[`cy.origin()`](/api/commands/origin)</strong>
2222

23-
Programmatic authentication for Cognito is now considered a legacy
24-
recommendation. As of Cypress [v12.0.0](https://on.cypress.io/changelog#12-0-0),
25-
you can easily authenticate to federated AWS Cognito as Cypress tests are no
26-
longer limited to visiting domains of a single origin.
23+
Typically, logging in a user within your app by authenticating via a third-party
24+
provider requires visiting a login page hosted on a different domain. Before
25+
Cypress [v12.0.0](https://on.cypress.io/changelog#12-0-0), Cypress tests were
26+
limited to visiting domains of the same origin, making programmatic login the
27+
only option for authenticating users with a third-party API. As of Cypress
28+
[v12.0.0](https://on.cypress.io/changelog#12-0-0), Cypress tests are no longer
29+
limited to visiting domains of a single origin, meaning you can easily
30+
authenticate to federated AWS Cognito via the UI!
2731

2832
</Alert>
2933

@@ -158,33 +162,30 @@ const awsConfig = require('./aws-exports-es5.js')
158162

159163
## Custom Command for Amazon Cognito Authentication
160164

161-
There are two ways you can authenticate to AWS Cognito
165+
There are two ways you can authenticate to AWS Cognito:
162166

163167
- [Login with cy.origin()](/guides/end-to-end-testing/amazon-cognito-authentication#Login-with-cy-origin)
164-
- [Legacy Programmatic Access](/guides/end-to-end-testing/amazon-cognito-authentication#Programmatic-Login-Legacy)
168+
- [Programmatic Access](/guides/end-to-end-testing/amazon-cognito-authentication#Programmatic-Login)
165169

166170
### Login with [`cy.origin()`](/api/commands/origin)
167171

168-
Next, we'll write a command to perform a login to
169-
[Amazon Cognito](https://aws.amazon.com/cognito) using
170-
[`cy.origin()`](/api/commands/origin) to navigate to the cognito origin,
171-
inputting a user credentials, and signing in via OAuth to redirect back to our
172-
application, and caching the results with
173-
[`cy.session()`](/api/commands/session).
172+
Next, we'll write a custom command called `loginByCognito` to perform a login to
173+
[Amazon Cognito](https://aws.amazon.com/cognito). This command will use
174+
[`cy.origin()`](/api/commands/origin) to
174175

175-
In this `loginByCognito` command, we redirect to the cognito login screen by
176-
authenticating with `Sign in with AWS`. We then enter our user credentials and
177-
sign in, which redirects us back to the realworld app.
176+
1. navigate to the Cognito origin
177+
2. input user credentials
178+
3. sign in and redirect back to the
179+
[Cypress Real World App](https://github.com/cypress-io/cypress-realworld-app)
180+
4. cache the results with [`cy.session()`](/api/commands/session)
178181

179182
```jsx
180183
// cypress/support/auth-provider-commands/cognito.ts
181-
182-
/// Amazon Cognito
183-
Cypress.Commands.add('loginByCognito', (username, password) => {
184+
// Amazon Cognito
185+
const loginToCognito = (username: string, password: string) => {
184186
Cypress.log({
185187
displayName: 'COGNITO LOGIN',
186188
message: [`🔐 Authenticating | ${username}`],
187-
// @ts-ignore
188189
autoEnd: false,
189190
})
190191

@@ -202,21 +203,27 @@ Cypress.Commands.add('loginByCognito', (username, password) => {
202203
},
203204
},
204205
({ username, password }) => {
205-
// cognito log in page has some elements of the same id but are off screen. we only want the visible elements to log in
206+
// Cognito log in page has some elements of the same id but are off screen.
207+
// We only want the visible elements to log in
206208
cy.get('input[name="username"]:visible').type(username)
207209
cy.get('input[name="password"]:visible').type(password, {
210+
// use log: false to prevent your password from showing in the Command Log
208211
log: false,
209212
})
210213
cy.get('input[name="signInSubmitButton"]:visible').click()
211214
}
212215
)
213216

214217
// give a few seconds for redirect to settle
215-
// eslint-disable-next-line cypress/no-unnecessary-waiting
216218
cy.wait(2000)
217219

218220
// verify we have made it passed the login screen
219221
cy.contains('Get Started').should('be.visible')
222+
}
223+
224+
// right now our custom command is light. More on this later!
225+
Cypress.Commands.add('loginByCognito', (username, password) => {
226+
return loginToCognito(username, password)
220227
})
221228
```
222229

@@ -259,47 +266,13 @@ Lastly, we can refactor our login command to take advantage of
259266
have to reauthenticate with everything test.
260267

261268
```jsx
269+
// cypress/support/auth-provider-commands/cognito.ts
262270
// Amazon Cognito
263271
Cypress.Commands.add('loginByCognito', (username, password) => {
264272
cy.session(
265273
`cognito-${username}`,
266274
() => {
267-
Cypress.log({
268-
displayName: 'COGNITO LOGIN',
269-
message: [`🔐 Authenticating | ${username}`],
270-
// @ts-ignore
271-
autoEnd: false,
272-
})
273-
274-
cy.visit('/')
275-
cy.contains('Sign in with AWS', {
276-
includeShadowDom: true,
277-
}).click()
278-
279-
cy.origin(
280-
Cypress.env('cognito_domain'),
281-
{
282-
args: {
283-
username,
284-
password,
285-
},
286-
},
287-
({ username, password }) => {
288-
// cognito log in page has some elements of the same id but are off screen. we only want the visible elements to log in
289-
cy.get('input[name="username"]:visible').type(username)
290-
cy.get('input[name="password"]:visible').type(password, {
291-
log: false,
292-
})
293-
cy.get('input[name="signInSubmitButton"]:visible').click()
294-
}
295-
)
296-
297-
// give a few seconds for redirect to settle
298-
// eslint-disable-next-line cypress/no-unnecessary-waiting
299-
cy.wait(2000)
300-
301-
// verify we have made it passed the login screen
302-
cy.contains('Get Started').should('be.visible')
275+
return loginToCognito(username, password)
303276
},
304277
{
305278
validate() {
@@ -314,12 +287,7 @@ Cypress.Commands.add('loginByCognito', (username, password) => {
314287

315288
<DocsVideo src="/img/examples/cognito-session-restore.mp4"></DocsVideo>
316289

317-
Unlike programmatic login, authenticating with
318-
[`cy.origin()`](/api/commands/origin) does not require adapting the application
319-
to work. It simply works without intervention exactly how your users would
320-
consume the application!
321-
322-
### Programmatic Login (Legacy)
290+
### Programmatic Login
323291

324292
Next, we'll write a command to perform a programmatic login into
325293
[Amazon Cognito](https://aws.amazon.com/cognito) and set items in localStorage
@@ -419,7 +387,17 @@ describe('Cognito', function () {
419387
})
420388
```
421389

422-
#### Adapting an Amazon Cognito App for Testing
390+
### Adapting an Amazon Cognito App for Testing
391+
392+
<Alert type="info">
393+
394+
<strong class="alert-header">Programmatic Login</strong>
395+
396+
Unlike programmatic login, authenticating with
397+
[`cy.origin()`](/api/commands/origin) does not require adapting the application
398+
to work. This step is only needed if implementing programmatic login.
399+
400+
</Alert>
423401

424402
The
425403
[Cypress Real World App](https://github.com/cypress-io/cypress-realworld-app) is

0 commit comments

Comments
 (0)