Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(api7): add e2e on latest version #185

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,24 @@ jobs:
# Run API7 E2E tests
- name: Run E2E tests
run: npx nx run backend-api7:e2e

api7-latest:
runs-on: ubuntu-latest
if: contains(github.event.pull_request.labels.*.name, 'test/api7-latest') || github.event_name == 'push'
env:
BACKEND_API7_DOWNLOAD_URL: ${{ secrets.BACKEND_API7_LATEST_DOWNLOAD_URL }}
BACKEND_API7_LICENSE: ${{ secrets.BACKEND_API7_LICENSE }}
steps:
- uses: actions/checkout@v4

# Build and test ADC CLI
- uses: actions/setup-node@v4
with:
node-version: 'lts/*'
- uses: pnpm/action-setup@v4
- name: Install dependencies
run: pnpm install

# Run API7 E2E tests
- name: Run E2E tests
run: npx nx run backend-api7:e2e
67 changes: 63 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 @@ -60,12 +117,14 @@ const generateToken = async () => {
},
{ validateStatus: () => true },
);
console.log(resp.data);

process.env.TOKEN = resp.data.value.token;
};

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

Expand Down
Loading