From 338546ad5fe9f81e905ed00de0113ac7718f5187 Mon Sep 17 00:00:00 2001 From: Tristan Huet Date: Fri, 24 Jan 2025 14:58:00 +0100 Subject: [PATCH] test: allow usage of CYPRESS_BASE_URL to run tests on deployed webapps --- cypress/commons/actions/generic/Login.js | 10 ++++++++-- cypress/commons/constants/generic/TestConstants.js | 3 +-- cypress/commons/utils/apiUtils.js | 12 ++++++++++-- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/cypress/commons/actions/generic/Login.js b/cypress/commons/actions/generic/Login.js index c2f5e976f..23708907a 100644 --- a/cypress/commons/actions/generic/Login.js +++ b/cypress/commons/actions/generic/Login.js @@ -58,8 +58,14 @@ function login(options) { if (auth.USE_API_KEY) return; cy.getAllLocalStorage().then((localStorage) => { - expect(localStorage[BASE_URL].authProvider).not.to.eq(undefined); - expect(localStorage[BASE_URL].authAccessToken).not.to.eq(undefined); + // Can't use `Cypress.config('baseUrl')` below, because when deployed, the webapp base URL can include + // a subpath after the domain name (e.g. "/cosmotech-webapp/brewery"), that is not present in the local + // storage key + // Use location.host instead of hostname to also get the port number (e.g. localhost:3000), it is + // necessary when the webapp runs locally + const baseUrl = `${window.location.protocol}//${window.location.host}`; + expect(localStorage[baseUrl].authProvider).not.to.eq(undefined); + expect(localStorage[baseUrl].authAccessToken).not.to.eq(undefined); }); }, } diff --git a/cypress/commons/constants/generic/TestConstants.js b/cypress/commons/constants/generic/TestConstants.js index df02bb67d..1348e27fa 100644 --- a/cypress/commons/constants/generic/TestConstants.js +++ b/cypress/commons/constants/generic/TestConstants.js @@ -6,8 +6,7 @@ export { ORGANIZATION_ID } from '../../../../src/config/GlobalConfiguration'; export const URL_ROOT = 'https://dev.api.cosmotech.com'; export const AUTH_QUERY_URL = 'https://login.microsoftonline.com/e413b834-8be8-4822-a370-be619545cb49/oauth2/v2.0/token'; -export const LOCAL_WEBAPP_URL = 'http://localhost:3000'; -export const URL_POWERBI = `${LOCAL_WEBAPP_URL}/api/get-embed-info`; +export const GET_EMBED_INFO_ENDPOINT = '/api/get-embed-info'; export const PAGE_NAME = { SCENARIO: '/scenario', diff --git a/cypress/commons/utils/apiUtils.js b/cypress/commons/utils/apiUtils.js index e71562909..16835e1c6 100644 --- a/cypress/commons/utils/apiUtils.js +++ b/cypress/commons/utils/apiUtils.js @@ -8,7 +8,13 @@ import { SCENARIO_EXAMPLE, SCENARIO_RUN_EXAMPLE, } from '../../fixtures/stubbing/default'; -import { API_ENDPOINT, API_REGEX, AUTH_QUERY_URL, URL_POWERBI, URL_ROOT } from '../constants/generic/TestConstants'; +import { + API_ENDPOINT, + API_REGEX, + AUTH_QUERY_URL, + GET_EMBED_INFO_ENDPOINT, + URL_ROOT, +} from '../constants/generic/TestConstants'; import { stub } from '../services/stubbing'; import { authUtils } from './authUtils'; import { fileUtils } from './fileUtils'; @@ -745,7 +751,9 @@ const interceptGetSolution = (solutionId) => { const interceptPowerBIAzureFunction = () => { if (authUtils.USE_API_KEY) return; const alias = forgeAlias('reqPowerBI'); - cy.intercept('POST', URL_POWERBI, { statusCode: 200 }).as(alias); + const baseUrl = Cypress.config('baseUrl'); + const interceptUrl = `${baseUrl}${GET_EMBED_INFO_ENDPOINT}`; + cy.intercept('POST', interceptUrl, { statusCode: 200 }).as(alias); return alias; };