Skip to content

Commit e8be041

Browse files
committed
test: fix global setup
1 parent 5d46633 commit e8be041

File tree

1 file changed

+62
-4
lines changed

1 file changed

+62
-4
lines changed

libs/backend-api7/e2e/support/global-setup.ts

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,29 @@ import { Agent } from 'node:https';
55

66
const httpClient = axios.create({
77
baseURL: 'https://localhost:7443',
8-
auth: {
9-
username: 'admin',
10-
password: 'admin',
11-
},
8+
withCredentials: true,
129
httpsAgent: new Agent({
1310
rejectUnauthorized: false,
1411
}),
1512
});
1613

14+
httpClient.interceptors.response.use((response) => {
15+
if (response.headers['set-cookie']?.[0]) {
16+
//@ts-expect-error forced
17+
httpClient.sessionToken = response.headers['set-cookie']?.[0].split(';')[0];
18+
}
19+
return response;
20+
});
21+
22+
httpClient.interceptors.request.use(
23+
(config) => {
24+
//@ts-expect-error forced
25+
config.headers['Cookie'] = httpClient.sessionToken;
26+
return config;
27+
},
28+
(error) => Promise.reject(error),
29+
);
30+
1731
const setupAPI7 = async () => {
1832
return new Promise<void>((resolve, reject) => {
1933
const ls = spawn(
@@ -45,13 +59,56 @@ const setupAPI7 = async () => {
4559
});
4660
};
4761

62+
const initUser = async (username = 'admin', password = 'admin') => {
63+
console.log('Log in');
64+
await httpClient.post(`/api/login`, {
65+
username: username,
66+
password: password,
67+
});
68+
69+
console.log('Modify password');
70+
await httpClient.put(`/api/password`, {
71+
old_password: password,
72+
new_password: 'Admin12345!',
73+
});
74+
75+
//@ts-expect-error forced
76+
httpClient.sessionToken = '';
77+
78+
console.log('Log in again');
79+
await httpClient.post(`/api/login`, {
80+
username: username,
81+
password: 'Admin12345!',
82+
});
83+
};
84+
4885
const activateAPI7 = async () => {
86+
console.log('Upload license');
4987
await httpClient.put(`/api/license`, {
5088
data: process.env.BACKEND_API7_LICENSE,
5189
});
5290
};
5391

5492
const generateToken = async () => {
93+
console.log('Create test user');
94+
const user = await httpClient.post(`/api/invites`, {
95+
username: 'test',
96+
password: 'test',
97+
});
98+
const userId: string = user.data.value.id;
99+
100+
console.log('Update role');
101+
await httpClient.put(`/api/users/${userId}/assigned_roles`, {
102+
roles: ['super_admin_id'],
103+
});
104+
105+
//@ts-expect-error forced
106+
httpClient.sessionToken = '';
107+
108+
console.log('Log in to test user');
109+
await initUser('test', 'test');
110+
111+
console.log('Generate token');
55112
const resp = await httpClient.post<{ value: { token: string } }>(
56113
`/api/tokens`,
57114
{
@@ -66,6 +123,7 @@ const generateToken = async () => {
66123

67124
export default async () => {
68125
if (process.env['SKIP_API7_SETUP'] !== 'true') await setupAPI7();
126+
await initUser();
69127
await activateAPI7();
70128
await generateToken();
71129

0 commit comments

Comments
 (0)