personal-info.cy.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /**
  2. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-License-Identifier: AGPL-3.0-or-later
  4. */
  5. import type { User } from '@nextcloud/cypress'
  6. import { handlePasswordConfirmation } from './usersUtils.ts'
  7. let user: User
  8. enum Visibility {
  9. Private = 'Private',
  10. Local = 'Local',
  11. Federated = 'Federated',
  12. Public = 'Published'
  13. }
  14. const ALL_VISIBILITIES = [Visibility.Public, Visibility.Private, Visibility.Local, Visibility.Federated]
  15. /**
  16. * Get the input connected to a specific label
  17. * @param label The content of the label
  18. */
  19. const inputForLabel = (label: string) => cy.contains('label', label).then((el) => cy.get(`#${el.attr('for')}`))
  20. /**
  21. * Get the property visibility button
  22. * @param property The property to which to look for the button
  23. */
  24. const getVisibilityButton = (property: string) => cy.get(`button[aria-label*="Change scope level of ${property.toLowerCase()}"`)
  25. /**
  26. * Validate a specifiy visibility is set for a property
  27. * @param property The property
  28. * @param active The active visibility
  29. */
  30. const validateActiveVisibility = (property: string, active: Visibility) => {
  31. getVisibilityButton(property)
  32. .should('have.attr', 'aria-label')
  33. .and('match', new RegExp(`current scope is ${active}`, 'i'))
  34. getVisibilityButton(property)
  35. .click()
  36. cy.get('ul[role="menu"]')
  37. .contains('button', active)
  38. .should('have.attr', 'aria-checked', 'true')
  39. // close menu
  40. getVisibilityButton(property)
  41. .click()
  42. }
  43. /**
  44. * Set a specific visibility for a property
  45. * @param property The property
  46. * @param active The visibility to set
  47. */
  48. const setActiveVisibility = (property: string, active: Visibility) => {
  49. getVisibilityButton(property)
  50. .click()
  51. cy.get('ul[role="menu"]')
  52. .contains('button', active)
  53. .click({ force: true })
  54. handlePasswordConfirmation(user.password)
  55. cy.wait('@submitSetting')
  56. }
  57. /**
  58. * Helper to check that setting all visibilities on a property is possible
  59. * @param property The property to test
  60. * @param defaultVisibility The default visibility of that property
  61. * @param allowedVisibility Visibility that is allowed and need to be checked
  62. */
  63. const checkSettingsVisibility = (property: string, defaultVisibility: Visibility = Visibility.Local, allowedVisibility: Visibility[] = ALL_VISIBILITIES) => {
  64. getVisibilityButton(property)
  65. .scrollIntoView()
  66. validateActiveVisibility(property, defaultVisibility)
  67. allowedVisibility.forEach((active) => {
  68. setActiveVisibility(property, active)
  69. cy.reload()
  70. getVisibilityButton(property).scrollIntoView()
  71. validateActiveVisibility(property, active)
  72. })
  73. // TODO: Fix this in vue library then enable this test again
  74. /* // Test that not allowed options are disabled
  75. ALL_VISIBILITIES.filter((v) => !allowedVisibility.includes(v)).forEach((disabled) => {
  76. getVisibilityButton(property)
  77. .click()
  78. cy.get('ul[role="dialog"')
  79. .contains('button', disabled)
  80. .should('exist')
  81. .and('have.attr', 'disabled', 'true')
  82. }) */
  83. }
  84. const genericProperties = ['Location', 'X (formerly Twitter)', 'Fediverse']
  85. const nonfederatedProperties = ['Organisation', 'Role', 'Headline', 'About']
  86. describe('Settings: Change personal information', { testIsolation: true }, () => {
  87. let snapshot: string = ''
  88. before(() => {
  89. // ensure we can set locale and language
  90. cy.runOccCommand('config:system:delete force_language')
  91. cy.runOccCommand('config:system:delete force_locale')
  92. cy.createRandomUser().then(($user) => {
  93. user = $user
  94. cy.modifyUser(user, 'language', 'en')
  95. cy.modifyUser(user, 'locale', 'en_US')
  96. // Make sure the user is logged in at least once
  97. // before the snapshot is taken to speed up the tests
  98. cy.login(user)
  99. cy.visit('/settings/user')
  100. cy.backupDB().then(($snapshot) => {
  101. snapshot = $snapshot
  102. })
  103. })
  104. })
  105. after(() => {
  106. cy.runOccCommand('config:system:set force_language --value en')
  107. cy.runOccCommand('config:system:set force_locale --value en_US')
  108. })
  109. beforeEach(() => {
  110. cy.login(user)
  111. cy.visit('/settings/user')
  112. cy.intercept('PUT', /ocs\/v2.php\/cloud\/users\//).as('submitSetting')
  113. })
  114. afterEach(() => {
  115. cy.restoreDB(snapshot)
  116. })
  117. it('Can dis- and enable the profile', () => {
  118. cy.visit(`/u/${user.userId}`)
  119. cy.contains('h2', user.userId).should('be.visible')
  120. cy.visit('/settings/user')
  121. cy.contains('Enable profile').click()
  122. handlePasswordConfirmation(user.password)
  123. cy.wait('@submitSetting')
  124. cy.visit(`/u/${user.userId}`, { failOnStatusCode: false })
  125. cy.contains('h2', 'Profile not found').should('be.visible')
  126. cy.visit('/settings/user')
  127. cy.contains('Enable profile').click()
  128. handlePasswordConfirmation(user.password)
  129. cy.wait('@submitSetting')
  130. cy.visit(`/u/${user.userId}`, { failOnStatusCode: false })
  131. cy.contains('h2', user.userId).should('be.visible')
  132. })
  133. it('Can change language', () => {
  134. cy.intercept('GET', /settings\/user/).as('reload')
  135. inputForLabel('Language').scrollIntoView()
  136. inputForLabel('Language').type('Ned')
  137. cy.contains('li[role="option"]', 'Nederlands')
  138. .click()
  139. cy.wait('@reload')
  140. // expect language changed
  141. inputForLabel('Taal').scrollIntoView()
  142. cy.contains('section', 'Help met vertalen')
  143. })
  144. it('Can change locale', () => {
  145. cy.intercept('GET', /settings\/user/).as('reload')
  146. cy.clock(new Date(2024, 0, 10))
  147. // Default is US
  148. cy.contains('section', '01/10/2024')
  149. inputForLabel('Locale').scrollIntoView()
  150. inputForLabel('Locale').type('German')
  151. cy.contains('li[role="option"]', 'German (Germany')
  152. .click()
  153. cy.wait('@reload')
  154. // expect locale changed
  155. inputForLabel('Locale').scrollIntoView()
  156. cy.contains('section', '10.01.2024')
  157. })
  158. it('Can set primary email and change its visibility', () => {
  159. cy.contains('label', 'Email').scrollIntoView()
  160. // Check invalid input
  161. inputForLabel('Email').type('foo bar')
  162. inputForLabel('Email').then(($el) => expect(($el.get(0) as HTMLInputElement).checkValidity()).to.be.false)
  163. // handle valid input
  164. inputForLabel('Email').type('{selectAll}hello@example.com')
  165. handlePasswordConfirmation(user.password)
  166. cy.wait('@submitSetting')
  167. cy.reload()
  168. inputForLabel('Email').should('have.value', 'hello@example.com')
  169. checkSettingsVisibility(
  170. 'Email',
  171. Visibility.Federated,
  172. // It is not possible to set it as private
  173. ALL_VISIBILITIES.filter((v) => v !== Visibility.Private),
  174. )
  175. // check it is visible on the profile
  176. cy.visit(`/u/${user.userId}`)
  177. cy.contains('a', 'hello@example.com').should('be.visible').and('have.attr', 'href', 'mailto:hello@example.com')
  178. })
  179. it('Can delete primary email', () => {
  180. cy.contains('label', 'Email').scrollIntoView()
  181. inputForLabel('Email').type('{selectAll}hello@example.com')
  182. handlePasswordConfirmation(user.password)
  183. cy.wait('@submitSetting')
  184. // check after reload
  185. cy.reload()
  186. inputForLabel('Email').should('have.value', 'hello@example.com')
  187. // delete email
  188. cy.get('button[aria-label="Remove primary email"]').click({ force: true })
  189. cy.wait('@submitSetting')
  190. // check after reload
  191. cy.reload()
  192. inputForLabel('Email').should('have.value', '')
  193. })
  194. it('Can set and delete additional emails', () => {
  195. cy.get('button[aria-label="Add additional email"]').should('be.disabled')
  196. // we need a primary email first
  197. cy.contains('label', 'Email').scrollIntoView()
  198. inputForLabel('Email').type('{selectAll}primary@example.com')
  199. handlePasswordConfirmation(user.password)
  200. cy.wait('@submitSetting')
  201. // add new email
  202. cy.get('button[aria-label="Add additional email"]')
  203. .click()
  204. // without any value we should not be able to add a second additional
  205. cy.get('button[aria-label="Add additional email"]').should('be.disabled')
  206. // fill the first additional
  207. inputForLabel('Additional email address 1')
  208. .type('1@example.com')
  209. handlePasswordConfirmation(user.password)
  210. cy.wait('@submitSetting')
  211. // add second additional email
  212. cy.get('button[aria-label="Add additional email"]')
  213. .click()
  214. // fill the second additional
  215. inputForLabel('Additional email address 2')
  216. .type('2@example.com')
  217. handlePasswordConfirmation(user.password)
  218. cy.wait('@submitSetting')
  219. // check the content is saved
  220. cy.reload()
  221. inputForLabel('Additional email address 1')
  222. .should('have.value', '1@example.com')
  223. inputForLabel('Additional email address 2')
  224. .should('have.value', '2@example.com')
  225. // delete the first
  226. cy.get('button[aria-label="Options for additional email address 1"]')
  227. .click({ force: true })
  228. cy.contains('button[role="menuitem"]', 'Delete email')
  229. .click({ force: true })
  230. handlePasswordConfirmation(user.password)
  231. cy.reload()
  232. inputForLabel('Additional email address 1')
  233. .should('have.value', '2@example.com')
  234. })
  235. it('Can set Full name and change its visibility', () => {
  236. cy.contains('label', 'Full name').scrollIntoView()
  237. // handle valid input
  238. inputForLabel('Full name').type('{selectAll}Jane Doe')
  239. handlePasswordConfirmation(user.password)
  240. cy.wait('@submitSetting')
  241. cy.reload()
  242. inputForLabel('Full name').should('have.value', 'Jane Doe')
  243. checkSettingsVisibility(
  244. 'Full name',
  245. Visibility.Federated,
  246. // It is not possible to set it as private
  247. ALL_VISIBILITIES.filter((v) => v !== Visibility.Private),
  248. )
  249. // check it is visible on the profile
  250. cy.visit(`/u/${user.userId}`)
  251. cy.contains('h2', 'Jane Doe').should('be.visible')
  252. })
  253. it('Can set Phone number and its visibility', () => {
  254. cy.contains('label', 'Phone number').scrollIntoView()
  255. // Check invalid input
  256. inputForLabel('Phone number').type('foo bar')
  257. inputForLabel('Phone number').should('have.attr', 'class').and('contain', '--error')
  258. // handle valid input
  259. inputForLabel('Phone number').type('{selectAll}+49 89 721010 99701')
  260. inputForLabel('Phone number').should('have.attr', 'class').and('not.contain', '--error')
  261. handlePasswordConfirmation(user.password)
  262. cy.wait('@submitSetting')
  263. cy.reload()
  264. inputForLabel('Phone number').should('have.value', '+498972101099701')
  265. checkSettingsVisibility('Phone number')
  266. // check it is visible on the profile
  267. cy.visit(`/u/${user.userId}`)
  268. cy.get('a[href="tel:+498972101099701"]').should('be.visible')
  269. })
  270. it('Can set Website and change its visibility', () => {
  271. cy.contains('label', 'Website').scrollIntoView()
  272. // Check invalid input
  273. inputForLabel('Website').type('foo bar')
  274. inputForLabel('Website').then(($el) => expect(($el.get(0) as HTMLInputElement).checkValidity()).to.be.false)
  275. // handle valid input
  276. inputForLabel('Website').type('{selectAll}http://example.com')
  277. handlePasswordConfirmation(user.password)
  278. cy.wait('@submitSetting')
  279. cy.reload()
  280. inputForLabel('Website').should('have.value', 'http://example.com')
  281. checkSettingsVisibility('Website')
  282. // check it is visible on the profile
  283. cy.visit(`/u/${user.userId}`)
  284. cy.contains('http://example.com').should('be.visible')
  285. })
  286. // Check generic properties that allow any visibility and any value
  287. genericProperties.forEach((property) => {
  288. it(`Can set ${property} and change its visibility`, () => {
  289. const uniqueValue = `${property.toUpperCase()} ${property.toLowerCase()}`
  290. cy.contains('label', property).scrollIntoView()
  291. inputForLabel(property).type(uniqueValue)
  292. handlePasswordConfirmation(user.password)
  293. cy.wait('@submitSetting')
  294. cy.reload()
  295. inputForLabel(property).should('have.value', uniqueValue)
  296. checkSettingsVisibility(property)
  297. // check it is visible on the profile
  298. cy.visit(`/u/${user.userId}`)
  299. cy.contains(uniqueValue).should('be.visible')
  300. })
  301. })
  302. // Check non federated properties - those where we need special configuration and only support local visibility
  303. nonfederatedProperties.forEach((property) => {
  304. it(`Can set ${property} and change its visibility`, () => {
  305. const uniqueValue = `${property.toUpperCase()} ${property.toLowerCase()}`
  306. cy.contains('label', property).scrollIntoView()
  307. inputForLabel(property).type(uniqueValue)
  308. handlePasswordConfirmation(user.password)
  309. cy.wait('@submitSetting')
  310. cy.reload()
  311. inputForLabel(property).should('have.value', uniqueValue)
  312. checkSettingsVisibility(property, Visibility.Local, [Visibility.Private, Visibility.Local])
  313. // check it is visible on the profile
  314. cy.visit(`/u/${user.userId}`)
  315. cy.contains(uniqueValue).should('be.visible')
  316. })
  317. })
  318. })