github_spec.rb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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('User authorization failed.')
  52. authenticated_as(admin)
  53. instance = instance_double(GitHub)
  54. expect(GitHub).to receive(:new).with(endpoint: endpoint, api_token: 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. context 'with activated github integration' do
  63. before do
  64. Setting.set('github_integration', true)
  65. Setting.set('github_config', { 'endpoint' => ENV['GITHUB_ENDPOINT'], 'api_token' => ENV['GITHUB_APITOKEN'] })
  66. end
  67. it 'does query objects without ticket id' do
  68. params = {
  69. links: [ ENV['GITHUB_ISSUE_LINK'] ],
  70. }
  71. authenticated_as(agent)
  72. instance = instance_double(GitHub)
  73. expect(GitHub).to receive(:new).and_return instance
  74. expect(instance).to receive(:issues_by_urls).and_return(
  75. {
  76. issues: [issue_data],
  77. url_replacements: []
  78. }
  79. )
  80. post '/api/v1/integration/github', params: params, as: :json
  81. expect(response).to have_http_status(:ok)
  82. expect(json_response).to be_a(Hash)
  83. expect(json_response).not_to be_blank
  84. expect(json_response['result']).to eq('ok')
  85. expect(json_response['response']).to eq([issue_data.deep_stringify_keys])
  86. end
  87. end
  88. it 'does save ticket issues' do
  89. ticket = create(:ticket, group: Group.first)
  90. params = {
  91. ticket_id: ticket.id,
  92. issue_links: [ ENV['GITHUB_ISSUE_LINK'] ],
  93. }
  94. authenticated_as(agent)
  95. post '/api/v1/integration/github_ticket_update', params: params, as: :json
  96. expect(response).to have_http_status(:ok)
  97. expect(json_response).to be_a(Hash)
  98. expect(json_response).not_to be_blank
  99. expect(json_response['result']).to eq('ok')
  100. expect(ticket.reload.preferences[:github][:issue_links]).to eq(params[:issue_links])
  101. end
  102. end
  103. end
  104. # rubocop:enable RSpec/StubbedMock,RSpec/MessageSpies