27 lines
742 B
TypeScript
27 lines
742 B
TypeScript
import type { ApiClient } from '@japa/api-client'
|
|
|
|
/**
|
|
* Logs in as Admin using the test HTTP client.
|
|
* Returns a Bearer token string.
|
|
*/
|
|
export async function loginAsAdmin(client: ApiClient): Promise<string> {
|
|
const email = process.env.ADMIN_EMAIL || 'admin@admin.com'
|
|
const password = process.env.ADMIN_PASSWORD || '1234qqqq'
|
|
|
|
const res = await client.post('/auth/login').json({ email, password })
|
|
|
|
res.assertStatus(200)
|
|
|
|
const body = res.body()
|
|
|
|
// returns: { type, token, user }
|
|
const token = body?.token?.release?.() || body?.token || body?.access_token
|
|
|
|
if (!token) {
|
|
console.error('Login response body:', body)
|
|
throw new Error('Could not acquire admin token from /auth/login response')
|
|
}
|
|
|
|
return token
|
|
}
|