authorization_spec.rb 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Authorization, type: :model do
  4. describe 'User assets' do
  5. subject(:authorization) { create(:twitter_authorization) }
  6. it 'does update assets after new authorizations created' do
  7. authorization.user.assets({})
  8. create(:twitter_authorization, provider: 'twitter2', user: authorization.user)
  9. assets = authorization.user.reload.assets({})
  10. expect(assets[:User][authorization.user.id]['accounts'].keys.count).to eq(2)
  11. end
  12. end
  13. describe 'Account linking notification', sends_notification_emails: true do
  14. subject(:authorization) { create(:authorization, user: agent, provider: provider) }
  15. let(:agent) { create(:agent) }
  16. let(:provider) { 'github' }
  17. let(:provider_name) { 'GitHub' }
  18. shared_examples 'sending out email notification' do
  19. it 'sends out an email notification' do
  20. check_notification do
  21. authorization
  22. sent(
  23. template: 'user_auth_provider',
  24. user: authorization.user,
  25. objects: hash_including({ user: authorization.user, provider: provider_name })
  26. )
  27. end
  28. end
  29. end
  30. shared_examples 'not sending out email notification' do
  31. it 'does not send out an email notification' do
  32. check_notification do
  33. authorization
  34. not_sent(
  35. template: 'user_auth_provider',
  36. user: authorization.user,
  37. objects: hash_including({ user: authorization.user, provider: provider_name })
  38. )
  39. end
  40. end
  41. end
  42. context 'with setting turned on' do
  43. before do
  44. Setting.set('auth_third_party_linking_notification', true)
  45. end
  46. context 'when linking with an existing account' do
  47. it_behaves_like 'sending out email notification'
  48. context 'when user has no email address' do
  49. let(:agent) { create(:agent, email: '') }
  50. it_behaves_like 'not sending out email notification'
  51. end
  52. end
  53. context 'when creating a new account' do
  54. let(:agent) { create(:agent, source: 'github') }
  55. it_behaves_like 'not sending out email notification'
  56. end
  57. context 'with SAML as the provider' do
  58. let(:provider) { 'saml' }
  59. let(:provider_name) { 'Custom Provider' }
  60. before do
  61. Setting.set('auth_saml_credentials', { display_name: provider_name })
  62. end
  63. it_behaves_like 'sending out email notification'
  64. end
  65. end
  66. context 'with setting turned off' do
  67. before do
  68. Setting.set('auth_third_party_linking_notification', false)
  69. end
  70. it_behaves_like 'not sending out email notification'
  71. end
  72. end
  73. end