validate_configuration_roundtrip_spec.rb 2.6 KB

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