github_spec.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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, 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(
  70. {
  71. issues: [issue_data],
  72. url_replacements: []
  73. }
  74. )
  75. expect(instance).to receive(:fix_urls_for_ticket)
  76. post '/api/v1/integration/github', params: params, as: :json
  77. expect(response).to have_http_status(:ok)
  78. expect(json_response).to be_a(Hash)
  79. expect(json_response).not_to be_blank
  80. expect(json_response['result']).to eq('ok')
  81. expect(json_response['response']).to eq([issue_data.deep_stringify_keys])
  82. end
  83. it 'does save ticket issues' do
  84. ticket = create(:ticket, group: Group.first)
  85. params = {
  86. ticket_id: ticket.id,
  87. issue_links: [ ENV['GITHUB_ISSUE_LINK'] ],
  88. }
  89. authenticated_as(agent)
  90. post '/api/v1/integration/github_ticket_update', params: params, as: :json
  91. expect(response).to have_http_status(:ok)
  92. expect(json_response).to be_a(Hash)
  93. expect(json_response).not_to be_blank
  94. expect(json_response['result']).to eq('ok')
  95. expect(ticket.reload.preferences[:github][:issue_links]).to eq(params[:issue_links])
  96. end
  97. end
  98. end
  99. # rubocop:enable RSpec/StubbedMock,RSpec/MessageSpies