From 2914c5821dc0c158ed5e61bc82a9afc5de952d88 Mon Sep 17 00:00:00 2001 From: Hynek Mlnarik Date: Tue, 13 Feb 2024 11:44:25 +0100 Subject: [PATCH] Add signing-in tests Fixes: #21245 Signed-off-by: Hynek Mlnarik --- .../src/account-security/SigningIn.tsx | 11 ++- .../test/account-security/signing-in.spec.ts | 92 +++++++++++++++++++ js/apps/account-ui/test/admin-client.ts | 33 +++++++ js/apps/account-ui/test/groups.spec.ts | 2 +- .../account-ui/test/realms/groups-realm.json | 2 +- 5 files changed, 136 insertions(+), 4 deletions(-) create mode 100644 js/apps/account-ui/test/account-security/signing-in.spec.ts diff --git a/js/apps/account-ui/src/account-security/SigningIn.tsx b/js/apps/account-ui/src/account-security/SigningIn.tsx index 2d2ba50e84..6e927e0dac 100644 --- a/js/apps/account-ui/src/account-security/SigningIn.tsx +++ b/js/apps/account-ui/src/account-security/SigningIn.tsx @@ -34,9 +34,10 @@ import { useEnvironment } from "../root/KeycloakContext"; type MobileLinkProps = { title: string; onClick: () => void; + testid?: string; }; -const MobileLink = ({ title, onClick }: MobileLinkProps) => { +const MobileLink = ({ title, onClick, testid }: MobileLinkProps) => { const [open, setOpen] = useState(false); return ( <> @@ -56,6 +57,7 @@ const MobileLink = ({ title, onClick }: MobileLinkProps) => { variant="link" onClick={onClick} className="pf-u-display-none pf-u-display-inline-flex-on-lg" + data-testid={testid} > {title} @@ -145,13 +147,18 @@ export const SigningIn = () => { title={t("setUpNew", { name: t(container.displayName as TFuncKey), })} + testid={`${container.category}/create`} /> )} - + {container.userCredentialMetadatas.length === 0 && ( { + // Tests for keycloak account console, section Signing in in Account security + test("Should see only password", async ({ page }) => { + await login(page, "jdoe", "jdoe", "groups"); + + await page.getByTestId("accountSecurity").click(); + await expect(page.getByTestId("account-security/signing-in")).toBeVisible(); + page.getByTestId("account-security/signing-in").click(); + + await expect( + page + .getByTestId("basic-authentication/credential-list") + .getByRole("listitem"), + ).toHaveCount(1); + await expect( + page + .getByTestId("basic-authentication/credential-list") + .getByRole("listitem"), + ).toContainText("My password"); + await expect(page.getByTestId("basic-authentication/create")).toBeHidden(); + + await expect( + page.getByTestId("two-factor/credential-list").getByRole("listitem"), + ).toHaveCount(1); + await expect( + page.getByTestId("two-factor/credential-list").getByRole("listitem"), + ).toContainText("not set up"); + await expect(page.getByTestId("two-factor/create")).toBeVisible(); + + await page.getByTestId("two-factor/create").click(); + await expect(page.locator("#kc-page-title")).toContainText( + "Mobile Authenticator Setup", + ); + }); +}); + +test.describe("Signing in 2", () => { + test.afterAll(async () => { + await deleteRealm(realm); + await importRealm(groupsRealm as RealmRepresentation); + }); + test("Password removal", async ({ page }) => { + const jdoeUser = await getUserByUsername("jdoe", realm); + + await login(page, "jdoe", "jdoe", "groups"); + + const credentials = await getCredentials(jdoeUser!.id!, realm); + deleteCredential(jdoeUser!.id!, credentials![0].id!, realm); + + await page.getByTestId("accountSecurity").click(); + await expect(page.getByTestId("account-security/signing-in")).toBeVisible(); + page.getByTestId("account-security/signing-in").click(); + + await expect( + page + .getByTestId("basic-authentication/credential-list") + .getByRole("listitem"), + ).toHaveCount(1); + await expect( + page + .getByTestId("basic-authentication/credential-list") + .getByRole("listitem"), + ).toContainText("not set up"); + await expect(page.getByTestId("basic-authentication/create")).toBeVisible(); + + await expect( + page.getByTestId("two-factor/credential-list").getByRole("listitem"), + ).toHaveCount(1); + await expect( + page.getByTestId("two-factor/credential-list").getByRole("listitem"), + ).toContainText("not set up"); + await expect(page.getByTestId("two-factor/create")).toBeVisible(); + + await page.getByTestId("basic-authentication/create").click(); + await expect(page.locator("#kc-page-title")).toContainText( + "Update password", + ); + }); +}); diff --git a/js/apps/account-ui/test/admin-client.ts b/js/apps/account-ui/test/admin-client.ts index eb57189c39..a683be9895 100644 --- a/js/apps/account-ui/test/admin-client.ts +++ b/js/apps/account-ui/test/admin-client.ts @@ -58,6 +58,11 @@ export async function createUser(user: UserRepresentation, realm: string) { } } +export async function getUserByUsername(username: string, realm: string) { + const users = await adminClient.users.find({ username, realm, exact: true }); + return users.length > 0 ? users[0] : undefined; +} + export async function deleteUser(username: string, realm: string) { try { const users = await adminClient.users.find({ username, realm }); @@ -67,3 +72,31 @@ export async function deleteUser(username: string, realm: string) { console.error(error); } } + +export async function updateUser(user: UserRepresentation, realm: string) { + try { + await adminClient.users.update({ id: user.id!, realm }, user); + } catch (error) { + console.error(error); + } +} + +export async function getCredentials(id: string, realm: string) { + try { + return await adminClient.users.getCredentials({ id, realm }); + } catch (error) { + console.error(error); + } +} + +export async function deleteCredential( + id: string, + credentialId: string, + realm: string, +) { + try { + await adminClient.users.deleteCredential({ id, credentialId, realm }); + } catch (error) { + console.error(error); + } +} diff --git a/js/apps/account-ui/test/groups.spec.ts b/js/apps/account-ui/test/groups.spec.ts index 6de9ae9602..42d381fc94 100644 --- a/js/apps/account-ui/test/groups.spec.ts +++ b/js/apps/account-ui/test/groups.spec.ts @@ -8,7 +8,7 @@ test.describe("Groups page", () => { await expect(page.getByTestId("group[1].name")).toHaveText("three"); }); - test("List my direct and indirect groups", async ({ page }) => { + test("List direct and indirect groups", async ({ page }) => { await login(page, "alice", "alice", "groups"); await page.getByTestId("groups").click(); diff --git a/js/apps/account-ui/test/realms/groups-realm.json b/js/apps/account-ui/test/realms/groups-realm.json index 2217fe5034..241f811b27 100644 --- a/js/apps/account-ui/test/realms/groups-realm.json +++ b/js/apps/account-ui/test/realms/groups-realm.json @@ -18,7 +18,7 @@ "enabled": true, "realmRoles": [], "clientRoles": { - "account": ["view-groups"] + "account": ["view-groups", "manage-account"] }, "credentials": [ {