file_spec.rb 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Store::File, type: :model do
  4. subject(:file) { described_class.add('foo') }
  5. describe '.add' do
  6. context 'with no preconfigured storage provider' do
  7. before { Setting.set('storage_provider', nil) }
  8. it 'defaults to the "DB" provider' do
  9. expect(file.provider).to eq('DB')
  10. end
  11. end
  12. context 'with a preconfigured storage provider' do
  13. before { Setting.set('storage_provider', 'File') }
  14. after { Store::Provider::File.delete(described_class.checksum('foo')) }
  15. it 'defaults to the "DB" provider' do
  16. expect(file.provider).to eq('File')
  17. end
  18. end
  19. end
  20. describe '.verify' do
  21. context 'when no Store::File records exist' do
  22. it 'returns true' do
  23. expect(described_class.verify).to be(true)
  24. end
  25. end
  26. context 'when all Store::File records have matching #content / #sha attributes' do
  27. before do
  28. file # create Store::File record
  29. end
  30. it 'returns true' do
  31. expect(described_class.verify).to be(true)
  32. end
  33. end
  34. context 'when at least one Store::File record’s #content / #sha attributes do not match' do
  35. before do
  36. file # create Store::File record
  37. Store::Provider::DB.last.update(data: 'bar')
  38. end
  39. it 'returns false' do
  40. expect(described_class.verify).to be(false)
  41. end
  42. end
  43. end
  44. describe '.move' do
  45. before { Setting.set('storage_provider', nil) }
  46. after { Store::Provider::File.delete(described_class.checksum('foo')) }
  47. let(:storage_path) { Rails.root.join('storage/fs') }
  48. it 'replaces all Store::Provider::{source} records with Store::Provider::{target} ones' do
  49. file # create Store::File record
  50. expect { described_class.move('DB', 'File') }
  51. .to change { file.reload.provider }.to('File')
  52. .and change(Store::Provider::DB, :count).by(-1)
  53. .and change { Dir[storage_path.join('**', '*')].select { |entry| File.file?(entry) }.count }.by(1)
  54. end
  55. context 'when no Store::File records of the source type exist' do
  56. it 'makes no changes and returns true' do
  57. file # create Store::File record
  58. expect { described_class.move('File', 'DB') }
  59. .not_to change { file.reload.provider }
  60. end
  61. end
  62. context 'when moving from "File" adapter to "DB"' do
  63. before { Setting.set('storage_provider', 'File') }
  64. it 'removes stored files from filesystem' do
  65. file # create Store::File record
  66. expect { described_class.move('File', 'DB') }
  67. .to change { file.reload.provider }.to('DB')
  68. .and change(Store::Provider::DB, :count).by(1)
  69. .and change { Dir[storage_path.join('*')].count }.by(-1)
  70. end
  71. end
  72. end
  73. end