From 7aadabaeea528ba8018b3178102e983df4240d83 Mon Sep 17 00:00:00 2001 From: spwoodcock Date: Wed, 27 Nov 2024 04:16:27 +0000 Subject: [PATCH] test: add temp auth config as a playwright workaround --- src/backend/app/auth/auth_routes.py | 1 - src/frontend/e2e/auth.setup.ts | 21 ++++-------- src/frontend/src/routes.jsx | 11 +++++++ .../src/views/PlaywrightTempLogin.tsx | 32 +++++++++++++++++++ 4 files changed, 49 insertions(+), 16 deletions(-) create mode 100644 src/frontend/src/views/PlaywrightTempLogin.tsx diff --git a/src/backend/app/auth/auth_routes.py b/src/backend/app/auth/auth_routes.py index 0482e2b4e4..91df93c4a0 100644 --- a/src/backend/app/auth/auth_routes.py +++ b/src/backend/app/auth/auth_routes.py @@ -315,7 +315,6 @@ async def temp_login( setting it as a cookie. Args: - request (Request): The incoming request object. email: email of non-osm user. Returns: diff --git a/src/frontend/e2e/auth.setup.ts b/src/frontend/e2e/auth.setup.ts index 1289cfa320..17f9c20584 100644 --- a/src/frontend/e2e/auth.setup.ts +++ b/src/frontend/e2e/auth.setup.ts @@ -1,24 +1,15 @@ -import { test as setup, expect } from '@playwright/test'; +import { test as setup } from '@playwright/test'; import path from 'path'; const authFile = path.join(__dirname, './.auth/user.json'); setup('authenticate', async ({ page }) => { - // Navigate to the app's base URL - await page.goto('/'); - await page.getByRole('button', { name: 'Sign in' }).click(); + // Note this sets a token so we can proceed, but the login will be + // overwritten by svcfmtm localadmin user (as DEBUG=True) + await page.goto('/playwright-temp-login/'); - // Select OSM login - await page.getByText('Personal OSM Account').click(); - await page.waitForSelector('text=Log in to OpenStreetMap'); - - // OSM Login page - await page.getByLabel('Email Address or Username').fill(process.env.OSM_USERNAME || 'username'); - await page.getByLabel('Password').fill(process.env.OSM_PASSWORD || 'password'); - await page.getByRole('button', { name: 'Log in' }).click(); - - // Wait for redirect and valid login (sign out button) - await page.waitForSelector('text=Sign Out'); + // Now check we are signed in as localadmin + await page.waitForSelector('text=localadmin'); // Save authentication state await page.context().storageState({ path: authFile }); diff --git a/src/frontend/src/routes.jsx b/src/frontend/src/routes.jsx index d7953927a4..72dd940813 100755 --- a/src/frontend/src/routes.jsx +++ b/src/frontend/src/routes.jsx @@ -8,6 +8,7 @@ import Organisation from '@/views/Organisation'; import CreateEditOrganization from '@/views/CreateEditOrganization'; import ApproveOrganization from '@/views/ApproveOrganization'; import OsmAuth from '@/views/OsmAuth'; +import PlaywrightTempLogin from '@/views/PlaywrightTempLogin'; import SubmissionDetails from '@/views/SubmissionDetails'; import CreateNewProject from '@/views/CreateNewProject'; import UnderConstruction from '@/views/UnderConstruction'; @@ -187,6 +188,16 @@ const routes = createBrowserRouter([ ), }, + { + path: '/playwright-temp-login/', + element: ( + Loading...}> + + + + + ), + }, { path: '/under-construction/', element: ( diff --git a/src/frontend/src/views/PlaywrightTempLogin.tsx b/src/frontend/src/views/PlaywrightTempLogin.tsx new file mode 100644 index 0000000000..d23bdf550b --- /dev/null +++ b/src/frontend/src/views/PlaywrightTempLogin.tsx @@ -0,0 +1,32 @@ +// This file is a workaround to populate state.login.authDetails +// the auth is overridden when testing due to DEBUG=True on the backend, +// but we need to update the frontend state to show we are logged in + +import axios from 'axios'; +import { getUserDetailsFromApi } from '@/utilfunctions/login'; +import { CommonActions } from '@/store/slices/CommonSlice'; +import CoreModules from '@/shared/CoreModules.js'; +import { LoginActions } from '@/store/slices/LoginSlice'; + +async function PlaywrightTempAuth() { + const dispatch = CoreModules.useAppDispatch(); + // Sets a cookie in the browser that is used for auth + await axios.get(`${import.meta.env.VITE_API_URL}/auth/temp-login`); + + const apiUser = await getUserDetailsFromApi(); + if (!apiUser) { + dispatch( + CommonActions.SetSnackBar({ + open: true, + message: 'Temp login failed. Try OSM.', + variant: 'error', + duration: 2000, + }), + ); + return; + } + + dispatch(LoginActions.setAuthDetails(apiUser)); +} + +export default PlaywrightTempAuth;