validate_configuration_inbound_spec.rb 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # Copyright (C) 2012-2025 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. }
  12. errors {
  13. message
  14. field
  15. }
  16. }
  17. }
  18. QUERY
  19. end
  20. let(:failing_configuration) do
  21. {
  22. 'adapter' => 'imap',
  23. 'host' => 'nonexisting.host.local',
  24. 'port' => 993,
  25. 'ssl' => 'ssl',
  26. 'user' => 'some@example.com',
  27. 'password' => 'password',
  28. 'folder' => 'some_folder',
  29. 'sslVerify' => false,
  30. }
  31. end
  32. let(:variables) { { 'inboundConfiguration' => failing_configuration } }
  33. let(:probe_full_response) { nil }
  34. let(:error) { nil }
  35. before do
  36. allow(EmailHelper::Probe).to receive(:inbound).and_return(probe_full_response) if probe_full_response
  37. allow_any_instance_of(Channel::Driver::Imap).to receive(:check_configuration).and_raise(error) if error
  38. gql.execute(query, variables: variables)
  39. end
  40. context 'when authenticated as admin', authenticated_as: :admin do
  41. let(:admin) { create(:admin) }
  42. context 'with successful probe' do
  43. let(:probe_full_response) { { result: 'ok', content_messages: 23 } }
  44. let(:expected_result) do
  45. {
  46. 'success' => true,
  47. 'mailboxStats' => {
  48. 'contentMessages' => 23,
  49. },
  50. 'errors' => nil,
  51. }
  52. end
  53. it 'finds configuration data' do
  54. expect(gql.result.data).to eq(expected_result)
  55. end
  56. end
  57. context 'with failed probe' do
  58. let(:error) { SocketError.new('getaddrinfo: nodename nor servname provided, or not known') }
  59. let(:expected_result) do
  60. {
  61. 'success' => false,
  62. 'mailboxStats' => nil,
  63. 'errors' => [{ 'field' => 'inbound.host', 'message' => 'The hostname could not be found.' }],
  64. }
  65. end
  66. it 'returns error messages' do
  67. expect(gql.result.data).to eq(expected_result)
  68. end
  69. end
  70. end
  71. context 'when authenticated as non-admin', authenticated_as: :agent do
  72. let(:agent) { create(:agent) }
  73. it 'fails with authentication error' do
  74. expect(gql.result.error_type).to eq(Exceptions::Forbidden)
  75. end
  76. end
  77. it_behaves_like 'graphql responds with error if unauthenticated'
  78. end