personal-info.cy.ts 12 KB

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