microsoft_graph_inbound_spec.rb 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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).to receive(:messages_is_too_old_verify?).and_return(true)
  50. end
  51. include_examples 'fetches the test message'
  52. end
  53. context 'when fetching from a custom folder' do
  54. before do
  55. channel.options['inbound']['options']['folder_id'] = folder['id']
  56. channel.save!
  57. end
  58. after do
  59. client.delete_message_folder(folder['id'])
  60. end
  61. include_examples 'fetches the test message'
  62. end
  63. end
  64. context 'without valid token' do
  65. before do
  66. channel.options['inbound']['options']['password'] = 'incorrect'
  67. channel.save!
  68. allow(channel).to receive(:refresh_xoauth2!)
  69. allow(Ticket).to receive(:new)
  70. end
  71. it 'raises an error' do
  72. expect(channel.fetch).to be(false)
  73. expect(channel.reload.status_in).to eq('error')
  74. expect(Ticket).not_to have_received(:new)
  75. end
  76. end
  77. end
  78. describe '#check_configuration' do
  79. let(:mail_subject) { "CI test for #{described_class}" }
  80. let(:folder_name) { "rspec-#{SecureRandom.uuid}" }
  81. let(:folder) do
  82. VCR.configure do |c|
  83. c.filter_sensitive_data('<FOLDER_NAME>') { folder_name }
  84. end
  85. client.create_message_folder(folder_name)
  86. end
  87. let(:options) { channel.options.dig('inbound', 'options').merge(folder_id: folder['id']) }
  88. let(:message) do
  89. {
  90. subject: mail_subject,
  91. body: { content: 'Test email' },
  92. from: {
  93. emailAddress: { address: 'from@example.com' }
  94. },
  95. toRecipients: [
  96. {
  97. emailAddress: { address: 'test@example.com' }
  98. }
  99. ],
  100. }
  101. end
  102. it 'returns zero with no messages in a folder' do
  103. response = described_class.new.check_configuration(options)
  104. expect(response).to include(
  105. result: 'ok',
  106. content_messages: 0,
  107. )
  108. end
  109. it 'returns count of all messages in a folder' do
  110. client.store_mocked_message(message, folder_id: folder['id'])
  111. msg = client.store_mocked_message(message, folder_id: folder['id'])
  112. client.mark_message_as_read(msg['id'])
  113. response = described_class.new.check_configuration(options)
  114. expect(response).to include(
  115. result: 'ok',
  116. content_messages: 2,
  117. )
  118. end
  119. end
  120. end