0
  ï    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access this resource.</p>
</body></html>
  E   ˆl–ß=¿Jå2ÛR‚ œPA¸&n41hß�þB7›YÆÚzB§‘é?ŸÄƒq×ŸÃ_�uÐb&=LyZËcõÃÂ  6   ˆl–ßi~” T¾R( 	Å+�nàS�ÿ�þB gáuù]‘ö�üÿÆƒh¹ÅÄÃ …    # Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'

# rubocop:disable RSpec/StubbedMock,RSpec/MessageSpies

RSpec.describe 'GitLab', authenticated_as: :agent, required_envs: %w[GITLAB_ENDPOINT GITLAB_APITOKEN], 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
      }

      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('User authorization failed.')

      authenticated_as(admin)
      instance = instance_double(GitLab)
      expect(GitLab).to receive(:new).with(endpoint: endpoint, api_token: token, verify_ssl: verify_ssl).exactly(2).and_return instance
      expect(instance).to receive(:verify!).exactly(2).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')

      Setting.set('gitlab_config', { 'endpoint' => endpoint, 'api_token' => token })
      post '/api/v1/integration/gitlab/verify', params: params.merge(api_token: SensitiveParamsHelper::SENSITIVE_MASK), 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

    context 'with activated gitlab integration' do
      before do
        Setting.set('gitlab_integration', true)
        Setting.set('gitlab_config', { 'endpoint' => ENV['GITLAB_ENDPOINT'], 'api_token' => ENV['GITLAB_APITOKEN'] })
      end

      it 'does query objects without ticket id' do
        params = {
          links: [ issue_link ],
        }

        instance = instance_double(GitLab)
        expect(GitLab).to receive(:new).and_return instance
        expect(instance).to receive(:issues_by_urls).and_return(
          {
            issues:           [issue_data],
            url_replacements: []
          }
        )

        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
    end

    context 'when agent has no access to the ticket', required