channels_email_spec.rb 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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/verify/ID', aggregate_failures: true, authenticated_as: :admin do
  6. let(:group) { create(:group) }
  7. let(:params) do
  8. {
  9. inbound: inbound_params,
  10. outbound: outbound_params,
  11. meta: meta_params,
  12. group_id: group_id,
  13. group_email_address: group_email_address,
  14. group_email_address_id: group_email_address_id,
  15. }
  16. end
  17. let(:inbound_params) do
  18. {
  19. adapter: 'imap',
  20. options: {
  21. host: 'nonexisting.host.local',
  22. port: 993,
  23. ssl: true,
  24. user: 'some@example.com',
  25. password: 'xyz',
  26. ssl_verify: true,
  27. archive: true,
  28. archive_before: '2025-01-01T00.00.000Z',
  29. archive_state_id: Ticket::State.find_by(name: 'open').id
  30. },
  31. }
  32. end
  33. let(:outbound_params) do
  34. {
  35. adapter: 'smtp',
  36. options: {
  37. host: 'nonexisting.host.local',
  38. port: 465,
  39. start_tls: true,
  40. user: 'some@example.com',
  41. password: 'xyz',
  42. ssl_verify: true,
  43. ssl: true,
  44. domain: 'example.com',
  45. enable_starttls_auto: true,
  46. },
  47. }
  48. end
  49. let(:meta_params) do
  50. {
  51. realname: 'Testing',
  52. email: 'some@example.com',
  53. password: 'xyz',
  54. }
  55. end
  56. let(:group_id) { nil }
  57. let(:group_email_address) { false }
  58. let(:group_email_address_id) { nil }
  59. before do
  60. Channel.where(area: 'Email::Account').each(&:destroy)
  61. allow(EmailHelper::Verify).to receive(:email).and_return({ result: 'ok' })
  62. end
  63. it 'creates new channel' do
  64. post '/api/v1/channels_email_verify', params: params
  65. expect(response).to have_http_status(:ok)
  66. expect(Channel.last).to have_attributes(
  67. group_id: Group.first.id,
  68. options: include(
  69. inbound: include(
  70. options: include(
  71. archive: 'true',
  72. archive_before: '2025-01-01T00.00.000Z',
  73. archive_state_id: Ticket::State.find_by(name: 'open').id.to_s,
  74. )
  75. )
  76. )
  77. )
  78. end
  79. context 'when group email address handling is used' do
  80. let(:group) { create(:group, email_address: nil) }
  81. let(:group_id) { group.id }
  82. context 'when group email address should be set' do
  83. let(:group_email_address) { true }
  84. it 'creates new channel' do
  85. post '/api/v1/channels_email_verify', params: params
  86. expect(response).to have_http_status(:ok)
  87. expect(Channel.last.group.email_address_id).to eq(EmailAddress.find_by(channel_id: Channel.last.id).id)
  88. end
  89. end
  90. context 'when group email address should not be set' do
  91. let(:group_email_address) { false }
  92. it 'creates new channel' do
  93. post '/api/v1/channels_email_verify', params: params
  94. expect(response).to have_http_status(:ok)
  95. expect(Channel.last.group.email_address_id).to be_nil
  96. end
  97. end
  98. end
  99. end
  100. end