1
0

personal-info.cy.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. })
  97. cy.wait(500)
  98. cy.backupDB().then(($snapshot) => {
  99. snapshot = $snapshot
  100. })
  101. })
  102. after(() => {
  103. cy.runOccCommand('config:system:set force_language --value en')
  104. cy.runOccCommand('config:system:set force_locale --value en_US')
  105. })
  106. beforeEach(() => {
  107. cy.login(user)
  108. cy.visit('/settings/user')
  109. cy.intercept('PUT', /ocs\/v2.php\/cloud\/users\//).as('submitSetting')
  110. })
  111. afterEach(() => {
  112. cy.restoreDB(snapshot)
  113. })
  114. it('Can dis- and enable the profile', () => {
  115. cy.visit(`/u/${user.userId}`)
  116. cy.contains('h2', user.userId).should('be.visible')
  117. cy.visit('/settings/user')
  118. cy.contains('Enable profile').click()
  119. handlePasswordConfirmation(user.password)
  120. cy.wait('@submitSetting')
  121. cy.visit(`/u/${user.userId}`, { failOnStatusCode: false })
  122. cy.contains('h2', 'Profile not found').should('be.visible')
  123. cy.visit('/settings/user')
  124. cy.contains('Enable profile').click()
  125. handlePasswordConfirmation(user.password)
  126. cy.wait('@submitSetting')
  127. cy.visit(`/u/${user.userId}`, { failOnStatusCode: false })
  128. cy.contains('h2', user.userId).should('be.visible')
  129. })
  130. it('Can change language', () => {
  131. cy.intercept('GET', /settings\/user/).as('reload')
  132. inputForLabel('Language').scrollIntoView()
  133. inputForLabel('Language').type('Ned')
  134. cy.contains('li[role="option"]', 'Nederlands')
  135. .click()
  136. cy.wait('@reload')
  137. // expect language changed
  138. inputForLabel('Taal').scrollIntoView()
  139. cy.contains('section', 'Help met vertalen')
  140. })
  141. it('Can change locale', () => {
  142. cy.intercept('GET', /settings\/user/).as('reload')
  143. cy.clock(new Date(2024, 0, 10))
  144. // Default is US
  145. cy.contains('section', '01/10/2024')
  146. inputForLabel('Locale').scrollIntoView()
  147. inputForLabel('Locale').type('German')
  148. cy.contains('li[role="option"]', 'German (Germany')
  149. .click()
  150. cy.wait('@reload')
  151. // expect locale changed
  152. inputForLabel('Locale').scrollIntoView()
  153. cy.contains('section', '10.01.2024')
  154. })
  155. it('Can set primary email and change its visibility', () => {
  156. cy.contains('label', 'Email').scrollIntoView()
  157. // Check invalid input
  158. inputForLabel('Email').type('foo bar')
  159. inputForLabel('Email').then(($el) => expect(($el.get(0) as HTMLInputElement).checkValidity()).to.be.false)
  160. // handle valid input
  161. inputForLabel('Email').type('{selectAll}hello@example.com')
  162. handlePasswordConfirmation(user.password)
  163. cy.wait('@submitSetting')
  164. cy.reload()
  165. inputForLabel('Email').should('have.value', 'hello@example.com')
  166. checkSettingsVisibility(
  167. 'Email',
  168. Visibility.Federated,
  169. // It is not possible to set it as private
  170. ALL_VISIBILITIES.filter((v) => v !== Visibility.Private),
  171. )
  172. // check it is visible on the profile
  173. cy.visit(`/u/${user.userId}`)
  174. cy.contains('a', 'hello@example.com').should('be.visible').and('have.attr', 'href', 'mailto:hello@example.com')
  175. })
  176. it('Can delete primary email', () => {
  177. cy.contains('label', 'Email').scrollIntoView()
  178. inputForLabel('Email').type('{selectAll}hello@example.com')
  179. handlePasswordConfirmation(user.password)
  180. cy.wait('@submitSetting')
  181. // check after reload
  182. cy.reload()
  183. inputForLabel('Email').should('have.value', 'hello@example.com')
  184. // delete email
  185. cy.get('button[aria-label="Remove primary email"]').click({ force: true })
  186. cy.wait('@submitSetting')
  187. // check after reload
  188. cy.reload()
  189. inputForLabel('Email').should('have.value', '')
  190. })
  191. it('Can set and delete additional emails', () => {
  192. cy.get('button[aria-label="Add additional email"]').should('be.disabled')
  193. // we need a primary email first
  194. cy.contains('label', 'Email').scrollIntoView()
  195. inputForLabel('Email').type('{selectAll}primary@example.com')
  196. handlePasswordConfirmation(user.password)
  197. cy.wait('@submitSetting')
  198. // add new email
  199. cy.get('button[aria-label="Add additional email"]')
  200. .click()
  201. // without any value we should not be able to add a second additional
  202. cy.get('button[aria-label="Add additional email"]').should('be.disabled')
  203. // fill the first additional
  204. inputForLabel('Additional email address 1')
  205. .type('1@example.com')
  206. handlePasswordConfirmation(user.password)
  207. cy.wait('@submitSetting')
  208. // add second additional email
  209. cy.get('button[aria-label="Add additional email"]')
  210. .click()
  211. // fill the second additional
  212. inputForLabel('Additional email address 2')
  213. .type('2@example.com')
  214. handlePasswordConfirmation(user.password)
  215. cy.wait('@submitSetting')
  216. // check the content is saved
  217. cy.reload()
  218. inputForLabel('Additional email address 1')
  219. .should('have.value', '1@example.com')
  220. inputForLabel('Additional email address 2')
  221. .should('have.value', '2@example.com')
  222. // delete the first
  223. cy.get('button[aria-label="Options for additional email address 1"]')
  224. .click({ force: true })
  225. cy.contains('button[role="menuitem"]', 'Delete email')
  226. .click({ force: true })
  227. handlePasswordConfirmation(user.password)
  228. cy.reload()
  229. inputForLabel('Additional email address 1')
  230. .should('have.value', '2@example.com')
  231. })
  232. it('Can set Full name and change its visibility', () => {
  233. cy.contains('label', 'Full name').scrollIntoView()
  234. // handle valid input
  235. inputForLabel('Full name').type('{selectAll}Jane Doe')
  236. handlePasswordConfirmation(user.password)
  237. cy.wait('@submitSetting')
  238. cy.reload()
  239. inputForLabel('Full name').should('have.value', 'Jane Doe')
  240. checkSettingsVisibility(
  241. 'Full name',
  242. Visibility.Federated,
  243. // It is not possible to set it as private
  244. ALL_VISIBILITIES.filter((v) => v !== Visibility.Private),
  245. )
  246. // check it is visible on the profile
  247. cy.visit(`/u/${user.userId}`)
  248. cy.contains('h2', 'Jane Doe').should('be.visible')
  249. })
  250. it('Can set Phone number and its visibility', () => {
  251. cy.contains('label', 'Phone number').scrollIntoView()
  252. // Check invalid input
  253. inputForLabel('Phone number').type('foo bar')
  254. inputForLabel('Phone number').should('have.attr', 'class').and('contain', '--error')
  255. // handle valid input
  256. inputForLabel('Phone number').type('{selectAll}+49 89 721010 99701')
  257. inputForLabel('Phone number').should('have.attr', 'class').and('not.contain', '--error')
  258. handlePasswordConfirmation(user.password)
  259. cy.wait('@submitSetting')
  260. cy.reload()
  261. inputForLabel('Phone number').should('have.value', '+498972101099701')
  262. checkSettingsVisibility('Phone number')
  263. // check it is visible on the profile
  264. cy.visit(`/u/${user.userId}`)
  265. cy.get('a[href="tel:+498972101099701"]').should('be.visible')
  266. })
  267. it('Can set Website and change its visibility', () => {
  268. cy.contains('label', 'Website').scrollIntoView()
  269. // Check invalid input
  270. inputForLabel('Website').type('foo bar')
  271. inputForLabel('Website').then(($el) => expect(($el.get(0) as HTMLInputElement).checkValidity()).to.be.false)
  272. // handle valid input
  273. inputForLabel('Website').type('{selectAll}http://example.com')
  274. handlePasswordConfirmation(user.password)
  275. cy.wait('@submitSetting')
  276. cy.reload()
  277. inputForLabel('Website').should('have.value', 'http://example.com')
  278. checkSettingsVisibility('Website')
  279. // check it is visible on the profile
  280. cy.visit(`/u/${user.userId}`)
  281. cy.contains('http://example.com').should('be.visible')
  282. })
  283. // Check generic properties that allow any visibility and any value
  284. genericProperties.forEach((property) => {
  285. it(`Can set ${property} and change its visibility`, () => {
  286. const uniqueValue = `${property.toUpperCase()} ${property.toLowerCase()}`
  287. cy.contains('label', property).scrollIntoView()
  288. inputForLabel(property).type(uniqueValue)
  289. handlePasswordConfirmation(user.password)
  290. cy.wait('@submitSetting')
  291. cy.reload()
  292. inputForLabel(property).should('have.value', uniqueValue)
  293. checkSettingsVisibility(property)
  294. // check it is visible on the profile
  295. cy.visit(`/u/${user.userId}`)
  296. cy.contains(uniqueValue).should('be.visible')
  297. })
  298. })
  299. // Check non federated properties - those where we need special configuration and only support local visibility
  300. nonfederatedProperties.forEach((property) => {
  301. it(`Can set ${property} and change its visibility`, () => {
  302. const uniqueValue = `${property.toUpperCase()} ${property.toLowerCase()}`
  303. cy.contains('label', property).scrollIntoView()
  304. inputForLabel(property).type(uniqueValue)
  305. handlePasswordConfirmation(user.password)
  306. cy.wait('@submitSetting')
  307. cy.reload()
  308. inputForLabel(property).should('have.value', uniqueValue)
  309. checkSettingsVisibility(property, Visibility.Local, [Visibility.Private, Visibility.Local])
  310. // check it is visible on the profile
  311. cy.visit(`/u/${user.userId}`)
  312. cy.contains(uniqueValue).should('be.visible')
  313. })
  314. })
  315. })