personal_file_uploader_spec.rb 2.52 KB
Newer Older
1
2
# frozen_string_literal: true

3
4
5
require 'spec_helper'

describe PersonalFileUploader do
6
7
8
  let(:model) { create(:personal_snippet) }
  let(:uploader) { described_class.new(model) }
  let(:upload) { create(:upload, :personal_snippet_upload) }
9
  let(:secret) { SecureRandom.hex }
10

11
  subject { uploader }
12

13
  shared_examples '#base_dir' do
14
    before do
15
      subject.instance_variable_set(:@secret, secret)
16
    end
17

18
19
    it 'is prefixed with uploads/-/system' do
      allow(uploader).to receive(:file).and_return(double(extension: 'txt', filename: 'file_name'))
20

21
      expect(described_class.base_dir(model)).to eq("uploads/-/system/personal_snippet/#{model.id}")
22
    end
23
24
  end

25
  shared_examples '#to_h' do
26
    before do
27
      subject.instance_variable_set(:@secret, secret)
28
    end
29

30
    it 'is correct' do
31
      allow(uploader).to receive(:file).and_return(double(extension: 'txt', filename: 'file_name'))
32
      expected_url = "/uploads/-/system/personal_snippet/#{model.id}/#{secret}/file_name"
33
34
35
36
37
38
39
40

      expect(uploader.to_h).to eq(
        alt: 'file_name',
        url: expected_url,
        markdown: "[file_name](#{expected_url})"
      )
    end
  end
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
  describe '#upload_paths' do
    it 'builds correct paths for both local and remote storage' do
      paths = uploader.upload_paths('test.jpg')

      expect(paths.first).to match(%r[\h+\/test.jpg])
      expect(paths.second).to match(%r[^personal_snippet\/\d+\/\h+\/test.jpg])
    end
  end

  context 'object_store is LOCAL' do
    it_behaves_like 'builds correct paths',
                    store_dir: %r[uploads/-/system/personal_snippet/\d+/\h+],
                    upload_path: %r[\h+/\S+],
                    absolute_path: %r[#{CarrierWave.root}/uploads/-/system/personal_snippet\/\d+\/\h+\/\S+$]

    it_behaves_like '#base_dir'
    it_behaves_like '#to_h'
  end

  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[\d+/\h+],
                    upload_path: %r[^personal_snippet\/\d+\/\h+\/<filename>]

    it_behaves_like '#base_dir'
    it_behaves_like '#to_h'
  end

76
77
  describe "#migrate!" do
    before do
78
      uploader.store!(fixture_file_upload('spec/fixtures/doc_sample.txt'))
79
80
81
82
83
84
      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
85
end