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

update playwright tests to verify status on system user requests #453

Merged
merged 2 commits into from
Dec 11, 2024
Merged
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
24 changes: 23 additions & 1 deletion frontend/playwright/api-requests/ApiRequests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export class ApiRequests {
}
const errorText = await response.text();
console.error('Failed to fetch system users:', response.status, errorText);
console.log(response.status);
throw new Error(`Failed to fetch system users: ${response.statusText}`);
}

Expand Down Expand Up @@ -134,6 +133,29 @@ export class ApiRequests {
}
}

public async getStatusForSystemUserRequest<T>(
token: string,
systemRequestId: string,
): Promise<T> {
//Hardcode for now to test:
const endpoint = `v1/systemuser/request/vendor/${systemRequestId}`;
const url = `${process.env.API_BASE_URL}${endpoint}`;

const response = await fetch(url, {
method: 'GET',
headers: {
Authorization: `Bearer ${token}`,
},
});

if (!response.ok) {
throw new Error(`Failed to fetch status for system user request. Status: ${response.status}`);
}

const data = await response.json();
return data;
}

generatePayloadSystemUserRequest(): PostSystemUserRequestPayload {
const randomString = Date.now(); // Current timestamp in milliseconds
const randomNum = Math.random().toString(36);
Expand Down
32 changes: 26 additions & 6 deletions frontend/playwright/e2eTests/approveSystemUserRequest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,33 @@ test.describe('Godkjenn og avvis Systembrukerforespørsel', () => {

test('Avvis Systembrukerforespørsel', async ({ page }): Promise<void> => {
//Generate confirmUrl from API
const confirmUrl = await prepareSystemUserRequest(api, token);
const systemUserRequestResponse = await prepareSystemUserRequest(api, token);

await page.goto(confirmUrl);
await page.goto(systemUserRequestResponse.confirmUrl);
await page.getByRole('button', { name: 'Avvis' }).click();

//Expect user to be logged out
await expect(loginPage.LOGIN_BUTTON).toBeVisible();
await expect(page).toHaveURL('https://info.altinn.no');

//Read from status api to verify that status is not rejected after clicking "Avvis"
const statusApiRequest = await getStatusForRequestApi(systemUserRequestResponse.id, token);
expect(statusApiRequest.status).toBe('Rejected');
});

test('Godkjenn Systembrukerforespørsel', async ({ page }): Promise<void> => {
const confirmUrl = await prepareSystemUserRequest(api, token);
const systemUserRequestResponse = await prepareSystemUserRequest(api, token);

await page.goto(confirmUrl);
await page.goto(systemUserRequestResponse.confirmUrl);
await page.getByRole('button', { name: 'Godkjenn' }).click();

//Expect user to be logged out
await expect(loginPage.LOGIN_BUTTON).toBeVisible();
await expect(page).toHaveURL('https://info.altinn.no');

//Read from status api to verify that status is not Accepted after clicking "Avvis"
const statusApiRequest = await getStatusForRequestApi(systemUserRequestResponse.id, token);
expect(statusApiRequest.status).toBe('Accepted');
});

async function prepareSystemUserRequest(api: ApiRequests, tokenclass: Token) {
Expand All @@ -43,7 +51,19 @@ test.describe('Godkjenn og avvis Systembrukerforespørsel', () => {
'altinn:authentication/systemuser.request.read altinn:authentication/systemuser.request.write';
const token = await tokenclass.getEnterpriseAltinnToken(scopes);
const endpoint = 'v1/systemuser/request/vendor';
const apiResponse = await api.sendPostRequest<{ confirmUrl: string }>(payload, endpoint, token);
return apiResponse.confirmUrl; // Return the Confirmation URL to use in the test
const apiResponse = await api.sendPostRequest<{ confirmUrl: string; id: string }>(
payload,
endpoint,
token,
);
return { confirmUrl: apiResponse.confirmUrl, id: apiResponse.id }; // Return the Confirmation URL and the ID to use in the test
}

async function getStatusForRequestApi(id: string, tokenclass: Token) {
const scopes =
'altinn:authentication/systemuser.request.read altinn:authentication/systemuser.request.write';
const token = await tokenclass.getEnterpriseAltinnToken(scopes);
const statusResponse = api.getStatusForSystemUserRequest<{ status: string }>(token, id);
return statusResponse;
}
});
Loading