login_spec.rb 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Login', authenticated_as: false, type: :system do
  4. context 'with standard authentication' do
  5. before do
  6. visit '/'
  7. end
  8. it 'fqdn is visible on login page' do
  9. expect(page).to have_css('.login p', text: Setting.get('fqdn'))
  10. end
  11. it 'Login with wrong credentials' do
  12. within('#login') do
  13. fill_in 'username', with: 'admin@example.com'
  14. fill_in 'password', with: 'wrong'
  15. click_on('Sign in')
  16. end
  17. expect(page).to have_css('#login .alert')
  18. end
  19. end
  20. context 'with enabled two factor authentication' do
  21. let(:user) { User.find_by(login: 'admin@example.com') }
  22. context 'with security keys method' do
  23. before do
  24. skip('Mocking of Web Authentication API is currently supported only in Chrome.') if Capybara.current_driver != :zammad_chrome
  25. stub_const('Auth::BRUTE_FORCE_SLEEP', 0)
  26. visit '/'
  27. # We can only mock the security key within the loaded app.
  28. two_factor_pref
  29. refresh
  30. within('#login') do
  31. fill_in 'username', with: 'admin@example.com'
  32. fill_in 'password', with: 'test'
  33. click_on('Sign in')
  34. end
  35. end
  36. context 'with the configured security key present' do
  37. let(:two_factor_pref) { create(:user_two_factor_preference, :mocked_security_keys, user: user, page: page) }
  38. it 'signs in with the correct security key present' do
  39. expect(page).to have_no_selector('#login')
  40. end
  41. end
  42. context 'with the incorrect security key present' do
  43. let(:two_factor_pref) { create(:user_two_factor_preference, :mocked_security_keys, user: user, page: page, wrong_key: true) }
  44. it 'shows error and retry button' do
  45. expect(page).to have_css('#login .alert')
  46. expect(page).to have_css('.js-retry')
  47. end
  48. end
  49. end
  50. context 'with authenticator app method' do
  51. let(:token) { two_factor_pref.configuration[:code] }
  52. let!(:two_factor_pref) { create(:user_two_factor_preference, :authenticator_app, user: user) }
  53. before do
  54. stub_const('Auth::BRUTE_FORCE_SLEEP', 0)
  55. visit '/'
  56. within('#login') do
  57. fill_in 'username', with: 'admin@example.com'
  58. fill_in 'password', with: 'test'
  59. click_on('Sign in')
  60. end
  61. end
  62. it 'login with correct payload' do
  63. within('#login') do
  64. fill_in 'security_code', with: token
  65. click_on('Sign in')
  66. end
  67. expect(page).to have_no_selector('#login')
  68. end
  69. it 'login with wrong payload' do
  70. within('#login') do
  71. fill_in 'security_code', with: 'asd'
  72. click_on('Sign in')
  73. end
  74. expect(page).to have_css('#login .alert')
  75. end
  76. end
  77. context 'with recovery code' do
  78. let(:token) { 'token' }
  79. let(:two_factor_pref) { create(:user_two_factor_preference, :authenticator_app, user: user) }
  80. let(:recovery_2fa) { create(:user_two_factor_preference, :recovery_codes, recovery_code: token, user: user) }
  81. before do
  82. two_factor_pref && recovery_2fa
  83. Setting.set('two_factor_authentication_recovery_codes', recovery_codes_enabled)
  84. visit '/'
  85. within('#login') do
  86. fill_in 'username', with: 'admin@example.com'
  87. fill_in 'password', with: 'test'
  88. click_on('Sign in')
  89. end
  90. end
  91. context 'when recovery code is enabled' do
  92. let(:recovery_codes_enabled) { true }
  93. before do
  94. click_on 'Try another method'
  95. click_on 'recovery codes'
  96. end
  97. it 'login with correct payload' do
  98. within('#login') do
  99. fill_in 'security_code', with: token
  100. click_on('Sign in')
  101. end
  102. expect(page).to have_no_selector('#login')
  103. end
  104. it 'login with wrong payload' do
  105. within('#login') do
  106. fill_in 'security_code', with: 'wrong token'
  107. click_on('Sign in')
  108. end
  109. expect(page).to have_css('#login .alert')
  110. end
  111. end
  112. context 'when recovery code is disabled' do
  113. let(:recovery_codes_enabled) { false }
  114. it 'recovery code link is hidden' do
  115. expect(page).to have_no_text 'Try another method'
  116. end
  117. end
  118. end
  119. end
  120. end