guess_configuration_spec.rb 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. # Copyright (C) 2012-2024 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. archivePossible
  30. archiveWeekRange
  31. }
  32. }
  33. }
  34. }
  35. QUERY
  36. end
  37. let(:variables) { { emailAddress: 'admin@example.com', password: '1234' } }
  38. let(:probe_full_response) { { result: 'failed' } }
  39. before do
  40. allow(EmailHelper::Probe).to receive(:full).and_return(probe_full_response)
  41. gql.execute(query, variables: variables)
  42. end
  43. context 'when authenticated as admin', authenticated_as: :admin do
  44. let(:admin) { create(:admin) }
  45. context 'with successful probe' do
  46. let(:probe_full_response) do
  47. # Example from function documentation
  48. {
  49. result: 'ok',
  50. content_messages: 23,
  51. archive_possible: true,
  52. archive_week_range: 2,
  53. setting: {
  54. inbound: {
  55. adapter: 'imap',
  56. options: {
  57. host: 'imap.gmail.com',
  58. port: 993,
  59. ssl: probe_ssl,
  60. start_tls: probe_starttls,
  61. user: 'some@example.com',
  62. password: 'password',
  63. folder: 'some_folder',
  64. ssl_verify: false,
  65. },
  66. },
  67. outbound: {
  68. adapter: 'smtp',
  69. options: {
  70. host: 'smtp.gmail.com',
  71. port: 25,
  72. user: 'some@example.com',
  73. password: 'password',
  74. ssl_verify: false,
  75. },
  76. },
  77. },
  78. }
  79. end
  80. let(:expected_result) do
  81. {
  82. 'result' => {
  83. 'mailboxStats' => {
  84. 'contentMessages' => 23,
  85. 'archivePossible' => true,
  86. 'archiveWeekRange' => 2,
  87. },
  88. 'inboundConfiguration' => {
  89. 'adapter' => 'imap',
  90. 'host' => 'imap.gmail.com',
  91. 'port' => 993,
  92. 'ssl' => expected_ssl,
  93. 'user' => 'some@example.com',
  94. 'password' => 'password',
  95. 'folder' => 'some_folder',
  96. 'sslVerify' => false,
  97. },
  98. 'outboundConfiguration' => {
  99. 'adapter' => 'smtp',
  100. 'host' => 'smtp.gmail.com',
  101. 'port' => 25,
  102. 'user' => 'some@example.com',
  103. 'password' => 'password',
  104. 'sslVerify' => false,
  105. }
  106. }
  107. }
  108. end
  109. context 'when both SSL and STARTTLS are off' do
  110. let(:probe_ssl) { false }
  111. let(:probe_starttls) { false }
  112. let(:expected_ssl) { 'off' }
  113. it 'finds configuration data' do
  114. expect(gql.result.data).to eq(expected_result)
  115. end
  116. end
  117. context 'when both STARTTLS is on' do
  118. let(:probe_ssl) { false }
  119. let(:probe_starttls) { true }
  120. let(:expected_ssl) { 'starttls' }
  121. it 'finds configuration data' do
  122. expect(gql.result.data).to eq(expected_result)
  123. end
  124. end
  125. context 'when both SSL is on' do
  126. let(:probe_ssl) { true }
  127. let(:probe_starttls) { false }
  128. let(:expected_ssl) { 'ssl' }
  129. it 'finds configuration data' do
  130. expect(gql.result.data).to eq(expected_result)
  131. end
  132. end
  133. context 'when both SSL and STARTTLS are on' do
  134. let(:probe_ssl) { true }
  135. let(:probe_starttls) { true }
  136. let(:expected_ssl) { 'starttls' }
  137. it 'finds configuration data' do
  138. expect(gql.result.data).to eq(expected_result)
  139. end
  140. end
  141. end
  142. context 'with failed probe' do
  143. let(:probe_full_response) do
  144. {
  145. result: 'failed',
  146. }
  147. end
  148. let(:expected_result) do
  149. {
  150. 'result' => {
  151. 'mailboxStats' => nil,
  152. 'inboundConfiguration' => nil,
  153. 'outboundConfiguration' => nil,
  154. }
  155. }
  156. end
  157. it 'finds configuration data' do
  158. expect(gql.result.data).to eq(expected_result)
  159. end
  160. end
  161. end
  162. context 'when authenticated as non-admin', authenticated_as: :agent do
  163. let(:agent) { create(:agent) }
  164. it 'fails with authentication error' do
  165. expect(gql.result.error_type).to eq(Exceptions::Forbidden)
  166. end
  167. end
  168. it_behaves_like 'graphql responds with error if unauthenticated'
  169. end