google_spec.rb 5.4 KB

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