summaryrefslogtreecommitdiff
path: root/spec/controllers/jira_connect/events_controller_spec.rb
blob: 8a07f69e480823f5203c2c7c61dacf1468ac2cdc (plain)
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe JiraConnect::EventsController do
  describe '#installed' do
    let(:client_key) { '1234' }
    let(:shared_secret) { 'secret' }
    let(:params) do
      {
        clientKey: client_key,
        sharedSecret: shared_secret,
        baseUrl: 'https://test.atlassian.net'
      }
    end

    subject do
      post :installed, params: params
    end

    it 'saves the jira installation data' do
      expect { subject }.to change { JiraConnectInstallation.count }.by(1)
    end

    it 'saves the correct values' do
      subject

      installation = JiraConnectInstallation.find_by_client_key(client_key)

      expect(installation.shared_secret).to eq(shared_secret)
      expect(installation.base_url).to eq('https://test.atlassian.net')
    end

    context 'client key already exists' do
      it 'returns 422' do
        create(:jira_connect_installation, client_key: client_key)

        subject

        expect(response).to have_gitlab_http_status(:unprocessable_entity)
      end
    end

    context 'when it is a version update and shared_secret is not sent' do
      let(:params) do
        {
          clientKey: client_key,
          baseUrl: 'https://test.atlassian.net'
        }
      end

      it 'validates the JWT token in authorization header and returns 200 without creating a new installation' do
        create(:jira_connect_installation, client_key: client_key, shared_secret: shared_secret)
        request.headers["Authorization"] = "Bearer #{Atlassian::Jwt.encode({ iss: client_key }, shared_secret)}"

        expect { subject }.not_to change { JiraConnectInstallation.count }
        expect(response).to have_gitlab_http_status(:ok)
      end
    end

    describe '#uninstalled' do
      let!(:installation) { create(:jira_connect_installation) }
      let(:qsh) { Atlassian::Jwt.create_query_string_hash('https://gitlab.test/events/uninstalled', 'POST', 'https://gitlab.test') }

      before do
        request.headers['Authorization'] = "JWT #{auth_token}"
      end

      subject { post :uninstalled }

      context 'when JWT is invalid' do
        let(:auth_token) { 'invalid_token' }

        it 'returns 403' do
          subject

          expect(response).to have_gitlab_http_status(:forbidden)
        end

        it 'does not delete the installation' do
          expect { subject }.not_to change { JiraConnectInstallation.count }
        end
      end

      context 'when JWT is valid' do
        let(:auth_token) do
          Atlassian::Jwt.encode({ iss: installation.client_key, qsh: qsh }, installation.shared_secret)
        end

        it 'deletes the installation' do
          expect { subject }.to change { JiraConnectInstallation.count }.by(-1)
        end
      end
    end
  end
end