summaryrefslogtreecommitdiff
path: root/spec/features/projects/jobs/permissions_spec.rb
blob: c3c0043a6efc7fe31e840e7649e28a54380f9199 (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Project Jobs Permissions', feature_category: :projects do
  using RSpec::Parameterized::TableSyntax

  let_it_be_with_reload(:group) { create(:group, name: 'some group') }
  let_it_be_with_reload(:project) { create(:project, :repository, namespace: group) }
  let_it_be_with_reload(:pipeline) { create(:ci_empty_pipeline, project: project, sha: project.commit.sha, ref: 'master') }
  let_it_be(:user) { create(:user) }
  let_it_be(:job) { create(:ci_build, :running, :coverage, :trace_artifact, pipeline: pipeline) }

  before do
    sign_in(user)

    project.enable_ci
  end

  describe 'jobs pages' do
    shared_examples 'recent job page details responds with status' do |status|
      before do
        visit project_job_path(project, job)
      end

      it { expect(status_code).to eq(status) }
    end

    shared_examples 'project jobs page responds with status' do |status|
      before do
        visit project_jobs_path(project)
      end

      it { expect(status_code).to eq(status) }
    end

    context 'when public access for jobs is disabled' do
      before do
        project.update!(public_builds: false)
      end

      context 'when user is a guest' do
        before do
          project.add_guest(user)
        end

        it_behaves_like 'recent job page details responds with status', 404
        it_behaves_like 'project jobs page responds with status', 404
      end

      context 'when project is internal' do
        before do
          project.update!(visibility_level: Gitlab::VisibilityLevel::INTERNAL)
        end

        it_behaves_like 'recent job page details responds with status', 404
        it_behaves_like 'project jobs page responds with status', 404
      end
    end

    context 'when public access for jobs is enabled' do
      before do
        project.update!(public_builds: true)
      end

      context 'when user is a guest' do
        before do
          project.add_guest(user)
        end

        it_behaves_like 'recent job page details responds with status', 200
        it_behaves_like 'project jobs page responds with status', 200
      end

      context 'when user is a developer' do
        before do
          project.add_developer(user)
        end

        it_behaves_like 'recent job page details responds with status', 200
        it_behaves_like 'project jobs page responds with status', 200
      end

      context 'when project is internal' do
        before do
          project.update!(visibility_level: Gitlab::VisibilityLevel::INTERNAL)
        end

        it_behaves_like 'recent job page details responds with status', 200 do
          it 'renders job details', :js do
            expect(page).to have_content "Job #{job.name}"
            expect(page).to have_css '.log-line'
          end
        end

        it_behaves_like 'project jobs page responds with status', 200 do
          it 'renders job', :js do
            page.within('[data-testid="jobs-table"]') do
              expect(page).to have_content("##{job.id}")
                .and have_content(job.sha[0..7])
                .and have_content(job.ref)
                .and have_content(job.name)
            end
          end
        end
      end
    end
  end

  describe 'artifacts page' do
    context 'when recent job has artifacts available' do
      before_all do
        archive = fixture_file_upload('spec/fixtures/ci_build_artifacts.zip')

        create(:ci_job_artifact, :archive, file: archive, job: job)
      end

      context 'when public access for jobs is disabled' do
        before do
          project.update!(public_builds: false)
        end

        context 'when user with guest role' do
          before do
            project.add_guest(user)
          end

          it 'responds with 404 status' do
            visit download_project_job_artifacts_path(project, job)

            expect(status_code).to eq(404)
          end
        end

        context 'when user with reporter role' do
          before do
            project.add_reporter(user)
          end

          it 'starts download artifact' do
            visit download_project_job_artifacts_path(project, job)

            expect(status_code).to eq(200)
            expect(page.response_headers['Content-Type']).to eq 'application/zip'
            expect(page.response_headers['Content-Transfer-Encoding']).to eq 'binary'
          end
        end
      end
    end
  end

  context 'with CI_DEBUG_TRACE' do
    let_it_be(:ci_instance_variable) { create(:ci_instance_variable, key: 'CI_DEBUG_TRACE') }

    describe 'trace endpoint' do
      let_it_be(:job) { create(:ci_build, :trace_artifact, pipeline: pipeline) }

      where(:public_builds, :user_project_role, :ci_debug_trace, :expected_status_code) do
        true         | 'developer'      | true  | 200
        true         | 'guest'          | true  | 403
        true         | 'developer'      | false | 200
        true         | 'guest'          | false | 200
        false        | 'developer'      | true  | 200
        false        | 'guest'          | true  | 403
        false        | 'developer'      | false | 200
        false        | 'guest'          | false | 403
      end

      with_them do
        before do
          ci_instance_variable.update!(value: ci_debug_trace)
          project.update!(public_builds: public_builds)
          project.add_role(user, user_project_role)
        end

        it 'renders trace to authorized users' do
          visit trace_project_job_path(project, job)

          expect(status_code).to eq(expected_status_code)
        end
      end
    end

    describe 'raw page' do
      let_it_be(:job) { create(:ci_build, :running, :coverage, :trace_artifact, pipeline: pipeline) }

      where(:public_builds, :user_project_role, :ci_debug_trace, :expected_status_code, :expected_msg) do
        true         | 'developer'      | true  | 200 | nil
        true         | 'guest'          | true  | 403 | 'You must have developer or higher permissions'
        true         | 'developer'      | false | 200 | nil
        true         | 'guest'          | false | 200 | nil
        false        | 'developer'      | true  | 200 | nil
        false        | 'guest'          | true  | 403 | 'You must have developer or higher permissions'
        false        | 'developer'      | false | 200 | nil
        false        | 'guest'          | false | 403 | 'The current user is not authorized to access the job log'
      end

      with_them do
        before do
          ci_instance_variable.update!(value: ci_debug_trace)
          project.update!(public_builds: public_builds)
          project.add_role(user, user_project_role)
        end

        it 'renders raw trace to authorized users' do
          visit raw_project_job_path(project, job)

          expect(status_code).to eq(expected_status_code)
          expect(page).to have_content(expected_msg)
        end
      end
    end
  end

  context 'with CI_DEBUG_SERVICES' do
    let_it_be(:ci_instance_variable) { create(:ci_instance_variable, key: 'CI_DEBUG_SERVICES') }

    describe 'trace endpoint and raw page' do
      let_it_be(:job) { create(:ci_build, :running, :coverage, :trace_artifact, pipeline: pipeline) }

      where(:public_builds, :user_project_role, :ci_debug_services, :expected_status_code, :expected_msg) do
        true         | 'developer'      | true  | 200 | nil
        true         | 'guest'          | true  | 403 | 'You must have developer or higher permissions'
        true         | nil              | true  | 404 | 'Page Not Found Make sure the address is correct'
        true         | 'developer'      | false | 200 | nil
        true         | 'guest'          | false | 200 | nil
        true         | nil              | false | 404 | 'Page Not Found Make sure the address is correct'
        false        | 'developer'      | true  | 200 | nil
        false        | 'guest'          | true  | 403 | 'You must have developer or higher permissions'
        false        | nil              | true  | 404 | 'Page Not Found Make sure the address is correct'
        false        | 'developer'      | false | 200 | nil
        false        | 'guest'          | false | 403 | 'The current user is not authorized to access the job log'
        false        | nil              | false | 404 | 'Page Not Found Make sure the address is correct'
      end

      with_them do
        before do
          ci_instance_variable.update!(value: ci_debug_services)
          project.update!(public_builds: public_builds)
          user_project_role && project.add_role(user, user_project_role)
        end

        it 'renders trace to authorized users' do
          visit trace_project_job_path(project, job)

          expect(status_code).to eq(expected_status_code)
        end

        it 'renders raw trace to authorized users' do
          visit raw_project_job_path(project, job)

          expect(status_code).to eq(expected_status_code)
          expect(page).to have_content(expected_msg)
        end
      end
    end
  end
end