2024-02-19 18:03:51 +01:00
|
|
|
import {
|
|
|
|
LoginResponseDto,
|
|
|
|
defaults,
|
|
|
|
login,
|
|
|
|
setAdminOnboarding,
|
|
|
|
signUpAdmin,
|
|
|
|
} from '@immich/sdk';
|
2024-02-13 19:08:49 +01:00
|
|
|
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-13 19:08:49 +01:00
|
|
|
|
2024-02-19 18:03:51 +01:00
|
|
|
export const app = 'http://127.0.0.1:2283/api';
|
2024-02-13 19:08:49 +01:00
|
|
|
|
2024-02-19 18:03:51 +01:00
|
|
|
defaults.baseUrl = app;
|
2024-02-13 19:08:49 +01:00
|
|
|
|
2024-02-19 18:03:51 +01:00
|
|
|
const setBaseUrl = () => (defaults.baseUrl = app);
|
|
|
|
export const asAuthHeader = (accessToken: string) => ({
|
2024-02-13 19:08:49 +01:00
|
|
|
Authorization: `Bearer ${accessToken}`,
|
|
|
|
});
|
|
|
|
|
2024-02-19 18:03:51 +01:00
|
|
|
let client: pg.Client | null = null;
|
|
|
|
|
|
|
|
export const dbUtils = {
|
|
|
|
setup: () => {
|
2024-02-13 19:08:49 +01:00
|
|
|
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-13 19:08:49 +01:00
|
|
|
|
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-13 19:08:49 +01:00
|
|
|
|
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) =>
|
2024-02-13 19:08:49 +01:00
|
|
|
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
|
|
|
]),
|
2024-02-13 19:08:49 +01:00
|
|
|
};
|