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

snippets_repository_pipeline_spec.rb « pipelines « projects « bulk_imports « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 85946c5e0f95d2d738a561eda1f223d4be2fd2fe (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe BulkImports::Projects::Pipelines::SnippetsRepositoryPipeline, feature_category: :importers do
  let(:user) { create(:user) }
  let(:project) { create(:project) }
  let(:bulk_import) { create(:bulk_import, user: user) }
  let(:bulk_import_configuration) { create(:bulk_import_configuration, bulk_import: bulk_import) }
  let!(:matched_snippet) { create(:snippet, project: project, created_at: "1981-12-13T23:59:59Z") }
  let(:entity) do
    create(
      :bulk_import_entity,
      :project_entity,
      project: project,
      bulk_import: bulk_import_configuration.bulk_import,
      source_full_path: 'source/full/path',
      destination_slug: 'My-Destination-Project',
      destination_namespace: project.full_path
    )
  end

  let(:tracker) { create(:bulk_import_tracker, entity: entity) }
  let(:context) { BulkImports::Pipeline::Context.new(tracker) }

  subject(:pipeline) { described_class.new(context) }

  let(:http_url_to_repo) { 'https://example.com/foo/bar/snippets/42.git' }
  let(:data) do
    [
      {
        'title' => matched_snippet.title,
        'httpUrlToRepo' => http_url_to_repo,
        'createdAt' => matched_snippet.created_at.to_s
      }
    ]
  end

  let(:page_info) do
    {
      'next_page' => 'eyJpZCI6IjIyMDA2OTYifQ',
      'has_next_page' => false
    }
  end

  let(:extracted_data) { BulkImports::Pipeline::ExtractedData.new(data: data, page_info: page_info) }

  describe 'extractor' do
    it 'is a GraphqlExtractor with Graphql::GetSnippetRepositoryQuery' do
      expect(described_class.get_extractor).to eq(
        klass: BulkImports::Common::Extractors::GraphqlExtractor,
        options: {
          query: BulkImports::Projects::Graphql::GetSnippetRepositoryQuery
        })
    end
  end

  describe '#run', :clean_gitlab_redis_cache do
    let(:validation_response) { double(Hash, 'error?': false) }

    before do
      allow_next_instance_of(BulkImports::Common::Extractors::GraphqlExtractor) do |extractor|
        allow(extractor).to receive(:extract).and_return(extracted_data)
      end

      allow_next_instance_of(Snippets::RepositoryValidationService) do |repository_validation|
        allow(repository_validation).to receive(:execute).and_return(validation_response)
      end
    end

    shared_examples 'skippable snippet' do
      it 'does not create snippet repo' do
        pipeline.run

        expect(Gitlab::GlRepository::SNIPPET.repository_for(matched_snippet).exists?).to be false
      end
    end

    context 'when a snippet is not matched' do
      let(:data) do
        [
          {
            'title' => 'unmatched title',
            'httpUrlToRepo' => http_url_to_repo,
            'createdAt' => matched_snippet.created_at.to_s
          }
        ]
      end

      it_behaves_like 'skippable snippet'
    end

    context 'when httpUrlToRepo is empty' do
      let(:data) do
        [
          {
            'title' => matched_snippet.title,
            'createdAt' => matched_snippet.created_at.to_s
          }
        ]
      end

      it_behaves_like 'skippable snippet'
    end

    context 'when a snippet matches' do
      context 'when snippet url is valid' do
        it 'creates snippet repo' do
          expect { pipeline.run }
            .to change { Gitlab::GlRepository::SNIPPET.repository_for(matched_snippet).exists? }.to true
        end

        it 'skips already cached snippets' do
          pipeline.run

          data.first.tap { |d| d['createdAt'] = matched_snippet.created_at.to_s } # Reset data to original state

          expect(pipeline).not_to receive(:load)

          pipeline.run

          expect(Gitlab::GlRepository::SNIPPET.repository_for(matched_snippet).exists?).to be true
        end

        it 'updates snippets statistics' do
          allow_next_instance_of(Repository) do |repository|
            allow(repository).to receive(:fetch_as_mirror)
          end

          service = double(Snippets::UpdateStatisticsService)

          expect(Snippets::UpdateStatisticsService).to receive(:new).with(kind_of(Snippet)).and_return(service)
          expect(service).to receive(:execute)

          pipeline.run
        end

        it 'fetches snippet repo from url' do
          expect_next_instance_of(Repository) do |repository|
            expect(repository)
              .to receive(:fetch_as_mirror)
              .with("https://oauth2:#{bulk_import_configuration.access_token}@example.com/foo/bar/snippets/42.git")
          end

          pipeline.run
        end
      end

      context 'when url is invalid' do
        context 'when not a real URL' do
          let(:http_url_to_repo) { 'http://0.0.0.0' }

          it_behaves_like 'skippable snippet'
        end

        context 'when scheme is blocked' do
          let(:http_url_to_repo) { 'file://example.com/foo/bar/snippets/42.git' }

          it_behaves_like 'skippable snippet'

          it 'logs the failure' do
            pipeline.run

            expect(tracker.entity.failures.first).to be_present
            expect(tracker.entity.failures.first.exception_message).to eq('Only allowed schemes are http, https')
          end
        end
      end

      context 'when snippet is invalid' do
        let(:validation_response) { double(Hash, 'error?': true) }

        before do
          allow_next_instance_of(Repository) do |repository|
            allow(repository).to receive(:fetch_as_mirror)
          end
        end

        it 'does not leave a hanging SnippetRepository behind' do
          pipeline.run

          expect(SnippetRepository.where(snippet_id: matched_snippet.id).exists?).to be false
        end

        it 'does not call UpdateStatisticsService' do
          expect(Snippets::UpdateStatisticsService).not_to receive(:new)

          pipeline.run
        end

        it_behaves_like 'skippable snippet'
      end
    end
  end
end