@appsemble/e2e 0.40.4 → 0.40.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/fixtures/test/accessibility/index.d.ts +75 -0
- package/fixtures/test/accessibility/index.js +73 -0
- package/fixtures/test/index.d.ts +4 -2
- package/fixtures/test/index.js +19 -2
- package/fixtures/test/live-app/index.js +1 -1
- package/package.json +7 -5
- package/playwright.config.js +2 -1
- package/tests/demo-apps/accessibility.spec.d.ts +1 -0
- package/tests/demo-apps/accessibility.spec.js +42 -0
- package/tests/demo-apps/mail.spec.d.ts +1 -0
- package/tests/demo-apps/mail.spec.js +31 -0
- package/tests/static-ui/apps.spec.js +3 -3
- package/tests/studio/logins/keycloak-entra-broker.spec.d.ts +1 -0
- package/tests/studio/logins/keycloak-entra-broker.spec.js +219 -0
- package/tests/studio/logins/keycloak-oidc.spec.js +17 -98
- package/tests/studio/logins/keycloak-saml.spec.js +17 -118
- package/tests/studio/organizations.spec.js +4 -0
- package/utils/keycloak-admin.d.ts +115 -0
- package/utils/keycloak-admin.js +272 -0
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
const { KEYCLOAK_ADMIN_PASSWORD = 'password', KEYCLOAK_ADMIN_USERNAME = 'admin', KEYCLOAK_BASE_URL, } = process.env;
|
|
2
|
+
/**
|
|
3
|
+
* Perform an authenticated request against the Keycloak admin REST API.
|
|
4
|
+
*
|
|
5
|
+
* @param path The path relative to `KEYCLOAK_BASE_URL`.
|
|
6
|
+
* @param options The request options: a serialized `body`, the `expectedStatuses` treated as
|
|
7
|
+
* successful, the HTTP `method`, and a Keycloak admin bearer `token`.
|
|
8
|
+
* @returns The raw fetch response.
|
|
9
|
+
*/
|
|
10
|
+
export async function requestKc(path, { body, expectedStatuses, method, token, }) {
|
|
11
|
+
const response = await fetch(`${KEYCLOAK_BASE_URL}${path}`, {
|
|
12
|
+
body,
|
|
13
|
+
headers: {
|
|
14
|
+
authorization: `Bearer ${token}`,
|
|
15
|
+
'content-type': 'application/json',
|
|
16
|
+
},
|
|
17
|
+
method,
|
|
18
|
+
});
|
|
19
|
+
if (!expectedStatuses.includes(response.status)) {
|
|
20
|
+
throw new Error(`${method} ${path} returned ${response.status}: ${await response.text()}`);
|
|
21
|
+
}
|
|
22
|
+
return response;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Obtain a Keycloak admin token from the `master` realm.
|
|
26
|
+
*
|
|
27
|
+
* The Keycloak service may still be starting when the tests begin, so this retries for a while.
|
|
28
|
+
*
|
|
29
|
+
* @returns The `admin-cli` access token.
|
|
30
|
+
*/
|
|
31
|
+
export async function getAdminToken() {
|
|
32
|
+
for (let attempt = 0; attempt < 60; attempt += 1) {
|
|
33
|
+
try {
|
|
34
|
+
const response = await fetch(`${KEYCLOAK_BASE_URL}/realms/master/protocol/openid-connect/token`, {
|
|
35
|
+
body: new URLSearchParams({
|
|
36
|
+
client_id: 'admin-cli',
|
|
37
|
+
grant_type: 'password',
|
|
38
|
+
password: KEYCLOAK_ADMIN_PASSWORD,
|
|
39
|
+
username: KEYCLOAK_ADMIN_USERNAME,
|
|
40
|
+
}),
|
|
41
|
+
headers: { 'content-type': 'application/x-www-form-urlencoded' },
|
|
42
|
+
method: 'POST',
|
|
43
|
+
});
|
|
44
|
+
if (response.ok) {
|
|
45
|
+
const { access_token: token } = (await response.json());
|
|
46
|
+
return token;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
// The service may still be starting.
|
|
51
|
+
}
|
|
52
|
+
await new Promise((resolve) => {
|
|
53
|
+
setTimeout(resolve, 1000);
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
throw new Error('Could not obtain Keycloak admin token');
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Recreate a realm from scratch by deleting any existing realm and creating an empty one.
|
|
60
|
+
*
|
|
61
|
+
* @param token A Keycloak admin bearer token.
|
|
62
|
+
* @param realm The realm name.
|
|
63
|
+
*/
|
|
64
|
+
export async function createRealm(token, realm) {
|
|
65
|
+
await requestKc(`/admin/realms/${realm}`, {
|
|
66
|
+
expectedStatuses: [204, 404],
|
|
67
|
+
method: 'DELETE',
|
|
68
|
+
token,
|
|
69
|
+
});
|
|
70
|
+
await requestKc('/admin/realms', {
|
|
71
|
+
body: JSON.stringify({ enabled: true, realm }),
|
|
72
|
+
expectedStatuses: [201],
|
|
73
|
+
method: 'POST',
|
|
74
|
+
token,
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Create a group, optionally nested under a parent group.
|
|
79
|
+
*
|
|
80
|
+
* @param token A Keycloak admin bearer token.
|
|
81
|
+
* @param realm The realm name.
|
|
82
|
+
* @param name The group name.
|
|
83
|
+
* @param parentGroupId The id of the parent group, when creating a nested group.
|
|
84
|
+
* @returns The id of the created group.
|
|
85
|
+
*/
|
|
86
|
+
export async function createGroup(token, realm, name, parentGroupId) {
|
|
87
|
+
const path = parentGroupId
|
|
88
|
+
? `/admin/realms/${realm}/groups/${parentGroupId}/children`
|
|
89
|
+
: `/admin/realms/${realm}/groups`;
|
|
90
|
+
const response = await requestKc(path, {
|
|
91
|
+
body: JSON.stringify({ name }),
|
|
92
|
+
expectedStatuses: [201],
|
|
93
|
+
method: 'POST',
|
|
94
|
+
token,
|
|
95
|
+
});
|
|
96
|
+
return response.headers.get('location').split('/').pop();
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Create an enabled user with a permanent password.
|
|
100
|
+
*
|
|
101
|
+
* @param token A Keycloak admin bearer token.
|
|
102
|
+
* @param realm The realm name.
|
|
103
|
+
* @param user The user to create: `email` (also used as the username), `firstName`, `lastName`,
|
|
104
|
+
* and `password`.
|
|
105
|
+
* @returns The id of the created user.
|
|
106
|
+
*/
|
|
107
|
+
export async function createUser(token, realm, { email, firstName, lastName, password, }) {
|
|
108
|
+
const response = await requestKc(`/admin/realms/${realm}/users`, {
|
|
109
|
+
body: JSON.stringify({
|
|
110
|
+
credentials: [{ temporary: false, type: 'password', value: password }],
|
|
111
|
+
email,
|
|
112
|
+
emailVerified: true,
|
|
113
|
+
enabled: true,
|
|
114
|
+
firstName,
|
|
115
|
+
lastName,
|
|
116
|
+
username: email,
|
|
117
|
+
}),
|
|
118
|
+
expectedStatuses: [201],
|
|
119
|
+
method: 'POST',
|
|
120
|
+
token,
|
|
121
|
+
});
|
|
122
|
+
return response.headers.get('location').split('/').pop();
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Add a user to a group.
|
|
126
|
+
*
|
|
127
|
+
* @param token A Keycloak admin bearer token.
|
|
128
|
+
* @param realm The realm name.
|
|
129
|
+
* @param userId The user id.
|
|
130
|
+
* @param groupId The group id.
|
|
131
|
+
*/
|
|
132
|
+
export async function joinGroup(token, realm, userId, groupId) {
|
|
133
|
+
await requestKc(`/admin/realms/${realm}/users/${userId}/groups/${groupId}`, {
|
|
134
|
+
expectedStatuses: [204],
|
|
135
|
+
method: 'PUT',
|
|
136
|
+
token,
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
function wrapCertificate(certificate) {
|
|
140
|
+
const lines = certificate.match(/.{1,64}/g);
|
|
141
|
+
if (!lines) {
|
|
142
|
+
throw new Error('Could not wrap Keycloak signing certificate');
|
|
143
|
+
}
|
|
144
|
+
return `-----BEGIN CERTIFICATE-----\n${lines.join('\n')}\n-----END CERTIFICATE-----`;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Fetch the active PEM-wrapped signing certificate of a realm.
|
|
148
|
+
*
|
|
149
|
+
* @param token A Keycloak admin bearer token.
|
|
150
|
+
* @param realm The realm name.
|
|
151
|
+
* @returns The PEM-wrapped signing certificate.
|
|
152
|
+
*/
|
|
153
|
+
export async function getSigningCertificate(token, realm) {
|
|
154
|
+
const response = await requestKc(`/admin/realms/${realm}/keys`, {
|
|
155
|
+
expectedStatuses: [200],
|
|
156
|
+
method: 'GET',
|
|
157
|
+
token,
|
|
158
|
+
});
|
|
159
|
+
const { keys } = (await response.json());
|
|
160
|
+
const certificate = keys.find((key) => key.use === 'SIG' && key.status === 'ACTIVE' && key.certificate)?.certificate;
|
|
161
|
+
if (!certificate) {
|
|
162
|
+
throw new Error('Could not find Keycloak signing certificate');
|
|
163
|
+
}
|
|
164
|
+
return wrapCertificate(certificate);
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Create a confidential OIDC client whose tokens carry group memberships as a `groups` claim.
|
|
168
|
+
*
|
|
169
|
+
* This mirrors the client the plain OIDC login test uses, but points its redirect URI at a
|
|
170
|
+
* Keycloak identity broker endpoint so a broker realm can federate to this realm.
|
|
171
|
+
*
|
|
172
|
+
* @param token A Keycloak admin bearer token.
|
|
173
|
+
* @param realm The upstream realm name.
|
|
174
|
+
* @param client The client to create: `clientId`, `clientSecret`, and the allowed `redirectUris`
|
|
175
|
+
* (typically the broker endpoint).
|
|
176
|
+
*/
|
|
177
|
+
export async function createUpstreamOidcClient(token, realm, { clientId, clientSecret, redirectUris, }) {
|
|
178
|
+
await requestKc(`/admin/realms/${realm}/clients`, {
|
|
179
|
+
body: JSON.stringify({
|
|
180
|
+
clientAuthenticatorType: 'client-secret',
|
|
181
|
+
clientId,
|
|
182
|
+
directAccessGrantsEnabled: true,
|
|
183
|
+
enabled: true,
|
|
184
|
+
protocol: 'openid-connect',
|
|
185
|
+
protocolMappers: [
|
|
186
|
+
{
|
|
187
|
+
config: {
|
|
188
|
+
'access.token.claim': 'true',
|
|
189
|
+
'claim.name': 'groups',
|
|
190
|
+
'full.path': 'true',
|
|
191
|
+
'id.token.claim': 'true',
|
|
192
|
+
'userinfo.token.claim': 'true',
|
|
193
|
+
},
|
|
194
|
+
name: 'groups',
|
|
195
|
+
protocol: 'openid-connect',
|
|
196
|
+
protocolMapper: 'oidc-group-membership-mapper',
|
|
197
|
+
},
|
|
198
|
+
],
|
|
199
|
+
publicClient: false,
|
|
200
|
+
redirectUris,
|
|
201
|
+
secret: clientSecret,
|
|
202
|
+
standardFlowEnabled: true,
|
|
203
|
+
webOrigins: ['+'],
|
|
204
|
+
}),
|
|
205
|
+
expectedStatuses: [201],
|
|
206
|
+
method: 'POST',
|
|
207
|
+
token,
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Create an OIDC identity provider that federates a broker realm to an upstream realm.
|
|
212
|
+
*
|
|
213
|
+
* @param token A Keycloak admin bearer token.
|
|
214
|
+
* @param realm The broker realm name.
|
|
215
|
+
* @param config The identity provider configuration: the `alias`, the `displayName` shown on the
|
|
216
|
+
* broker login page, the upstream `authorizationUrl`, `tokenUrl`, `userInfoUrl`, and `jwksUrl`
|
|
217
|
+
* endpoints, and the `clientId` and `clientSecret` registered in the upstream realm.
|
|
218
|
+
*/
|
|
219
|
+
export async function createOidcIdentityProvider(token, realm, { alias, authorizationUrl, clientId, clientSecret, displayName, jwksUrl, tokenUrl, userInfoUrl, }) {
|
|
220
|
+
await requestKc(`/admin/realms/${realm}/identity-provider/instances`, {
|
|
221
|
+
body: JSON.stringify({
|
|
222
|
+
alias,
|
|
223
|
+
displayName,
|
|
224
|
+
enabled: true,
|
|
225
|
+
providerId: 'oidc',
|
|
226
|
+
storeToken: false,
|
|
227
|
+
trustEmail: true,
|
|
228
|
+
config: {
|
|
229
|
+
authorizationUrl,
|
|
230
|
+
clientAuthMethod: 'client_secret_post',
|
|
231
|
+
clientId,
|
|
232
|
+
clientSecret,
|
|
233
|
+
defaultScope: 'openid email profile',
|
|
234
|
+
jwksUrl,
|
|
235
|
+
syncMode: 'FORCE',
|
|
236
|
+
tokenUrl,
|
|
237
|
+
useJwksUrl: 'true',
|
|
238
|
+
userInfoUrl,
|
|
239
|
+
validateSignature: 'true',
|
|
240
|
+
},
|
|
241
|
+
}),
|
|
242
|
+
expectedStatuses: [201],
|
|
243
|
+
method: 'POST',
|
|
244
|
+
token,
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Add a mapper that imports the upstream `groups` claim into a `groups` attribute on brokered
|
|
249
|
+
* users, so a downstream client in the broker realm can re-emit it as a `groups` claim.
|
|
250
|
+
*
|
|
251
|
+
* @param token A Keycloak admin bearer token.
|
|
252
|
+
* @param realm The broker realm name.
|
|
253
|
+
* @param alias The identity provider alias.
|
|
254
|
+
*/
|
|
255
|
+
export async function createIdpGroupMapper(token, realm, alias) {
|
|
256
|
+
await requestKc(`/admin/realms/${realm}/identity-provider/instances/${alias}/mappers`, {
|
|
257
|
+
body: JSON.stringify({
|
|
258
|
+
config: {
|
|
259
|
+
claim: 'groups',
|
|
260
|
+
syncMode: 'FORCE',
|
|
261
|
+
'user.attribute': 'groups',
|
|
262
|
+
},
|
|
263
|
+
identityProviderAlias: alias,
|
|
264
|
+
identityProviderMapper: 'oidc-user-attribute-idp-mapper',
|
|
265
|
+
name: 'groups',
|
|
266
|
+
}),
|
|
267
|
+
expectedStatuses: [201],
|
|
268
|
+
method: 'POST',
|
|
269
|
+
token,
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
//# sourceMappingURL=keycloak-admin.js.map
|