channels_google_spec.rb 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Google channel API endpoints', type: :request do
  4. let(:admin) { create(:admin) }
  5. let!(:google_channel) { create(:google_channel) }
  6. describe 'DELETE /api/v1/channels_google', authenticated_as: :admin do
  7. context 'without a email address relation' do
  8. let(:params) do
  9. {
  10. id: google_channel.id
  11. }
  12. end
  13. it 'responds 200 OK' do
  14. delete '/api/v1/channels_google', params: params, as: :json
  15. expect(response).to have_http_status(:ok)
  16. end
  17. it 'google channel deleted' do
  18. expect { delete '/api/v1/channels_google', params: params, as: :json }.to change(Channel, :count).by(-1)
  19. end
  20. end
  21. context 'with a email address relation' do
  22. let(:params) do
  23. {
  24. id: google_channel.id
  25. }
  26. end
  27. before do
  28. create(:email_address, channel: google_channel)
  29. end
  30. it 'responds 200 OK' do
  31. delete '/api/v1/channels_google', params: params, as: :json
  32. expect(response).to have_http_status(:ok)
  33. end
  34. it 'google channel and related email address deleted' do
  35. expect { delete '/api/v1/channels_google', params: params, as: :json }.to change(Channel, :count).by(-1).and change(EmailAddress, :count).by(-1)
  36. end
  37. end
  38. end
  39. describe 'POST /api/v1/channels_google/inbound/ID' do
  40. let(:channel) { create(:google_channel) }
  41. let(:group) { create(:group) }
  42. before do
  43. Channel.where(area: 'Google::Account').each(&:destroy)
  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. it 'does not update inbound options of the channel' do
  48. expect do
  49. post "/api/v1/channels_google/inbound/#{channel.id}", params: { group_id: group.id, options: { folder: 'SomeFolder', keep_on_server: 'true' } }
  50. end.not_to change(channel, :updated_at)
  51. end
  52. end
  53. describe 'POST /api/v1/channels_google/verify/ID', aggregate_failures: true, authenticated_as: :admin do
  54. let(:channel) { create(:google_channel) }
  55. let(:group) { create(:group) }
  56. before do
  57. Channel.where(area: 'Google::Account').each(&:destroy)
  58. end
  59. it 'updates inbound options of the channel' do
  60. post "/api/v1/channels_google_verify/#{channel.id}", params: { group_id: group.id, options: { folder: 'SomeFolder', keep_on_server: 'true', archive: 'true', archive_before: '2025-01-01T00.00.000Z', archive_state_id: Ticket::State.find_by(name: 'open').id } }
  61. expect(response).to have_http_status(:ok)
  62. channel.reload
  63. expect(channel).to have_attributes(
  64. group_id: group.id,
  65. options: include(
  66. inbound: include(
  67. options: include(
  68. folder: 'SomeFolder',
  69. keep_on_server: 'true',
  70. archive: 'true',
  71. archive_before: '2025-01-01T00.00.000Z',
  72. archive_state_id: Ticket::State.find_by(name: 'open').id.to_s,
  73. )
  74. )
  75. )
  76. )
  77. end
  78. end
  79. end