gitlab_spec.rb 5.4 KB

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