channels_microsoft365_spec.rb 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Microsoft365 channel API endpoints', type: :request do
  4. let(:admin) { create(:admin) }
  5. let!(:microsoft365_channel) { create(:microsoft365_channel) }
  6. describe 'DELETE /api/v1/channels_microsoft365', authenticated_as: :admin do
  7. context 'without a email address relation' do
  8. let(:params) do
  9. {
  10. id: microsoft365_channel.id
  11. }
  12. end
  13. it 'responds 200 OK' do
  14. delete '/api/v1/channels_microsoft365', params: params, as: :json
  15. expect(response).to have_http_status(:ok)
  16. end
  17. it 'microsoft365 channel deleted' do
  18. expect { delete '/api/v1/channels_microsoft365', params: params, as: :json }.to change(Channel, :count).by(-1)
  19. end
  20. end
  21. context 'with a email address relation' do
  22. let(:params) do
  23. {
  24. id: microsoft365_channel.id
  25. }
  26. end
  27. before do
  28. create(:email_address, channel: microsoft365_channel)
  29. end
  30. it 'responds 200 OK' do
  31. delete '/api/v1/channels_microsoft365', params: params, as: :json
  32. expect(response).to have_http_status(:ok)
  33. end
  34. it 'microsoft365 channel and related email address deleted' do
  35. expect { delete '/api/v1/channels_microsoft365', params: params, as: :json }.to change(Channel, :count).by(-1).and change(EmailAddress, :count).by(-1)
  36. end
  37. end
  38. end
  39. describe 'POST /api/v1/channels_microsoft365/inbound/ID' do
  40. let(:channel) { create(:microsoft365_channel) }
  41. let(:group) { create(:group) }
  42. before do
  43. Channel.where(area: 'Microsoft365::Account').each(&:destroy)
  44. allow_any_instance_of(Channel).to receive(:refresh_xoauth2!).and_return(true)
  45. allow(EmailHelper::Probe).to receive(:inbound).and_return({ result: 'ok' })
  46. end
  47. it 'does not update inbound options of the channel' do
  48. expect do
  49. post "/api/v1/channels_microsoft365/inbound/#{channel.id}", params: { group_id: group.id, options: { folder: 'SomeFolder', keep_on_server: 'true' } }
  50. end.not_to change(channel, :updated_at)
  51. end
  52. end
  53. describe 'POST /api/v1/channels_microsoft365/verify/ID', aggregate_failures: true, authenticated_as: :admin do
  54. let(:channel) { create(:microsoft365_channel) }
  55. let(:group) { create(:group, email_address_id: nil) }
  56. let(:email_address) { create(:email_address, channel: channel) }
  57. before do
  58. Channel.where(area: 'Microsoft365::Account').each(&:destroy)
  59. email_address
  60. end
  61. it 'updates inbound options of the channel' do
  62. post "/api/v1/channels_microsoft365_verify/#{channel.id}", params: { group_id: group.id, options: { folder: 'SomeFolder', keep_on_server: 'true', archive: 'true', archive_before: '2025-01-01T00.00.000Z', archive_state_id: Ticket::State.find_by(name: 'open').id } }
  63. expect(response).to have_http_status(:ok)
  64. channel.reload
  65. expect(channel).to have_attributes(
  66. group_id: group.id,
  67. options: include(
  68. inbound: include(
  69. options: include(
  70. folder: 'SomeFolder',
  71. keep_on_server: 'true',
  72. archive: 'true',
  73. archive_before: '2025-01-01T00.00.000Z',
  74. archive_state_id: Ticket::State.find_by(name: 'open').id.to_s,
  75. )
  76. )
  77. )
  78. )
  79. end
  80. context 'when group email address is used' do
  81. it 'updates the group email address' do
  82. post "/api/v1/channels_microsoft365_verify/#{channel.id}", params: { group_email_address: true, group_id: group.id, options: { folder_id: 'AAMkAD=', keep_on_server: 'true' } }
  83. expect(response).to have_http_status(:ok)
  84. expect(channel.group.reload.email_address_id).to eq(email_address.id)
  85. end
  86. context 'when group email should not be changed' do
  87. it 'does not update the group email address' do
  88. post "/api/v1/channels_microsoft365_verify/#{channel.id}", params: { group_email_address: false, group_id: group.id, options: { folder_id: 'AAMkAD=', keep_on_server: 'true' } }
  89. expect(response).to have_http_status(:ok)
  90. expect(channel.reload.group.email_address_id).to be_nil
  91. end
  92. end
  93. context 'when group email should be changed to specific email address' do
  94. let(:email_address2) { create(:email_address, channel: channel) }
  95. it 'updates the group email address' do
  96. post "/api/v1/channels_microsoft365_verify/#{channel.id}", params: { group_email_address: true, group_email_address_id: email_address2.id, group_id: group.id, options: { folder_id: 'AAMkAD=', keep_on_server: 'true' } }
  97. expect(response).to have_http_status(:ok)
  98. expect(channel.reload.group.email_address_id).to eq(email_address2.id)
  99. end
  100. end
  101. end
  102. end
  103. end