123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
- require 'rails_helper'
- # rubocop:disable RSpec/StubbedMock,RSpec/MessageSpies
- RSpec.describe 'GitLab', type: :request do
- let(:token) { 't0k3N' }
- let(:endpoint) { 'https://git.example.com/api/graphql' }
- let(:verify_ssl) { true }
- let(:issue_link) { 'https://git.example.com/project/repo/-/issues/1' }
- let!(:admin) do
- create(:admin, groups: Group.all)
- end
- let!(:agent) do
- create(:agent, groups: Group.all)
- end
- let(:issue_data) do
- {
- id: '1',
- title: 'Example issue',
- url: issue_link,
- icon_state: 'open',
- milestone: 'important milestone',
- assignees: ['zammad-robot'],
- labels: [
- {
- color: '#FF0000',
- text_color: '#FFFFFF',
- title: 'critical'
- },
- {
- color: '#0033CC',
- text_color: '#FFFFFF',
- title: 'label1'
- },
- {
- color: '#D1D100',
- text_color: '#FFFFFF',
- title: 'special'
- }
- ],
- }
- end
- describe 'request handling' do
- it 'does verify integration' do
- params = {
- endpoint: endpoint,
- api_token: token,
- verify_ssl: verify_ssl
- }
- authenticated_as(agent)
- post '/api/v1/integration/gitlab/verify', params: params, as: :json
- expect(response).to have_http_status(:forbidden)
- expect(json_response).to be_a(Hash)
- expect(json_response).not_to be_blank
- expect(json_response['error']).to eq('Not authorized (user)!')
- authenticated_as(admin)
- instance = instance_double(GitLab)
- expect(GitLab).to receive(:new).with(endpoint, token, verify_ssl: verify_ssl).and_return instance
- expect(instance).to receive(:verify!).and_return(true)
- post '/api/v1/integration/gitlab/verify', params: params, as: :json
- expect(response).to have_http_status(:ok)
- expect(json_response).to be_a(Hash)
- expect(json_response).not_to be_blank
- expect(json_response['result']).to eq('ok')
- end
- it 'does query objects' do
- params = {
- links: [ issue_link ],
- }
- authenticated_as(agent)
- instance = instance_double(GitLab)
- expect(GitLab).to receive(:new).and_return instance
- expect(instance).to receive(:issues_by_urls).and_return([issue_data])
- post '/api/v1/integration/gitlab', params: params, as: :json
- expect(response).to have_http_status(:ok)
- expect(json_response).to be_a(Hash)
- expect(json_response).not_to be_blank
- expect(json_response['result']).to eq('ok')
- expect(json_response['response']).to eq([issue_data.deep_stringify_keys])
- end
- it 'does save ticket issues' do
- ticket = create(:ticket, group: Group.first)
- params = {
- ticket_id: ticket.id,
- issue_links: [ issue_link ],
- }
- authenticated_as(agent)
- post '/api/v1/integration/gitlab_ticket_update', params: params, as: :json
- expect(response).to have_http_status(:ok)
- expect(json_response).to be_a(Hash)
- expect(json_response).not_to be_blank
- expect(json_response['result']).to eq('ok')
- expect(ticket.reload.preferences[:gitlab][:issue_links]).to eq(params[:issue_links])
- end
- context 'with SSL verification support' do
- describe '.verify' do
- def request
- params = {
- endpoint: endpoint,
- api_token: token,
- verify_ssl: verify_ssl,
- }
- authenticated_as(admin)
- post '/api/v1/integration/gitlab/verify', params: params, as: :json
- expect(response).to have_http_status(:ok)
- end
- it 'does verify SSL' do
- allow(UserAgent).to receive(:get_http)
- request
- expect(UserAgent).to have_received(:get_http).with(URI::HTTPS, hash_including(verify_ssl: true)).once
- end
- context 'with SSL verification turned off' do
- let(:verify_ssl) { false }
- it 'does not verify SSL' do
- allow(UserAgent).to receive(:get_http)
- request
- expect(UserAgent).to have_received(:get_http).with(URI::HTTPS, hash_including(verify_ssl: false)).once
- end
- end
- end
- describe '.query' do
- before do
- Setting.set('gitlab_integration', true)
- Setting.set('gitlab_config', {
- endpoint: endpoint,
- api_token: token,
- verify_ssl: verify_ssl,
- })
- end
- def request
- params = {
- links: [ issue_link ],
- }
- authenticated_as(agent)
- post '/api/v1/integration/gitlab', params: params, as: :json
- expect(response).to have_http_status(:ok)
- end
- it 'does verify SSL' do
- allow(UserAgent).to receive(:get_http)
- request
- expect(UserAgent).to have_received(:get_http).with(URI::HTTPS, hash_including(verify_ssl: true)).once
- end
- context 'with SSL verification turned off' do
- let(:verify_ssl) { false }
- it 'does not verify SSL' do
- allow(UserAgent).to receive(:get_http)
- request
- expect(UserAgent).to have_received(:get_http).with(URI::HTTPS, hash_including(verify_ssl: false)).once
- end
- end
- end
- end
- end
- end
- # rubocop:enable RSpec/StubbedMock,RSpec/MessageSpies
|