From 0c1498a84deaacd2acab815b413a69ce908bf371 Mon Sep 17 00:00:00 2001 From: Mrugesh Mohapatra <1884376+raisedadead@users.noreply.github.com> Date: Wed, 27 Aug 2025 22:47:21 +0530 Subject: [PATCH] fix(environment): Use DEPLOYMENT_ENV and DEPLOYMENT_TLD (#61925) --- .github/workflows/deploy-api.yml | 5 ++++- .../exam-environment/routes/exam-environment.test.ts | 2 +- api/src/routes/protected/user.test.ts | 12 +++++++----- api/src/routes/protected/user.ts | 2 +- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/.github/workflows/deploy-api.yml b/.github/workflows/deploy-api.yml index d8218108136..6fb71f7cce8 100644 --- a/.github/workflows/deploy-api.yml +++ b/.github/workflows/deploy-api.yml @@ -112,7 +112,8 @@ jobs: # LOKI_URL # Variables set from SetupJob DEPLOYMENT_VERSION: ${{ needs.build.outputs.tagname }} - DEPLOYMENT_ENV: ${{ needs.setup-jobs.outputs.site_tld }} + DEPLOYMENT_ENV: ${{ needs.setup-jobs.outputs.tgt_env_long }} + DEPLOYMENT_TLD: ${{ needs.setup-jobs.outputs.site_tld }} FCC_API_LOG_LEVEL: ${{ needs.setup-jobs.outputs.api_log_lvl }} # Stack name STACK_NAME: ${{ needs.setup-jobs.outputs.tgt_env_short }}-api @@ -148,6 +149,7 @@ jobs: echo -e '\nLOG:Adding deployment variables...' { echo \"DEPLOYMENT_VERSION=$DEPLOYMENT_VERSION\" + echo \"DEPLOYMENT_TLD=$DEPLOYMENT_TLD\" echo \"DEPLOYMENT_ENV=$DEPLOYMENT_ENV\" echo \"FCC_API_LOG_LEVEL=$FCC_API_LOG_LEVEL\" } >> .env @@ -173,6 +175,7 @@ jobs: \"STRIPE_SECRET_KEY\" \"LOKI_URL\" \"DEPLOYMENT_VERSION\" + \"DEPLOYMENT_TLD\" \"DEPLOYMENT_ENV\" \"FCC_API_LOG_LEVEL\" ) diff --git a/api/src/exam-environment/routes/exam-environment.test.ts b/api/src/exam-environment/routes/exam-environment.test.ts index 2ed49f30a9d..40887abb349 100644 --- a/api/src/exam-environment/routes/exam-environment.test.ts +++ b/api/src/exam-environment/routes/exam-environment.test.ts @@ -31,7 +31,7 @@ vi.mock('../../utils/env', async importOriginal => { return { ...actual, FCC_ENABLE_EXAM_ENVIRONMENT: 'true', - DEPLOYMENT_ENV: 'org' + DEPLOYMENT_ENV: 'production' }; }); diff --git a/api/src/routes/protected/user.test.ts b/api/src/routes/protected/user.test.ts index eebba1b1410..ff112fffe01 100644 --- a/api/src/routes/protected/user.test.ts +++ b/api/src/routes/protected/user.test.ts @@ -40,7 +40,7 @@ import { getMsTranscriptApiUrl } from './user'; const mockedFetch = vi.fn(); vi.spyOn(globalThis, 'fetch').mockImplementation(mockedFetch); -let mockDeploymentEnv = 'dev'; +let mockDeploymentEnv = 'staging'; vi.mock('../../utils/env', async () => { const actualEnv = await vi.importActual('../../utils/env'); @@ -1300,11 +1300,11 @@ Thanks and regards, describe('/user/exam-environment/token', () => { beforeEach(() => { - mockDeploymentEnv = 'org'; + mockDeploymentEnv = 'staging'; }); afterAll(() => { - mockDeploymentEnv = 'dev'; + mockDeploymentEnv = 'production'; }); afterEach(async () => { @@ -1316,6 +1316,7 @@ Thanks and regards, }); test('POST generates a new token if one does not exist', async () => { + mockDeploymentEnv = 'production'; const response = await superPost('/user/exam-environment/token'); const { examEnvironmentAuthorizationToken } = response.body; @@ -1338,6 +1339,7 @@ Thanks and regards, }); test('POST only allows for one token per user id', async () => { + mockDeploymentEnv = 'production'; const token = await fastifyTestInstance.prisma.examEnvironmentAuthorizationToken.create( { @@ -1372,14 +1374,14 @@ Thanks and regards, test('POST does not generate a new token in non-production environments for non-staff', async () => { // Override deployment environment for this test - mockDeploymentEnv = 'dev'; + mockDeploymentEnv = 'staging'; const response = await superPost('/user/exam-environment/token'); expect(response.status).toBe(403); }); test('POST does generate a new token in non-production environments for staff', async () => { // Override deployment environment for this test - mockDeploymentEnv = 'dev'; + mockDeploymentEnv = 'staging'; await fastifyTestInstance.prisma.user.update({ where: { id: defaultUserId diff --git a/api/src/routes/protected/user.ts b/api/src/routes/protected/user.ts index 37e4ef5277d..a1b80ab50e7 100644 --- a/api/src/routes/protected/user.ts +++ b/api/src/routes/protected/user.ts @@ -519,7 +519,7 @@ async function examEnvironmentTokenHandler( // In non-production environments, only staff are allowed to generate a token if ( - DEPLOYMENT_ENV !== 'org' && + DEPLOYMENT_ENV !== 'production' && (!req.user?.email?.endsWith('@freecodecamp.org') || !req.user?.emailVerified) ) {