jobs_spec.rb 13.5 KB
Newer Older
1 2
require 'spec_helper'

3
describe API::Jobs, api: true do
4 5 6
  include ApiHelpers

  let(:user) { create(:user) }
Kamil Trzcinski's avatar
Kamil Trzcinski committed
7
  let(:api_user) { user }
8
  let!(:project) { create(:project, :repository, creator: user, public_builds: false) }
9
  let!(:developer) { create(:project_member, :developer, user: user, project: project) }
10 11
  let(:reporter) { create(:project_member, :reporter, project: project) }
  let(:guest) { create(:project_member, :guest, project: project) }
12
  let!(:pipeline) { create(:ci_empty_pipeline, project: project, sha: project.commit.id, ref: project.default_branch) }
13
  let!(:build) { create(:ci_build, pipeline: pipeline) }
14

15 16
  describe 'GET /projects/:id/jobs' do
    let(:query) { Hash.new }
Kamil Trzcinski's avatar
Kamil Trzcinski committed
17

Lin Jen-Shin's avatar
Lin Jen-Shin committed
18
    before do
19
      get api("/projects/#{project.id}/jobs", api_user), query
Lin Jen-Shin's avatar
Lin Jen-Shin committed
20
    end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
21

22
    context 'authorized user' do
23
      it 'returns project jobs' do
24
        expect(response).to have_http_status(200)
25
        expect(response).to include_pagination_headers
26 27 28
        expect(json_response).to be_an Array
      end

29 30 31 32 33
      it 'returns correct values' do
        expect(json_response).not_to be_empty
        expect(json_response.first['commit']['id']).to eq project.commit.id
      end

34 35
      it 'returns pipeline data' do
        json_build = json_response.first
36

37 38 39 40 41 42 43
        expect(json_build['pipeline']).not_to be_empty
        expect(json_build['pipeline']['id']).to eq build.pipeline.id
        expect(json_build['pipeline']['ref']).to eq build.pipeline.ref
        expect(json_build['pipeline']['sha']).to eq build.pipeline.sha
        expect(json_build['pipeline']['status']).to eq build.pipeline.status
      end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
44
      context 'filter project with one scope element' do
45
        let(:query) { { 'scope' => 'pending' } }
46

Kamil Trzcinski's avatar
Kamil Trzcinski committed
47
        it do
48
          expect(response).to have_http_status(200)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
49 50
          expect(json_response).to be_an Array
        end
51 52
      end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
53
      context 'filter project with array of scope elements' do
54
        let(:query) { { scope: %w(pending running) } }
55

Kamil Trzcinski's avatar
Kamil Trzcinski committed
56
        it do
57
          expect(response).to have_http_status(200)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
58 59
          expect(json_response).to be_an Array
        end
60
      end
61

Kamil Trzcinski's avatar
Kamil Trzcinski committed
62
      context 'respond 400 when scope contains invalid state' do
63
        let(:query) { { scope: %w(unknown running) } }
64

65
        it { expect(response).to have_http_status(400) }
66
      end
67 68 69
    end

    context 'unauthorized user' do
Kamil Trzcinski's avatar
Kamil Trzcinski committed
70
      let(:api_user) { nil }
71

Valery Sizov's avatar
Valery Sizov committed
72
      it 'does not return project builds' do
73
        expect(response).to have_http_status(401)
74 75 76 77
      end
    end
  end

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
  describe 'GET /projects/:id/pipelines/:pipeline_id/jobs' do
    let(:query) { Hash.new }

    before do
      get api("/projects/#{project.id}/pipelines/#{pipeline.id}/jobs", api_user), query
    end

    context 'authorized user' do
      it 'returns pipeline jobs' do
        expect(response).to have_http_status(200)
        expect(response).to include_pagination_headers
        expect(json_response).to be_an Array
      end

      it 'returns correct values' do
        expect(json_response).not_to be_empty
        expect(json_response.first['commit']['id']).to eq project.commit.id
      end

      it 'returns pipeline data' do
        json_build = json_response.first

        expect(json_build['pipeline']).not_to be_empty
        expect(json_build['pipeline']['id']).to eq build.pipeline.id
        expect(json_build['pipeline']['ref']).to eq build.pipeline.ref
        expect(json_build['pipeline']['sha']).to eq build.pipeline.sha
        expect(json_build['pipeline']['status']).to eq build.pipeline.status
      end

      context 'filter jobs with one scope element' do
        let(:query) { { 'scope' => 'pending' } }

        it do
          expect(response).to have_http_status(200)
          expect(json_response).to be_an Array
        end
      end

      context 'filter jobs with array of scope elements' do
117
        let(:query) { { scope: %w(pending running) } }
