mirror of
https://github.com/go-vikunja/vikunja.git
synced 2026-05-22 02:30:56 -05:00
Add web tests covering the authorize endpoint, token exchange, PKCE verification, single-use codes, and refresh token rotation. Add unit tests for redirect URI validation and PKCE. Add E2E test for the full browser-based authorization code flow with login redirect. Extract setupApiUrl helper for E2E tests to avoid duplication.
67 lines
1.9 KiB
TypeScript
67 lines
1.9 KiB
TypeScript
import type {Page, APIRequestContext} from '@playwright/test'
|
|
import {UserFactory} from '../factories/user'
|
|
import {TEST_PASSWORD} from './constants'
|
|
|
|
/**
|
|
* Sets up the API URL in the page's localStorage and window so the frontend
|
|
* knows where to send requests. Call this before navigating to any page.
|
|
*/
|
|
export async function setupApiUrl(page: Page) {
|
|
// Use 127.0.0.1 instead of localhost to match the frontend's origin for CORS
|
|
const apiUrl = process.env.API_URL || 'http://127.0.0.1:3456/api/v1'
|
|
await page.addInitScript(({apiUrl}) => {
|
|
window.localStorage.setItem('API_URL', apiUrl)
|
|
window.API_URL = apiUrl
|
|
}, {apiUrl})
|
|
}
|
|
|
|
/**
|
|
* This authenticates a user and puts the token in local storage which allows us to perform authenticated requests.
|
|
* Returns the user and token for use in tests that need to make authenticated API calls.
|
|
*/
|
|
export async function login(page: Page | null, apiContext: APIRequestContext, user?: any) {
|
|
if (!user) {
|
|
throw new Error('Needs user')
|
|
}
|
|
|
|
// Login via API
|
|
const response = await apiContext.post('login', {
|
|
data: {
|
|
username: user.username,
|
|
password: TEST_PASSWORD,
|
|
},
|
|
})
|
|
|
|
if (!response.ok()) {
|
|
throw new Error(`Login failed: ${response.status()} ${response.statusText()}`)
|
|
}
|
|
|
|
const body = await response.json()
|
|
const token = body.token
|
|
|
|
// Set token and API_URL before navigating (only if page is provided)
|
|
if (page) {
|
|
await setupApiUrl(page)
|
|
await page.addInitScript(({token}) => {
|
|
window.localStorage.setItem('token', token)
|
|
}, {token})
|
|
}
|
|
|
|
return {user, token}
|
|
}
|
|
|
|
export async function createFakeUser() {
|
|
const [u] = await UserFactory.create(1)
|
|
return u
|
|
}
|
|
|
|
/**
|
|
* Helper function to set up authentication for a test suite
|
|
* Returns the created user for use in tests
|
|
*/
|
|
export function createFakeUserAndLogin() {
|
|
// This returns undefined and instead relies on Playwright's beforeEach hooks
|
|
// The actual user will be available through the test context
|
|
return undefined
|
|
}
|