BigW Consortium Gitlab

file_mover_spec.rb 2.31 KB
Newer Older
1 2 3 4 5
require 'spec_helper'

describe FileMover do
  let(:filename) { 'banana_sample.gif' }
  let(:file) { fixture_file_upload(Rails.root.join('spec', 'fixtures', filename)) }
6 7
  let(:temp_file_path) { File.join('uploads/-/system/temp', 'secret55', filename) }

8
  let(:temp_description) do
9 10
    "test ![banana_sample](/#{temp_file_path}) "\
    "same ![banana_sample](/#{temp_file_path}) "
11
  end
12
  let(:file_path) { File.join('uploads/-/system/personal_snippet', snippet.id.to_s, 'secret55', filename) }
13 14 15 16 17
  let(:snippet) { create(:personal_snippet, description: temp_description) }

  subject { described_class.new(file_path, snippet).execute }

  describe '#execute' do
18 19
    before do
      expect(FileUtils).to receive(:mkdir_p).with(a_string_including(File.dirname(file_path)))
20
      expect(FileUtils).to receive(:move).with(a_string_including(temp_file_path), a_string_including(file_path))
21 22 23 24 25 26 27 28 29 30
      allow_any_instance_of(CarrierWave::SanitizedFile).to receive(:exists?).and_return(true)
      allow_any_instance_of(CarrierWave::SanitizedFile).to receive(:size).and_return(10)
    end

    context 'when move and field update successful' do
      it 'updates the description correctly' do
        subject

        expect(snippet.reload.description)
          .to eq(
31 32
            "test ![banana_sample](/uploads/-/system/personal_snippet/#{snippet.id}/secret55/banana_sample.gif) "\
            "same ![banana_sample](/uploads/-/system/personal_snippet/#{snippet.id}/secret55/banana_sample.gif) "
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
          )
      end

      it 'creates a new update record' do
        expect { subject }.to change { Upload.count }.by(1)
      end
    end

    context 'when update_markdown fails' do
      before do
        expect(FileUtils).to receive(:move).with(a_string_including(file_path), a_string_including(temp_file_path))
      end

      subject { described_class.new(file_path, snippet, :non_existing_field).execute }

      it 'does not update the description' do
        subject
50

51 52
        expect(snippet.reload.description)
          .to eq(
53 54
            "test ![banana_sample](/uploads/-/system/temp/secret55/banana_sample.gif) "\
            "same ![banana_sample](/uploads/-/system/temp/secret55/banana_sample.gif) "
55 56
          )
      end
57

58 59 60
      it 'does not create a new update record' do
        expect { subject }.not_to change { Upload.count }
      end
61 62 63
    end
  end
end