118 119 120 121 122 123 124 125

        it do
          expect(response).to have_http_status(200)
          expect(json_response).to be_an Array
        end
      end

      context 'respond 400 when scope contains invalid state' do
126
        let(:query) { { scope: %w(unknown running) } }
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149

        it { expect(response).to have_http_status(400) }
      end

      context 'jobs in different pipelines' do
        let!(:pipeline2) { create(:ci_empty_pipeline, project: project) }
        let!(:build2) { create(:ci_build, pipeline: pipeline2) }

        it 'excludes jobs from other pipelines' do
          json_response.each { |job| expect(job['pipeline']['id']).to eq(pipeline.id) }
        end
      end
    end

    context 'unauthorized user' do
      let(:api_user) { nil }

      it 'does not return jobs' do
        expect(response).to have_http_status(401)
      end
    end
  end

150
  describe 'GET /projects/:id/jobs/:job_id' do
Lin Jen-Shin's avatar
Lin Jen-Shin committed
151
    before do
152
      get api("/projects/#{project.id}/jobs/#{build.id}", api_user)
Lin Jen-Shin's avatar
Lin Jen-Shin committed
153
    end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
154

155
    context 'authorized user' do
Filipa Lacerda's avatar
Filipa Lacerda committed
156
      it 'returns specific job data' do
157
        expect(response).to have_http_status(200)
158 159
        expect(json_response['name']).to eq('test')
      end
160 161 162 163 164 165 166 167 168

      it 'returns pipeline data' do
        json_build = json_response
        expect(json_build['pipeline']).not_to be_empty
        expect(json_build['pipeline']['id']).to eq build.pipeline.id
        expect(json_build['pipeline']['ref']).to eq build.pipeline.ref
        expect(json_build['pipeline']['sha']).to eq build.pipeline.sha
        expect(json_build['pipeline']['status']).to eq build.pipeline.status
      end
169 170 171
    end

    context 'unauthorized user' do
Kamil Trzcinski's avatar
Kamil Trzcinski committed
172
      let(:api_user) { nil }
173

Filipa Lacerda's avatar
Filipa Lacerda committed
174
      it 'does not return specific job data' do
175
        expect(response).to have_http_status(401)
176 177 178 179
      end
    end
  end

180
  describe 'GET /projects/:id/jobs/:job_id/artifacts' do
Lin Jen-Shin's avatar
Lin Jen-Shin committed
181
    before do
182
      get api("/projects/#{project.id}/jobs/#{build.id}/artifacts", api_user)
Lin Jen-Shin's avatar
Lin Jen-Shin committed
183
    end
184

Filipa Lacerda's avatar
Filipa Lacerda committed
185
    context 'job with artifacts' do
186
      let(:build) { create(:ci_build, :artifacts, pipeline: pipeline) }
187

Kamil Trzcinski's avatar
Kamil Trzcinski committed
188 189
      context 'authorized user' do
        let(:download_headers) do
190 191
          { 'Content-Transfer-Encoding' => 'binary',
            'Content-Disposition' => 'attachment; filename=ci_build_artifacts.zip' }
Kamil Trzcinski's avatar
Kamil Trzcinski committed
192
        end
193

Filipa Lacerda's avatar
Filipa Lacerda committed
194
        it 'returns specific job artifacts' do
195
          expect(response).to have_http_status(200)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
196
          expect(response.headers).to include(download_headers)
197
          expect(response.body).to match_file(build.artifacts_file.file.file)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
198
        end
199 200
      end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
201 202
      context 'unauthorized user' do
        let(:api_user) { nil }
203

Filipa Lacerda's avatar
Filipa Lacerda committed
204
        it 'does not return specific job artifacts' do
205
          expect(response).to have_http_status(401)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
206
        end
207 208
      end
    end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
209

Filipa Lacerda's avatar
Filipa Lacerda committed
210
    it 'does not return job artifacts if not uploaded' do
211
      expect(response).to have_http_status(404)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
212
    end
213 214
  end

215
  describe 'GET /projects/:id/artifacts/:ref_name/download?job=name' do
216
    let(:api_user) { reporter.user }
217 218 219 220 221
    let(:build) { create(:ci_build, :artifacts, pipeline: pipeline) }

    before do
      build.success
    end
222

223 224
    def get_for_ref(ref = pipeline.ref, job = build.name)
      get api("/projects/#{project.id}/jobs/artifacts/#{ref}/download", api_user), job: job
225 226
    end

227
    context 'when not logged in' do
228
      let(:api_user) { nil }
229 230

      before do
231
        get_for_ref
232 233
      end

234
      it 'gives 401' do
235 236 237 238
        expect(response).to have_http_status(401)
      end
    end

239
    context 'when logging as guest' do
