microsoft_graph_spec.rb 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. require 'requests/channel_admin/base_examples'
  4. RSpec.describe 'Microsoft Graph channel admin API endpoints', aggregate_failures: true, authenticated_as: :user, type: :request do
  5. let(:user) { create(:admin) }
  6. it_behaves_like 'base channel management', factory: :microsoft_graph_channel, path: :microsoft_graph
  7. describe 'GET /api/v1/channels_admin/microsoft_graph' do
  8. let!(:channel) { create(:microsoft_graph_channel) }
  9. it 'list the channel' do
  10. get '/api/v1/channels/admin/microsoft_graph'
  11. expect(response).to have_http_status(:ok)
  12. expect(json_response).to include(
  13. 'not_used_email_address_ids' => [],
  14. 'channel_ids' => [channel.id],
  15. 'callback_url' => ExternalCredential.callback_url('microsoft_graph'),
  16. )
  17. end
  18. end
  19. describe 'GET /api/v1/channels_admin/microsoft_graph/ID/folders' do
  20. let!(:channel) { create(:microsoft_graph_channel) }
  21. let(:folders) do
  22. [
  23. {
  24. 'id' => Base64.strict_encode64(Faker::Crypto.unique.sha256),
  25. 'displayName' => Faker::Lorem.unique.word,
  26. 'childFolders' => [],
  27. },
  28. ]
  29. end
  30. before do
  31. allow_any_instance_of(Channel).to receive(:refresh_xoauth2!).and_return(true)
  32. allow_any_instance_of(MicrosoftGraph).to receive(:get_message_folders_tree).and_return(folders)
  33. end
  34. it 'fetches mailbox folder information' do
  35. get "/api/v1/channels/admin/microsoft_graph/#{channel.id}/folders"
  36. expect(response).to have_http_status(:ok)
  37. expect(json_response).to include('folders' => folders)
  38. end
  39. context 'when API raises an error' do
  40. before do
  41. allow_any_instance_of(MicrosoftGraph).to receive(:get_message_folders_tree).and_raise(MicrosoftGraph::ApiError, { message: 'Error message', code: 'Error code' })
  42. end
  43. it 'includes both error message and code' do
  44. get "/api/v1/channels/admin/microsoft_graph/#{channel.id}/folders"
  45. expect(response).to have_http_status(:ok)
  46. expect(json_response).to include('error' => {
  47. 'message' => 'Error message (Error code)',
  48. 'code' => 'Error code',
  49. })
  50. end
  51. end
  52. end
  53. describe 'POST /api/v1/channels_admin/microsoft_graph/group/ID' do
  54. let!(:channel) { create(:microsoft_graph_channel) }
  55. let!(:group) { create(:group) }
  56. it 'updates destination group of the channel' do
  57. post "/api/v1/channels/admin/microsoft_graph/group/#{channel.id}", params: { group_id: group.id }
  58. expect(response).to have_http_status(:ok)
  59. expect(channel.reload.group).to eq(group)
  60. end
  61. end
  62. describe 'POST /api/v1/channels_admin/microsoft_graph/inbound/ID' do
  63. let(:channel) { create(:microsoft_graph_channel) }
  64. let(:group) { create(:group) }
  65. before do
  66. allow_any_instance_of(Channel).to receive(:refresh_xoauth2!).and_return(true)
  67. allow(EmailHelper::Probe).to receive(:inbound).and_return({ result: 'ok' })
  68. end
  69. it 'does not update inbound options of the channel' do
  70. expect do
  71. post "/api/v1/channels/admin/microsoft_graph/inbound/#{channel.id}", params: { group_id: group.id, options: { folder_id: 'AAMkAD=', keep_on_server: 'true' } }
  72. end.not_to change(channel, :updated_at)
  73. end
  74. end
  75. describe 'POST /api/v1/channels_admin/microsoft_graph/verify/ID' do
  76. let(:channel) { create(:microsoft_graph_channel) }
  77. let(:group) { create(:group) }
  78. it 'updates inbound options of the channel' do
  79. post "/api/v1/channels/admin/microsoft_graph/verify/#{channel.id}", params: { group_id: group.id, options: { folder_id: 'AAMkAD=', keep_on_server: 'true', archive: 'true', archive_before: '2025-01-01T00.00.000Z', archive_state_id: Ticket::State.find_by(name: 'closed').id } }
  80. expect(response).to have_http_status(:ok)
  81. channel.reload
  82. expect(channel).to have_attributes(
  83. group_id: group.id,
  84. options: include(
  85. inbound: include(
  86. options: include(
  87. folder_id: 'AAMkAD=',
  88. keep_on_server: 'true',
  89. archive: 'true',
  90. archive_before: '2025-01-01T00.00.000Z',
  91. archive_state_id: Ticket::State.find_by(name: 'closed').id.to_s,
  92. )
  93. )
  94. )
  95. )
  96. end
  97. end
  98. end