Skip to content

Commit 8500d57

Browse files
committed
test(playwright): add group and subadmin provisioning helpers
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent 919630a commit 8500d57

1 file changed

Lines changed: 104 additions & 4 deletions

File tree

playwright/support/nc-provisioning.ts

Lines changed: 104 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,21 @@ type OcsResponse<T = unknown> = {
1818
}
1919
}
2020

21+
function toStringList(data: unknown): string[] {
22+
if (Array.isArray(data)) {
23+
return data.filter((item): item is string => typeof item === 'string')
24+
}
25+
26+
if (data && typeof data === 'object') {
27+
const nested = data as { groups?: unknown[] }
28+
if (Array.isArray(nested.groups)) {
29+
return nested.groups.filter((item): item is string => typeof item === 'string')
30+
}
31+
}
32+
33+
return []
34+
}
35+
2136
async function ocsRequest(
2237
request: APIRequestContext,
2338
method: 'GET' | 'POST' | 'PUT' | 'DELETE',
@@ -45,10 +60,6 @@ async function ocsRequest(
4560
failOnStatusCode: false,
4661
})
4762

48-
if (!response.ok() && response.status() !== 404) {
49-
throw new Error(`OCS request failed: ${method} ${path}${response.status()} ${await response.text()}`)
50-
}
51-
5263
const text = await response.text()
5364
if (!text) {
5465
return { ocs: { meta: { status: 'ok', statuscode: response.status(), message: '' }, data: {} } } as OcsResponse
@@ -92,6 +103,95 @@ export async function deleteUser(
92103
await ocsRequest(request, 'DELETE', `/cloud/users/${userId}`)
93104
}
94105

106+
// ---------------------------------------------------------------------------
107+
// Groups and delegated administration
108+
// ---------------------------------------------------------------------------
109+
110+
/**
111+
* Creates a group if it does not exist.
112+
*/
113+
export async function ensureGroupExists(
114+
request: APIRequestContext,
115+
groupId: string,
116+
): Promise<void> {
117+
const check = await ocsRequest(request, 'GET', `/cloud/groups?search=${encodeURIComponent(groupId)}`)
118+
const groups = toStringList(check.ocs.data)
119+
if (groups.includes(groupId)) {
120+
return
121+
}
122+
123+
const create = await ocsRequest(request, 'POST', '/cloud/groups', undefined, undefined, {
124+
groupid: groupId,
125+
})
126+
if (create.ocs.meta.statuscode !== 200 && create.ocs.meta.statuscode !== 102) {
127+
throw new Error(`Failed to create group "${groupId}": ${create.ocs.meta.message}`)
128+
}
129+
}
130+
131+
/**
132+
* Adds a user to a group.
133+
*/
134+
export async function ensureUserInGroup(
135+
request: APIRequestContext,
136+
userId: string,
137+
groupId: string,
138+
): Promise<void> {
139+
const groupsResponse = await ocsRequest(request, 'GET', `/cloud/users/${encodeURIComponent(userId)}/groups`)
140+
const groups = toStringList(groupsResponse.ocs.data)
141+
if (groups.includes(groupId)) {
142+
return
143+
}
144+
145+
const add = await ocsRequest(
146+
request,
147+
'POST',
148+
`/cloud/users/${encodeURIComponent(userId)}/groups`,
149+
undefined,
150+
undefined,
151+
{ groupid: groupId },
152+
)
153+
if (add.ocs.meta.statuscode !== 200) {
154+
throw new Error(`Failed to add user "${userId}" to group "${groupId}": ${add.ocs.meta.message}`)
155+
}
156+
157+
const verify = await ocsRequest(request, 'GET', `/cloud/users/${encodeURIComponent(userId)}/groups`)
158+
if (!toStringList(verify.ocs.data).includes(groupId)) {
159+
throw new Error(`User "${userId}" is not in group "${groupId}" after assignment.`)
160+
}
161+
}
162+
163+
/**
164+
* Grants subadmin rights for a specific group.
165+
*/
166+
export async function ensureSubadminOfGroup(
167+
request: APIRequestContext,
168+
userId: string,
169+
groupId: string,
170+
): Promise<void> {
171+
const subadmins = await ocsRequest(request, 'GET', `/cloud/users/${encodeURIComponent(userId)}/subadmins`)
172+
const groups = toStringList(subadmins.ocs.data)
173+
if (groups.includes(groupId)) {
174+
return
175+
}
176+
177+
const grant = await ocsRequest(
178+
request,
179+
'POST',
180+
`/cloud/users/${encodeURIComponent(userId)}/subadmins`,
181+
undefined,
182+
undefined,
183+
{ groupid: groupId },
184+
)
185+
if (grant.ocs.meta.statuscode !== 200) {
186+
throw new Error(`Failed to grant subadmin for user "${userId}" in group "${groupId}": ${grant.ocs.meta.message}`)
187+
}
188+
189+
const verify = await ocsRequest(request, 'GET', `/cloud/users/${encodeURIComponent(userId)}/subadmins`)
190+
if (!toStringList(verify.ocs.data).includes(groupId)) {
191+
throw new Error(`User "${userId}" was not granted subadmin rights for group "${groupId}".`)
192+
}
193+
}
194+
95195
// ---------------------------------------------------------------------------
96196
// App config (equivalent to `occ config:app:set`)
97197
// ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)