microsoft365_imap_spec.rb 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Manage > Channels > Microsoft 365 IMAP Email', time_zone: 'Europe/London', type: :system do
  4. let(:client_id) { SecureRandom.uuid }
  5. let(:client_secret) { SecureRandom.urlsafe_base64(40) }
  6. let(:client_tenant) { SecureRandom.uuid }
  7. let(:callback_url) { "#{Setting.get('http_type')}://#{Setting.get('fqdn')}#{Rails.configuration.api_path}/external_credentials/microsoft365/callback" }
  8. context 'without an existing app configuration' do
  9. before do
  10. visit '#channels/microsoft365'
  11. end
  12. it 'creates a new app configuration' do
  13. find('.btn--success', text: 'Connect Microsoft 365 App').click
  14. in_modal do
  15. fill_in 'client_id', with: client_id
  16. fill_in 'client_secret', with: client_secret
  17. fill_in 'client_tenant', with: client_tenant
  18. check_input_field_value('callback_url', callback_url)
  19. click_on 'Submit'
  20. end
  21. expect(ExternalCredential.last).to have_attributes(
  22. name: 'microsoft365',
  23. credentials: include(client_id:, client_secret:, client_tenant:)
  24. )
  25. end
  26. end
  27. context 'with an existing app configuration' do
  28. let(:external_credential) { create(:microsoft365_credential) }
  29. before do
  30. external_credential
  31. end
  32. # includes initial setup
  33. context 'when editing an account' do
  34. let(:channel) do
  35. create(:microsoft365_channel, active: false)
  36. .tap do |channel|
  37. channel.options[:inbound][:options].merge! folder: folder1, keep_on_server: true
  38. channel.save!
  39. end
  40. end
  41. let(:state) { Ticket::State.find_by(name: 'open') }
  42. let(:folder1) { 'Folder1' }
  43. let(:folder2) { 'Folder2' }
  44. before do
  45. channel
  46. allow_any_instance_of(Channel).to receive(:refresh_xoauth2!).and_return(true)
  47. allow(EmailHelper::Probe).to receive(:inbound).and_return({ result: 'ok' })
  48. end
  49. context 'when editing a freshly added account' do
  50. before do
  51. visit "#channels/microsoft365/#{channel.id}"
  52. end
  53. context 'when no emails exist' do
  54. before do
  55. allow(EmailHelper::Probe).to receive(:inbound).and_return({ result: 'ok', content_messages: 0 })
  56. end
  57. it 'does not display archive dialog but saves channel' do
  58. in_modal do
  59. fill_in 'options::folder', with: folder2
  60. click_on 'Submit'
  61. end
  62. expect(channel.reload).to have_attributes(
  63. active: true,
  64. options: include(inbound: include(options: include(folder: folder2)))
  65. )
  66. end
  67. end
  68. context 'when some emails exist' do
  69. before do
  70. allow(EmailHelper::Probe).to receive(:inbound).and_return({ result: 'ok', content_messages: 123 })
  71. end
  72. it 'displays inbound configuration dialog' do
  73. visit "#channels/microsoft365/#{channel.id}"
  74. in_modal do
  75. fill_in 'options::folder', with: folder2
  76. set_select_field_label('options::keep_on_server', 'no')
  77. click_on 'Submit'
  78. end
  79. in_modal do
  80. set_select_field_value('options::archive_state_id', state.id.to_s)
  81. set_date_field_value('options::archive_before', '12/01/2024')
  82. click_on 'Submit'
  83. end
  84. expect(channel.reload).to have_attributes(
  85. active: true,
  86. options: include(
  87. inbound: include(
  88. options: include(
  89. folder: folder2,
  90. keep_on_server: false,
  91. archive: true,
  92. archive_state_id: state.id.to_s,
  93. archive_before: '2024-12-01T08:00:00.000Z'
  94. ),
  95. ),
  96. ),
  97. )
  98. end
  99. end
  100. end
  101. context 'when editing an existing channel' do
  102. before do
  103. channel.options[:inbound][:options]
  104. .merge!(archive: true, archive_state_id: state.id.to_s, archive_before: '2024-12-01T08:00:00.000Z')
  105. channel.save!
  106. allow(EmailHelper::Probe).to receive(:inbound).and_return({ result: 'ok', content_messages: 0 })
  107. visit '#channels/microsoft365'
  108. find('.js-editInbound', text: 'Edit').click
  109. end
  110. it 'displays inbound configuration dialog' do
  111. in_modal do
  112. expect(page).to have_field('options::folder', with: folder1)
  113. check_select_field_value('options::keep_on_server', 'true')
  114. fill_in 'options::folder', with: folder2
  115. set_select_field_label('options::keep_on_server', 'no')
  116. click_on 'Submit'
  117. end
  118. in_modal do
  119. check_switch_field_value('options::archive', true)
  120. check_select_field_value('options::archive_state_id', state.id.to_s)
  121. check_date_field_value('options::archive_before', '12/01/2024')
  122. click '.js-switch'
  123. click_on 'Submit'
  124. end
  125. expect(channel.reload).to have_attributes(
  126. active: false,
  127. options: include(
  128. inbound: include(
  129. options: include(
  130. folder: folder2,
  131. keep_on_server: false,
  132. archive: false,
  133. archive_state_id: state.id.to_s,
  134. archive_before: '2024-12-01T08:00:00.000Z'
  135. ),
  136. ),
  137. ),
  138. )
  139. end
  140. end
  141. end
  142. end
  143. end