login_spec.rb 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Mobile > Login', app: :mobile, authenticated_as: false, type: :system do
  4. context 'when logging in with two factor auth' do
  5. let(:user) { User.find_by(login: 'admin@example.com') }
  6. let(:code) { two_factor_pref.configuration[:code] }
  7. let(:recover_code_enabled) { false }
  8. let!(:two_factor_pref) { create(:user_two_factor_preference, :authenticator_app, user:) }
  9. let(:token) { 'token' }
  10. before do
  11. Setting.set('two_factor_authentication_method_authenticator_app', true)
  12. visit '/login'
  13. login(
  14. username: 'admin@example.com',
  15. password: 'test',
  16. remember_me: true,
  17. skip_waiting: true,
  18. )
  19. end
  20. it 'can login with correct code' do
  21. expect(page).to have_no_text('Try another method')
  22. find_input('Security Code').type(code)
  23. find_button('Sign in').click
  24. expect(page).to have_text('Home')
  25. logout
  26. expect_current_route '/login'
  27. end
  28. end
  29. context 'when loggin in via external authentication provider', authenticated_as: false, integration: true, integration_standalone: :saml, required_envs: %w[KEYCLOAK_BASE_URL KEYCLOAK_ADMIN_USER KEYCLOAK_ADMIN_PASSWORD] do
  30. let(:zammad_base_url) { "#{Capybara.app_host}:#{Capybara.current_session.server.port}" }
  31. let(:zammad_saml_metadata) { "#{zammad_base_url}/auth/saml/metadata" }
  32. let(:saml_base_url) { ENV['KEYCLOAK_BASE_URL'] }
  33. let(:saml_client_json) { Rails.root.join('test/data/saml/zammad-client.json').read.gsub('#ZAMMAD_BASE_URL', zammad_base_url) }
  34. let(:saml_realm_zammad_descriptor) { "#{saml_base_url}/realms/zammad/protocol/saml/descriptor" }
  35. let(:saml_realm_zammad_accounts) { "#{saml_base_url}/realms/zammad/account" }
  36. before do
  37. saml_configure_keycloak(zammad_saml_metadata:, saml_client_json:)
  38. saml_configure_zammad(saml_base_url:, saml_realm_zammad_descriptor:)
  39. end
  40. it 'can login via external authentication provider' do
  41. visit '/login'
  42. expect(page).to have_text('Or sign in using')
  43. expect(page).to have_text('SAML')
  44. find_button('SAML').click
  45. saml_login_keycloak
  46. # Workaround: SAML redirects in CI don't work correctly because of missing HTTP referrer headers.
  47. # The redirect will go to / instead of /mobile, so check where we are and switch to mobile if needed.
  48. expect(page).to have_text('JD') # Logged in, either in legacy interface or in mobile.
  49. begin
  50. # Check if we are already in mobile.
  51. page.find('span', text: 'Home', wait: 0)
  52. rescue
  53. visit '/'
  54. expect(page).to have_text('Home')
  55. end
  56. # Manual logout
  57. click_on 'JD' # avatar
  58. click_on 'Sign out'
  59. expect(page).to have_current_path(%r{/login})
  60. wait_for_test_flag('applicationLoaded.loaded', skip_clearing: true)
  61. visit '/'
  62. expect_current_route '/login'
  63. visit saml_realm_zammad_accounts
  64. expect(page).to have_text('Sign in')
  65. end
  66. end
  67. end