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

file_store_mounter_spec.rb « concerns « models « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 459f3d35668f40ec70c2f75d454d76a070631a23 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe FileStoreMounter, :aggregate_failures do
  let(:uploader_class) do
    Class.new do
      def object_store
        :object_store
      end
    end
  end

  let(:test_class) { Class.new { include(FileStoreMounter) } }

  let(:uploader_instance) { uploader_class.new }

  describe '.mount_file_store_uploader' do
    using RSpec::Parameterized::TableSyntax

    subject(:mount_file_store_uploader) do
      test_class.mount_file_store_uploader uploader_class, skip_store_file: skip_store_file, file_field: file_field
    end

    where(:skip_store_file, :file_field) do
      true   | :file
      false  | :file
      false  | :signed_file
      true   | :signed_file
    end

    with_them do
      it 'defines instance methods and registers a callback' do
        expect(test_class).to receive(:mount_uploader).with(file_field, uploader_class)
        expect(test_class).to receive(:define_method).with("update_#{file_field}_store")
        expect(test_class).to receive(:define_method).with("store_#{file_field}_now!")

        if skip_store_file
          expect(test_class).to receive(:skip_callback).with(:save, :after, "store_#{file_field}!".to_sym)
          expect(test_class).not_to receive(:after_save)
        else
          expect(test_class).not_to receive(:skip_callback)
          expect(test_class)
            .to receive(:after_save)
                  .with("update_#{file_field}_store".to_sym, if: "saved_change_to_#{file_field}?".to_sym)
        end

        mount_file_store_uploader
      end
    end

    context 'with an unknown file_field' do
      let(:skip_store_file) { false }
      let(:file_field) { 'unknown' }

      it do
        expect { mount_file_store_uploader }.to raise_error(ArgumentError, 'file_field not allowed: unknown')
      end
    end
  end

  context 'with an instance' do
    let(:instance) { test_class.new }

    before do
      allow(test_class).to receive(:mount_uploader)
      allow(test_class).to receive(:after_save)
      test_class.mount_file_store_uploader uploader_class
    end

    describe '#update_file_store' do
      subject(:update_file_store) { instance.update_file_store }

      it 'calls update column' do
        expect(instance).to receive(:file).and_return(uploader_instance)
        expect(instance).to receive(:update_column).with('file_store', :object_store)

        update_file_store
      end
    end

    describe '#store_file_now!' do
      subject(:store_file_now!) { instance.store_file_now! }

      it 'calls the dynamic functions' do
        expect(instance).to receive(:store_file!)
        expect(instance).to receive(:update_file_store)

        store_file_now!
      end
    end
  end
end