set_notification_configuration_spec.rb 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Mutations::Channel::Email::SetNotificationConfiguration, type: :graphql do
  4. let(:query) do
  5. <<~QUERY
  6. mutation channelEmailSetNotificationConfiguration($outboundConfiguration: ChannelEmailOutboundConfigurationInput!) {
  7. channelEmailSetNotificationConfiguration(outboundConfiguration: $outboundConfiguration) {
  8. success
  9. errors {
  10. message
  11. field
  12. }
  13. }
  14. }
  15. QUERY
  16. end
  17. let(:smtp_configuration) do
  18. {
  19. adapter: 'smtp',
  20. host: 'smtp.example.com',
  21. port: 25,
  22. user: 'some@example.com',
  23. password: 'password',
  24. sslVerify: false,
  25. }
  26. end
  27. let(:variables) { { 'outboundConfiguration' => smtp_configuration } }
  28. before do
  29. Setting.set('system_online_service', system_online_service) if defined?(system_online_service)
  30. gql.execute(query, variables: variables)
  31. end
  32. context 'when authenticated as admin', authenticated_as: :admin do
  33. let(:admin) { create(:admin) }
  34. it 'returns success' do
  35. expect(gql.result.data).to include({ 'success' => true })
  36. end
  37. it 'sets smtp to active and updates configuration' do
  38. expect(channel_by_adapter('smtp')).to have_attributes(
  39. active: true,
  40. status_out: 'ok',
  41. last_log_out: nil,
  42. options: include(
  43. outbound: include(
  44. adapter: 'smtp',
  45. options: include(
  46. host: 'smtp.example.com',
  47. port: 25,
  48. user: 'some@example.com',
  49. password: 'password',
  50. ssl_verify: false,
  51. )
  52. )
  53. )
  54. )
  55. end
  56. context 'when runs in a hosted environment' do
  57. let(:system_online_service) { true }
  58. it 'fails with authentication error' do
  59. expect(gql.result.error_type).to eq(Exceptions::Forbidden)
  60. end
  61. end
  62. end
  63. context 'when authenticated as non-admin', authenticated_as: :agent do
  64. let(:agent) { create(:agent) }
  65. it 'fails with authentication error' do
  66. expect(gql.result.error_type).to eq(Exceptions::Forbidden)
  67. end
  68. end
  69. it_behaves_like 'graphql responds with error if unauthenticated'
  70. def channel_by_adapter(adapter)
  71. Channel
  72. .where(area: 'Email::Notification')
  73. .to_a
  74. .find { _1.options.dig(:outbound, :adapter) == adapter }
  75. end
  76. end