trabajo-afectivo/spec/models/store/file_spec.rb
2021-07-09 16:38:23 +00:00

96 lines
2.8 KiB
Ruby
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
require 'rails_helper'
RSpec.describe Store::File, type: :model do
subject(:file) { described_class.add('foo') }
describe '.add' do
context 'with no preconfigured storage provider' do
before { Setting.set('storage_provider', nil) }
it 'defaults to the "DB" provider' do
expect(file.provider).to eq('DB')
end
end
context 'with a preconfigured storage provider' do
before { Setting.set('storage_provider', 'File') }
after { Store::Provider::File.delete(Digest::SHA256.hexdigest('foo')) }
it 'defaults to the "DB" provider' do
expect(file.provider).to eq('File')
end
end
end
describe '.verify' do
context 'when no Store::File records exist' do
it 'returns true' do
expect(described_class.verify).to be(true)
end
end
context 'when all Store::File records have matching #content / #sha attributes' do
before do
file # create Store::File record
end
it 'returns true' do
expect(described_class.verify).to be(true)
end
end
context 'when at least one Store::File records #content / #sha attributes do not match' do
before do
file # create Store::File record
Store::Provider::DB.last.update(data: 'bar')
end
it 'returns false' do
expect(described_class.verify).to be(false)
end
end
end
describe '.move' do
before { Setting.set('storage_provider', nil) }
after { Store::Provider::File.delete(Digest::SHA256.hexdigest('foo')) }
let(:storage_path) { Rails.root.join('storage/fs') }
it 'replaces all Store::Provider::{source} records with Store::Provider::{target} ones' do
file # create Store::File record
expect { described_class.move('DB', 'File') }
.to change { file.reload.provider }.to('File')
.and change { Store::Provider::DB.count }.by(-1)
.and change { Dir[storage_path.join('**', '*')].select { |entry| File.file?(entry) }.count }.by(1)
end
context 'when no Store::File records of the source type exist' do
it 'makes no changes and returns true' do
file # create Store::File record
expect { described_class.move('File', 'DB') }
.not_to change { file.reload.provider }
end
end
context 'when moving from "File" adapter to "DB"' do
before { Setting.set('storage_provider', 'File') }
it 'removes stored files from filesystem' do
file # create Store::File record
expect { described_class.move('File', 'DB') }
.to change { file.reload.provider }.to('DB')
.and change { Store::Provider::DB.count }.by(1)
.and change { Dir[storage_path.join('*')].count }.by(-1)
end
end
end
end