omniauth_callbacks_spec.rb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe 'OmniAuth callbacks' do
  4. shared_examples 'omniauth provider callbacks' do |provider|
  5. subject { post send "user_#{provider}_omniauth_callback_path" }
  6. context 'with full information in response' do
  7. before do
  8. mock_omniauth(provider, {
  9. provider: provider.to_s,
  10. uid: '123',
  11. info: {
  12. verified: 'true',
  13. email: 'user@host.example',
  14. },
  15. })
  16. end
  17. context 'without a matching user' do
  18. it 'creates a user and an identity and redirects to root path' do
  19. expect { subject }
  20. .to change(User, :count)
  21. .by(1)
  22. .and change(Identity, :count)
  23. .by(1)
  24. .and change(LoginActivity, :count)
  25. .by(1)
  26. expect(User.last.email).to eq('user@host.example')
  27. expect(Identity.find_by(user: User.last).uid).to eq('123')
  28. expect(response).to redirect_to(root_path)
  29. end
  30. end
  31. context 'with a matching user and no matching identity' do
  32. before do
  33. Fabricate(:user, email: 'user@host.example')
  34. end
  35. it 'matches the existing user, creates an identity, and redirects to root path' do
  36. expect { subject }
  37. .to not_change(User, :count)
  38. .and change(Identity, :count)
  39. .by(1)
  40. .and change(LoginActivity, :count)
  41. .by(1)
  42. expect(Identity.find_by(user: User.last).uid).to eq('123')
  43. expect(response).to redirect_to(root_path)
  44. end
  45. end
  46. context 'with a matching user and a matching identity' do
  47. before do
  48. user = Fabricate(:user, email: 'user@host.example')
  49. Fabricate(:identity, user: user, uid: '123', provider: provider)
  50. end
  51. it 'matches the existing records and redirects to root path' do
  52. expect { subject }
  53. .to not_change(User, :count)
  54. .and not_change(Identity, :count)
  55. .and change(LoginActivity, :count)
  56. .by(1)
  57. expect(response).to redirect_to(root_path)
  58. end
  59. end
  60. end
  61. context 'with a response missing email address' do
  62. before do
  63. mock_omniauth(provider, {
  64. provider: provider.to_s,
  65. uid: '123',
  66. info: {
  67. verified: 'true',
  68. },
  69. })
  70. end
  71. it 'redirects to the auth setup page' do
  72. expect { subject }
  73. .to change(User, :count)
  74. .by(1)
  75. .and change(Identity, :count)
  76. .by(1)
  77. .and change(LoginActivity, :count)
  78. .by(1)
  79. expect(response).to redirect_to(auth_setup_path(missing_email: '1'))
  80. end
  81. end
  82. context 'when a user cannot be built' do
  83. before do
  84. allow(User).to receive(:find_for_omniauth).and_return(User.new)
  85. end
  86. it 'redirects to the new user signup page' do
  87. expect { subject }
  88. .to not_change(User, :count)
  89. .and not_change(Identity, :count)
  90. .and not_change(LoginActivity, :count)
  91. expect(response).to redirect_to(new_user_registration_url)
  92. end
  93. end
  94. end
  95. describe '#openid_connect', if: ENV['OIDC_ENABLED'] == 'true' && ENV['OIDC_SCOPE'].present? do
  96. include_examples 'omniauth provider callbacks', :openid_connect
  97. end
  98. describe '#cas', if: ENV['CAS_ENABLED'] == 'true' do
  99. include_examples 'omniauth provider callbacks', :cas
  100. end
  101. describe '#saml', if: ENV['SAML_ENABLED'] == 'true' do
  102. include_examples 'omniauth provider callbacks', :saml
  103. end
  104. end