1
0
Fork 0
mirror of https://github.com/immich-app/immich.git synced 2025-01-19 18:26:46 +01:00
immich/e2e/src/utils.ts

101 lines
2.3 KiB
TypeScript
Raw Normal View History

2024-02-19 18:03:51 +01:00
import {
LoginResponseDto,
defaults,
login,
setAdminOnboarding,
signUpAdmin,
} from '@immich/sdk';
import { BrowserContext } from '@playwright/test';
2024-02-19 18:03:51 +01:00
import pg from 'pg';
import { loginDto, signupDto } from 'src/fixtures';
2024-02-19 18:03:51 +01:00
export const app = 'http://127.0.0.1:2283/api';
2024-02-19 18:03:51 +01:00
defaults.baseUrl = app;
2024-02-19 18:03:51 +01:00
const setBaseUrl = () => (defaults.baseUrl = app);
export const asAuthHeader = (accessToken: string) => ({
Authorization: `Bearer ${accessToken}`,
});
2024-02-19 18:03:51 +01:00
let client: pg.Client | null = null;
export const dbUtils = {
setup: () => {
setBaseUrl();
2024-02-19 18:03:51 +01:00
},
reset: async () => {
try {
if (!client) {
client = new pg.Client(
'postgres://postgres:postgres@127.0.0.1:5433/immich'
);
await client.connect();
}
2024-02-19 18:03:51 +01:00
for (const table of ['user_token', 'users', 'system_metadata']) {
await client.query(`DELETE FROM ${table} CASCADE;`);
}
} catch (error) {
console.error('Failed to reset database', error);
throw error;
}
},
teardown: async () => {
try {
if (client) {
await client.end();
client = null;
}
} catch (error) {
console.error('Failed to teardown database', error);
throw error;
}
},
};
2024-02-19 18:03:51 +01:00
export const apiUtils = {
adminSetup: async () => {
await signUpAdmin({ signUpDto: signupDto.admin });
const response = await login({ loginCredentialDto: loginDto.admin });
await setAdminOnboarding({ headers: asAuthHeader(response.accessToken) });
return response;
},
};
export const webUtils = {
setAuthCookies: async (context: BrowserContext, response: LoginResponseDto) =>
await context.addCookies([
{
name: 'immich_access_token',
value: response.accessToken,
domain: '127.0.0.1',
path: '/',
expires: 1742402728,
httpOnly: true,
secure: false,
sameSite: 'Lax',
},
{
name: 'immich_auth_type',
value: 'password',
domain: '127.0.0.1',
path: '/',
expires: 1742402728,
httpOnly: true,
secure: false,
sameSite: 'Lax',
},
{
name: 'immich_is_authenticated',
value: 'true',
domain: '127.0.0.1',
path: '/',
expires: 1742402728,
httpOnly: false,
secure: false,
sameSite: 'Lax',
},
2024-02-19 18:03:51 +01:00
]),
};