From f7163a857722e0b34d0d84e020c2ea16b52affa4 Mon Sep 17 00:00:00 2001 From: Florian Zia Date: Tue, 8 Oct 2024 19:40:17 +0200 Subject: [PATCH 1/7] feat: Force feature flags for E2E tests --- src/db/tables/featureFlags.ts | 22 +++++++++++++++++++--- src/e2e/pages/authPage.ts | 2 +- src/middleware.ts | 7 +++++++ 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/db/tables/featureFlags.ts b/src/db/tables/featureFlags.ts index 1df406204cb..642b6ca0367 100644 --- a/src/db/tables/featureFlags.ts +++ b/src/db/tables/featureFlags.ts @@ -5,6 +5,7 @@ import createDbConnection from "../connect"; import { logger } from "../../app/functions/server/logging"; import { FeatureFlagRow } from "knex/types/tables"; +import { headers } from "next/headers"; const knex = createDbConnection(); @@ -75,11 +76,26 @@ export async function getEnabledFeatureFlags( return void subQuery; }); - const enabledFlagNames = await query; - - return enabledFlagNames.map( + const enabledFlagNames = (await query).map( (row: { name: string }) => row.name as FeatureFlagName, ); + + // Enable feature flags for local E2E tests via URL query params + if (process.env.E2E_TEST_ENV === "local") { + const forcedFeatureFlags = headers().get("x-forced-feature-flags"); + if (forcedFeatureFlags) { + const forcedFeatureFlagsFiltered = forcedFeatureFlags + .split(",") + .filter((forcedFeatureFlag) => + featureFlagNames.includes(forcedFeatureFlag as FeatureFlagName), + ); + return [ + ...new Set([...enabledFlagNames, ...forcedFeatureFlagsFiltered]), + ] as FeatureFlagName[]; + } + } + + return enabledFlagNames; } /** diff --git a/src/e2e/pages/authPage.ts b/src/e2e/pages/authPage.ts index f773efff27f..149edd3b8b1 100644 --- a/src/e2e/pages/authPage.ts +++ b/src/e2e/pages/authPage.ts @@ -96,7 +96,7 @@ export class AuthPage { async initSilentAuth() { await this.page.goto( - `${process.env.E2E_TEST_BASE_URL as string}/?utm_source=moz-account&utm_campaign=settings-promo&utm_content=monitor-free`, + `${process.env.E2E_TEST_BASE_URL as string}/?feature_flags=PromptNoneAuthFlow&utm_source=moz-account&utm_campaign=settings-promo&utm_content=monitor-free`, ); // FxA can take a while to load on stage: await this.page.waitForURL("**/oauth/**"); diff --git a/src/middleware.ts b/src/middleware.ts index ce7b0609530..17799ce7d25 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -12,6 +12,13 @@ export function middleware(request: NextRequest) { const requestHeaders = new Headers(request.headers); requestHeaders.set("x-nonce", nonce); + + if (process.env.E2E_TEST_ENV === "local") { + const forcedFeatureFlags = + request.nextUrl.searchParams.get("feature_flags"); + requestHeaders.set("x-forced-feature-flags", forcedFeatureFlags ?? ""); + } + // Add the CSP to the request headers - that will make Next.js detect it and // add it to the inline `