1# frozen_string_literal: true 2 3require 'spec_helper' 4 5RSpec.describe Projects::HashedStorage::RollbackAttachmentsService do 6 subject(:service) { described_class.new(project: project, old_disk_path: project.disk_path, logger: nil) } 7 8 let(:project) { create(:project, :repository, skip_disk_validation: true) } 9 let(:legacy_storage) { Storage::LegacyProject.new(project) } 10 let(:hashed_storage) { Storage::Hashed.new(project) } 11 12 let!(:upload) { Upload.find_by(path: file_uploader.upload_path) } 13 let(:file_uploader) { build(:file_uploader, project: project) } 14 let(:old_disk_path) { File.join(base_path(hashed_storage), upload.path) } 15 let(:new_disk_path) { File.join(base_path(legacy_storage), upload.path) } 16 17 describe '#execute' do 18 context 'when succeeds' do 19 it 'moves attachments to legacy storage layout' do 20 expect(File.file?(old_disk_path)).to be_truthy 21 expect(File.file?(new_disk_path)).to be_falsey 22 expect(File.exist?(base_path(hashed_storage))).to be_truthy 23 expect(File.exist?(base_path(legacy_storage))).to be_falsey 24 expect(FileUtils).to receive(:mv).with(base_path(hashed_storage), base_path(legacy_storage)).and_call_original 25 26 service.execute 27 28 expect(File.exist?(base_path(legacy_storage))).to be_truthy 29 expect(File.exist?(base_path(hashed_storage))).to be_falsey 30 expect(File.file?(old_disk_path)).to be_falsey 31 expect(File.file?(new_disk_path)).to be_truthy 32 end 33 34 it 'returns true' do 35 expect(service.execute).to be_truthy 36 end 37 38 it 'sets skipped to false' do 39 service.execute 40 41 expect(service.skipped?).to be_falsey 42 end 43 end 44 45 context 'when original folder does not exist anymore' do 46 before do 47 FileUtils.rm_rf(base_path(hashed_storage)) 48 end 49 50 it 'skips moving folders and go to next' do 51 expect(FileUtils).not_to receive(:mv).with(base_path(hashed_storage), base_path(legacy_storage)) 52 53 service.execute 54 55 expect(File.exist?(base_path(legacy_storage))).to be_falsey 56 expect(File.file?(new_disk_path)).to be_falsey 57 end 58 59 it 'returns true' do 60 expect(service.execute).to be_truthy 61 end 62 63 it 'sets skipped to true' do 64 service.execute 65 66 expect(service.skipped?).to be_truthy 67 end 68 end 69 70 context 'when target folder already exists' do 71 before do 72 FileUtils.mkdir_p(base_path(legacy_storage)) 73 end 74 75 it 'raises AttachmentCannotMoveError' do 76 expect(FileUtils).not_to receive(:mv).with(base_path(legacy_storage), base_path(hashed_storage)) 77 78 expect { service.execute }.to raise_error(Projects::HashedStorage::AttachmentCannotMoveError) 79 end 80 end 81 82 it 'works even when project validation fails' do 83 allow(project).to receive(:valid?) { false } 84 85 expect { service.execute }.to change { project.hashed_storage?(:attachments) }.to(false) 86 end 87 end 88 89 describe '#old_disk_path' do 90 it 'returns old disk_path for project' do 91 expect(service.old_disk_path).to eq(project.disk_path) 92 end 93 end 94 95 describe '#new_disk_path' do 96 it 'returns new disk_path for project' do 97 service.execute 98 99 expect(service.new_disk_path).to eq(project.full_path) 100 end 101 end 102 103 def base_path(storage) 104 File.join(FileUploader.root, storage.disk_path) 105 end 106end 107