aboutsummaryrefslogtreecommitdiffstats
path: root/cypress/e2e/settings/users-group-admin.cy.ts
blob: 5b5dcfd33a8e2741e3e6337097c0d90732cf3e4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/**
 * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */
/// <reference types="cypress-if" />
import { User } from '@nextcloud/cypress'
import { getUserListRow, handlePasswordConfirmation } from './usersUtils'
// eslint-disable-next-line n/no-extraneous-import
import randomString from 'crypto-random-string'

const admin = new User('admin', 'admin')
const john = new User('john', '123456')

/**
 * Make a user subadmin of a group.
 *
 * @param user - The user to make subadmin
 * @param group - The group the user should be subadmin of
 */
function makeSubAdmin(user: User, group: string): void {
	cy.request({
		url: `${Cypress.config('baseUrl')!.replace('/index.php', '')}/ocs/v2.php/cloud/users/${user.userId}/subadmins`,
		method: 'POST',
		auth: {
			user: admin.userId,
			password: admin.userId,
		},
		headers: {
			'OCS-ApiRequest': 'true',
		},
		body: {
			groupid: group,
		},
	})
}

describe('Settings: Create accounts as a group admin', function() {

	let subadmin: User
	let group: string

	beforeEach(() => {
		group = randomString(7)
		cy.deleteUser(john)
		cy.createRandomUser().then((user) => {
			subadmin = user
			cy.runOccCommand(`group:add '${group}'`)
			cy.runOccCommand(`group:adduser '${group}' '${subadmin.userId}'`)
			makeSubAdmin(subadmin, group)
		})
	})

	it('Can create a user with prefilled single group', () => {
		cy.login(subadmin)
		// open the User settings
		cy.visit('/settings/users')

		// open the New user modal
		cy.get('button#new-user-button').click()

		cy.get('form[data-test="form"]').within(() => {
			// see that the correct group is preselected
			cy.contains('[data-test="groups"] .vs__selected', group).should('be.visible')
			// see that the username is ""
			cy.get('input[data-test="username"]').should('exist').and('have.value', '')
			// set the username to john
			cy.get('input[data-test="username"]').type(john.userId)
			// see that the username is john
			cy.get('input[data-test="username"]').should('have.value', john.userId)
			// see that the password is ""
			cy.get('input[type="password"]').should('exist').and('have.value', '')
			// set the password to 123456
			cy.get('input[type="password"]').type(john.password)
			// see that the password is 123456
			cy.get('input[type="password"]').should('have.value', john.password)
		})

		cy.get('form[data-test="form"]').parents('[role="dialog"]').within(() => {
			// submit the new user form
			cy.get('button[type="submit"]').click({ force: true })
		})

		// Make sure no confirmation modal is shown
		handlePasswordConfirmation(admin.password)

		// see that the created user is in the list
		getUserListRow(john.userId)
			// see that the list of users contains the user john
			.contains(john.userId).should('exist')
	})

	it('Can create a new user when member of multiple groups', () => {
		const group2 = randomString(7)
		cy.runOccCommand(`group:add '${group2}'`)
		cy.runOccCommand(`group:adduser '${group2}' '${subadmin.userId}'`)
		makeSubAdmin(subadmin, group2)

		cy.login(subadmin)
		// open the User settings
		cy.visit('/settings/users')

		// open the New user modal
		cy.get('button#new-user-button').click()

		cy.get('form[data-test="form"]').within(() => {
			// see that no group is pre-selected
			cy.get('[data-test="groups"] .vs__selected').should('not.exist')
			// see both groups are available
			cy.findByRole('combobox', { name: /member of the following groups/i })
				.should('be.visible')
				.click()
			// can select both groups
			cy.document().its('body')
				.findByRole('listbox', { name: 'Options' })
				.should('be.visible')
				.as('options')
				.findAllByRole('option')
				.should('have.length', 2)
				.get('@options')
				.findByRole('option', { name: group })
				.should('be.visible')
				.get('@options')
				.findByRole('option', { name: group2 })
				.should('be.visible')
				.click()
			// see group is selected
			cy.contains('[data-test="groups"] .vs__selected', group2).should('be.visible')

			// see that the username is ""
			cy.get('input[data-test="username"]').should('exist').and('have.value', '')
			// set the username to john
			cy.get('input[data-test="username"]').type(john.userId)
			// see that the username is john
			cy.get('input[data-test="username"]').should('have.value', john.userId)
			// see that the password is ""
			cy.get('input[type="password"]').should('exist').and('have.value', '')
			// set the password to 123456
			cy.get('input[type="password"]').type(john.password)
			// see that the password is 123456
			cy.get('input[type="password"]').should('have.value', john.password)
		})

		cy.get('form[data-test="form"]').parents('[role="dialog"]').within(() => {
			// submit the new user form
			cy.get('button[type="submit"]').click({ force: true })
		})

		// Make sure no confirmation modal is shown
		handlePasswordConfirmation(admin.password)

		// see that the created user is in the list
		getUserListRow(john.userId)
			// see that the list of users contains the user john
			.contains(john.userId).should('exist')
	})

	it('Only sees groups they are subadmin of', () => {
		const group2 = randomString(7)
		cy.runOccCommand(`group:add '${group2}'`)
		cy.runOccCommand(`group:adduser '${group2}' '${subadmin.userId}'`)
		// not a subadmin!

		cy.login(subadmin)
		// open the User settings
		cy.visit('/settings/users')

		// open the New user modal
		cy.get('button#new-user-button').click()

		cy.get('form[data-test="form"]').within(() => {
			// see that the subadmin group is pre-selected
			cy.contains('[data-test="groups"] .vs__selected', group).should('be.visible')
			// see only the subadmin group is available
			cy.findByRole('combobox', { name: /member of the following groups/i })
				.should('be.visible')
				.click()
			// can select both groups
			cy.document().its('body')
				.findByRole('listbox', { name: 'Options' })
				.should('be.visible')
				.as('options')
				.findAllByRole('option')
				.should('have.length', 1)
		})
	})
})