summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/background_migration/move_personal_snippet_files_spec.rb
blob: ee60e498b5991aa711b7c96094f1e24dcd5f8a56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
require 'spec_helper'

describe Gitlab::BackgroundMigration::MovePersonalSnippetFiles do
  let(:test_dir) { File.join(Rails.root, 'tmp', 'tests', 'move_snippet_files_test') }
  let(:old_uploads_dir) { File.join('uploads', 'system', 'personal_snippet') }
  let(:new_uploads_dir) { File.join('uploads', '-', 'system', 'personal_snippet') }
  let(:snippet) do
    snippet = create(:personal_snippet)
    create_upload_for_snippet(snippet)
    snippet.update_attributes!(description: markdown_linking_file(snippet))
    snippet
  end

  let(:migration) { described_class.new }

  before do
    allow(migration).to receive(:base_directory) { test_dir }
  end

  describe '#perform' do
    it 'moves the file on the disk'  do
      expected_path = File.join(test_dir, new_uploads_dir, snippet.id.to_s, "secret#{snippet.id}", 'upload.txt')

      migration.perform(old_uploads_dir, new_uploads_dir)

      expect(File.exist?(expected_path)).to be_truthy
    end

    it 'updates the markdown of the snippet' do
      expected_path = File.join(new_uploads_dir, snippet.id.to_s, "secret#{snippet.id}", 'upload.txt')
      expected_markdown = "[an upload](#{expected_path})"

      migration.perform(old_uploads_dir, new_uploads_dir)

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

    it 'updates the markdown of notes' do
      expected_path = File.join(new_uploads_dir, snippet.id.to_s, "secret#{snippet.id}", 'upload.txt')
      expected_markdown = "with [an upload](#{expected_path})"

      note = create(:note_on_personal_snippet, noteable: snippet, note: "with #{markdown_linking_file(snippet)}")

      migration.perform(old_uploads_dir, new_uploads_dir)

      expect(note.reload.note).to eq(expected_markdown)
    end
  end

  def create_upload_for_snippet(snippet)
    snippet_path = path_for_file_in_snippet(snippet)
    path = File.join(old_uploads_dir, snippet.id.to_s, snippet_path)
    absolute_path = File.join(test_dir, path)

    FileUtils.mkdir_p(File.dirname(absolute_path))
    FileUtils.touch(absolute_path)

    create(:upload, model: snippet, path: snippet_path, uploader: PersonalFileUploader)
  end

  def path_for_file_in_snippet(snippet)
    secret = "secret#{snippet.id}"
    filename = 'upload.txt'

    File.join(secret, filename)
  end

  def markdown_linking_file(snippet)
    path = File.join(old_uploads_dir, snippet.id.to_s, path_for_file_in_snippet(snippet))
    "[an upload](#{path})"
  end
end