summaryrefslogtreecommitdiff
path: root/spec/requests/api/graphql/ci/job_spec.rb
blob: b0514a0a9633d8e55bb52b0ca978726a7114f5d3 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Query.project(fullPath).pipelines.job(id)' do
  include GraphqlHelpers

  around do |example|
    travel_to(Time.current) { example.run }
  end

  let_it_be(:user) { create_default(:user) }
  let_it_be(:project) { create(:project, :repository, :public) }
  let_it_be(:pipeline) { create(:ci_pipeline, project: project) }

  let_it_be(:prepare_stage) { create(:ci_stage_entity, pipeline: pipeline, project: project, name: 'prepare') }
  let_it_be(:test_stage) { create(:ci_stage_entity, pipeline: pipeline, project: project, name: 'test') }

  let_it_be(:job_1) { create(:ci_build, pipeline: pipeline, stage: 'prepare', name: 'Job 1') }
  let_it_be(:job_2) { create(:ci_build, pipeline: pipeline, stage: 'test', name: 'Job 2') }
  let_it_be(:job_3) { create(:ci_build, pipeline: pipeline, stage: 'test', name: 'Job 3') }

  let(:path_to_job) do
    [
      [:project,   { full_path: project.full_path }],
      [:pipelines, { first: 1 }],
      [:nodes,     nil],
      [:job,       { id: global_id_of(job_2) }]
    ]
  end

  let(:query) do
    wrap_fields(query_graphql_path(query_path, all_graphql_fields_for(terminal_type)))
  end

  describe 'scalar fields' do
    let(:path) { [:project, :pipelines, :nodes, 0, :job] }
    let(:query_path) { path_to_job }
    let(:terminal_type) { 'CiJob' }

    it 'retrieves scalar fields' do
      job_2.update!(
        created_at: 40.seconds.ago,
        queued_at: 32.seconds.ago,
        started_at: 30.seconds.ago,
        finished_at: 5.seconds.ago
      )
      post_graphql(query, current_user: user)

      expect(graphql_data_at(*path)).to match a_hash_including(
        'id' => global_id_of(job_2),
        'name' => job_2.name,
        'allowFailure' => job_2.allow_failure,
        'duration' => 25,
        'queuedDuration' => 2.0,
        'status' => job_2.status.upcase
      )
    end

    context 'when fetching by name' do
      before do
        query_path.last[1] = { name: job_2.name }
      end

      it 'retrieves scalar fields' do
        post_graphql(query, current_user: user)

        expect(graphql_data_at(*path)).to match a_hash_including(
          'id' => global_id_of(job_2),
          'name' => job_2.name
        )
      end
    end
  end

  describe '.detailedStatus' do
    let(:path) { [:project, :pipelines, :nodes, 0, :job, :detailed_status] }
    let(:query_path) { path_to_job + [:detailed_status] }
    let(:terminal_type) { 'DetailedStatus' }

    it 'retrieves detailed status' do
      post_graphql(query, current_user: user)

      expect(graphql_data_at(*path)).to match a_hash_including(
        'text' => 'pending',
        'label' => 'pending',
        'action' => a_hash_including('buttonTitle' => 'Cancel this job', 'icon' => 'cancel')
      )
    end
  end

  describe '.stage' do
    let(:path) { [:project, :pipelines, :nodes, 0, :job, :stage] }
    let(:query_path) { path_to_job + [:stage] }
    let(:terminal_type) { 'CiStage' }

    it 'returns appropriate data' do
      post_graphql(query, current_user: user)

      expect(graphql_data_at(*path)).to match a_hash_including(
        'name' => test_stage.name,
        'jobs' => a_hash_including(
          'nodes' => contain_exactly(
            a_hash_including('id' => global_id_of(job_2)),
            a_hash_including('id' => global_id_of(job_3))
          )
        )
      )
    end
  end
end