summaryrefslogtreecommitdiff
path: root/spec/services/ci/web_ide_config_service_spec.rb
blob: 7522103ccb7585f370ed53bae12b7cde80257672 (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
# frozen_string_literal: true

require 'spec_helper'

describe Ci::WebIdeConfigService do
  let_it_be(:project) { create(:project, :repository) }
  let_it_be(:user) { create(:user) }
  let(:sha) { 'sha' }

  describe '#execute' do
    subject { described_class.new(project, user, sha: sha).execute }

    context 'when insufficient permission' do
      it 'returns an error' do
        is_expected.to include(
          status: :error,
          message: 'Insufficient permissions to read configuration')
      end
    end

    context 'for developer' do
      before do
        project.add_developer(user)
      end

      context 'when file is missing' do
        it 'returns an error' do
          is_expected.to include(
            status: :error,
            message: "Failed to load Web IDE config file '.gitlab/.gitlab-webide.yml' for sha")
        end
      end

      context 'when file is present' do
        before do
          allow(project.repository).to receive(:blob_data_at).with('sha', anything) do
            config_content
          end
        end

        context 'content is not valid' do
          let(:config_content) { 'invalid content' }

          it 'returns an error' do
            is_expected.to include(
              status: :error,
              message: "Invalid configuration format")
          end
        end

        context 'content is valid, but terminal not defined' do
          let(:config_content) { '{}' }

          it 'returns success' do
            is_expected.to include(
              status: :success,
              terminal: nil)
          end
        end

        context 'content is valid, with enabled terminal' do
          let(:config_content) { 'terminal: {}' }

          it 'returns success' do
            is_expected.to include(
              status: :success,
              terminal: {
                tag_list: [],
                yaml_variables: [],
                options: { script: ["sleep 60"] }
              })
          end
        end

        context 'content is valid, with custom terminal' do
          let(:config_content) { 'terminal: { before_script: [ls] }' }

          it 'returns success' do
            is_expected.to include(
              status: :success,
              terminal: {
                tag_list: [],
                yaml_variables: [],
                options: { before_script: ["ls"], script: ["sleep 60"] }
              })
          end
        end
      end
    end
  end
end