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

config_service_spec.rb « static_site_editor « services « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fed373828a107e5c069c6523d09dddfbf72555fe (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
118
119
120
121
122
123
124
125
126
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe StaticSiteEditor::ConfigService do
  let_it_be(:project) { create(:project, :repository) }
  let_it_be(:user) { create(:user) }

  # params
  let(:ref) { 'master' }
  let(:path) { 'README.md' }
  let(:return_url) { double(:return_url) }

  # stub data
  let(:generated_data) { { generated: true } }
  let(:file_data) { { file: true } }

  describe '#execute' do
    subject(:execute) do
      described_class.new(
        container: project,
        current_user: user,
        params: {
          ref: ref,
          path: path,
          return_url: return_url
        }
      ).execute
    end

    context 'when insufficient permission' do
      it 'returns an error' do
        expect(execute).to be_error
        expect(execute.message).to eq('Insufficient permissions to read configuration')
      end
    end

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

        allow_next_instance_of(Gitlab::StaticSiteEditor::Config::GeneratedConfig) do |config|
          allow(config).to receive(:data) { generated_data }
        end
      end

      context 'when reading file from repo fails with an unexpected error' do
        let(:unexpected_error) { RuntimeError.new('some unexpected error') }

        before do
          allow(project.repository).to receive(:blob_data_at).and_raise(unexpected_error)
        end

        it 'returns an error response' do
          expect(Gitlab::ErrorTracking).to receive(:track_and_raise_exception).with(unexpected_error).and_call_original
          expect { execute }.to raise_error(unexpected_error)
        end
      end

      context 'when file is missing' do
        before do
          allow(project.repository).to receive(:blob_data_at).and_raise(GRPC::NotFound)
          expect_next_instance_of(Gitlab::StaticSiteEditor::Config::FileConfig, '{}') do |config|
            allow(config).to receive(:valid?) { true }
            allow(config).to receive(:to_hash_with_defaults) { file_data }
          end
        end

        it 'returns default config' do
          expect(execute).to be_success
          expect(execute.payload).to eq(generated: true, file: true)
        end
      end

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

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

          before do
            expect_next_instance_of(Gitlab::StaticSiteEditor::Config::FileConfig, config_content) do |config|
              error = 'error'
              allow(config).to receive_message_chain('errors.first') { error }
              allow(config).to receive(:valid?) { false }
            end
          end

          it 'returns an error' do
            expect(execute).to be_error
            expect(execute.message).to eq('Invalid configuration format')
          end
        end

        context 'and configuration is valid' do
          # NOTE: This has to be a valid config, even though it is mocked, because
          #       `expect_next_instance_of` executes the constructor logic.
          let(:config_content) { 'static_site_generator: middleman' }

          before do
            expect_next_instance_of(Gitlab::StaticSiteEditor::Config::FileConfig, config_content) do |config|
              allow(config).to receive(:valid?) { true }
              allow(config).to receive(:to_hash_with_defaults) { file_data }
            end
          end

          it 'returns merged generated data and config file data' do
            expect(execute).to be_success
            expect(execute.payload).to eq(generated: true, file: true)
          end

          it 'returns an error if any keys would be overwritten by the merge' do
            generated_data[:duplicate_key] = true
            file_data[:duplicate_key] = true
            expect(execute).to be_error
            expect(execute.message).to match(/duplicate key.*duplicate_key.*found/i)
          end
        end
      end
    end
  end
end