microsoft_graph_inbound_spec.rb 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. # Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Channel::Driver::MicrosoftGraphInbound, :aggregate_failures, integration: true, required_envs: %w[MICROSOFTGRAPH_REFRESH_TOKEN MICROSOFT365_CLIENT_ID MICROSOFT365_CLIENT_SECRET MICROSOFT365_CLIENT_TENANT MICROSOFT365_USER], use_vcr: true do # , retry: 5, retry_wait: 30.seconds do
  4. let(:channel) do
  5. create(:microsoft_graph_channel).tap(&:refresh_xoauth2!).tap do |channel|
  6. VCR.configure do |c|
  7. c.filter_sensitive_data('<MICROSOFTGRAPH_ACCESS_TOKEN>') { channel.options['inbound']['options']['password'] }
  8. c.filter_sensitive_data('<MICROSOFT365_USER_ESCAPED>') { CGI.escapeURIComponent(ENV['MICROSOFT365_USER']) }
  9. end
  10. end
  11. end
  12. let(:client_access_token) { channel.options['inbound']['options']['password'] }
  13. let(:client) { MicrosoftGraph.new(access_token: client_access_token, mailbox: ENV['MICROSOFT365_USER']) }
  14. describe '#fetch' do
  15. context 'with valid token' do
  16. let(:mail_subject) { "CI test for #{described_class}" }
  17. let(:folder_name) { "rspec-#{SecureRandom.uuid}" }
  18. let(:folder) do
  19. VCR.configure do |c|
  20. c.filter_sensitive_data('<FOLDER_NAME>') { folder_name }
  21. end
  22. client.create_message_folder(folder_name)
  23. end
  24. let(:message) do
  25. {
  26. subject: mail_subject,
  27. body: { content: 'Test email' },
  28. from: {
  29. emailAddress: { address: 'from@example.com' }
  30. },
  31. toRecipients: [
  32. {
  33. emailAddress: { address: 'test@example.com' }
  34. }
  35. ],
  36. }
  37. end
  38. shared_examples 'fetches the test message' do
  39. it 'fetches the test message' do
  40. client.store_mocked_message(message, folder_id: channel.options['inbound']['options']['folder_id'] || 'inbox')
  41. expect { channel.fetch }.to change(Ticket, :count)
  42. expect(Ticket.find_by(title: mail_subject)).to be_present
  43. expect(channel.reload.status_in).to eq('ok')
  44. end
  45. end
  46. context 'when fetching from the inbox' do
  47. before do
  48. # No special time-based treatment for existing verify messages. This might break VCR cassette handling.
  49. allow_any_instance_of(described_class::MessageValidator)
  50. .to receive(:fresh_verify_message?).and_return(false)
  51. end
  52. include_examples 'fetches the test message'
  53. end
  54. context 'when fetching from a custom folder' do
  55. before do
  56. channel.options['inbound']['options']['folder_id'] = folder['id']
  57. channel.save!
  58. end
  59. after do
  60. client.delete_message_folder(folder['id'])
  61. end
  62. include_examples 'fetches the test message'
  63. end
  64. context 'when fetching oversized emails' do
  65. before do
  66. client.store_mocked_message(message, folder_id: channel.options['inbound']['options']['folder_id'] || 'inbox')
  67. Setting.set('postmaster_max_size', 0.00001)
  68. end
  69. context 'with email reply' do
  70. it 'creates email reply correctly' do
  71. expect_any_instance_of(described_class).to receive(:process_oversized_mail)
  72. channel.fetch
  73. end
  74. end
  75. context 'without email reply' do
  76. before do
  77. Setting.set('postmaster_send_reject_if_mail_too_large', false)
  78. end
  79. it 'does not create email reply' do
  80. expect_any_instance_of(described_class).not_to receive(:process_oversized_mail)
  81. channel.fetch
  82. expect(channel.reload.status_in).to eq('error')
  83. end
  84. end
  85. end
  86. end
  87. context 'without valid token' do
  88. before do
  89. channel.options['inbound']['options']['password'] = 'incorrect'
  90. channel.save!
  91. allow(channel).to receive(:refresh_xoauth2!)
  92. allow(Ticket).to receive(:new)
  93. end
  94. it 'raises an error' do
  95. expect(channel.fetch).to be(false)
  96. expect(channel.reload.status_in).to eq('error')
  97. expect(Ticket).not_to have_received(:new)
  98. end
  99. end
  100. end
  101. describe '#check_configuration' do
  102. let(:mail_subject) { "CI test for #{described_class}" }
  103. let(:folder_name) { "rspec-#{SecureRandom.uuid}" }
  104. let(:folder) do
  105. VCR.configure do |c|
  106. c.filter_sensitive_data('<FOLDER_NAME>') { folder_name }
  107. end
  108. client.create_message_folder(folder_name)
  109. end
  110. let(:options) { channel.options.dig('inbound', 'options').merge(folder_id: folder['id']) }
  111. let(:message) do
  112. {
  113. subject: mail_subject,
  114. body: { content: 'Test email' },
  115. from: {
  116. emailAddress: { address: 'from@example.com' }
  117. },
  118. toRecipients: [
  119. {
  120. emailAddress: { address: 'test@example.com' }
  121. }
  122. ],
  123. }
  124. end
  125. it 'returns zero with no messages in a folder' do
  126. response = described_class.new.check_configuration(options)
  127. expect(response).to include(
  128. result: 'ok',
  129. content_messages: 0,
  130. )
  131. end
  132. it 'returns count of all messages in a folder' do
  133. client.store_mocked_message(message, folder_id: folder['id'])
  134. msg = client.store_mocked_message(message, folder_id: folder['id'])
  135. client.mark_message_as_read(msg['id'])
  136. response = described_class.new.check_configuration(options)
  137. expect(response).to include(
  138. result: 'ok',
  139. content_messages: 2,
  140. )
  141. end
  142. end
  143. end