summaryrefslogtreecommitdiff
path: root/spec/graphql/types/terraform/state_version_type_spec.rb
blob: b015a2045da7a179e8a1fdc67cfb13fe6a1c974e (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe GitlabSchema.types['TerraformStateVersion'] do
  include GraphqlHelpers

  it { expect(described_class.graphql_name).to eq('TerraformStateVersion') }
  it { expect(described_class).to require_graphql_authorizations(:read_terraform_state) }

  describe 'fields' do
    let(:fields) { %i[id created_by_user job download_path serial created_at updated_at] }

    it { expect(described_class).to have_graphql_fields(fields) }

    it { expect(described_class.fields['id'].type).to be_non_null }
    it { expect(described_class.fields['createdByUser'].type).not_to be_non_null }
    it { expect(described_class.fields['job'].type).not_to be_non_null }
    it { expect(described_class.fields['downloadPath'].type).not_to be_non_null }
    it { expect(described_class.fields['serial'].type).not_to be_non_null }
    it { expect(described_class.fields['createdAt'].type).to be_non_null }
    it { expect(described_class.fields['updatedAt'].type).to be_non_null }
  end

  describe 'query' do
    let_it_be(:project) { create(:project) }
    let_it_be(:user) { create(:user) }
    let_it_be(:terraform_state) { create(:terraform_state, :with_version, :locked, project: project) }

    before do
      project.add_developer(user)
    end

    let(:query) do
      <<~GRAPHQL
        query {
          project(fullPath: "#{project.full_path}") {
            terraformState(name: "#{terraform_state.name}") {
              latestVersion {
                id
                job {
                  name
                }
              }
            }
          }
        }
      GRAPHQL
    end

    subject(:execute) { GitlabSchema.execute(query, context: { current_user: user }).as_json }

    shared_examples 'returning latest version' do
      it 'returns latest version of terraform state' do
        expect(execute.dig('data', 'project', 'terraformState', 'latestVersion', 'id')).to eq(
          global_id_of(terraform_state.latest_version)
        )
      end
    end

    it_behaves_like 'returning latest version'

    it 'returns job of the latest version' do
      expect(execute.dig('data', 'project', 'terraformState', 'latestVersion', 'job')).to be_present
    end

    context 'when user cannot read jobs' do
      before do
        allow(Ability).to receive(:allowed?).and_call_original
        allow(Ability).to receive(:allowed?).with(user, :read_commit_status, terraform_state.latest_version).and_return(false)
      end

      it_behaves_like 'returning latest version'

      it 'does not return job of the latest version' do
        expect(execute.dig('data', 'project', 'terraformState', 'latestVersion', 'job')).not_to be_present
      end
    end
  end
end