BigW Consortium Gitlab

file_uploader_spec.rb 1.45 KB
Newer Older
1
require 'spec_helper'
2 3

describe FileUploader do
4
  let(:uploader) { described_class.new(build_stubbed(:empty_project)) }
5

6 7 8 9 10 11 12 13 14 15 16 17
  describe '.absolute_path' do
    it 'returns the correct absolute path by building it dynamically' do
      project = build_stubbed(:project)
      upload = double(model: project, path: 'secret/foo.jpg')

      dynamic_segment = project.path_with_namespace

      expect(described_class.absolute_path(upload))
        .to end_with("#{dynamic_segment}/secret/foo.jpg")
    end
  end

18 19 20
  describe 'initialize' do
    it 'generates a secret if none is provided' do
      expect(SecureRandom).to receive(:hex).and_return('secret')
21

22
      uploader = described_class.new(double)
23

24
      expect(uploader.secret).to eq 'secret'
25 26
    end

27 28
    it 'accepts a secret parameter' do
      expect(SecureRandom).not_to receive(:hex)
29

30
      uploader = described_class.new(double, 'secret')
31

32
      expect(uploader.secret).to eq 'secret'
33
    end
34
  end
35 36 37

  describe '#move_to_cache' do
    it 'is true' do
38
      expect(uploader.move_to_cache).to eq(true)
39 40 41 42 43
    end
  end

  describe '#move_to_store' do
    it 'is true' do
44
      expect(uploader.move_to_store).to eq(true)
45 46
    end
  end
47 48 49 50 51 52 53 54 55

  describe '#relative_path' do
    it 'removes the leading dynamic path segment' do
      fixture = Rails.root.join('spec', 'fixtures', 'rails_sample.jpg')
      uploader.store!(fixture_file_upload(fixture))

      expect(uploader.relative_path).to match(/\A\h{32}\/rails_sample.jpg\z/)
    end
  end
56
end