Skip to content

Commit

Permalink
update playwright tests to verify status on system user requests (#453)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nyeng authored Dec 11, 2024
1 parent 462e282 commit 54af1c8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
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;
}
});

0 comments on commit 54af1c8

Please sign in to comment.