microsoft_graph_outbound_spec.rb 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Channel::Driver::MicrosoftGraphOutbound, :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['outbound']['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['outbound']['options']['password'] }
  13. let(:client) { MicrosoftGraph.new(access_token: client_access_token, mailbox: ENV['MICROSOFT365_USER']) }
  14. describe '.deliver' do
  15. let(:mail_subject) { "CI test for #{described_class}" }
  16. let(:mail) do
  17. {
  18. to: ENV['MICROSOFT365_USER'],
  19. subject: mail_subject,
  20. body: 'Test email',
  21. }
  22. end
  23. context 'with valid token' do
  24. it 'sends mail' do
  25. expect { channel.deliver(mail) }.not_to raise_error
  26. expect(channel.reload.status_out).to eq('ok')
  27. end
  28. end
  29. context 'without valid token' do
  30. before do
  31. channel.options['outbound']['options']['password'] = 'incorrect'
  32. channel.save!
  33. allow(channel).to receive(:refresh_xoauth2!)
  34. end
  35. it 'raises an error' do
  36. expect { channel.deliver(mail) }.to raise_error(Channel::DeliveryError)
  37. end
  38. end
  39. end
  40. end