BigW Consortium Gitlab

uploads_rewriter_spec.rb 1.87 KB
Newer Older
1 2 3 4 5 6 7 8 9
require 'spec_helper'

describe Gitlab::Gfm::UploadsRewriter do
  let(:user) { create(:user) }
  let(:old_project) { create(:project) }
  let(:new_project) { create(:project) }
  let(:rewriter) { described_class.new(text, old_project, user) }

  context 'text contains links to uploads' do
10 11 12 13 14 15
    let(:image_uploader) do
      build(:file_uploader, project: old_project)
    end

    let(:zip_uploader) do
      build(:file_uploader, project: old_project,
16
                            fixture: 'ci_build_artifacts.zip')
17 18 19 20 21
    end

    let(:text) do
      "Text and #{image_uploader.to_markdown} and #{zip_uploader.to_markdown}"
    end
22 23 24

    describe '#rewrite' do
      let!(:new_text) { rewriter.rewrite(new_project) }
25

26 27 28 29 30 31 32
      let(:old_files) { [image_uploader, zip_uploader].map(&:file) }
      let(:new_files) do
        described_class.new(new_text, new_project, user).files
      end

      let(:old_paths) { old_files.map(&:path) }
      let(:new_paths) { new_files.map(&:path) }
33 34

      it 'rewrites content' do
35
        expect(new_text).not_to eq text
36 37 38 39
        expect(new_text.length).to eq text.length
      end

      it 'copies files' do
40
        expect(new_files).to all(exist)
41
        expect(old_paths).not_to match_array new_paths
42 43
        expect(old_paths).to all(include(old_project.path_with_namespace))
        expect(new_paths).to all(include(new_project.path_with_namespace))
44 45 46
      end

      it 'does not remove old files' do
47 48 49 50
        expect(old_files).to all(exist)
      end

      it 'generates a new secret for each file' do
51 52
        expect(new_paths).not_to include image_uploader.secret
        expect(new_paths).not_to include zip_uploader.secret
53 54 55
      end
    end

56 57
    describe '#needs_rewrite?' do
      subject { rewriter.needs_rewrite? }
58 59 60 61 62 63 64 65 66
      it { is_expected.to eq true }
    end

    describe '#files' do
      subject { rewriter.files }
      it { is_expected.to be_an(Array) }
    end
  end
end