gitlab_spec.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. require 'rails_helper'
  2. # rubocop:disable RSpec/StubbedMock,RSpec/MessageSpies
  3. RSpec.describe 'GitLab', type: :request do
  4. let(:token) { 't0k3N' }
  5. let(:endpoint) { 'https://git.example.com/api/graphql' }
  6. let!(:admin) do
  7. create(:admin, groups: Group.all)
  8. end
  9. let!(:agent) do
  10. create(:agent, groups: Group.all)
  11. end
  12. let(:issue_data) do
  13. {
  14. id: '1',
  15. title: 'Example issue',
  16. url: ENV['GITLAB_ISSUE_LINK'],
  17. icon_state: 'open',
  18. milestone: 'important milestone',
  19. assignees: ['zammad-robot'],
  20. labels: [
  21. {
  22. color: '#FF0000',
  23. text_color: '#FFFFFF',
  24. title: 'critical'
  25. },
  26. {
  27. color: '#0033CC',
  28. text_color: '#FFFFFF',
  29. title: 'label1'
  30. },
  31. {
  32. color: '#D1D100',
  33. text_color: '#FFFFFF',
  34. title: 'special'
  35. }
  36. ],
  37. }
  38. end
  39. let(:dummy_schema) do
  40. {
  41. a: :b
  42. }
  43. end
  44. describe 'request handling' do
  45. it 'does verify integration' do
  46. params = {
  47. endpoint: endpoint,
  48. api_token: token,
  49. }
  50. authenticated_as(agent)
  51. post '/api/v1/integration/gitlab/verify', params: params, as: :json
  52. expect(response).to have_http_status(:forbidden)
  53. expect(json_response).to be_a_kind_of(Hash)
  54. expect(json_response).not_to be_blank
  55. expect(json_response['error']).to eq('Not authorized (user)!')
  56. authenticated_as(admin)
  57. instance = instance_double('GitLab')
  58. expect(GitLab).to receive(:new).with(endpoint, token).and_return instance
  59. expect(instance).to receive(:verify!).and_return(true)
  60. post '/api/v1/integration/gitlab/verify', params: params, as: :json
  61. expect(response).to have_http_status(:ok)
  62. expect(json_response).to be_a_kind_of(Hash)
  63. expect(json_response).not_to be_blank
  64. expect(json_response['result']).to eq('ok')
  65. end
  66. it 'does query objects' do
  67. params = {
  68. links: [ ENV['GITLAB_ISSUE_LINK'] ],
  69. }
  70. authenticated_as(agent)
  71. instance = instance_double('GitLab')
  72. expect(GitLab).to receive(:new).and_return instance
  73. expect(instance).to receive(:issues_by_urls).and_return([issue_data])
  74. post '/api/v1/integration/gitlab', params: params, as: :json
  75. expect(response).to have_http_status(:ok)
  76. expect(json_response).to be_a_kind_of(Hash)
  77. expect(json_response).not_to be_blank
  78. expect(json_response['result']).to eq('ok')
  79. expect(json_response['response']).to eq([issue_data.deep_stringify_keys])
  80. end
  81. it 'does save ticket issues' do
  82. ticket = create(:ticket, group: Group.first)
  83. params = {
  84. ticket_id: ticket.id,
  85. issue_links: [ ENV['GITLAB_ISSUE_LINK'] ],
  86. }
  87. authenticated_as(agent)
  88. post '/api/v1/integration/gitlab_ticket_update', params: params, as: :json
  89. expect(response).to have_http_status(:ok)
  90. expect(json_response).to be_a_kind_of(Hash)
  91. expect(json_response).not_to be_blank
  92. expect(json_response['result']).to eq('ok')
  93. expect(ticket.reload.preferences[:gitlab][:issue_links]).to eq(params[:issue_links])
  94. end
  95. end
  96. end
  97. # rubocop:enable RSpec/StubbedMock,RSpec/MessageSpies