guess_configuration_spec.rb 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. # Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Mutations::Channel::Email::GuessConfiguration, type: :graphql do
  4. let(:query) do
  5. <<~QUERY
  6. mutation channelEmailGuessConfiguration($emailAddress: String!, $password: String!) {
  7. channelEmailGuessConfiguration(emailAddress: $emailAddress, password: $password) {
  8. result {
  9. inboundConfiguration {
  10. adapter
  11. host
  12. port
  13. ssl
  14. user
  15. password
  16. sslVerify
  17. folder
  18. }
  19. outboundConfiguration {
  20. adapter
  21. host
  22. port
  23. user
  24. password
  25. sslVerify
  26. }
  27. mailboxStats {
  28. contentMessages
  29. }
  30. }
  31. }
  32. }
  33. QUERY
  34. end
  35. let(:variables) { { emailAddress: 'admin@example.com', password: '1234' } }
  36. let(:probe_full_response) { { result: 'failed' } }
  37. before do
  38. allow(EmailHelper::Probe).to receive(:full).and_return(probe_full_response)
  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) do
  45. # Example from function documentation
  46. {
  47. result: 'ok',
  48. content_messages: 23,
  49. setting: {
  50. inbound: {
  51. adapter: 'imap',
  52. options: {
  53. host: 'imap.gmail.com',
  54. port: 993,
  55. ssl: probe_ssl,
  56. start_tls: probe_starttls,
  57. user: 'some@example.com',
  58. password: 'password',
  59. folder: 'some_folder',
  60. ssl_verify: false,
  61. },
  62. },
  63. outbound: {
  64. adapter: 'smtp',
  65. options: {
  66. host: 'smtp.gmail.com',
  67. port: 25,
  68. user: 'some@example.com',
  69. password: 'password',
  70. ssl_verify: false,
  71. },
  72. },
  73. },
  74. }
  75. end
  76. let(:expected_result) do
  77. {
  78. 'result' => {
  79. 'mailboxStats' => {
  80. 'contentMessages' => 23,
  81. },
  82. 'inboundConfiguration' => {
  83. 'adapter' => 'imap',
  84. 'host' => 'imap.gmail.com',
  85. 'port' => 993,
  86. 'ssl' => expected_ssl,
  87. 'user' => 'some@example.com',
  88. 'password' => 'password',
  89. 'folder' => 'some_folder',
  90. 'sslVerify' => false,
  91. },
  92. 'outboundConfiguration' => {
  93. 'adapter' => 'smtp',
  94. 'host' => 'smtp.gmail.com',
  95. 'port' => 25,
  96. 'user' => 'some@example.com',
  97. 'password' => 'password',
  98. 'sslVerify' => false,
  99. }
  100. }
  101. }
  102. end
  103. context 'when both SSL and STARTTLS are off' do
  104. let(:probe_ssl) { false }
  105. let(:probe_starttls) { false }
  106. let(:expected_ssl) { 'off' }
  107. it 'finds configuration data' do
  108. expect(gql.result.data).to eq(expected_result)
  109. end
  110. end
  111. context 'when both STARTTLS is on' do
  112. let(:probe_ssl) { false }
  113. let(:probe_starttls) { true }
  114. let(:expected_ssl) { 'starttls' }
  115. it 'finds configuration data' do
  116. expect(gql.result.data).to eq(expected_result)
  117. end
  118. end
  119. context 'when both SSL is on' do
  120. let(:probe_ssl) { true }
  121. let(:probe_starttls) { false }
  122. let(:expected_ssl) { 'ssl' }
  123. it 'finds configuration data' do
  124. expect(gql.result.data).to eq(expected_result)
  125. end
  126. end
  127. context 'when both SSL and STARTTLS are on' do
  128. let(:probe_ssl) { true }
  129. let(:probe_starttls) { true }
  130. let(:expected_ssl) { 'starttls' }
  131. it 'finds configuration data' do
  132. expect(gql.result.data).to eq(expected_result)
  133. end
  134. end
  135. end
  136. context 'with failed probe' do
  137. let(:probe_full_response) do
  138. {
  139. result: 'failed',
  140. }
  141. end
  142. let(:expected_result) do
  143. {
  144. 'result' => {
  145. 'mailboxStats' => nil,
  146. 'inboundConfiguration' => nil,
  147. 'outboundConfiguration' => nil,
  148. }
  149. }
  150. end
  151. it 'finds configuration data' do
  152. expect(gql.result.data).to eq(expected_result)
  153. end
  154. end
  155. end
  156. context 'when authenticated as non-admin', authenticated_as: :agent do
  157. let(:agent) { create(:agent) }
  158. it 'fails with authentication error' do
  159. expect(gql.result.error_type).to eq(Exceptions::Forbidden)
  160. end
  161. end
  162. it_behaves_like 'graphql responds with error if unauthenticated'
  163. end