Add E2E tests for OAuth authentication flows and provider integrations

- Implemented comprehensive E2E tests for OAuth buttons on login and register pages, including Google and GitHub provider interactions.
- Verified OAuth provider buttons' visibility, icons, and proper API integration with mock endpoints.
- Added button interaction tests to ensure correct API calls for authorization and state handling.
- Updated `playwright.config.ts` to include the new `auth-oauth.spec.ts` in test configurations.
- Extended mock handlers in `overrides.ts` and `auth.ts` to support OAuth-specific API workflows and demo scenarios.
This commit is contained in:
Felipe Cardoso
2025-11-25 10:40:37 +01:00
parent b49678b7df
commit fcbcff99e9
4 changed files with 326 additions and 0 deletions

View File

@@ -14,6 +14,7 @@
import { http, HttpResponse, delay } from 'msw';
import { generateMockToken } from '../utils/tokens';
import { validateCredentials, setCurrentUser, currentUser } from '../data/users';
import config from '@/config/app.config';
const API_BASE_URL = process.env.NEXT_PUBLIC_API_BASE_URL || 'http://localhost:8000';
const NETWORK_DELAY = 300; // ms - simulate realistic network delay
@@ -101,4 +102,84 @@ export const overrideHandlers = [
expires_in: 900,
});
}),
/**
* OAuth Providers Override
* Returns list of available OAuth providers for demo mode
*/
http.get(`${API_BASE_URL}/api/v1/oauth/providers`, async () => {
await delay(NETWORK_DELAY);
return HttpResponse.json({
enabled: true,
providers: [
{ provider: 'google', name: 'Google' },
{ provider: 'github', name: 'GitHub' },
],
});
}),
/**
* OAuth Authorization URL Override
* Returns mock authorization URL (in demo mode, this won't actually redirect)
*/
http.get(`${API_BASE_URL}/api/v1/oauth/authorize/:provider`, async ({ params }) => {
await delay(NETWORK_DELAY);
const { provider } = params;
// In demo mode, we return a mock URL that will show a demo message
return HttpResponse.json({
authorization_url: `${config.app.url}/en/login?demo_oauth=${provider}`,
state: `demo-state-${Date.now()}`,
});
}),
/**
* OAuth Callback Override
* Handles mock OAuth callback in demo mode
*/
http.post(`${API_BASE_URL}/api/v1/oauth/callback/:provider`, async ({ params }) => {
await delay(NETWORK_DELAY);
const { provider } = params;
// Create a demo user based on the provider
const demoOAuthUser = {
id: `oauth-demo-${Date.now()}`,
email: `demo.${provider}@example.com`,
first_name: 'Demo',
last_name: `${provider === 'google' ? 'Google' : 'GitHub'} User`,
phone_number: null,
is_active: true,
is_superuser: false,
created_at: new Date().toISOString(),
updated_at: new Date().toISOString(),
last_login: new Date().toISOString(),
organization_count: 0,
};
setCurrentUser(demoOAuthUser);
return HttpResponse.json({
access_token: generateMockToken('access', demoOAuthUser.id),
refresh_token: generateMockToken('refresh', demoOAuthUser.id),
token_type: 'bearer',
expires_in: 900,
is_new_user: true,
});
}),
/**
* OAuth Accounts Override
* Returns linked OAuth accounts for the current user
*/
http.get(`${API_BASE_URL}/api/v1/oauth/accounts`, async () => {
await delay(NETWORK_DELAY);
// In demo mode, return empty accounts (user can "link" them)
return HttpResponse.json({
accounts: [],
});
}),
];