Welcome to mirror list, hosted at ThFree Co, Russian Federation.

config_resolver_spec.rb « ci « resolvers « graphql « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dc030b1313b7a161fa2bb47934cc914e9dd67e64 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Resolvers::Ci::ConfigResolver do
  include GraphqlHelpers

  describe '#resolve' do
    let_it_be(:user) { create(:user) }
    let_it_be(:project) { create(:project, :repository) }
    let_it_be(:sha) { nil }

    let_it_be(:content) do
      File.read(Rails.root.join('spec/support/gitlab_stubs/gitlab_ci_includes.yml'))
    end

    subject(:response) do
      resolve(described_class,
              args: { project_path: project.full_path, content: content, sha: sha },
              ctx:  { current_user: user })
    end

    shared_examples 'a valid config file' do
      let(:fake_result) do
        ::Gitlab::Ci::Lint::Result.new(
          merged_yaml: content,
          jobs: [],
          errors: [],
          warnings: [],
          includes: []
        )
      end

      it 'lints the ci config file and returns the merged yaml file' do
        expect(response[:status]).to eq(:valid)
        expect(response[:merged_yaml]).to eq(content)
        expect(response[:includes]).to eq([])
        expect(response[:errors]).to be_empty
        expect(::Gitlab::Ci::Lint).to have_received(:new).with(current_user: user, project: project, sha: sha)
      end
    end

    context 'when the user can create a pipeline' do
      let(:ci_lint) do
        ci_lint_double = instance_double(::Gitlab::Ci::Lint)
        allow(ci_lint_double).to receive(:validate).and_return(fake_result)

        ci_lint_double
      end

      before do
        allow(::Gitlab::Ci::Lint).to receive(:new).and_return(ci_lint)

        project.add_developer(user)
      end

      context 'with a valid .gitlab-ci.yml' do
        context 'with a sha' do
          let(:sha) { '1231231' }

          it_behaves_like 'a valid config file'
        end

        context 'without a sha' do
          it_behaves_like 'a valid config file'
        end
      end

      context 'with an invalid .gitlab-ci.yml' do
        let(:content) { 'invalid' }

        let(:fake_result) do
          Gitlab::Ci::Lint::Result.new(
            jobs: [],
            merged_yaml: content,
            errors: ['Invalid configuration format'],
            warnings: [],
            includes: []
          )
        end

        it 'responds with errors about invalid syntax' do
          expect(response[:status]).to eq(:invalid)
          expect(response[:errors]).to match_array(['Invalid configuration format'])
        end
      end

      context 'with an invalid SHA' do
        let_it_be(:sha) { ':' }

        let(:ci_lint) do
          ci_lint_double = instance_double(::Gitlab::Ci::Lint)
          allow(ci_lint_double).to receive(:validate).and_raise(GRPC::InvalidArgument)

          ci_lint_double
        end

        it 'logs the invalid SHA to Sentry' do
          expect(Gitlab::ErrorTracking).to receive(:track_and_raise_exception)
            .with(GRPC::InvalidArgument, sha: ':')

          response
        end
      end
    end

    context 'when the user cannot create a pipeline' do
      before do
        project.add_guest(user)
      end

      it 'returns an error stating that the user cannot access the linting' do
        expect(response).to be_instance_of(::Gitlab::Graphql::Errors::ResourceNotAvailable)
      end
    end
  end
end