validate_configuration_inbound_spec.rb 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Mutations::Channel::Email::ValidateConfigurationInbound, type: :graphql do
  4. let(:query) do
  5. <<~QUERY
  6. mutation channelEmailValidateConfigurationInbound($inboundConfiguration: ChannelEmailInboundConfigurationInput!) {
  7. channelEmailValidateConfigurationInbound(inboundConfiguration: $inboundConfiguration) {
  8. success
  9. mailboxStats {
  10. contentMessages
  11. archivePossible
  12. archiveWeekRange
  13. }
  14. errors {
  15. message
  16. field
  17. }
  18. }
  19. }
  20. QUERY
  21. end
  22. let(:failing_configuration) do
  23. {
  24. 'adapter' => 'imap',
  25. 'host' => 'nonexisting.host.local',
  26. 'port' => 993,
  27. 'ssl' => 'ssl',
  28. 'user' => 'some@example.com',
  29. 'password' => 'password',
  30. 'folder' => 'some_folder',
  31. 'sslVerify' => false,
  32. }
  33. end
  34. let(:variables) { { 'inboundConfiguration' => failing_configuration } }
  35. let(:probe_full_response) { nil }
  36. before do
  37. allow(EmailHelper::Probe).to receive(:inbound).and_return(probe_full_response) if probe_full_response
  38. allow_any_instance_of(Channel::Driver::Imap).to receive(:fetch).and_raise(Errno::EHOSTUNREACH)
  39. gql.execute(query, variables: variables)
  40. end
  41. context 'when authenticated as admin', authenticated_as: :admin do
  42. let(:admin) { create(:admin) }
  43. context 'with successful probe' do
  44. let(:probe_full_response) { { result: 'ok', content_messages: 23, archive_possible: true, archive_week_range: 2 } }
  45. let(:expected_result) do
  46. {
  47. 'success' => true,
  48. 'mailboxStats' => {
  49. 'contentMessages' => 23,
  50. 'archivePossible' => true,
  51. 'archiveWeekRange' => 2,
  52. },
  53. 'errors' => nil,
  54. }
  55. end
  56. it 'finds configuration data' do
  57. expect(gql.result.data).to eq(expected_result)
  58. end
  59. end
  60. context 'with failed probe' do
  61. let(:expected_result) do
  62. {
  63. 'success' => false,
  64. 'mailboxStats' => nil,
  65. 'errors' => [{ 'field' => 'inbound.host', 'message' => 'There is no route to this host.' }],
  66. }
  67. end
  68. it 'returns error messages' do
  69. expect(gql.result.data).to eq(expected_result)
  70. end
  71. end
  72. end
  73. context 'when authenticated as non-admin', authenticated_as: :agent do
  74. let(:agent) { create(:agent) }
  75. it 'fails with authentication error' do
  76. expect(gql.result.error_type).to eq(Exceptions::Forbidden)
  77. end
  78. end
  79. it_behaves_like 'graphql responds with error if unauthenticated'
  80. end