validate_configuration_outbound_spec.rb 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Mutations::Channel::Email::ValidateConfigurationOutbound, type: :graphql do
  4. let(:query) do
  5. <<~QUERY
  6. mutation channelEmailValidateConfigurationOutbound($outboundConfiguration: ChannelEmailOutboundConfigurationInput!, $emailAddress: String!) {
  7. channelEmailValidateConfigurationOutbound(outboundConfiguration: $outboundConfiguration, emailAddress: $emailAddress) {
  8. success
  9. errors {
  10. message
  11. field
  12. }
  13. }
  14. }
  15. QUERY
  16. end
  17. let(:failing_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(:variables) { { 'outboundConfiguration' => failing_configuration, emailAddress: 'some.sender@example.com' } }
  28. let(:probe_full_response) { nil }
  29. before do
  30. allow(EmailHelper::Probe).to receive(:outbound).and_return(probe_full_response) if probe_full_response
  31. allow_any_instance_of(Channel::Driver::Smtp).to receive(:deliver).and_raise(Errno::EHOSTUNREACH)
  32. gql.execute(query, variables: variables)
  33. end
  34. context 'when authenticated as admin', authenticated_as: :admin do
  35. let(:admin) { create(:admin) }
  36. context 'with successful probe' do
  37. let(:probe_full_response) { { result: 'ok' } }
  38. it 'finds configuration data' do
  39. expect(gql.result.data).to eq({ 'success' => true, 'errors' => nil })
  40. end
  41. end
  42. context 'with failed probe' do
  43. it 'returns error messages' do
  44. expect(gql.result.data).to eq({ 'success' => false, 'errors' => [{ 'field' => 'outbound.host', 'message' => 'There is no route to this host.' }] })
  45. end
  46. end
  47. end
  48. context 'when authenticated as non-admin', authenticated_as: :agent do
  49. let(:agent) { create(:agent) }
  50. it 'fails with authentication error' do
  51. expect(gql.result.error_type).to eq(Exceptions::Forbidden)
  52. end
  53. end
  54. it_behaves_like 'graphql responds with error if unauthenticated'
  55. end