2025-02-22 14:04:01 +03:00
|
|
|
import { type Page } from '@playwright/test';
|
2025-01-27 16:12:41 +03:00
|
|
|
|
2025-03-14 20:44:23 +03:00
|
|
|
import { type ICurrentUser, type IUserLoginDTO } from '../../src/features/auth/backend/types';
|
2025-03-06 21:09:44 +03:00
|
|
|
import { BACKEND_URL } from './constants';
|
2025-01-27 16:12:41 +03:00
|
|
|
|
2025-03-02 19:42:19 +03:00
|
|
|
const dataAnonymousAuth: ICurrentUser = {
|
|
|
|
id: null,
|
|
|
|
username: '',
|
|
|
|
is_staff: false,
|
|
|
|
editor: []
|
|
|
|
};
|
|
|
|
|
|
|
|
const dataAdminAuth: ICurrentUser = {
|
|
|
|
id: 1,
|
|
|
|
username: 'admin',
|
|
|
|
is_staff: true,
|
|
|
|
editor: []
|
|
|
|
};
|
|
|
|
|
|
|
|
const dataUserAuth: ICurrentUser = {
|
|
|
|
id: 2,
|
|
|
|
username: 'user',
|
|
|
|
is_staff: false,
|
|
|
|
editor: [2]
|
|
|
|
};
|
|
|
|
|
2025-03-06 21:09:44 +03:00
|
|
|
let currentAuth = dataAnonymousAuth;
|
|
|
|
|
|
|
|
export async function setupAuth(context: Page) {
|
|
|
|
await context.route(`${BACKEND_URL}/users/api/auth`, async route => {
|
|
|
|
await route.fulfill({ json: currentAuth });
|
2025-03-02 19:42:19 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2025-03-06 21:09:44 +03:00
|
|
|
export function authAnonymous() {
|
|
|
|
currentAuth = dataAnonymousAuth;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function authAdmin() {
|
|
|
|
currentAuth = dataAdminAuth;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function authUser() {
|
|
|
|
currentAuth = dataUserAuth;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function setupLogin(page: Page) {
|
|
|
|
await page.route(`${BACKEND_URL}/users/api/login`, async route => {
|
|
|
|
const data = route.request().postDataJSON() as IUserLoginDTO;
|
|
|
|
if (data.password !== 'password') {
|
|
|
|
await route.fulfill({
|
|
|
|
status: 400,
|
|
|
|
json: {
|
|
|
|
detail: 'Invalid credentials'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (data.username === 'admin') {
|
|
|
|
authAdmin();
|
|
|
|
} else {
|
|
|
|
authUser();
|
|
|
|
}
|
|
|
|
await route.fulfill({ status: 200 });
|
2025-03-02 19:42:19 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2025-03-06 21:09:44 +03:00
|
|
|
export async function setupLogout(page: Page) {
|
|
|
|
await page.route(`${BACKEND_URL}/users/api/logout`, async route => {
|
|
|
|
authAnonymous();
|
|
|
|
await route.fulfill({ status: 200 });
|
2025-01-27 16:12:41 +03:00
|
|
|
});
|
|
|
|
}
|