move_personal_snippets_files_spec.rb 7.13 KB
Newer Older
1 2 3
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20170612071012_move_personal_snippets_files.rb')

4
describe MovePersonalSnippetsFiles, :migration do
5 6 7
  let(:migration) { described_class.new }
  let(:test_dir) { File.join(Rails.root, "tmp", "tests", "move_snippet_files_test") }
  let(:uploads_dir) { File.join(test_dir, 'uploads') }
8
  let(:new_uploads_dir) { File.join(uploads_dir, '-', 'system') }
9

10 11 12 13 14 15 16
  let(:notes) { table(:notes) }
  let(:snippets) { table(:snippets) }
  let(:uploads) { table(:uploads) }

  let(:user) { table(:users).create!(email: 'user@example.com', projects_limit: 10) }
  let(:project) { table(:projects).create!(name: 'gitlab', namespace_id: 1) }

17 18 19 20 21 22 23 24 25
  before do
    allow(CarrierWave).to receive(:root).and_return(test_dir)
    allow(migration).to receive(:base_directory).and_return(test_dir)
    FileUtils.remove_dir(test_dir) if File.directory?(test_dir)
    allow(migration).to receive(:say)
  end

  describe "#up" do
    let(:snippet) do
26
      snippet = snippets.create!(author_id: user.id)
27 28 29 30 31 32
      create_upload('picture.jpg', snippet)
      snippet.update(description: markdown_linking_file('picture.jpg', snippet))
      snippet
    end

    let(:snippet_with_missing_file) do
33
      snippet = snippets.create!(author_id: user.id, project_id: project.id)
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
      create_upload('picture.jpg', snippet, create_file: false)
      snippet.update(description: markdown_linking_file('picture.jpg', snippet))
      snippet
    end

    it 'moves the files' do
      source_path = File.join(uploads_dir, model_file_path('picture.jpg', snippet))
      destination_path = File.join(new_uploads_dir, model_file_path('picture.jpg', snippet))

      migration.up

      expect(File.exist?(source_path)).to be_falsy
      expect(File.exist?(destination_path)).to be_truthy
    end

    describe 'updating the markdown' do
      it 'includes the new path when the file exists' do
        secret = "secret#{snippet.id}"
52
        file_location = "/uploads/-/system/personal_snippet/#{snippet.id}/#{secret}/picture.jpg"
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

        migration.up

        expect(snippet.reload.description).to include(file_location)
      end

      it 'does not update the markdown when the file is missing' do
        secret = "secret#{snippet_with_missing_file.id}"
        file_location = "/uploads/personal_snippet/#{snippet_with_missing_file.id}/#{secret}/picture.jpg"

        migration.up

        expect(snippet_with_missing_file.reload.description).to include(file_location)
      end

      it 'updates the note markdown' do
        secret = "secret#{snippet.id}"
70
        file_location = "/uploads/-/system/personal_snippet/#{snippet.id}/#{secret}/picture.jpg"
71
        markdown = markdown_linking_file('picture.jpg', snippet)
72 73 74 75
        note = notes.create!(noteable_id: snippet.id,
                             noteable_type: Snippet,
                             note: "with #{markdown}",
                             author_id: user.id)
76 77 78 79 80 81 82 83 84 85

        migration.up

        expect(note.reload.note).to include(file_location)
      end
    end
  end

  describe "#down" do
    let(:snippet) do
86
      snippet = snippets.create!(author_id: user.id)
87 88 89 90 91 92
      create_upload('picture.jpg', snippet, in_new_path: true)
      snippet.update(description: markdown_linking_file('picture.jpg', snippet, in_new_path: true))
      snippet
    end

    let(:snippet_with_missing_file) do
93
      snippet = snippets.create!(author_id: user.id)
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
      create_upload('picture.jpg', snippet, create_file: false, in_new_path: true)
      snippet.update(description: markdown_linking_file('picture.jpg', snippet, in_new_path: true))
      snippet
    end

    it 'moves the files' do
      source_path = File.join(new_uploads_dir, model_file_path('picture.jpg', snippet))
      destination_path = File.join(uploads_dir, model_file_path('picture.jpg', snippet))

      migration.down

      expect(File.exist?(source_path)).to be_falsey
      expect(File.exist?(destination_path)).to be_truthy
    end

    describe 'updating the markdown' do
      it 'includes the new path when the file exists' do
        secret = "secret#{snippet.id}"
        file_location = "/uploads/personal_snippet/#{snippet.id}/#{secret}/picture.jpg"

        migration.down

        expect(snippet.reload.description).to include(file_location)
      end

      it 'keeps the markdown as is when the file is missing' do
        secret = "secret#{snippet_with_missing_file.id}"
121
        file_location = "/uploads/-/system/personal_snippet/#{snippet_with_missing_file.id}/#{secret}/picture.jpg"
122 123 124 125 126 127 128 129 130 131

        migration.down

        expect(snippet_with_missing_file.reload.description).to include(file_location)
      end

      it 'updates the note markdown' do
        markdown = markdown_linking_file('picture.jpg', snippet, in_new_path: true)
        secret = "secret#{snippet.id}"
        file_location = "/uploads/personal_snippet/#{snippet.id}/#{secret}/picture.jpg"
132 133 134 135
        note = notes.create!(noteable_id: snippet.id,
                             noteable_type: Snippet,
                             note: "with #{markdown}",
                             author_id: user.id)
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150

        migration.down

        expect(note.reload.note).to include(file_location)
      end
    end
  end

  describe '#update_markdown' do
    it 'escapes sql in the snippet description' do
      migration.instance_variable_set('@source_relative_location', '/uploads/personal_snippet')
      migration.instance_variable_set('@destination_relative_location', '/uploads/system/personal_snippet')

      secret = '123456789'
      filename = 'hello.jpg'
151
      snippet = snippets.create!(author_id: user.id)
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176

      path_before = "/uploads/personal_snippet/#{snippet.id}/#{secret}/#{filename}"
      path_after = "/uploads/system/personal_snippet/#{snippet.id}/#{secret}/#{filename}"
      description_before = "Hello world; ![image](#{path_before})'; select * from users;"
      description_after = "Hello world; ![image](#{path_after})'; select * from users;"

      migration.update_markdown(snippet.id, secret, filename, description_before)

      expect(snippet.reload.description).to eq(description_after)
    end
  end

  def create_upload(filename, snippet, create_file: true, in_new_path: false)
    secret = "secret#{snippet.id}"
    absolute_path = if in_new_path
                      File.join(new_uploads_dir, model_file_path(filename, snippet))
                    else
                      File.join(uploads_dir, model_file_path(filename, snippet))
                    end

    if create_file
      FileUtils.mkdir_p(File.dirname(absolute_path))
      FileUtils.touch(absolute_path)
    end

177 178 179 180 181
    uploads.create!(model_id: snippet.id,
                    model_type: snippet.class,
                    path: "#{secret}/#{filename}",
                    uploader: PersonalFileUploader,
                    size: 100.kilobytes)
182 183 184 185 186
  end

  def markdown_linking_file(filename, snippet, in_new_path: false)
    markdown =  "![#{filename.split('.')[0]}]"
    markdown += '(/uploads'
187
    markdown += '/-/system' if in_new_path
188 189 190 191 192 193 194 195 196 197
    markdown += "/#{model_file_path(filename, snippet)})"
    markdown
  end

  def model_file_path(filename, snippet)
    secret = "secret#{snippet.id}"

    File.join('personal_snippet', snippet.id.to_s, secret, filename)
  end
end