240
      let(:api_user) { guest.user }
241 242

      before do
243
        get_for_ref
244 245 246 247 248 249 250
      end

      it 'gives 403' do
        expect(response).to have_http_status(403)
      end
    end

Filipa Lacerda's avatar
Filipa Lacerda committed
251
    context 'non-existing job' do
252 253
      shared_examples 'not found' do
        it { expect(response).to have_http_status(:not_found) }
254 255
      end

256 257
      context 'has no such ref' do
        before do
258
          get_for_ref('TAIL')
259 260
        end

261
        it_behaves_like 'not found'
262 263
      end

Filipa Lacerda's avatar
Filipa Lacerda committed
264
      context 'has no such job' do
265
        before do
266
          get_for_ref(pipeline.ref, 'NOBUILD')
267 268
        end

269
        it_behaves_like 'not found'
270
      end
271 272
    end

Filipa Lacerda's avatar
Filipa Lacerda committed
273
    context 'find proper job' do
274
      shared_examples 'a valid file' do
275
        let(:download_headers) do
276 277 278
          { 'Content-Transfer-Encoding' => 'binary',
            'Content-Disposition' =>
              "attachment; filename=#{build.artifacts_file.filename}" }
279
        end
280

281 282
        it { expect(response).to have_http_status(200) }
        it { expect(response.headers).to include(download_headers) }
283 284 285 286
      end

      context 'with regular branch' do
        before do
Kamil Trzcinski's avatar
Kamil Trzcinski committed
287
          pipeline.reload
288 289 290
          pipeline.update(ref: 'master',
                          sha: project.commit('master').sha)

291
          get_for_ref('master')
292 293
        end

294
        it_behaves_like 'a valid file'
295 296
      end

297 298
      context 'with branch name containing slash' do
        before do
Kamil Trzcinski's avatar
Kamil Trzcinski committed
299
          pipeline.reload
300 301 302 303 304
          pipeline.update(ref: 'improve/awesome',
                          sha: project.commit('improve/awesome').sha)
        end

        before do
305
          get_for_ref('improve/awesome')
306 307
        end

308
        it_behaves_like 'a valid file'
309
      end
310 311 312
    end
  end

313
  describe 'GET /projects/:id/jobs/:job_id/trace' do
314
    let(:build) { create(:ci_build, :trace, pipeline: pipeline) }
315

Lin Jen-Shin's avatar
Lin Jen-Shin committed
316
    before do
317
      get api("/projects/#{project.id}/jobs/#{build.id}/trace", api_user)
Lin Jen-Shin's avatar
Lin Jen-Shin committed
318
    end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
319

320
    context 'authorized user' do
Filipa Lacerda's avatar
Filipa Lacerda committed
321
      it 'returns specific job trace' do
322
        expect(response).to have_http_status(200)
323
        expect(response.body).to eq(build.trace.raw)
324 325 326 327
      end
    end

    context 'unauthorized user' do
Kamil Trzcinski's avatar
Kamil Trzcinski committed
328
      let(:api_user) { nil }
329

Filipa Lacerda's avatar
Filipa Lacerda committed
330
      it 'does not return specific job trace' do
331
        expect(response).to have_http_status(401)
332 333 334
      end
    end
  end
335

336
  describe 'POST /projects/:id/jobs/:job_id/cancel' do
Lin Jen-Shin's avatar
Lin Jen-Shin committed
337
    before do
338
      post api("/projects/#{project.id}/jobs/#{build.id}/cancel", api_user)
Lin Jen-Shin's avatar
Lin Jen-Shin committed
339
    end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
340

341
    context 'authorized user' do
342
      context 'user with :update_build persmission' do
Filipa Lacerda's avatar
Filipa Lacerda committed
343
        it 'cancels running or pending job' do
344
          expect(response).to have_http_status(201)
345 346 347 348
          expect(project.builds.first.status).to eq('canceled')
        end
      end

349
      context 'user without :update_build permission' do
350
        let(:api_user) { reporter.user }
351

Filipa Lacerda's avatar
Filipa Lacerda committed
352
        it 'does not cancel job' do
353
          expect(response).to have_http_status(403)
354 355 356 357 358
        end
      end
    end

    context 'unauthorized user' do
Kamil Trzcinski's avatar
Kamil Trzcinski committed
359
      let(:api_user) { nil }
360

Filipa Lacerda's avatar
Filipa Lacerda committed
361
      it 'does not cancel job' do
362
        expect(response).to have_http_status(401)
363 364 365 366
      end
    end
  end

367
  describe 'POST /projects/:id/jobs/:job_id/retry' do
368
    let(:build) { create(:ci_build, :canceled, pipeline: pipeline) }
