File: subscriptions_controller_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (138 lines) | stat: -rw-r--r-- 6,234 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe JiraConnect::SubscriptionsController, feature_category: :integrations do
  describe 'GET /-/jira_connect/subscriptions' do
    let_it_be(:installation) { create(:jira_connect_installation, instance_url: 'http://self-managed-gitlab.com') }
    let(:qsh) do
      Atlassian::Jwt.create_query_string_hash('https://gitlab.test/subscriptions', 'GET', 'https://gitlab.test')
    end

    let(:jwt) { Atlassian::Jwt.encode({ iss: installation.client_key, qsh: qsh }, installation.shared_secret) }
    let(:cors_request_headers) { { 'Origin' => 'https://gitlab.com' } }
    let(:path) { '/-/jira_connect/subscriptions' }
    let(:params) { { jwt: jwt } }

    before do
      stub_application_setting(jira_connect_proxy_url: 'https://gitlab.com')
    end

    subject(:content_security_policy) do
      get path, params: params, headers: cors_request_headers

      response.headers['Content-Security-Policy']
    end

    it { is_expected.to include('http://self-managed-gitlab.com/-/jira_connect/') }
    it { is_expected.to include('http://self-managed-gitlab.com/api/') }
    it { is_expected.to include('http://self-managed-gitlab.com/oauth/') }
    it { is_expected.to include('frame-ancestors \'self\' https://*.atlassian.net https://*.jira.com') }

    context 'with additional iframe ancestors' do
      before do
        allow(Gitlab.config.jira_connect).to receive(:additional_iframe_ancestors).and_return(['http://localhost:*', 'http://dev.gitlab.com'])
      end

      it {
        is_expected.to include('frame-ancestors \'self\' https://*.atlassian.net https://*.jira.com http://localhost:* http://dev.gitlab.com')
      }
    end

    context 'with no self-managed instance configured' do
      let_it_be(:installation) { create(:jira_connect_installation, instance_url: '') }

      it { is_expected.not_to include('http://self-managed-gitlab.com/-/jira_connect/') }
      it { is_expected.not_to include('http://self-managed-gitlab.com/api/') }
      it { is_expected.not_to include('http://self-managed-gitlab.com/oauth/') }
    end

    context 'when json format' do
      let(:path) { '/-/jira_connect/subscriptions.json' }

      it 'allows cross-origin requests', :aggregate_failures do
        get path, params: params, headers: cors_request_headers

        expect(response.headers['Access-Control-Allow-Origin']).to eq 'https://gitlab.com'
        expect(response.headers['Access-Control-Allow-Methods']).to eq 'GET, OPTIONS'
        expect(response.headers['Access-Control-Allow-Credentials']).to be_nil
      end
    end
  end

  describe 'OPTIONS /-/jira_connect/subscriptions' do
    let(:cors_request_headers) { { 'Origin' => 'https://gitlab.com', 'access-control-request-method' => 'GET' } }

    before do
      stub_application_setting(jira_connect_proxy_url: 'https://gitlab.com')
    end

    it 'allows cross-origin requests', :aggregate_failures do
      options '/-/jira_connect/subscriptions.json', headers: cors_request_headers

      expect(response.headers['Access-Control-Allow-Origin']).to eq 'https://gitlab.com'
      expect(response.headers['Access-Control-Allow-Methods']).to eq 'GET, OPTIONS'
      expect(response.headers['Access-Control-Allow-Credentials']).to be_nil
    end
  end

  describe 'OPTIONS /-/jira_connect/subscriptions/:id' do
    let(:cors_request_headers) { { 'Origin' => 'https://gitlab.com', 'access-control-request-method' => 'DELETE' } }

    before do
      stub_application_setting(jira_connect_proxy_url: 'https://gitlab.com')
    end

    it 'allows cross-origin requests', :aggregate_failures do
      options '/-/jira_connect/subscriptions/1', headers: cors_request_headers

      expect(response.headers['Access-Control-Allow-Origin']).to eq 'https://gitlab.com'
      expect(response.headers['Access-Control-Allow-Methods']).to eq 'DELETE, OPTIONS'
      expect(response.headers['Access-Control-Allow-Credentials']).to be_nil
    end
  end

  describe 'DELETE /-/jira_connect/subscriptions/:id' do
    let_it_be(:installation) { create(:jira_connect_installation, instance_url: 'http://self-managed-gitlab.com') }
    let_it_be(:subscription) { create(:jira_connect_subscription, installation: installation) }
    let(:stub_service_response) { ::ServiceResponse.success }

    let(:qsh) do
      Atlassian::Jwt.create_query_string_hash('https://gitlab.test/subscriptions', 'GET', 'https://gitlab.test')
    end

    let(:jwt) { Atlassian::Jwt.encode({ iss: installation.client_key, qsh: qsh }, installation.shared_secret) }
    let(:cors_request_headers) { { 'Origin' => 'https://gitlab.com' } }
    let(:params) { { jwt: jwt, format: :json } }

    before do
      stub_application_setting(jira_connect_proxy_url: 'https://gitlab.com')
      allow_next_instance_of(JiraConnectSubscriptions::DestroyService) do |service|
        allow(service).to receive(:execute).and_return(stub_service_response)
      end
    end

    it 'allows cross-origin requests', :aggregate_failures do
      delete "/-/jira_connect/subscriptions/#{subscription.id}", params: params, headers: cors_request_headers

      expect(response.headers['Access-Control-Allow-Origin']).to eq 'https://gitlab.com'
      expect(response.headers['Access-Control-Allow-Methods']).to eq 'DELETE, OPTIONS'
      expect(response.headers['Access-Control-Allow-Credentials']).to be_nil
      expect(response).to have_gitlab_http_status(:ok)
    end

    context 'when the service responds with an error' do
      let(:stub_service_response) { ::ServiceResponse.error(message: 'some error', reason: :unprocessable_entity) }

      it 'rejects request with status-code', :aggregate_failures do
        delete "/-/jira_connect/subscriptions/#{subscription.id}", params: params, headers: cors_request_headers

        expect(response.headers['Access-Control-Allow-Origin']).to eq 'https://gitlab.com'
        expect(response.headers['Access-Control-Allow-Methods']).to eq 'DELETE, OPTIONS'
        expect(response.headers['Access-Control-Allow-Credentials']).to be_nil
        expect(response.body).to eq "{\"error\":\"some error\"}"
        expect(response).to have_gitlab_http_status(:unprocessable_entity)
      end
    end
  end
end