aboutsummaryrefslogtreecommitdiffstats
path: root/cypress/e2e/settings/personal-info.cy.ts
blob: d64cadbe6142e78ae96dbb3faa807f018dbe9155 (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/**
 * @copyright Copyright (c) 2024 Ferdinand Thiessen <opensource@fthiessen.de>
 *
 * @author Ferdinand Thiessen <opensource@fthiessen.de>
 *
 * @license AGPL-3.0-or-later
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */

import type { User } from '@nextcloud/cypress'
import { handlePasswordConfirmation } from './usersUtils.ts'

let user: User

enum Visibility {
	Private = 'Private',
	Local = 'Local',
	Federated = 'Federated',
	Public = 'Published'
}

const ALL_VISIBILITIES = [Visibility.Public, Visibility.Private, Visibility.Local, Visibility.Federated]

/**
 * Get the input connected to a specific label
 * @param label The content of the label
 */
const inputForLabel = (label: string) => cy.contains('label', label).then((el) => cy.get(`#${el.attr('for')}`))

/**
 * Get the property visibility button
 * @param property The property to which to look for the button
 */
const getVisibilityButton = (property: string) => cy.get(`button[aria-label*="Change scope level of ${property.toLowerCase()}"`)

/**
 * Validate a specifiy visibility is set for a property
 * @param property The property
 * @param active The active visibility
 */
const validateActiveVisibility = (property: string, active: Visibility) => {
	getVisibilityButton(property)
		.should('have.attr', 'aria-label')
		.and('match', new RegExp(`current scope is ${active}`, 'i'))
	getVisibilityButton(property)
		.click()
	cy.get('ul[role="dialog"')
		.contains('button', active)
		.should('have.attr', 'aria-pressed', 'true')

	// close menu
	getVisibilityButton(property)
		.click()
}

/**
 * Set a specific visibility for a property
 * @param property The property
 * @param active The visibility to set
 */
const setActiveVisibility = (property: string, active: Visibility) => {
	getVisibilityButton(property)
		.click()
	cy.get('ul[role="dialog"')
		.contains('button', active)
		.click({ force: true })
	handlePasswordConfirmation(user.password)

	cy.wait('@submitSetting')
}

/**
 * Helper to check that setting all visibilities on a property is possible
 * @param property The property to test
 * @param defaultVisibility The default visibility of that property
 * @param allowedVisibility Visibility that is allowed and need to be checked
 */
const checkSettingsVisibility = (property: string, defaultVisibility: Visibility = Visibility.Local, allowedVisibility: Visibility[] = ALL_VISIBILITIES) => {
	getVisibilityButton(property)
		.scrollIntoView()

	validateActiveVisibility(property, defaultVisibility)

	allowedVisibility.forEach((active) => {
		setActiveVisibility(property, active)

		cy.reload()
		getVisibilityButton(property).scrollIntoView()

		validateActiveVisibility(property, active)
	})

	// TODO: Fix this in vue library then enable this test again
	/* // Test that not allowed options are disabled
	ALL_VISIBILITIES.filter((v) => !allowedVisibility.includes(v)).forEach((disabled) => {
		getVisibilityButton(property)
			.click()
		cy.get('ul[role="dialog"')
			.contains('button', disabled)
			.should('exist')
			.and('have.attr', 'disabled', 'true')
	}) */
}

const genericProperties = ['Location', 'X (formerly Twitter)', 'Fediverse']
const nonfederatedProperties = ['Organisation', 'Role', 'Headline', 'About']

describe('Settings: Change personal information', { testIsolation: true }, () => {

	before(() => {
		// ensure we can set locale and language
		cy.runOccCommand('config:system:delete force_language')
		cy.runOccCommand('config:system:delete force_locale')
	})

	after(() => {
		cy.runOccCommand('config:system:set force_language --value en')
		cy.runOccCommand('config:system:set force_locale --value en_US')
	})

	beforeEach(() => {
		cy.createRandomUser().then(($user) => {
			user = $user
			cy.modifyUser(user, 'language', 'en')
			cy.modifyUser(user, 'locale', 'en_US')
			cy.login($user)
			cy.visit('/settings/user')
		})
		cy.intercept('PUT', /ocs\/v2.php\/cloud\/users\//).as('submitSetting')
	})

	it('Can dis- and enable the profile', () => {
		cy.visit(`/u/${user.userId}`)
		cy.contains('h2', user.userId).should('be.visible')

		cy.visit('/settings/user')
		cy.contains('Enable profile').click()
		handlePasswordConfirmation(user.password)

		cy.visit(`/u/${user.userId}`, { failOnStatusCode: false })
		cy.contains('h2', 'Profile not found').should('be.visible')

		cy.visit('/settings/user')
		cy.contains('Enable profile').click()
		handlePasswordConfirmation(user.password)

		cy.visit(`/u/${user.userId}`, { failOnStatusCode: false })
		cy.contains('h2', user.userId).should('be.visible')
	})

	it('Can change language', () => {
		cy.intercept('GET', /settings\/user/).as('reload')
		inputForLabel('Language').scrollIntoView()
		inputForLabel('Language').type('Ned')
		cy.contains('li[role="option"]', 'Nederlands')
			.click()
		cy.wait('@reload')

		// expect language changed
		inputForLabel('Taal').scrollIntoView()
		cy.contains('section', 'Help met vertalen')
	})

	it('Can change locale', () => {
		cy.intercept('GET', /settings\/user/).as('reload')
		cy.clock(new Date(2024, 0, 10))

		// Default is US
		cy.contains('section', '01/10/2024')

		inputForLabel('Locale').scrollIntoView()
		inputForLabel('Locale').type('German')
		cy.contains('li[role="option"]', 'German (Germany')
			.click()
		cy.wait('@reload')

		// expect locale changed
		inputForLabel('Locale').scrollIntoView()
		cy.contains('section', '10.01.2024')
	})

	it('Can set primary email and change its visibility', () => {
		cy.contains('label', 'Email').scrollIntoView()
		// Check invalid input
		inputForLabel('Email').type('foo bar')
		inputForLabel('Email').then(($el) => expect(($el.get(0) as HTMLInputElement).checkValidity()).to.be.false)
		// handle valid input
		inputForLabel('Email').type('{selectAll}hello@example.com')
		handlePasswordConfirmation(user.password)

		cy.wait('@submitSetting')
		cy.reload()
		inputForLabel('Email').should('have.value', 'hello@example.com')

		checkSettingsVisibility(
			'Email',
			Visibility.Federated,
			// It is not possible to set it as private
			ALL_VISIBILITIES.filter((v) => v !== Visibility.Private),
		)

		// check it is visible on the profile
		cy.visit(`/u/${user.userId}`)
		cy.contains('a', 'hello@example.com').should('be.visible').and('have.attr', 'href', 'mailto:hello@example.com')
	})

	it('Can delete primary email', () => {
		cy.contains('label', 'Email').scrollIntoView()
		inputForLabel('Email').type('{selectAll}hello@example.com')
		handlePasswordConfirmation(user.password)
		cy.wait('@submitSetting')

		// check after reload
		cy.reload()
		inputForLabel('Email').should('have.value', 'hello@example.com')

		// delete email
		cy.get('button[aria-label="Remove primary email"]').click({ force: true })
		cy.wait('@submitSetting')

		// check after reload
		cy.reload()
		inputForLabel('Email').should('have.value', '')
	})

	it('Can set and delete additional emails', () => {
		cy.get('button[aria-label="Add additional email"]').should('be.disabled')
		// we need a primary email first
		cy.contains('label', 'Email').scrollIntoView()
		inputForLabel('Email').type('{selectAll}primary@example.com')
		handlePasswordConfirmation(user.password)
		cy.wait('@submitSetting')

		// add new email
		cy.get('button[aria-label="Add additional email"]')
			.click()

		// without any value we should not be able to add a second additional
		cy.get('button[aria-label="Add additional email"]').should('be.disabled')

		// fill the first additional
		inputForLabel('Additional email address 1')
			.type('1@example.com')
		handlePasswordConfirmation(user.password)
		cy.wait('@submitSetting')

		// add second additional email
		cy.get('button[aria-label="Add additional email"]')
			.click()

		// fill the second additional
		inputForLabel('Additional email address 2')
			.type('2@example.com')
		handlePasswordConfirmation(user.password)
		cy.wait('@submitSetting')

		// check the content is saved
		cy.reload()
		inputForLabel('Additional email address 1')
			.should('have.value', '1@example.com')
		inputForLabel('Additional email address 2')
			.should('have.value', '2@example.com')

		// delete the first
		cy.get('button[aria-label="Options for additional email address 1"]')
			.click({ force: true })
		cy.contains('button[role="menuitem"]', 'Delete email')
			.click({ force: true })
		handlePasswordConfirmation(user.password)

		cy.reload()
		inputForLabel('Additional email address 1')
			.should('have.value', '2@example.com')
	})

	it('Can set Full name and change its visibility', () => {
		cy.contains('label', 'Full name').scrollIntoView()
		// handle valid input
		inputForLabel('Full name').type('{selectAll}Jane Doe')
		handlePasswordConfirmation(user.password)

		cy.wait('@submitSetting')
		cy.reload()
		inputForLabel('Full name').should('have.value', 'Jane Doe')

		checkSettingsVisibility(
			'Full name',
			Visibility.Federated,
			// It is not possible to set it as private
			ALL_VISIBILITIES.filter((v) => v !== Visibility.Private),
		)

		// check it is visible on the profile
		cy.visit(`/u/${user.userId}`)
		cy.contains('h2', 'Jane Doe').should('be.visible')
	})

	it('Can set Phone number and its visibility', () => {
		cy.contains('label', 'Phone number').scrollIntoView()
		// Check invalid input
		inputForLabel('Phone number').type('foo bar')
		inputForLabel('Phone number').should('have.attr', 'class').and('contain', '--error')
		// handle valid input
		inputForLabel('Phone number').type('{selectAll}+49 89 721010 99701')
		inputForLabel('Phone number').should('have.attr', 'class').and('not.contain', '--error')
		handlePasswordConfirmation(user.password)

		cy.wait('@submitSetting')
		cy.reload()
		inputForLabel('Phone number').should('have.value', '+498972101099701')

		checkSettingsVisibility('Phone number')

		// check it is visible on the profile
		cy.visit(`/u/${user.userId}`)
		cy.get('a[href="tel:+498972101099701"]').should('be.visible')
	})

	it('Can set Website and change its visibility', () => {
		cy.contains('label', 'Website').scrollIntoView()
		// Check invalid input
		inputForLabel('Website').type('foo bar')
		inputForLabel('Website').then(($el) => expect(($el.get(0) as HTMLInputElement).checkValidity()).to.be.false)
		// handle valid input
		inputForLabel('Website').type('{selectAll}http://example.com')
		handlePasswordConfirmation(user.password)

		cy.wait('@submitSetting')
		cy.reload()
		inputForLabel('Website').should('have.value', 'http://example.com')

		checkSettingsVisibility('Website')

		// check it is visible on the profile
		cy.visit(`/u/${user.userId}`)
		cy.contains('http://example.com').should('be.visible')
	})

	// Check generic properties that allow any visibility and any value
	genericProperties.forEach((property) => {
		it(`Can set ${property} and change its visibility`, () => {
			const uniqueValue = `${property.toUpperCase()} ${property.toLowerCase()}`
			cy.contains('label', property).scrollIntoView()
			inputForLabel(property).type(uniqueValue)
			handlePasswordConfirmation(user.password)

			cy.wait('@submitSetting')
			cy.reload()
			inputForLabel(property).should('have.value', uniqueValue)

			checkSettingsVisibility(property)

			// check it is visible on the profile
			cy.visit(`/u/${user.userId}`)
			cy.contains(uniqueValue).should('be.visible')
		})
	})

	// Check non federated properties - those where we need special configuration and only support local visibility
	nonfederatedProperties.forEach((property) => {
		it(`Can set ${property} and change its visibility`, () => {
			const uniqueValue = `${property.toUpperCase()} ${property.toLowerCase()}`
			cy.contains('label', property).scrollIntoView()
			inputForLabel(property).type(uniqueValue)
			handlePasswordConfirmation(user.password)

			cy.wait('@submitSetting')
			cy.reload()
			inputForLabel(property).should('have.value', uniqueValue)

			checkSettingsVisibility(property, Visibility.Local, [Visibility.Private, Visibility.Local])

			// check it is visible on the profile
			cy.visit(`/u/${user.userId}`)
			cy.contains(uniqueValue).should('be.visible')
		})
	})
})