summaryrefslogtreecommitdiff
path: root/spec/requests/api/error_tracking_collector_spec.rb
blob: 35d3ea01f876ec6a6ae2899cce2a725fcad14d92 (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
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
139
140
141
142
143
144
145
146
147
148
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe API::ErrorTrackingCollector do
  let_it_be(:project) { create(:project, :private) }
  let_it_be(:setting) { create(:project_error_tracking_setting, :integrated, project: project) }
  let_it_be(:client_key) { create(:error_tracking_client_key, project: project) }

  RSpec.shared_examples 'not found' do
    it 'reponds with 404' do
      subject

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

  RSpec.shared_examples 'bad request' do
    it 'responds with 400' do
      subject

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

  RSpec.shared_examples 'successful request' do
    it 'writes to the database and returns no content' do
      expect { subject }.to change { ErrorTracking::ErrorEvent.count }.by(1)

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

  describe "POST /error_tracking/collector/api/:id/envelope" do
    let_it_be(:raw_event) { fixture_file('error_tracking/event.txt') }
    let_it_be(:url) { "/error_tracking/collector/api/#{project.id}/envelope" }

    let(:params) { raw_event }
    let(:headers) { { 'X-Sentry-Auth' => "Sentry sentry_key=#{client_key.public_key}" } }

    subject { post api(url), params: params, headers: headers }

    context 'error tracking feature is disabled' do
      before do
        setting.update!(enabled: false)
      end

      it_behaves_like 'not found'
    end

    context 'integrated error tracking is disabled' do
      before do
        setting.update!(integrated: false)
      end

      it_behaves_like 'not found'
    end

    context 'auth headers are missing' do
      let(:headers) { {} }

      it_behaves_like 'bad request'
    end

    context 'public key is wrong' do
      let(:headers) { { 'X-Sentry-Auth' => "Sentry sentry_key=glet_1fedb514e17f4b958435093deb02048c" } }

      it_behaves_like 'not found'
    end

    context 'public key is inactive' do
      let(:client_key) { create(:error_tracking_client_key, :disabled, project: project) }

      it_behaves_like 'not found'
    end

    context 'empty body' do
      let(:params) { '' }

      it_behaves_like 'bad request'
    end

    context 'unknown request type' do
      let(:params) { fixture_file('error_tracking/unknown.txt') }

      it_behaves_like 'bad request'
    end

    context 'transaction request type' do
      let(:params) { fixture_file('error_tracking/transaction.txt') }

      it 'does nothing and returns no content' do
        expect { subject }.not_to change { ErrorTracking::ErrorEvent.count }

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

    it_behaves_like 'successful request'
  end

  describe "POST /error_tracking/collector/api/:id/store" do
    let_it_be(:raw_event) { fixture_file('error_tracking/parsed_event.json') }
    let_it_be(:url) { "/error_tracking/collector/api/#{project.id}/store" }

    let(:params) { raw_event }
    let(:headers) { { 'X-Sentry-Auth' => "Sentry sentry_key=#{client_key.public_key}" } }

    subject { post api(url), params: params, headers: headers }

    it_behaves_like 'successful request'

    context 'empty headers' do
      let(:headers) { {} }

      it_behaves_like 'bad request'
    end

    context 'empty body' do
      let(:params) { '' }

      it_behaves_like 'bad request'
    end

    context 'sentry_key as param and empty headers' do
      let(:url) { "/error_tracking/collector/api/#{project.id}/store?sentry_key=#{sentry_key}" }
      let(:headers) { {} }

      context 'key is wrong' do
        let(:sentry_key) { 'glet_1fedb514e17f4b958435093deb02048c' }

        it_behaves_like 'not found'
      end

      context 'key is empty' do
        let(:sentry_key) { '' }

        it_behaves_like 'bad request'
      end

      context 'key is correct' do
        let(:sentry_key) { client_key.public_key }

        it_behaves_like 'successful request'
      end
    end
  end
end