microsoft_graph_spec.rb 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 'updates inbound options of the channel' do
  70. post "/api/v1/channels/admin/microsoft_graph/inbound/#{channel.id}", params: { group_id: group.id, options: { folder_id: 'AAMkAD=', keep_on_server: 'true' } }
  71. expect(response).to have_http_status(:ok)
  72. expect(channel.reload.group).to eq(group)
  73. expect(channel.options['inbound']['options']).to include(
  74. 'folder_id' => 'AAMkAD=',
  75. 'keep_on_server' => 'true',
  76. )
  77. end
  78. end
  79. end