media_attachments_vacuum_spec.rb 2.1 KB

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