after_auth_spec.rb 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. require 'system/examples/security_keys_setup_examples'
  4. require 'system/examples/authenticator_app_setup_examples'
  5. RSpec.describe 'After Auth', type: :system do
  6. context 'with after auth module for 2FA', authenticated_as: :agent do
  7. let(:agent) { create(:agent).tap { |user| user.roles << role } }
  8. let(:role) { create(:role, :agent, name: '2FA') }
  9. before do
  10. Setting.set('two_factor_authentication_enforce_role_ids', [role.id])
  11. Setting.set('two_factor_authentication_method_authenticator_app', true)
  12. end
  13. shared_examples 'showing the modal' do
  14. it 'shows the modal' do
  15. expect_current_route 'dashboard'
  16. in_modal do
  17. expect(page).to have_text('Set up two-factor authentication')
  18. end
  19. end
  20. end
  21. context 'when logging in', authenticated_as: false do
  22. before do
  23. login(
  24. username: agent.login,
  25. password: 'test',
  26. )
  27. end
  28. it_behaves_like 'showing the modal'
  29. end
  30. context 'when already logged in' do
  31. before do
  32. visit '/'
  33. end
  34. it_behaves_like 'showing the modal'
  35. context 'with security keys method' do
  36. before do
  37. click_on 'Security Keys'
  38. end
  39. include_examples 'security keys setup' do
  40. let(:password_check) { false }
  41. end
  42. end
  43. context 'with authenticator app method' do
  44. before do
  45. click_on 'Authenticator App'
  46. end
  47. include_examples 'authenticator app setup' do
  48. let(:password_check) { false }
  49. end
  50. end
  51. context 'when user does not have sufficient permissions' do
  52. let(:agent) { create(:agent, roles: [role]) }
  53. it 'shows error message' do
  54. expect_current_route 'dashboard'
  55. in_modal do
  56. expect(page).to have_text("Two-factor authentication is required, but you don't have sufficient permissions to set it up. Please contact your administrator.")
  57. end
  58. end
  59. end
  60. end
  61. end
  62. end