Skip to content

Commit

Permalink
test: add temp auth config as a playwright workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
spwoodcock committed Nov 27, 2024
1 parent 8cb79db commit 7aadaba
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 16 deletions.
1 change: 0 additions & 1 deletion src/backend/app/auth/auth_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
21 changes: 6 additions & 15 deletions src/frontend/e2e/auth.setup.ts
Original file line number Diff line number Diff line change
@@ -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 });
Expand Down
11 changes: 11 additions & 0 deletions src/frontend/src/routes.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -187,6 +188,16 @@ const routes = createBrowserRouter([
</Suspense>
),
},
{
path: '/playwright-temp-login/',
element: (
<Suspense fallback={<div>Loading...</div>}>
<ErrorBoundary>
<PlaywrightTempLogin />
</ErrorBoundary>
</Suspense>
),
},
{
path: '/under-construction/',
element: (
Expand Down
32 changes: 32 additions & 0 deletions src/frontend/src/views/PlaywrightTempLogin.tsx
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 7aadaba

Please sign in to comment.