login.cy.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-License-Identifier: AGPL-3.0-or-later
  4. */
  5. import type { User } from '@nextcloud/cypress'
  6. import { getNextcloudUserMenu, getNextcloudUserMenuToggle } from '../../support/commonUtils'
  7. describe('Login', () => {
  8. let user: User
  9. let disabledUser: User
  10. after(() => cy.deleteUser(user))
  11. before(() => {
  12. // disable brute force protection
  13. cy.runOccCommand('config:system:set auth.bruteforce.protection.enabled --value false --type bool')
  14. cy.createRandomUser().then(($user) => {
  15. user = $user
  16. })
  17. cy.createRandomUser().then(($user) => {
  18. disabledUser = $user
  19. cy.runOccCommand(`user:disable '${disabledUser.userId}'`)
  20. })
  21. })
  22. beforeEach(() => {
  23. cy.logout()
  24. })
  25. it('log in with valid account and password', () => {
  26. // Given I visit the Home page
  27. cy.visit('/')
  28. // I see the login page
  29. cy.get('form[name="login"]').should('be.visible')
  30. // I log in with a valid account
  31. cy.get('form[name="login"]').within(() => {
  32. cy.get('input[name="user"]').type(user.userId)
  33. cy.get('input[name="password"]').type(user.password)
  34. cy.contains('button[data-login-form-submit]', 'Log in').click()
  35. })
  36. // see that the login is done
  37. cy.get('[data-login-form-submit]').if().should('not.contain', 'Logging in')
  38. // Then I see that the current page is the Files app
  39. cy.url().should('match', /apps\/dashboard(\/|$)/)
  40. })
  41. it('try to log in with valid account and invalid password', () => {
  42. // Given I visit the Home page
  43. cy.visit('/')
  44. // I see the login page
  45. cy.get('form[name="login"]').should('be.visible')
  46. // I log in with a valid account but invalid password
  47. cy.get('form[name="login"]').within(() => {
  48. cy.get('input[name="user"]').type(user.userId)
  49. cy.get('input[name="password"]').type(`${user.password}--wrong`)
  50. cy.contains('button', 'Log in').click()
  51. })
  52. // see that the login is done
  53. cy.get('[data-login-form-submit]').if().should('not.contain', 'Logging in')
  54. // Then I see that the current page is the Login page
  55. cy.url().should('match', /\/login/)
  56. // And I see that a wrong password message is shown
  57. cy.get('form[name="login"]').then(($el) => expect($el.text()).to.match(/Wrong.+password/i))
  58. cy.get('input[name="password"]:invalid').should('exist')
  59. })
  60. it('try to log in with valid account and invalid password', () => {
  61. // Given I visit the Home page
  62. cy.visit('/')
  63. // I see the login page
  64. cy.get('form[name="login"]').should('be.visible')
  65. // I log in with a valid account but invalid password
  66. cy.get('form[name="login"]').within(() => {
  67. cy.get('input[name="user"]').type(user.userId)
  68. cy.get('input[name="password"]').type(`${user.password}--wrong`)
  69. cy.contains('button', 'Log in').click()
  70. })
  71. // see that the login is done
  72. cy.get('[data-login-form-submit]').if().should('not.contain', 'Logging in')
  73. // Then I see that the current page is the Login page
  74. cy.url().should('match', /\/login/)
  75. // And I see that a wrong password message is shown
  76. cy.get('form[name="login"]').then(($el) => expect($el.text()).to.match(/Wrong.+password/i).and.to.match(/Wrong.+login/))
  77. cy.get('input[name="password"]:invalid').should('exist')
  78. })
  79. it('try to log in with invalid account', () => {
  80. // Given I visit the Home page
  81. cy.visit('/')
  82. // I see the login page
  83. cy.get('form[name="login"]').should('be.visible')
  84. // I log in with an invalid user but valid password
  85. cy.get('form[name="login"]').within(() => {
  86. cy.get('input[name="user"]').type(`${user.userId}--wrong`)
  87. cy.get('input[name="password"]').type(user.password)
  88. cy.contains('button', 'Log in').click()
  89. })
  90. // see that the login is done
  91. cy.get('[data-login-form-submit]').if().should('not.contain', 'Logging in')
  92. // Then I see that the current page is the Login page
  93. cy.url().should('match', /\/login/)
  94. // And I see that a wrong password message is shown
  95. cy.get('form[name="login"]').then(($el) => expect($el.text()).to.match(/Wrong.+password/i).and.to.match(/Wrong.+login/))
  96. cy.get('input[name="password"]:invalid').should('exist')
  97. })
  98. it('try to log in as disabled account', () => {
  99. // Given I visit the Home page
  100. cy.visit('/')
  101. // I see the login page
  102. cy.get('form[name="login"]').should('be.visible')
  103. // When I log in with user disabledUser and password
  104. cy.get('form[name="login"]').within(() => {
  105. cy.get('input[name="user"]').type(disabledUser.userId)
  106. cy.get('input[name="password"]').type(disabledUser.password)
  107. cy.contains('button', 'Log in').click()
  108. })
  109. // see that the login is done
  110. cy.get('[data-login-form-submit]').if().should('not.contain', 'Logging in')
  111. // Then I see that the current page is the Login page
  112. cy.url().should('match', /\/login/)
  113. // And I see that the disabled account message is shown
  114. cy.get('form[name="login"]').then(($el) => expect($el.text()).to.match(/Account.+disabled/i))
  115. cy.get('input[name="password"]:invalid').should('exist')
  116. })
  117. it('try to logout', () => {
  118. cy.login(user)
  119. // Given I visit the Home page
  120. cy.visit('/')
  121. // I see the dashboard
  122. cy.url().should('match', /apps\/dashboard(\/|$)/)
  123. // When click logout
  124. getNextcloudUserMenuToggle().should('exist').click()
  125. getNextcloudUserMenu().contains('a', 'Log out').click()
  126. // Then I see that the current page is the Login page
  127. cy.url().should('match', /\/login/)
  128. })
  129. })