summaryrefslogtreecommitdiff
path: root/spec/requests/api/error_tracking_collector_spec.rb
blob: 52d63410e7abbac476f76c5be7a6bfbaa168c9a7 (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
# 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, project: project) }

  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 }

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

    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

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

      it_behaves_like 'not found'
    end

    context 'feature flag is disabled' do
      before do
        stub_feature_flags(integrated_error_tracking: false)
      end

      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 '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
end