github_spec.rb 3.1 KB

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