File: import_export_upload_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (130 lines) | stat: -rw-r--r-- 4,384 bytes parent folder | download
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe ImportExportUpload do
  let(:project) { create(:project) }

  subject { described_class.new(project: project) }

  shared_examples 'stores the Import/Export file' do |method|
    it 'stores the import file' do
      subject.public_send("#{method}=", fixture_file_upload('spec/fixtures/project_export.tar.gz'))

      subject.save!

      url = "/uploads/-/system/import_export_upload/#{method}/#{subject.id}/project_export.tar.gz"

      expect(subject.public_send(method).url).to eq(url)
    end
  end

  context 'import' do
    it_behaves_like 'stores the Import/Export file', :import_file
  end

  context 'export' do
    it_behaves_like 'stores the Import/Export file', :export_file
  end

  describe 'associations' do
    it { is_expected.to belong_to(:project) }
    it { is_expected.to belong_to(:group) }
    it { is_expected.to belong_to(:user) }
  end

  describe 'scopes' do
    let_it_be(:upload1) { create(:import_export_upload, export_file: fixture_file_upload('spec/fixtures/project_export.tar.gz')) }
    let_it_be(:upload2) { create(:import_export_upload, export_file: nil) }
    let_it_be(:upload3) { create(:import_export_upload, export_file: fixture_file_upload('spec/fixtures/project_export.tar.gz'), updated_at: 25.hours.ago) }
    let_it_be(:upload4) { create(:import_export_upload, export_file: nil, updated_at: 2.days.ago) }

    describe '.with_export_file' do
      it 'returns uploads with export file' do
        expect(described_class.with_export_file).to contain_exactly(upload1, upload3)
      end
    end

    describe '.updated_before' do
      it 'returns uploads for a specified date' do
        expect(described_class.updated_before(24.hours.ago)).to contain_exactly(upload3, upload4)
      end
    end
  end

  context 'ActiveRecord callbacks' do
    let(:after_save_callbacks) { described_class._save_callbacks.select { |cb| cb.kind == :after } }
    let(:after_commit_callbacks) { described_class._commit_callbacks.select { |cb| cb.kind == :after } }

    def find_callback(callbacks, key)
      callbacks.find { |cb| cb.filter == key }
    end

    it 'export file is stored in after_commit callback' do
      expect(find_callback(after_commit_callbacks, :store_export_file!)).to be_present
      expect(find_callback(after_save_callbacks, :store_export_file!)).to be_nil
    end

    it 'import file is stored in after_save callback' do
      expect(find_callback(after_save_callbacks, :store_import_file!)).to be_present
      expect(find_callback(after_commit_callbacks, :store_import_file!)).to be_nil
    end
  end

  describe 'export file' do
    it '#export_file_exists? returns false' do
      expect(subject.export_file_exists?).to be false
    end

    it '#export_archive_exists? returns false' do
      expect(subject.export_archive_exists?).to be false
    end

    context 'with export' do
      let(:project_with_export) { create(:project, :with_export) }

      subject { described_class.with_export_file.find_by(project: project_with_export) }

      it '#export_file_exists? returns true' do
        expect(subject.export_file_exists?).to be true
      end

      it '#export_archive_exists? returns false' do
        expect(subject.export_archive_exists?).to be true
      end

      context 'when object file does not exist' do
        before do
          subject.export_file.file.delete
        end

        it '#export_file_exists? returns true' do
          expect(subject.export_file_exists?).to be true
        end

        it '#export_archive_exists? returns false' do
          expect(subject.export_archive_exists?).to be false
        end
      end

      context 'when checking object existence raises a error' do
        let(:exception) { Excon::Error::Forbidden.new('not allowed') }

        before do
          file = double
          allow(file).to receive(:exists?).and_raise(exception)
          allow(subject).to receive(:carrierwave_export_file).and_return(file)
        end

        it '#export_file_exists? returns true' do
          expect(subject.export_file_exists?).to be true
        end

        it '#export_archive_exists? returns false' do
          expect(Gitlab::ErrorTracking).to receive(:track_exception).with(exception)
          expect(subject.export_archive_exists?).to be false
        end
      end
    end
  end
end