Keycloak

Форк
0
/
linked-accounts.spec.ts 
120 строк · 4.3 Кб
1
import ClientRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientRepresentation";
2
import IdentityProviderRepresentation from "@keycloak/keycloak-admin-client/lib/defs/identityProviderRepresentation";
3
import { expect, test } from "@playwright/test";
4
import { randomUUID } from "node:crypto";
5

6
import {
7
  createClient,
8
  createIdentityProvider,
9
  createRandomUserWithPassword,
10
  deleteClient,
11
  deleteIdentityProvider,
12
  deleteUser,
13
  findClientByClientId,
14
  inRealm,
15
} from "../admin-client";
16
import groupsIdPClient from "../realms/groups-idp.json" assert { type: "json" };
17
import { getBaseUrl } from "../utils";
18

19
const realm = "groups";
20

21
test.describe("Account linking", () => {
22
  let groupIdPClientId: string;
23
  let user: string;
24
  // Tests for keycloak account console, section Account linking in Account security
25
  test.beforeAll(async () => {
26
    user = await createRandomUserWithPassword("user-" + randomUUID(), "pwd");
27

28
    const kcGroupsIdpId = await findClientByClientId("groups-idp");
29
    if (kcGroupsIdpId) {
30
      await deleteClient(kcGroupsIdpId);
31
    }
32
    groupIdPClientId = await createClient(
33
      groupsIdPClient as ClientRepresentation,
34
    );
35
    const baseUrl = getBaseUrl();
36
    const idp: IdentityProviderRepresentation = {
37
      alias: "master-idp",
38
      providerId: "oidc",
39
      enabled: true,
40
      config: {
41
        clientId: "groups-idp",
42
        clientSecret: "H0JaTc7VBu3HJR26vrzMxgidfJmgI5Dw",
43
        validateSignature: "false",
44
        tokenUrl: `${baseUrl}/realms/master/protocol/openid-connect/token`,
45
        jwksUrl: `${baseUrl}/realms/master/protocol/openid-connect/certs`,
46
        issuer: `${baseUrl}/realms/master`,
47
        authorizationUrl: `${baseUrl}/realms/master/protocol/openid-connect/auth`,
48
        logoutUrl: `${baseUrl}/realms/master/protocol/openid-connect/logout`,
49
        userInfoUrl: `${baseUrl}/realms/master/protocol/openid-connect/userinfo`,
50
      },
51
    };
52

53
    await inRealm(realm, () => createIdentityProvider(idp));
54
  });
55

56
  test.afterAll(async () => {
57
    await deleteUser(user);
58
  });
59
  test.afterAll(async () => {
60
    await deleteClient(groupIdPClientId);
61
  });
62
  test.afterAll(async () => {
63
    await inRealm(realm, () => deleteIdentityProvider("master-idp"));
64
  });
65

66
  test("Linking", async ({ page }) => {
67
    // If refactoring this, consider introduction of helper functions for individual pages - login, update profile etc.
68
    await page.goto(
69
      process.env.CI ? `/realms/${realm}/account` : `/?realm=${realm}`,
70
    );
71

72
    // Click the login via master-idp provider button
73
    await loginWithIdp(page, "master-idp");
74

75
    // Now the login at the master-idp should be visible
76
    await loginWithUsernamePassword(page, "admin", "admin");
77

78
    // Now the update-profile page should be visible
79
    await updateProfile(page, "test", "user", "testuser@keycloak.org");
80

81
    // Now the account console should be visible
82
    await page.getByTestId("accountSecurity").click();
83
    await expect(
84
      page.getByTestId("account-security/linked-accounts"),
85
    ).toBeVisible();
86
    await page.getByTestId("account-security/linked-accounts").click();
87
    await expect(
88
      page
89
        .getByTestId("linked-accounts/master-idp")
90
        .getByRole("button", { name: "Unlink account" }),
91
    ).toBeVisible();
92
    await page
93
      .getByTestId("linked-accounts/master-idp")
94
      .getByRole("button", { name: "Unlink account" })
95
      .click();
96

97
    // Expect an error shown that the account cannot be unlinked
98
    await expect(page.getByLabel("Danger Alert")).toBeVisible();
99
  });
100
});
101

102
async function updateProfile(page, firstName, lastName, email) {
103
  await expect(
104
    page.getByRole("heading", { name: "Update Account Information" }),
105
  ).toBeVisible();
106
  await page.getByLabel("Email", { exact: true }).fill(email);
107
  await page.getByLabel("First name", { exact: true }).fill(firstName);
108
  await page.getByLabel("Last name", { exact: true }).fill(lastName);
109
  await page.getByRole("button", { name: "Submit" }).click();
110
}
111

112
async function loginWithUsernamePassword(page, username, password) {
113
  await page.getByLabel("Username").fill(username);
114
  await page.getByLabel("Password", { exact: true }).fill(password);
115
  await page.getByRole("button", { name: "Sign In" }).click();
116
}
117

118
async function loginWithIdp(page, idpAlias: string) {
119
  await page.getByRole("link", { name: idpAlias }).click();
120
}
121

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.