From f4b870c66a34e70e00507ac1655da33ed6aeb8fc Mon Sep 17 00:00:00 2001 From: Henry Davies Date: Thu, 8 Jan 2026 17:38:07 +0000 Subject: [PATCH] test: add login to cypress support commands --- cypress/e2e/index.cy.js | 41 +++---------------------------------- cypress/support/commands.js | 41 +++++++++++++++---------------------- 2 files changed, 19 insertions(+), 63 deletions(-) diff --git a/cypress/e2e/index.cy.js b/cypress/e2e/index.cy.js index 16cb40d..5d4b90c 100644 --- a/cypress/e2e/index.cy.js +++ b/cypress/e2e/index.cy.js @@ -1,5 +1,3 @@ -import { refreshToken } from '../../src/spotify/auth/refreshToken'; - describe('index', () => { it('displays login button when user is not logged in', () => { cy.visit('https://d1e7htx1c4j3w0.cloudfront.net/'); @@ -50,18 +48,7 @@ describe('index', () => { }); it('displays username when user is logged in', () => { - cy.wrap(null).then(async () => { - process.env.SPOTIFY_CLIENT_ID = Cypress.env('SPOTIFY_CLIENT_ID'); - process.env.SPOTIFY_CLIENT_SECRET = Cypress.env('SPOTIFY_CLIENT_SECRET'); - process.env.AUTH_REDIRECT_URI = Cypress.env('AUTH_REDIRECT_URI'); - process.env.CREATE_PLAYLIST_LAMBDA_ARN = Cypress.env( - 'CREATE_PLAYLIST_LAMBDA_ARN' - ); - const accessToken = await refreshToken( - Cypress.env('TEST_USER_REFRESH_TOKEN') - ); - cy.setCookie('access_token', accessToken); - }); + cy.loginAsTestUser(); cy.visit(`https://d1e7htx1c4j3w0.cloudfront.net/`); @@ -69,18 +56,7 @@ describe('index', () => { }); it('hides login button when user is logged in', () => { - cy.wrap(null).then(async () => { - process.env.SPOTIFY_CLIENT_ID = Cypress.env('SPOTIFY_CLIENT_ID'); - process.env.SPOTIFY_CLIENT_SECRET = Cypress.env('SPOTIFY_CLIENT_SECRET'); - process.env.AUTH_REDIRECT_URI = Cypress.env('AUTH_REDIRECT_URI'); - process.env.CREATE_PLAYLIST_LAMBDA_ARN = Cypress.env( - 'CREATE_PLAYLIST_LAMBDA_ARN' - ); - const accessToken = await refreshToken( - Cypress.env('TEST_USER_REFRESH_TOKEN') - ); - cy.setCookie('access_token', accessToken); - }); + cy.loginAsTestUser(); cy.visit(`https://d1e7htx1c4j3w0.cloudfront.net/`); @@ -91,18 +67,7 @@ describe('index', () => { }); it("informs user playlist will be available soon when 'Create Playlist' is clicked", () => { - cy.wrap(null).then(async () => { - process.env.SPOTIFY_CLIENT_ID = Cypress.env('SPOTIFY_CLIENT_ID'); - process.env.SPOTIFY_CLIENT_SECRET = Cypress.env('SPOTIFY_CLIENT_SECRET'); - process.env.AUTH_REDIRECT_URI = Cypress.env('AUTH_REDIRECT_URI'); - process.env.CREATE_PLAYLIST_LAMBDA_ARN = Cypress.env( - 'CREATE_PLAYLIST_LAMBDA_ARN' - ); - const accessToken = await refreshToken( - Cypress.env('TEST_USER_REFRESH_TOKEN') - ); - cy.setCookie('access_token', accessToken); - }); + cy.loginAsTestUser(); cy.visit(`https://d1e7htx1c4j3w0.cloudfront.net/`); diff --git a/cypress/support/commands.js b/cypress/support/commands.js index 119ab03..4fee20a 100644 --- a/cypress/support/commands.js +++ b/cypress/support/commands.js @@ -1,25 +1,16 @@ -// *********************************************** -// This example commands.js shows you how to -// create various custom commands and overwrite -// existing commands. -// -// For more comprehensive examples of custom -// commands please read more here: -// https://on.cypress.io/custom-commands -// *********************************************** -// -// -// -- This is a parent command -- -// Cypress.Commands.add('login', (email, password) => { ... }) -// -// -// -- This is a child command -- -// Cypress.Commands.add('drag', { prevSubject: 'element'}, (subject, options) => { ... }) -// -// -// -- This is a dual command -- -// Cypress.Commands.add('dismiss', { prevSubject: 'optional'}, (subject, options) => { ... }) -// -// -// -- This will overwrite an existing command -- -// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... }) +import { refreshToken } from '../../src/spotify/auth/refreshToken'; + +Cypress.Commands.add('loginAsTestUser', () => { + cy.wrap(null).then(async () => { + process.env.SPOTIFY_CLIENT_ID = Cypress.env('SPOTIFY_CLIENT_ID'); + process.env.SPOTIFY_CLIENT_SECRET = Cypress.env('SPOTIFY_CLIENT_SECRET'); + process.env.AUTH_REDIRECT_URI = Cypress.env('AUTH_REDIRECT_URI'); + process.env.CREATE_PLAYLIST_LAMBDA_ARN = Cypress.env( + 'CREATE_PLAYLIST_LAMBDA_ARN' + ); + const accessToken = await refreshToken( + Cypress.env('TEST_USER_REFRESH_TOKEN') + ); + cy.setCookie('access_token', accessToken); + }); +});