summaryrefslogtreecommitdiff
path: root/spec/controllers/projects/settings/operations_controller_spec.rb
blob: 99568ddaa2851ebc8d93b3ac1dcbf15a6dfd5c88 (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# frozen_string_literal: true

require 'spec_helper'

describe Projects::Settings::OperationsController do
  set(:user) { create(:user) }
  set(:project) { create(:project) }

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

  context 'error tracking' do
    describe 'GET #show' do
      it 'renders show template' do
        get :show, params: project_params(project)

        expect(response).to have_gitlab_http_status(:ok)
        expect(response).to render_template(:show)
      end

      context 'with existing setting' do
        let!(:error_tracking_setting) do
          create(:project_error_tracking_setting, project: project)
        end

        it 'loads existing setting' do
          get :show, params: project_params(project)

          expect(controller.helpers.error_tracking_setting)
            .to eq(error_tracking_setting)
        end
      end

      context 'without an existing setting' do
        it 'builds a new setting' do
          get :show, params: project_params(project)

          expect(controller.helpers.error_tracking_setting).to be_new_record
        end
      end

      context 'with insufficient permissions' do
        before do
          project.add_reporter(user)
        end

        it 'renders 404' do
          get :show, params: project_params(project)

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

      context 'as an anonymous user' do
        before do
          sign_out(user)
        end

        it 'redirects to signup page' do
          get :show, params: project_params(project)

          expect(response).to redirect_to(new_user_session_path)
        end
      end
    end

    describe 'PATCH #update' do
      let(:operations_update_service) { spy(:operations_update_service) }
      let(:operations_url) { project_settings_operations_url(project) }

      let(:error_tracking_params) do
        {
          error_tracking_setting_attributes: {
            enabled: '1',
            api_host: 'http://url',
            token: 'token',
            project: {
              slug: 'sentry-project',
              name: 'Sentry Project',
              organization_slug: 'sentry-org',
              organization_name: 'Sentry Org'
            }
          }
        }
      end

      let(:error_tracking_permitted) do
        ActionController::Parameters.new(error_tracking_params).permit!
      end

      context 'format json' do
        context 'when update succeeds' do
          before do
            stub_operations_update_service_returning(status: :success)
          end

          it 'returns success status' do
            patch :update, params: project_params(project, error_tracking_params, format: :json)

            body = Gitlab::Utils.deep_indifferent_access(JSON.parse(response.body))

            expect(body).to eq('status' => 'success', 'message' => 'Your changes have been saved')
          end
        end

        context 'when update fails' do
          before do
            stub_operations_update_service_returning(status: :error, message: 'error message')
          end

          it 'returns error' do
            patch :update, params: project_params(project, error_tracking_params, format: :json)

            body = Gitlab::Utils.deep_indifferent_access(JSON.parse(response.body))

            expect(response).to have_gitlab_http_status(:bad_request)
            expect(body[:message]).not_to be_nil
          end
        end
      end

      context 'with insufficient permissions' do
        before do
          project.add_reporter(user)
        end

        it 'renders 404' do
          patch :update, params: project_params(project)

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

      context 'as an anonymous user' do
        before do
          sign_out(user)
        end

        it 'redirects to signup page' do
          patch :update, params: project_params(project)

          expect(response).to redirect_to(new_user_session_path)
        end
      end
    end

    private

    def stub_operations_update_service_returning(return_value = {})
      expect(::Projects::Operations::UpdateService)
        .to receive(:new).with(project, user, error_tracking_permitted)
        .and_return(operations_update_service)
      expect(operations_update_service).to receive(:execute)
        .and_return(return_value)
    end
  end

  private

  def project_params(project, project_params = {}, other_params = {})
    {
      namespace_id: project.namespace,
      project_id: project,
      project: project_params
    }.merge(other_params)
  end
end