Skip to content

Commit 81622ec

Browse files
committed
add global setup for tests
1 parent 6c10427 commit 81622ec

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* eslint-disable @typescript-eslint/no-namespace */
2+
/* eslint-disable @typescript-eslint/consistent-type-definitions */
3+
4+
import type { LoginCredentials } from '@opendatacapture/schemas/auth';
5+
import type { InitAppOptions } from '@opendatacapture/schemas/setup';
6+
import { expect, test } from '@playwright/test';
7+
8+
const initOptions: InitAppOptions = {
9+
admin: {
10+
firstName: 'Jane',
11+
lastName: 'Doe',
12+
password: 'DataCapture2025',
13+
username: 'admin'
14+
},
15+
enableExperimentalFeatures: false,
16+
initDemo: true
17+
};
18+
19+
declare global {
20+
namespace NodeJS {
21+
interface ProcessEnv {
22+
ADMIN_ACCESS_TOKEN: string;
23+
ADMIN_PASSWORD: string;
24+
ADMIN_USERNAME: string;
25+
GLOBAL_SETUP_COMPLETE?: '1';
26+
}
27+
}
28+
}
29+
30+
test.skip(() => process.env.GLOBAL_SETUP_COMPLETE === '1');
31+
32+
test.describe.serial(() => {
33+
test.describe.serial('setup', () => {
34+
test('initial setup', async ({ request }) => {
35+
const response = await request.get('/api/v1/setup');
36+
expect(response.status()).toBe(200);
37+
await expect(response.json()).resolves.toMatchObject({ isSetup: false });
38+
});
39+
test('successful setup', async ({ request }) => {
40+
const response = await request.post('/api/v1/setup', {
41+
data: initOptions
42+
});
43+
expect(response.status()).toBe(201);
44+
await expect(response.json()).resolves.toStrictEqual({ success: true });
45+
});
46+
test('setup state after initialization', async ({ request }) => {
47+
const response = await request.get('/api/v1/setup');
48+
expect(response.status()).toBe(200);
49+
await expect(response.json()).resolves.toMatchObject({ isSetup: true });
50+
});
51+
});
52+
test.describe.serial('auth', () => {
53+
test('login', async ({ request }) => {
54+
const { password, username } = initOptions.admin;
55+
const response = await request.post('/api/v1/auth/login', {
56+
data: { password, username } satisfies LoginCredentials
57+
});
58+
expect(response.status()).toBe(200);
59+
const { accessToken } = await response.json();
60+
expect(typeof accessToken).toBe('string');
61+
process.env.ADMIN_ACCESS_TOKEN = accessToken;
62+
process.env.ADMIN_USERNAME = username;
63+
process.env.ADMIN_PASSWORD = password;
64+
process.env.GLOBAL_SETUP_COMPLETE = '1';
65+
});
66+
});
67+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { expect, test } from '@playwright/test';
2+
3+
test('delete database', async ({ request }) => {
4+
const response = await request.delete('/api/v1/setup', {
5+
headers: {
6+
Authorization: `Bearer ${process.env.ADMIN_ACCESS_TOKEN}`
7+
}
8+
});
9+
expect(response.status()).toBe(200);
10+
});

0 commit comments

Comments
 (0)