login_spec.rb 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. visit '/login'
  12. login(
  13. username: 'admin@example.com',
  14. password: 'test',
  15. remember_me: true,
  16. skip_waiting: true,
  17. )
  18. end
  19. it 'can login with correct code' do
  20. expect(page).to have_no_text('Try another method')
  21. find_input('Security Code').type(code)
  22. find_button('Sign in').click
  23. expect(page).to have_text('Home')
  24. logout
  25. expect_current_route '/login'
  26. end
  27. end
  28. 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
  29. let(:zammad_base_url) { "#{Capybara.app_host}:#{Capybara.current_session.server.port}" }
  30. let(:zammad_saml_metadata) { "#{zammad_base_url}/auth/saml/metadata" }
  31. let(:saml_base_url) { ENV['KEYCLOAK_BASE_URL'] }
  32. let(:saml_client_json) { Rails.root.join('test/data/saml/zammad-client.json').read.gsub('#ZAMMAD_BASE_URL', zammad_base_url) }
  33. let(:saml_realm_zammad_descriptor) { "#{saml_base_url}/realms/zammad/protocol/saml/descriptor" }
  34. let(:saml_realm_zammad_accounts) { "#{saml_base_url}/realms/zammad/account" }
  35. before do
  36. saml_configure_keycloak(zammad_saml_metadata:, saml_client_json:)
  37. saml_configure_zammad(saml_base_url:, saml_realm_zammad_descriptor:)
  38. end
  39. it 'can login via external authentication provider' do
  40. visit '/login'
  41. expect(page).to have_text('Or sign in using')
  42. expect(page).to have_text('SAML')
  43. find_button('SAML').click
  44. saml_login_keycloak
  45. # Workaround: SAML redirects in CI don't work because of missing HTTP referrer headers.
  46. visit '/'
  47. expect(page).to have_text('Home')
  48. # Manual logout
  49. click_on 'JD' # avatar
  50. click_on 'Sign out'
  51. expect(page).to have_current_path(%r{/login})
  52. wait_for_test_flag('applicationLoaded.loaded', skip_clearing: true)
  53. visit '/'
  54. expect_current_route '/login'
  55. visit saml_realm_zammad_accounts
  56. expect(page).to have_text('Sign in')
  57. end
  58. end
  59. end