summaryrefslogtreecommitdiff
path: root/spec/migrations/move_uploads_to_system_dir_spec.rb
blob: ca11a2004c556679abd7cb117c0053a0e36f5599 (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
require "spec_helper"
require Rails.root.join("db", "migrate", "20170316163845_move_uploads_to_system_dir.rb")

describe MoveUploadsToSystemDir do
  let(:migration) { described_class.new }
  let(:test_dir) { File.join(Rails.root, "tmp", "move_uploads_test") }
  let(:uploads_dir) { File.join(test_dir, "public", "uploads") }
  let(:new_uploads_dir) { File.join(uploads_dir, "-", "system") }

  before do
    FileUtils.remove_dir(test_dir) if File.directory?(test_dir)
    FileUtils.mkdir_p(uploads_dir)
    allow(migration).to receive(:base_directory).and_return(test_dir)
    allow(migration).to receive(:say)
  end

  describe "#up" do
    before do
      FileUtils.mkdir_p(File.join(uploads_dir, 'user'))
      FileUtils.touch(File.join(uploads_dir, 'user', 'dummy.file'))
    end

    it 'moves the directory to the new path' do
      expected_path = File.join(new_uploads_dir, 'user', 'dummy.file')

      migration.up

      expect(File.exist?(expected_path)).to be(true)
    end

    it 'creates a symlink in the old location' do
      symlink_path = File.join(uploads_dir, 'user')
      expected_path = File.join(symlink_path, 'dummy.file')

      migration.up

      expect(File.exist?(expected_path)).to be(true)
      expect(File.symlink?(symlink_path)).to be(true)
    end
  end

  describe "#down" do
    before do
      FileUtils.mkdir_p(File.join(new_uploads_dir, 'user'))
      FileUtils.touch(File.join(new_uploads_dir, 'user', 'dummy.file'))
    end

    it 'moves the directory to the old path' do
      expected_path = File.join(uploads_dir, 'user', 'dummy.file')

      migration.down

      expect(File.exist?(expected_path)).to be(true)
    end

    it 'removes the symlink if it existed' do
      FileUtils.ln_s(File.join(new_uploads_dir, 'user'), File.join(uploads_dir, 'user'))

      directory = File.join(uploads_dir, 'user')
      expected_path = File.join(directory, 'dummy.file')

      migration.down

      expect(File.exist?(expected_path)).to be(true)
      expect(File.symlink?(directory)).to be(false)
    end
  end
end