Skip to content

Commit 39fc97d

Browse files
authored
chore: update auth examples to use cy.task instead of Cypress.env() (#1673)
1 parent 3c1f822 commit 39fc97d

File tree

5 files changed

+84
-44
lines changed

5 files changed

+84
-44
lines changed

cypress.config.ts

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,20 @@ export default defineConfig({
3535
paginationPageSize: process.env.PAGINATION_PAGE_SIZE,
3636

3737
// Auth0
38-
auth0_username: process.env.AUTH0_USERNAME,
39-
auth0_password: process.env.AUTH0_PASSWORD,
4038
auth0_domain: process.env.VITE_AUTH0_DOMAIN,
4139

4240
// Okta
43-
okta_username: process.env.OKTA_USERNAME,
44-
okta_password: process.env.OKTA_PASSWORD,
4541
okta_domain: process.env.VITE_OKTA_DOMAIN,
4642
okta_client_id: process.env.VITE_OKTA_CLIENTID,
4743
okta_programmatic_login: process.env.OKTA_PROGRAMMATIC_LOGIN || false,
4844

4945
// Amazon Cognito
50-
cognito_username: process.env.AWS_COGNITO_USERNAME,
51-
cognito_password: process.env.AWS_COGNITO_PASSWORD,
5246
cognito_domain: process.env.AWS_COGNITO_DOMAIN,
5347
cognito_programmatic_login: false,
5448
awsConfig: awsConfig.default,
5549

5650
// Google
57-
googleRefreshToken: process.env.GOOGLE_REFRESH_TOKEN,
5851
googleClientId: process.env.VITE_GOOGLE_CLIENTID,
59-
googleClientSecret: process.env.VITE_GOOGLE_CLIENT_SECRET,
6052
},
6153
component: {
6254
devServer: {
@@ -105,6 +97,38 @@ export default defineConfig({
10597
"find:database"(queryPayload) {
10698
return queryDatabase(queryPayload, (data, attrs) => _.find(data.results, attrs));
10799
},
100+
getAuth0Credentials() {
101+
const username = process.env.AUTH0_USERNAME;
102+
const password = process.env.AUTH0_PASSWORD;
103+
if (!username || !password) {
104+
throw new Error("AUTH0_USERNAME and AUTH0_PASSWORD must be set");
105+
}
106+
return { username, password };
107+
},
108+
getOktaCredentials() {
109+
const username = process.env.OKTA_USERNAME;
110+
const password = process.env.OKTA_PASSWORD;
111+
if (!username || !password) {
112+
throw new Error("OKTA_USERNAME and OKTA_PASSWORD must be set");
113+
}
114+
return { username, password };
115+
},
116+
getCognitoCredentials() {
117+
const username = process.env.AWS_COGNITO_USERNAME;
118+
const password = process.env.AWS_COGNITO_PASSWORD;
119+
if (!username || !password) {
120+
throw new Error("AWS_COGNITO_USERNAME and AWS_COGNITO_PASSWORD must be set");
121+
}
122+
return { username, password };
123+
},
124+
getGoogleCredentials() {
125+
const refreshToken = process.env.GOOGLE_REFRESH_TOKEN;
126+
const clientSecret = process.env.VITE_GOOGLE_CLIENT_SECRET;
127+
if (!refreshToken || !clientSecret) {
128+
throw new Error("GOOGLE_REFRESH_TOKEN and VITE_GOOGLE_CLIENT_SECRET must be set");
129+
}
130+
return { refreshToken, clientSecret };
131+
},
108132
});
109133

110134
codeCoverageTask(on, config);

cypress/support/commands.ts

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -351,38 +351,42 @@ Cypress.Commands.add("database", (operation, entity, query, logTask = false) =>
351351
Cypress.Commands.add("loginByGoogleApi", () => {
352352
cy.log("Logging in to Google");
353353

354-
cy.request({
355-
method: "POST",
356-
url: "https://www.googleapis.com/oauth2/v4/token",
357-
body: {
358-
grant_type: "refresh_token",
359-
client_id: Cypress.env("googleClientId"),
360-
client_secret: Cypress.env("googleClientSecret"),
361-
refresh_token: Cypress.env("googleRefreshToken"),
362-
},
363-
}).then(({ body }) => {
364-
const { access_token, id_token } = body;
365-
366-
cy.request({
367-
method: "GET",
368-
url: "https://www.googleapis.com/oauth2/v3/userinfo",
369-
headers: { Authorization: `Bearer ${access_token}` },
370-
}).then(({ body }) => {
371-
cy.log(body);
372-
const userItem = {
373-
token: id_token,
374-
user: {
375-
googleId: body.sub,
376-
email: body.email,
377-
givenName: body.given_name,
378-
familyName: body.family_name,
379-
imageUrl: body.picture,
354+
cy.task<{ clientSecret: string; refreshToken: string }>("getGoogleCredentials").then(
355+
({ clientSecret, refreshToken }) => {
356+
cy.request({
357+
method: "POST",
358+
url: "https://www.googleapis.com/oauth2/v4/token",
359+
body: {
360+
grant_type: "refresh_token",
361+
client_id: Cypress.env("googleClientId"),
362+
client_secret: clientSecret,
363+
refresh_token: refreshToken,
380364
},
381-
};
382-
383-
window.localStorage.setItem("googleCypress", JSON.stringify(userItem));
384-
385-
cy.visit("/");
386-
});
387-
});
365+
}).then(({ body }) => {
366+
const { access_token, id_token } = body;
367+
368+
cy.request({
369+
method: "GET",
370+
url: "https://www.googleapis.com/oauth2/v3/userinfo",
371+
headers: { Authorization: `Bearer ${access_token}` },
372+
}).then(({ body }) => {
373+
cy.log(body);
374+
const userItem = {
375+
token: id_token,
376+
user: {
377+
googleId: body.sub,
378+
email: body.email,
379+
givenName: body.given_name,
380+
familyName: body.family_name,
381+
imageUrl: body.picture,
382+
},
383+
};
384+
385+
window.localStorage.setItem("googleCypress", JSON.stringify(userItem));
386+
387+
cy.visit("/");
388+
});
389+
});
390+
}
391+
);
388392
});

cypress/tests/ui-auth-providers/auth0.spec.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ if (Cypress.env("auth0_username")) {
55
beforeEach(function () {
66
cy.task("db:seed");
77
cy.intercept("POST", "/graphql").as("createBankAccount");
8-
cy.loginToAuth0(Cypress.env("auth0_username"), Cypress.env("auth0_password"));
8+
cy.task<{ username: string; password: string }>("getAuth0Credentials").then(
9+
({ username, password }) => {
10+
cy.loginToAuth0(username, password);
11+
}
12+
);
913
cy.visit("/");
1014
});
1115

cypress/tests/ui-auth-providers/cognito.spec.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ if (Cypress.env("cognito_username")) {
1111

1212
cy.intercept("POST", apiGraphQL).as("createBankAccount");
1313

14-
cy.loginByCognitoApi(Cypress.env("cognito_username"), Cypress.env("cognito_password"));
14+
cy.task<{ username: string; password: string }>("getCognitoCredentials").then(
15+
({ username, password }) => {
16+
cy.loginByCognitoApi(username, password);
17+
}
18+
);
1519
});
1620

1721
it("should allow a visitor to login, onboard and logout", function () {

cypress/tests/ui-auth-providers/okta.spec.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ if (Cypress.env("okta_username")) {
88

99
cy.intercept("POST", "/bankAccounts").as("createBankAccount");
1010

11-
cy.loginByOktaApi(Cypress.env("okta_username"), Cypress.env("okta_password"));
11+
cy.task<{ username: string; password: string }>("getOktaCredentials").then(
12+
({ username, password }) => {
13+
cy.loginByOktaApi(username, password);
14+
}
15+
);
1216
});
1317

1418
it("should allow a visitor to login, onboard and logout", function () {

0 commit comments

Comments
 (0)