Kamil Trzcinski's avatar
Kamil Trzcinski committed
369

Lin Jen-Shin's avatar
Lin Jen-Shin committed
370
    before do
371
      post api("/projects/#{project.id}/jobs/#{build.id}/retry", api_user)
Lin Jen-Shin's avatar
Lin Jen-Shin committed
372
    end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
373

374
    context 'authorized user' do
Kamil Trzcinski's avatar
Kamil Trzcinski committed
375
      context 'user with :update_build permission' do
Filipa Lacerda's avatar
Filipa Lacerda committed
376
        it 'retries non-running job' do
377
          expect(response).to have_http_status(201)
378 379 380 381 382
          expect(project.builds.first.status).to eq('canceled')
          expect(json_response['status']).to eq('pending')
        end
      end

383
      context 'user without :update_build permission' do
384
        let(:api_user) { reporter.user }
385

Filipa Lacerda's avatar
Filipa Lacerda committed
386
        it 'does not retry job' do
387
          expect(response).to have_http_status(403)
388 389 390 391 392
        end
      end
    end

    context 'unauthorized user' do
Kamil Trzcinski's avatar
Kamil Trzcinski committed
393
      let(:api_user) { nil }
394

Filipa Lacerda's avatar
Filipa Lacerda committed
395
      it 'does not retry job' do
396
        expect(response).to have_http_status(401)
397 398 399
      end
    end
  end
400

401
  describe 'POST /projects/:id/jobs/:job_id/erase' do
402
    before do
403
      post api("/projects/#{project.id}/jobs/#{build.id}/erase", user)
404 405
    end

Filipa Lacerda's avatar
Filipa Lacerda committed
406
    context 'job is erasable' do
407
      let(:build) { create(:ci_build, :trace, :artifacts, :success, project: project, pipeline: pipeline) }
408

Filipa Lacerda's avatar
Filipa Lacerda committed
409
      it 'erases job content' do
410
        expect(response).to have_http_status(201)
411
        expect(build).not_to have_trace
412 413 414
        expect(build.artifacts_file.exists?).to be_falsy
        expect(build.artifacts_metadata.exists?).to be_falsy
      end
415

Filipa Lacerda's avatar
Filipa Lacerda committed
416
      it 'updates job' do
417 418 419
        build.reload
        expect(build.erased_at).to be_truthy
        expect(build.erased_by).to eq(user)
420
      end
421 422
    end

Filipa Lacerda's avatar
Filipa Lacerda committed
423
    context 'job is not erasable' do
424
      let(:build) { create(:ci_build, :trace, project: project, pipeline: pipeline) }
425

426
      it 'responds with forbidden' do
427
        expect(response).to have_http_status(403)
428 429 430
      end
    end
  end
431

432
  describe 'POST /projects/:id/jobs/:build_id/artifacts/keep' do
433
    before do
434
      post api("/projects/#{project.id}/jobs/#{build.id}/artifacts/keep", user)
435 436 437 438 439 440 441 442
    end

    context 'artifacts did not expire' do
      let(:build) do
        create(:ci_build, :trace, :artifacts, :success,
               project: project, pipeline: pipeline, artifacts_expire_at: Time.now + 7.days)
      end

443
      it 'keeps artifacts' do
444
        expect(response).to have_http_status(200)
445
        expect(build.reload.artifacts_expire_at).to be_nil
446 447 448 449 450 451
      end
    end

    context 'no artifacts' do
      let(:build) { create(:ci_build, project: project, pipeline: pipeline) }

452
      it 'responds with not found' do
453
        expect(response).to have_http_status(404)
454 455 456
      end
    end
  end
457

458
  describe 'POST /projects/:id/jobs/:job_id/play' do
459
    before do
460
      post api("/projects/#{project.id}/jobs/#{build.id}/play", user)
461 462
    end

Filipa Lacerda's avatar
Filipa Lacerda committed
463
    context 'on an playable job' do
464 465
      let(:build) { create(:ci_build, :manual, project: project, pipeline: pipeline) }

Filipa Lacerda's avatar
Filipa Lacerda committed
466
      it 'plays the job' do
467
        expect(response).to have_http_status(200)
468
        expect(json_response['user']['id']).to eq(user.id)
Z.J. van de Weg's avatar
Z.J. van de Weg committed
469
        expect(json_response['id']).to eq(build.id)
470 471 472
      end
    end

Filipa Lacerda's avatar
Filipa Lacerda committed
473
    context 'on a non-playable job' do
474 475
      it 'returns a status code 400, Bad Request' do
        expect(response).to have_http_status 400
Filipa Lacerda's avatar
Filipa Lacerda committed
476
        expect(response.body).to match("Unplayable Job")
477 478 479
      end
    end
  end
480
end