gitlab_spec.rb 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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', 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, 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. it 'does query objects' do
  66. params = {
  67. links: [ issue_link ],
  68. }
  69. authenticated_as(agent)
  70. instance = instance_double(GitLab)
  71. expect(GitLab).to receive(:new).and_return instance
  72. expect(instance).to receive(:issues_by_urls).and_return(
  73. {
  74. issues: [issue_data],
  75. url_replacements: []
  76. }
  77. )
  78. expect(instance).to receive(:fix_urls_for_ticket)
  79. post '/api/v1/integration/gitlab', params: params, as: :json
  80. expect(response).to have_http_status(:ok)
  81. expect(json_response).to be_a(Hash)
  82. expect(json_response).not_to be_blank
  83. expect(json_response['result']).to eq('ok')
  84. expect(json_response['response']).to eq([issue_data.deep_stringify_keys])
  85. end
  86. it 'does save ticket issues' do
  87. ticket = create(:ticket, group: Group.first)
  88. params = {
  89. ticket_id: ticket.id,
  90. issue_links: [ issue_link ],
  91. }
  92. authenticated_as(agent)
  93. post '/api/v1/integration/gitlab_ticket_update', params: params, as: :json
  94. expect(response).to have_http_status(:ok)
  95. expect(json_response).to be_a(Hash)
  96. expect(json_response).not_to be_blank
  97. expect(json_response['result']).to eq('ok')
  98. expect(ticket.reload.preferences[:gitlab][:issue_links]).to eq(params[:issue_links])
  99. end
  100. context 'with SSL verification support' do
  101. describe '.verify' do
  102. def request
  103. params = {
  104. endpoint: endpoint,
  105. api_token: token,
  106. verify_ssl: verify_ssl,
  107. }
  108. authenticated_as(admin)
  109. post '/api/v1/integration/gitlab/verify', params: params, as: :json
  110. expect(response).to have_http_status(:ok)
  111. end
  112. it 'does verify SSL' do
  113. allow(UserAgent).to receive(:get_http)
  114. request
  115. expect(UserAgent).to have_received(:get_http).with(URI::HTTPS, hash_including(verify_ssl: true)).once
  116. end
  117. context 'with SSL verification turned off' do
  118. let(:verify_ssl) { false }
  119. it 'does not verify SSL' do
  120. allow(UserAgent).to receive(:get_http)
  121. request
  122. expect(UserAgent).to have_received(:get_http).with(URI::HTTPS, hash_including(verify_ssl: false)).once
  123. end
  124. end
  125. end
  126. describe '.query' do
  127. before do
  128. Setting.set('gitlab_integration', true)
  129. Setting.set('gitlab_config', {
  130. endpoint: endpoint,
  131. api_token: token,
  132. verify_ssl: verify_ssl,
  133. })
  134. end
  135. def request
  136. params = {
  137. links: [ issue_link ],
  138. }
  139. authenticated_as(agent)
  140. post '/api/v1/integration/gitlab', params: params, as: :json
  141. expect(response).to have_http_status(:ok)
  142. end
  143. it 'does verify SSL' do
  144. allow(UserAgent).to receive(:get_http)
  145. request
  146. expect(UserAgent).to have_received(:get_http).with(URI::HTTPS, hash_including(verify_ssl: true)).once
  147. end
  148. context 'with SSL verification turned off' do
  149. let(:verify_ssl) { false }
  150. it 'does not verify SSL' do
  151. allow(UserAgent).to receive(:get_http)
  152. request
  153. expect(UserAgent).to have_received(:get_http).with(URI::HTTPS, hash_including(verify_ssl: false)).once
  154. end
  155. end
  156. end
  157. end
  158. end
  159. end
  160. # rubocop:enable RSpec/StubbedMock,RSpec/MessageSpies