summaryrefslogtreecommitdiff
path: root/spec/controllers/projects/error_tracking/stack_traces_controller_spec.rb
blob: 7c080504c3155ab34e65f8b8752cb77c0044204b (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Projects::ErrorTracking::StackTracesController do
  let_it_be(:project) { create(:project) }
  let_it_be(:user) { create(:user) }

  before do
    sign_in(user)
    project.add_maintainer(user)
  end

  describe 'GET #index' do
    let(:issue_id) { non_existing_record_id }
    let(:issue_stack_trace_service) { spy(:issue_stack_trace_service) }

    subject(:get_stack_trace) do
      get :index, params: { namespace_id: project.namespace, project_id: project, issue_id: issue_id, format: :json }
    end

    before do
      expect(ErrorTracking::IssueLatestEventService)
        .to receive(:new).with(project, user, issue_id: issue_id.to_s)
        .and_return(issue_stack_trace_service)
      expect(issue_stack_trace_service).to receive(:execute).and_return(service_response)

      get_stack_trace
    end

    context 'awaiting data' do
      let(:service_response) { { status: :error, http_status: :no_content }}

      it 'responds with no data' do
        expect(response).to have_gitlab_http_status(:no_content)
      end

      it_behaves_like 'sets the polling header'
    end

    context 'service result is successful' do
      let(:service_response) { { status: :success, latest_event: error_event } }
      let(:error_event) { build(:error_tracking_error_event) }

      it 'responds with success' do
        expect(response).to have_gitlab_http_status(:ok)
      end

      it 'responds with error' do
        expect(response).to match_response_schema('error_tracking/issue_stack_trace')
      end

      it 'highlights stack trace source code' do
        expect(json_response['error']).to eq(
          Gitlab::ErrorTracking::StackTraceHighlightDecorator.decorate(error_event).as_json
        )
      end

      it_behaves_like 'sets the polling header'
    end

    context 'service result is erroneous' do
      let(:error_message) { 'error message' }

      context 'without http_status' do
        let(:service_response) { { status: :error, message: error_message } }

        it 'responds with bad request' do
          expect(response).to have_gitlab_http_status(:bad_request)
        end

        it 'responds with error message' do
          expect(json_response['message']).to eq(error_message)
        end
      end

      context 'with explicit http_status' do
        let(:http_status) { :no_content }
        let(:service_response) { { status: :error, message: error_message, http_status: http_status } }

        it 'responds with custom http status' do
          expect(response).to have_gitlab_http_status(http_status)
        end

        it 'responds with error message' do
          expect(json_response['message']).to eq(error_message)
        end
      end
    end
  end
end