Skip to content

Commit 31b0c6c

Browse files
committed
chore: make changes to okta to have parity with cognito changes
1 parent 30ee0dd commit 31b0c6c

File tree

1 file changed

+36
-52
lines changed

1 file changed

+36
-52
lines changed

content/guides/end-to-end-testing/okta-authentication.md

Lines changed: 36 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ 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) with [Okta](https://okta.com) via a custom Cypress command
12-
- If needed, adapting your [Okta](https://okta.com) application for programmatic
13-
authentication during testing
14-
15-
</Alert>
10+
- Login to [Okta](https://okta.com) through the UI with
11+
[`cy.origin()`](/api/commands/origin)
12+
- Programmatically authenticate with [Okta](https://okta.com) via a custom
13+
Cypress command
14+
- Adapting your [Okta](https://okta.com) application for programmatic
15+
authentication during testing </Alert>
1616

1717
<Alert type="warning">
1818

@@ -28,11 +28,16 @@ other identity providers.
2828
<strong class="alert-header">Authenticate by visiting a different domain with
2929
[`cy.origin()`](/api/commands/origin)</strong>
3030

31-
Programmatic authentication for Okta is now considered a legacy recommendation.
32-
As of Cypress [v12.0.0](https://on.cypress.io/changelog#12-0-0), you can easily
31+
Typically, logging in a user within your app by authenticating via a third-party
32+
provider requires visiting a login page hosted on a different domain. Before
33+
Cypress [v12.0.0](https://on.cypress.io/changelog#12-0-0), Cypress tests were
34+
limited to visiting domains of the same origin, making programmatic login the
35+
only option for authenticating users with a third-party API. As of Cypress
36+
[v12.0.0](https://on.cypress.io/changelog#12-0-0), Cypress tests are no longer
37+
limited to visiting domains of a single origin, meaning you can easily
3338
authenticate to
3439
[Okta Universal Directory](https://www.okta.com/products/universal-directory/)
35-
as Cypress tests are no longer limited to visiting domains of a single origin.
40+
via the UI!
3641

3742
</Alert>
3843

@@ -71,36 +76,34 @@ require('dotenv').config()
7176

7277
## Custom Command for Okta Authentication
7378

74-
There are two ways you can authenticate to Okta
79+
There are two ways you can authenticate to Okta:
7580

7681
- [Login with cy.origin()](/guides/end-to-end-testing/okta-authentication#Login-with-cy-origin)
77-
- [Legacy Programmatic Access](/guides/end-to-end-testing/okta-authentication#Programmatic-Login-Legacy)
82+
- [Programmatic Access](/guides/end-to-end-testing/okta-authentication#Programmatic-Login)
7883

7984
### Login with [`cy.origin()`](/api/commands/origin)
8085

81-
Next, we'll write a command to perform a login to [Okta](https://okta.com) using
82-
[`cy.origin()`](/api/commands/origin) to navigate to Okta, inputting user
83-
credentials, and signing in via OAuth to redirect back to our application, and
84-
caching the results with [`cy.session()`](/api/commands/session).
86+
Next, we'll write a custom command called `loginByOkta` to perform a login to
87+
[Okta](https://okta.com). This command will use
88+
[`cy.origin()`](/api/commands/origin) to
8589

86-
In this `loginByOkta` command, we redirect to the okta login screen via
87-
navigation guards. We then enter our user credentials and sign in, which
88-
redirects us back to the
89-
[Cypress Real World App](https://github.com/cypress-io/cypress-realworld-app).
90+
1. navigate to the Okta origin
91+
2. input user credentials
92+
3. sign in and redirect back to the
93+
[Cypress Real World App](https://github.com/cypress-io/cypress-realworld-app)
94+
4. cache the results with [`cy.session()`](/api/commands/session)
9095

9196
```jsx
9297
// cypress/support/auth-provider-commands/okta.ts
9398
// Okta
94-
Cypress.Commands.add('loginByOkta', (username: string, password: string) => {
99+
const loginToOkta = (username: string, password: string) => {
95100
Cypress.log({
96101
displayName: 'OKTA LOGIN',
97102
message: [`🔐 Authenticating | ${username}`],
98-
// @ts-ignore
99103
autoEnd: false,
100104
})
101105

102106
cy.visit('/')
103-
104107
cy.origin(
105108
Cypress.env('okta_domain'),
106109
{ args: { username, password } },
@@ -114,6 +117,10 @@ Cypress.Commands.add('loginByOkta', (username: string, password: string) => {
114117
)
115118

116119
cy.get('[data-test="sidenav-username"]').should('contain', username)
120+
}
121+
// right now our custom command is light. More on this later!
122+
Cypress.Commands.add('loginByOkta', (username: string, password: string) => {
123+
return loginToOkta(username, password)
117124
})
118125
```
119126

@@ -133,7 +140,6 @@ is in the
133140
describe('Okta', function () {
134141
beforeEach(function () {
135142
cy.task('db:seed')
136-
137143
cy.loginByOkta(Cypress.env('okta_username'), Cypress.env('okta_password'))
138144
})
139145

@@ -155,28 +161,7 @@ Cypress.Commands.add('loginByOkta', (username: string, password: string) => {
155161
cy.session(
156162
`okta-${username}`,
157163
() => {
158-
Cypress.log({
159-
displayName: 'OKTA LOGIN',
160-
message: [`🔐 Authenticating | ${username}`],
161-
// @ts-ignore
162-
autoEnd: false,
163-
})
164-
165-
cy.visit('/')
166-
167-
cy.origin(
168-
Cypress.env('okta_domain'),
169-
{ args: { username, password } },
170-
({ username, password }) => {
171-
cy.get('input[name="identifier"]').type(username)
172-
cy.get('input[name="credentials.passcode"]').type(password, {
173-
log: false,
174-
})
175-
cy.get('[type="submit"]').click()
176-
}
177-
)
178-
179-
cy.get('[data-test="sidenav-username"]').should('contain', username)
164+
return loginToOkta(username, password)
180165
},
181166
{
182167
validate() {
@@ -190,12 +175,7 @@ Cypress.Commands.add('loginByOkta', (username: string, password: string) => {
190175

191176
<DocsVideo src="/img/examples/okta-session-restore.mp4"></DocsVideo>
192177

193-
Unlike programmatic login, authenticating with
194-
[`cy.origin()`](/api/commands/origin) does not require adapting the application
195-
to work. It simply works without intervention exactly how your users would
196-
consume the application!
197-
198-
### Programmatic Login (Legacy)
178+
### Programmatic Login
199179

200180
Next, we will write a command named `loginByOktaApi` to perform a programmatic
201181
login into [Okta](https://okta.com) and set an item in localStorage with the
@@ -297,7 +277,7 @@ is in the
297277

298278
</Alert>
299279

300-
#### Adapting an Okta App for Testing
280+
### Adapting an Okta App for Testing
301281

302282
<Alert type="info">
303283

@@ -307,6 +287,10 @@ The previous sections focused on the programmatic Okta authentication practice
307287
within Cypress tests. To use this practice it is assumed you are testing an app
308288
appropriately built or adapted to use Okta.
309289

290+
Unlike programmatic login, authenticating with
291+
[`cy.origin()`](/api/commands/origin) does not require adapting the application
292+
to work. This step is only needed if implementing programmatic login.
293+
310294
The following sections provides guidance on building or adapting an app to use
311295
Okta authentication.
312296

0 commit comments

Comments
 (0)