Skip to content

Commit

Permalink
test: fix global setup
Browse files Browse the repository at this point in the history
  • Loading branch information
bzp2010 committed Sep 15, 2024
1 parent 5d46633 commit e8be041
Showing 1 changed file with 62 additions and 4 deletions.
66 changes: 62 additions & 4 deletions libs/backend-api7/e2e/support/global-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,29 @@ import { Agent } from 'node:https';

const httpClient = axios.create({
baseURL: 'https://localhost:7443',
auth: {
username: 'admin',
password: 'admin',
},
withCredentials: true,
httpsAgent: new Agent({
rejectUnauthorized: false,
}),
});

httpClient.interceptors.response.use((response) => {
if (response.headers['set-cookie']?.[0]) {
//@ts-expect-error forced
httpClient.sessionToken = response.headers['set-cookie']?.[0].split(';')[0];
}
return response;
});

httpClient.interceptors.request.use(
(config) => {
//@ts-expect-error forced
config.headers['Cookie'] = httpClient.sessionToken;
return config;
},
(error) => Promise.reject(error),
);

const setupAPI7 = async () => {
return new Promise<void>((resolve, reject) => {
const ls = spawn(
Expand Down Expand Up @@ -45,13 +59,56 @@ const setupAPI7 = async () => {
});
};

const initUser = async (username = 'admin', password = 'admin') => {
console.log('Log in');
await httpClient.post(`/api/login`, {
username: username,
password: password,
});

console.log('Modify password');
await httpClient.put(`/api/password`, {
old_password: password,
new_password: 'Admin12345!',
});

//@ts-expect-error forced
httpClient.sessionToken = '';

console.log('Log in again');
await httpClient.post(`/api/login`, {
username: username,
password: 'Admin12345!',
});
};

const activateAPI7 = async () => {
console.log('Upload license');
await httpClient.put(`/api/license`, {
data: process.env.BACKEND_API7_LICENSE,
});
};

const generateToken = async () => {
console.log('Create test user');
const user = await httpClient.post(`/api/invites`, {
username: 'test',
password: 'test',
});
const userId: string = user.data.value.id;

console.log('Update role');
await httpClient.put(`/api/users/${userId}/assigned_roles`, {
roles: ['super_admin_id'],
});

//@ts-expect-error forced
httpClient.sessionToken = '';

console.log('Log in to test user');
await initUser('test', 'test');

console.log('Generate token');
const resp = await httpClient.post<{ value: { token: string } }>(
`/api/tokens`,
{
Expand All @@ -66,6 +123,7 @@ const generateToken = async () => {

export default async () => {
if (process.env['SKIP_API7_SETUP'] !== 'true') await setupAPI7();
await initUser();
await activateAPI7();
await generateToken();

Expand Down

0 comments on commit e8be041

Please sign in to comment.