github_spec.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 'GitHub', required_envs: %w[GITHUB_ENDPOINT GITHUB_APITOKEN], type: :request do
  5. let(:token) { 't0k3N' }
  6. let(:endpoint) { 'https://api.github.com/graphql' }
  7. let!(:admin) do
  8. create(:admin, groups: Group.all)
  9. end
  10. let!(:agent) do
  11. create(:agent, groups: Group.all)
  12. end
  13. let(:issue_data) do
  14. {
  15. id: '1575',
  16. title: 'GitHub integration',
  17. url: ENV['GITHUB_ISSUE_LINK'],
  18. icon_state: 'closed',
  19. milestone: '4.0',
  20. assignees: ['Thorsten'],
  21. labels: [
  22. {
  23. color: '#fef2c0',
  24. text_color: '#000000',
  25. title: 'feature backlog'
  26. },
  27. {
  28. color: '#bfdadc',
  29. text_color: '#000000',
  30. title: 'integration'
  31. }
  32. ],
  33. }
  34. end
  35. let(:dummy_schema) do
  36. {
  37. a: :b
  38. }
  39. end
  40. describe 'request handling' do
  41. it 'does verify integration' do
  42. params = {
  43. endpoint: endpoint,
  44. api_token: token,
  45. }
  46. authenticated_as(agent)
  47. post '/api/v1/integration/github/verify', params: params, as: :json
  48. expect(response).to have_http_status(:forbidden)
  49. expect(json_response).to be_a(Hash)
  50. expect(json_response).not_to be_blank
  51. expect(json_response['error']).to eq('Not authorized (user)!')
  52. authenticated_as(admin)
  53. instance = instance_double(GitHub)
  54. expect(GitHub).to receive(:new).with(endpoint, token).and_return instance
  55. expect(instance).to receive(:verify!).and_return(true)
  56. post '/api/v1/integration/github/verify', params: params, as: :json
  57. expect(response).to have_http_status(:ok)
  58. expect(json_response).to be_a(Hash)
  59. expect(json_response).not_to be_blank
  60. expect(json_response['result']).to eq('ok')
  61. end
  62. it 'does query objects' do
  63. params = {
  64. links: [ ENV['GITHUB_ISSUE_LINK'] ],
  65. }
  66. authenticated_as(agent)
  67. instance = instance_double(GitHub)
  68. expect(GitHub).to receive(:new).and_return instance
  69. expect(instance).to receive(:issues_by_urls).and_return([issue_data])
  70. post '/api/v1/integration/github', params: params, as: :json
  71. expect(response).to have_http_status(:ok)
  72. expect(json_response).to be_a(Hash)
  73. expect(json_response).not_to be_blank
  74. expect(json_response['result']).to eq('ok')
  75. expect(json_response['response']).to eq([issue_data.deep_stringify_keys])
  76. end
  77. it 'does save ticket issues' do
  78. ticket = create(:ticket, group: Group.first)
  79. params = {
  80. ticket_id: ticket.id,
  81. issue_links: [ ENV['GITHUB_ISSUE_LINK'] ],
  82. }
  83. authenticated_as(agent)
  84. post '/api/v1/integration/github_ticket_update', params: params, as: :json
  85. expect(response).to have_http_status(:ok)
  86. expect(json_response).to be_a(Hash)
  87. expect(json_response).not_to be_blank
  88. expect(json_response['result']).to eq('ok')
  89. expect(ticket.reload.preferences[:github][:issue_links]).to eq(params[:issue_links])
  90. end
  91. end
  92. end
  93. # rubocop:enable RSpec/StubbedMock,RSpec/MessageSpies