8000 Report an error when sharing a resource fails in the account console by Praiztl · Pull Request #55510 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Open
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
42 changes: 30 additions & 12 deletions js/apps/account-ui/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,44 @@ export const fetchPermission = async (
return parseResponse<Permission[]>(response);
};

export const updateRequest = (
export const updateRequest = async (
context: KeycloakContext<BaseEnvironment>,
resourceId: string,
username: string,
scopes: Scope[] | string[],
) =>
request(`/resources/${resourceId}/permissions`, context, {
method: "PUT",
body: [{ username, scopes }],
});
) => {
const response = await request(
`/resources/${resourceId}/permissions`,
context,
{
method: "PUT",
body: [{ username, scopes }],
},
);
if (!response.ok) {
throw await parseResponse(response);
Comment on lines +57 to +58
}
return response;
};

export const updatePermissions = (
export const updatePermissions = async (
context: KeycloakContext<BaseEnvironment>,
resourceId: string,
permissions: Permission[],
) =>
request(`/resources/${resourceId}/permissions`, context, {
method: "PUT",
body: permissions,
});
) => {
const response = await request(
`/resources/${resourceId}/permissions`,
context,
{
method: "PUT",
body: permissions,
},
);
if (!response.ok) {
throw await parseResponse(response);
}
return response;
};

function checkResponse<T>(response: T) {
if (!response) throw new Error("Could not fetch");
Expand Down
55 changes: 55 additions & 0 deletions js/apps/account-ui/test/resources.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,59 @@ test.describe("Resources", () => {
const rowData = page2.getByTestId("row[0].name");
await expect(rowData).toHaveText("one");
});

test("shows an error and no success alert when sharing a resource fails", async ({
page,
}) => {
await using testBed = await createTestBed(
resourcesRealm as RealmRepresentation,
);
await waitForRealmReady();

await login(page, testBed.realm);
await page.getByTestId("resources").click();

await page.getByTestId("expand-one").click();
await expect(page.getByText("This resource is not shared.")).toBeVisible();

await page.getByTestId("share-one").click();
await page.getByTestId("users").click();
await page.getByTestId("users").fill("alice");
await page.getByTestId("add").click();

await page
.getByTestId("permissions")
.getByRole("button", { expanded: false })
.click();
await page.getByRole("option", { name: "album:view" }).click();
await page
.getByTestId("permissions")
.getByRole("button", { expanded: true })
.click();

// Make the share request fail so the console's error handling is exercised.
await page.route(
"**/realms/*/account/resources/*/permissions",
async (route) => {
if (route.request().method() !== "PUT") {
return route.continue();
}
await route.fulfill({
status: 403,
contentType: "application/json",
body: JSON.stringify({
error: "access_denied",
error_description: "You are not allowed to share this resource.",
}),
});
},
);

await page.getByTestId("done").click();

await expect(
page.getByText("You are not allowed to share this resource."),
).toBeVisible();
await expect(page.getByText("Resource successfully shared.")).toBeHidden();
});
});
0