media_attachments_vacuum_spec.rb 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. require 'rails_helper'
  2. RSpec.describe Vacuum::MediaAttachmentsVacuum do
  3. let(:retention_period) { 7.days }
  4. subject { described_class.new(retention_period) }
  5. let(:remote_status) { Fabricate(:status, account: Fabricate(:account, domain: 'example.com')) }
  6. let(:local_status) { Fabricate(:status) }
  7. describe '#perform' do
  8. let!(:old_remote_media) { Fabricate(:media_attachment, remote_url: 'https://example.com/foo.png', status: remote_status, created_at: (retention_period + 1.day).ago, updated_at: (retention_period + 1.day).ago) }
  9. let!(:old_local_media) { Fabricate(:media_attachment, status: local_status, created_at: (retention_period + 1.day).ago, updated_at: (retention_period + 1.day).ago) }
  10. let!(:new_remote_media) { Fabricate(:media_attachment, remote_url: 'https://example.com/foo.png', status: remote_status) }
  11. let!(:new_local_media) { Fabricate(:media_attachment, status: local_status) }
  12. let!(:old_unattached_media) { Fabricate(:media_attachment, account_id: nil, created_at: 10.days.ago) }
  13. let!(:new_unattached_media) { Fabricate(:media_attachment, account_id: nil, created_at: 1.hour.ago) }
  14. before do
  15. subject.perform
  16. end
  17. it 'deletes cache of remote media attachments past the retention period' do
  18. expect(old_remote_media.reload.file).to be_blank
  19. end
  20. it 'does not touch local media attachments past the retention period' do
  21. expect(old_local_media.reload.file).to_not be_blank
  22. end
  23. it 'does not delete cache of remote media attachments within the retention period' do
  24. expect(new_remote_media.reload.file).to_not be_blank
  25. end
  26. it 'does not touch local media attachments within the retention period' do
  27. expect(new_local_media.reload.file).to_not be_blank
  28. end
  29. it 'deletes unattached media attachments past TTL' do
  30. expect { old_unattached_media.reload }.to raise_error(ActiveRecord::RecordNotFound)
  31. end
  32. it 'does not delete unattached media attachments within TTL' do
  33. expect(new_unattached_media.reload).to be_persisted
  34. end
  35. end
  36. end