summaryrefslogtreecommitdiff
path: root/spec/uploaders/namespace_file_uploader_spec.rb
blob: d09725ee4be4c64f793f8817f5a7524ba4593ea8 (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
require 'spec_helper'

IDENTIFIER = %r{\h+/\S+}

describe NamespaceFileUploader do
  let(:group) { build_stubbed(:group) }
  let(:uploader) { described_class.new(group) }
  let(:upload) { create(:upload, :namespace_upload, model: group) }

  subject { uploader }

  it_behaves_like 'builds correct paths',
                  store_dir: %r[uploads/-/system/namespace/\d+],
                  upload_path: IDENTIFIER,
                  absolute_path: %r[#{CarrierWave.root}/uploads/-/system/namespace/\d+/#{IDENTIFIER}]

  context "object_store is REMOTE" do
    before do
      stub_uploads_object_storage
    end

    include_context 'with storage', described_class::Store::REMOTE

    it_behaves_like 'builds correct paths',
                    store_dir: %r[namespace/\d+/\h+],
                    upload_path: IDENTIFIER
  end

  context '.base_dir' do
    it 'returns local storage base_dir without store param' do
      expect(described_class.base_dir(group)).to eq("uploads/-/system/namespace/#{group.id}")
    end

    it 'returns local storage base_dir when store param is Store::LOCAL' do
      expect(described_class.base_dir(group, ObjectStorage::Store::LOCAL)).to eq("uploads/-/system/namespace/#{group.id}")
    end

    it 'returns remote base_dir when store param is Store::REMOTE' do
      expect(described_class.base_dir(group, ObjectStorage::Store::REMOTE)).to eq("namespace/#{group.id}")
    end
  end

  describe '#workhorse_local_upload_path' do
    it 'returns the correct path in uploads directory' do
      expect(described_class.workhorse_local_upload_path).to end_with('/uploads/tmp/uploads')
    end
  end

  describe "#migrate!" do
    before do
      uploader.store!(fixture_file_upload(File.join('spec/fixtures/doc_sample.txt')))
      stub_uploads_object_storage
    end

    it_behaves_like "migrates", to_store: described_class::Store::REMOTE
    it_behaves_like "migrates", from_store: described_class::Store::REMOTE, to_store: described_class::Store::LOCAL
  end

  describe 'copy_to' do
    let(:group) { create(:group) }
    let(:moved) { described_class.copy_to(subject, group) }

    shared_examples 'returns a valid uploader' do
      it 'generates a new secret' do
        expect(subject).to be
        expect(described_class).to receive(:generate_secret).once.and_call_original
        expect(moved).to be
      end

      it 'creates new upload correctly' do
        upload = moved.upload

        expect(upload).not_to eq(subject.upload)
        expect(upload.model).to eq(group)
        expect(upload.uploader).to eq('NamespaceFileUploader')
        expect(upload.secret).not_to eq(subject.upload.secret)
      end

      it 'copies the file' do
        expect(subject.file).to exist
        expect(moved.file).to exist
        expect(subject.file).not_to eq(moved.file)
        expect(subject.object_store).to eq(moved.object_store)
      end
    end

    context 'files are stored locally' do
      before do
        subject.store!(fixture_file_upload('spec/fixtures/dk.png'))
      end

      include_examples 'returns a valid uploader'

      it 'copies the file to the correct location' do
        expect(moved.upload.path).to eq("#{moved.upload.secret}/dk.png")
        expect(moved.file.path).to end_with("system/namespace/#{group.id}/#{moved.upload.secret}/dk.png")
        expect(moved.filename).to eq('dk.png')
      end
    end

    context 'files are stored remotely' do
      before do
        stub_uploads_object_storage
        subject.store!(fixture_file_upload('spec/fixtures/dk.png'))
        subject.migrate!(ObjectStorage::Store::REMOTE)
      end

      include_examples 'returns a valid uploader'

      it 'copies the file to the correct location' do
        expect(moved.file.path).to eq("namespace/#{group.id}/#{moved.upload.secret}/dk.png")
        expect(moved.filename).to eq('dk.png')
      end
    end
  end
end