channels_email_spec.rb 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. # Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Email channel API endpoints', type: :request do
  4. let(:admin) { create(:admin) }
  5. describe 'POST /api/v1/channels_email_group/ID', aggregate_failures: true, authenticated_as: :admin do
  6. let(:channel) { create(:email_channel) }
  7. let(:email_address) { create(:email_address, channel: channel) }
  8. let(:group) { create(:group) }
  9. let(:group_email_address) { true }
  10. let(:params) do
  11. {
  12. group_id: group.id,
  13. group_email_address: group_email_address,
  14. }
  15. end
  16. it 'updates channel group' do
  17. post "/api/v1/channels_email_group/#{channel.id}", params: params
  18. expect(response).to have_http_status(:ok)
  19. expect(channel.reload.group_id).to eq(group.id)
  20. end
  21. end
  22. describe 'POST /api/v1/channels_email/verify/ID', aggregate_failures: true, authenticated_as: :admin do
  23. let(:group) { create(:group) }
  24. let(:params) do
  25. {
  26. inbound: inbound_params,
  27. outbound: outbound_params,
  28. meta: meta_params,
  29. group_id: group_id,
  30. group_email_address: group_email_address,
  31. group_email_address_id: group_email_address_id,
  32. channel_id: channel_id,
  33. }
  34. end
  35. let(:inbound_params) do
  36. {
  37. adapter: 'imap',
  38. options: {
  39. host: 'nonexisting.host.local',
  40. port: 993,
  41. ssl: true,
  42. user: 'some@example.com',
  43. password: 'xyz',
  44. ssl_verify: true,
  45. archive: true,
  46. archive_before: '2025-01-01T00.00.000Z',
  47. archive_state_id: Ticket::State.find_by(name: 'open').id
  48. },
  49. }
  50. end
  51. let(:outbound_params) do
  52. {
  53. adapter: 'smtp',
  54. options: {
  55. host: 'nonexisting.host.local',
  56. port: 465,
  57. start_tls: true,
  58. user: 'some@example.com',
  59. password: 'xyz',
  60. ssl_verify: true,
  61. ssl: true,
  62. domain: 'example.com',
  63. enable_starttls_auto: true,
  64. },
  65. }
  66. end
  67. let(:meta_params) do
  68. {
  69. realname: 'Testing',
  70. email: 'some@example.com',
  71. password: 'xyz',
  72. }
  73. end
  74. let(:group_id) { nil }
  75. let(:group_email_address) { false }
  76. let(:group_email_address_id) { nil }
  77. let(:channel_id) { nil }
  78. before do
  79. Channel.where(area: 'Email::Account').each(&:destroy)
  80. allow(EmailHelper::Verify).to receive(:email).and_return({ result: 'ok' })
  81. end
  82. it 'creates new channel' do
  83. post '/api/v1/channels_email_verify', params: params
  84. expect(response).to have_http_status(:ok)
  85. expect(Channel.last).to have_attributes(
  86. group_id: Group.first.id,
  87. options: include(
  88. inbound: include(
  89. options: include(
  90. archive: 'true',
  91. archive_before: '2025-01-01T00.00.000Z',
  92. archive_state_id: Ticket::State.find_by(name: 'open').id.to_s,
  93. )
  94. )
  95. )
  96. )
  97. end
  98. context 'when group email address handling is used' do
  99. let(:group) { create(:group, email_address: nil) }
  100. let(:group_id) { group.id }
  101. context 'when group email address should be set' do
  102. let(:group_email_address) { true }
  103. it 'creates new channel' do
  104. post '/api/v1/channels_email_verify', params: params
  105. expect(response).to have_http_status(:ok)
  106. expect(Channel.last.group.email_address_id).to eq(EmailAddress.find_by(channel_id: Channel.last.id).id)
  107. end
  108. end
  109. context 'when group email address should not be set' do
  110. let(:group_email_address) { false }
  111. it 'creates new channel' do
  112. post '/api/v1/channels_email_verify', params: params
  113. expect(response).to have_http_status(:ok)
  114. expect(Channel.last.group.email_address_id).to be_nil
  115. end
  116. end
  117. context 'when channel is already created' do
  118. let(:channel) { create(:email_channel) }
  119. let(:channel_id) { channel.id }
  120. let!(:email_address) { create(:email_address, channel: channel) }
  121. let(:group_email_address) { true }
  122. it 'updates channel' do
  123. post '/api/v1/channels_email_verify', params: params
  124. expect(response).to have_http_status(:ok)
  125. expect(channel.reload.group.email_address_id).to eq(email_address.id)
  126. end
  127. end
  128. end
  129. end
  130. end