channel_spec.rb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. require 'rails_helper'
  2. RSpec.describe Channel, type: :model do
  3. describe '.fetch' do
  4. describe '#refresh_xoauth2! fails' do
  5. let(:channel) { create(:channel, area: 'SomeXOAUTH2::Account', options: { adapter: 'DummyXOAUTH2', auth: { type: 'XOAUTH2' } }) }
  6. before do
  7. allow(ExternalCredential).to receive(:refresh_token).and_raise(RuntimeError)
  8. end
  9. it 'changes Channel status to error' do
  10. expect { described_class.fetch }.to change { channel.reload.status_in }.to('error')
  11. end
  12. end
  13. context 'when one adapter fetch fails' do
  14. let(:failing_adapter_class) do
  15. Class.new(Channel::Driver::Null) do
  16. def fetchable?(*)
  17. true
  18. end
  19. def fetch(*)
  20. raise 'some error'
  21. end
  22. end
  23. end
  24. let(:dummy_adapter_class) do
  25. Class.new(Channel::Driver::Null) do
  26. def fetchable?(*)
  27. true
  28. end
  29. end
  30. end
  31. let(:failing_channel) do
  32. create(:email_channel, inbound: {
  33. adapter: 'failing',
  34. options: {}
  35. })
  36. end
  37. let(:other_channel) do
  38. create(:email_channel, inbound: {
  39. adapter: 'dummy',
  40. options: {}
  41. })
  42. end
  43. before do
  44. allow(described_class).to receive(:driver_class).with('dummy').and_return(dummy_adapter_class)
  45. allow(described_class).to receive(:driver_class).with('failing').and_return(failing_adapter_class)
  46. failing_channel
  47. other_channel
  48. end
  49. it 'adds error flag to the failing Channel' do
  50. expect { described_class.fetch }.to change { failing_channel.reload.preferences[:last_fetch] }.and change { failing_channel.reload.status_in }.to('error')
  51. end
  52. it 'fetches others anyway' do
  53. expect { described_class.fetch }.to change { other_channel.reload.preferences[:last_fetch] }.and change { other_channel.reload.status_in }.to('ok')
  54. end
  55. end
  56. end
  57. context 'when authentication type is XOAUTH2' do
  58. shared_examples 'common XOAUTH2' do
  59. context 'when token refresh fails' do
  60. let(:exception) { DummyExternalCredentialsBackendError.new('something unexpected happened here') }
  61. before do
  62. stub_const('DummyExternalCredentialsBackendError', Class.new(StandardError))
  63. allow(ExternalCredential).to receive(:refresh_token).and_raise(exception)
  64. end
  65. it 'raises RuntimeError' do
  66. expect { channel.refresh_xoauth2! }.to raise_exception(RuntimeError, /#{exception.message}/)
  67. end
  68. end
  69. context 'when non-XOAUTH2 channels are present' do
  70. let!(:email_address) { create(:email_address, channel: create(:channel, area: 'Some::Other')) }
  71. before do
  72. # XOAUTH2 channels refresh their tokens on initialization
  73. allow(ExternalCredential).to receive(:refresh_token).and_return({
  74. access_token: 'S3CR3T'
  75. })
  76. channel
  77. end
  78. it "doesn't remove email address assignments" do
  79. expect { described_class.where(area: channel.area).find_each { nil } }.not_to change { email_address.reload.channel_id }
  80. end
  81. end
  82. end
  83. context 'when provider is Google' do
  84. it_behaves_like 'common XOAUTH2' do
  85. let(:channel) { create(:google_channel) }
  86. end
  87. end
  88. context 'when provider is Microsoft365' do
  89. it_behaves_like 'common XOAUTH2' do
  90. let(:channel) { create(:microsoft365_channel) }
  91. end
  92. end
  93. end
  94. end