gitlab_spec.rb 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. # rubocop:disable RSpec/StubbedMock,RSpec/MessageSpies
  4. RSpec.describe 'GitLab', required_envs: %w[GITLAB_ENDPOINT GITLAB_APITOKEN], type: :request do
  5. let(:token) { 't0k3N' }
  6. let(:endpoint) { 'https://git.example.com/api/graphql' }
  7. let(:verify_ssl) { true }
  8. let(:issue_link) { 'https://git.example.com/project/repo/-/issues/1' }
  9. let!(:admin) do
  10. create(:admin, groups: Group.all)
  11. end
  12. let!(:agent) do
  13. create(:agent, groups: Group.all)
  14. end
  15. let(:issue_data) do
  16. {
  17. id: '1',
  18. title: 'Example issue',
  19. url: issue_link,
  20. icon_state: 'open',
  21. milestone: 'important milestone',
  22. assignees: ['zammad-robot'],
  23. labels: [
  24. {
  25. color: '#FF0000',
  26. text_color: '#FFFFFF',
  27. title: 'critical'
  28. },
  29. {
  30. color: '#0033CC',
  31. text_color: '#FFFFFF',
  32. title: 'label1'
  33. },
  34. {
  35. color: '#D1D100',
  36. text_color: '#FFFFFF',
  37. title: 'special'
  38. }
  39. ],
  40. }
  41. end
  42. describe 'request handling' do
  43. it 'does verify integration' do
  44. params = {
  45. endpoint: endpoint,
  46. api_token: token,
  47. verify_ssl: verify_ssl
  48. }
  49. authenticated_as(agent)
  50. post '/api/v1/integration/gitlab/verify', params: params, as: :json
  51. expect(response).to have_http_status(:forbidden)
  52. expect(json_response).to be_a(Hash)
  53. expect(json_response).not_to be_blank
  54. expect(json_response['error']).to eq('User authorization failed.')
  55. authenticated_as(admin)
  56. instance = instance_double(GitLab)
  57. expect(GitLab).to receive(:new).with(endpoint: endpoint, api_token: token, verify_ssl: verify_ssl).and_return instance
  58. expect(instance).to receive(:verify!).and_return(true)
  59. post '/api/v1/integration/gitlab/verify', params: params, as: :json
  60. expect(response).to have_http_status(:ok)
  61. expect(json_response).to be_a(Hash)
  62. expect(json_response).not_to be_blank
  63. expect(json_response['result']).to eq('ok')
  64. end
  65. context 'with activated gitlab integration' do
  66. before do
  67. Setting.set('gitlab_integration', true)
  68. Setting.set('gitlab_config', { 'endpoint' => ENV['GITLAB_ENDPOINT'], 'api_token' => ENV['GITLAB_APITOKEN'] })
  69. end
  70. it 'does query objects without ticket id' do
  71. params = {
  72. links: [ issue_link ],
  73. }
  74. authenticated_as(agent)
  75. instance = instance_double(GitLab)
  76. expect(GitLab).to receive(:new).and_return instance
  77. expect(instance).to receive(:issues_by_urls).and_return(
  78. {
  79. issues: [issue_data],
  80. url_replacements: []
  81. }
  82. )
  83. post '/api/v1/integration/gitlab', params: params, as: :json
  84. expect(response).to have_http_status(:ok)
  85. expect(json_response).to be_a(Hash)
  86. expect(json_response).not_to be_blank
  87. expect(json_response['result']).to eq('ok')
  88. expect(json_response['response']).to eq([issue_data.deep_stringify_keys])
  89. end
  90. end
  91. it 'does save ticket issues' do
  92. ticket = create(:ticket, group: Group.first)
  93. params = {
  94. ticket_id: ticket.id,
  95. issue_links: [ issue_link ],
  96. }
  97. authenticated_as(agent)
  98. post '/api/v1/integration/gitlab_ticket_update', params: params, as: :json
  99. expect(response).to have_http_status(:ok)
  100. expect(json_response).to be_a(Hash)
  101. expect(json_response).not_to be_blank
  102. expect(json_response['result']).to eq('ok')
  103. expect(ticket.reload.preferences[:gitlab][:issue_links]).to eq(params[:issue_links])
  104. end
  105. context 'with SSL verification support' do
  106. describe '.verify' do
  107. def request
  108. params = {
  109. endpoint: endpoint,
  110. api_token: token,
  111. verify_ssl: verify_ssl,
  112. }
  113. authenticated_as(admin)
  114. post '/api/v1/integration/gitlab/verify', params: params, as: :json
  115. expect(response).to have_http_status(:ok)
  116. end
  117. it 'does verify SSL' do
  118. allow(UserAgent).to receive(:get_http)
  119. request
  120. expect(UserAgent).to have_received(:get_http).with(URI::HTTPS, hash_including(verify_ssl: true)).once
  121. end
  122. context 'with SSL verification turned off' do
  123. let(:verify_ssl) { false }
  124. it 'does not verify SSL' do
  125. allow(UserAgent).to receive(:get_http)
  126. request
  127. expect(UserAgent).to have_received(:get_http).with(URI::HTTPS, hash_including(verify_ssl: false)).once
  128. end
  129. end
  130. end
  131. describe '.query' do
  132. before do
  133. Setting.set('gitlab_integration', true)
  134. Setting.set('gitlab_config', {
  135. endpoint: endpoint,
  136. api_token: token,
  137. verify_ssl: verify_ssl,
  138. })
  139. end
  140. def request
  141. params = {
  142. links: [ issue_link ],
  143. }
  144. authenticated_as(agent)
  145. post '/api/v1/integration/gitlab', params: params, as: :json
  146. expect(response).to have_http_status(:ok)
  147. end
  148. it 'does verify SSL' do
  149. allow(UserAgent).to receive(:get_http)
  150. request
  151. expect(UserAgent).to have_received(:get_http).with(URI::HTTPS, hash_including(verify_ssl: true)).once
  152. end
  153. context 'with SSL verification turned off' do
  154. let(:verify_ssl) { false }
  155. it 'does not verify SSL' do
  156. allow(UserAgent).to receive(:get_http)
  157. request
  158. expect(UserAgent).to have_received(:get_http).with(URI::HTTPS, hash_including(verify_ssl: false)).once
  159. end
  160. end
  161. end
  162. end
  163. end
  164. end
  165. # rubocop:enable RSpec/StubbedMock,RSpec/MessageSpies