Skip to content

Commit

Permalink
RHIDP-4775 Enhance error handling in RBAC e2e tests (#1953)
Browse files Browse the repository at this point in the history
Added error tracking and logging to beforeEach, afterEach, and afterAll hooks to improve robustness and clarity. Skipped cleanup actions if errors occurred during retries to avoid inconsistent state.
  • Loading branch information
gustavolira authored Nov 22, 2024
1 parent 90ab8c5 commit a182baf
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 18 deletions.
53 changes: 42 additions & 11 deletions e2e-tests/playwright/e2e/plugins/rbac/rbac-api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ test.describe("Test RBAC plugin REST API", () => {
let uiHelper: UIhelper;
let page: Page;
let responseHelper: Response;
// Variable to track errors or test states
let hasErrors = false;
let retriesRemaining: number;

test.beforeAll(async ({ browser }, testInfo) => {
page = (await setupBrowser(browser, testInfo)).page;
retriesRemaining = testInfo.project.retries;

uiHelper = new UIhelper(page);
common = new Common(page);
Expand All @@ -23,9 +27,12 @@ test.describe("Test RBAC plugin REST API", () => {
responseHelper = new Response(apiToken);
});

test.beforeEach(
async () => await new Common(page).checkAndClickOnGHloginPopup(),
);
// eslint-disable-next-line no-empty-pattern
test.beforeEach(async ({}, testInfo) => {
console.log(
`beforeEach: Attempting setup for ${testInfo.title}, retry: ${testInfo.retry}`,
);
});

test("Test that roles and policies from GET request are what expected", async ({
request,
Expand Down Expand Up @@ -238,16 +245,38 @@ test.describe("Test RBAC plugin REST API", () => {
expect(deleteResponse.ok());
});

test.skip("Test catalog-entity refresh is denied after DELETE", async () => {
test("Test catalog-entity refresh is denied after DELETE", async () => {
await uiHelper.openSidebar("Catalog");
await uiHelper.selectMuiBox("Kind", "API");
await uiHelper.clickLink("Nexus Repo Manager 3");
expect(await uiHelper.isBtnVisible("Schedule entity refresh")).toBeFalsy();
});

test.afterAll(
"Cleanup by deleting all new policies and roles",
async ({ request }) => {
// eslint-disable-next-line no-empty-pattern
test.afterEach(async ({}, testInfo) => {
if (testInfo.status === "failed") {
console.log(`Test failed: ${testInfo.title}`);
hasErrors = true;

// Calculate the remaining retries by subtracting the current retry count from the total retries and adjusting for zero-based indexing.
retriesRemaining = testInfo.project.retries - testInfo.retry - 1;
console.log(`Retries remaining: ${retriesRemaining}`);
}
});

test.afterAll(async ({ request }) => {
if (hasErrors && retriesRemaining > 0) {
console.log(
`Skipping cleanup due to errors. Retries remaining: ${retriesRemaining}`,
);
return;
}

console.log(
`afterAll: Proceeding with cleanup. Retries remaining: ${retriesRemaining}`,
);

try {
const remainingPoliciesResponse = await request.get(
"/api/permission/policies/role/default/test",
responseHelper.getSimpleRequest(),
Expand All @@ -267,8 +296,10 @@ test.describe("Test RBAC plugin REST API", () => {
responseHelper.getSimpleRequest(),
);

expect(deleteRemainingPolicies.ok());
expect(deleteRole.ok());
},
);
expect(deleteRemainingPolicies.ok()).toBeTruthy();
expect(deleteRole.ok()).toBeTruthy();
} catch (error) {
console.error("Error during cleanup in afterAll:", error);
}
});
});
32 changes: 25 additions & 7 deletions e2e-tests/playwright/support/pages/rbac.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,13 +294,31 @@ export class Response {
};
}

async removeMetadataFromResponse(response: APIResponse) {
const responseJson = await response.json();
const responseClean = responseJson.map((list: any) => {
delete list.metadata;
return list;
});
return responseClean;
async removeMetadataFromResponse(response: APIResponse): Promise<any[]> {
try {
const responseJson = await response.json();

// Validate that the response is an array
if (!Array.isArray(responseJson)) {
console.warn(
`Expected an array but received: ${JSON.stringify(responseJson)}`,
);
return []; // Return an empty array as a fallback
}

// Clean metadata from the response
const responseClean = responseJson.map((item: any) => {
if (item.metadata) {
delete item.metadata;
}
return item;
});

return responseClean;
} catch (error) {
console.error("Error processing API response:", error);
throw new Error("Failed to process the API response");
}
}

async checkResponse(response: APIResponse, expected: string) {
Expand Down

0 comments on commit a182baf

Please sign in to comment.