diff --git a/spec/factories/hyrax_file_set.rb b/spec/factories/hyrax_file_set.rb index ab9c2d42e4..2305fa4cf7 100644 --- a/spec/factories/hyrax_file_set.rb +++ b/spec/factories/hyrax_file_set.rb @@ -70,6 +70,28 @@ end end + trait :under_lease do + association :lease, factory: :hyrax_lease + + after(:create) do |fs, _e| + Hyrax::LeaseManager.new(resource: fs).apply + fs.permission_manager.acl.save + end + end + + trait :with_expired_enforced_lease do + after(:build) do |fs, _evaluator| + fs.lease = FactoryBot.valkyrie_create(:hyrax_lease, :expired) + end + + after(:create) do |fs, _evaluator| + allow(Hyrax::TimeService).to receive(:time_in_utc).and_return(10.days.ago) + Hyrax::LeaseManager.new(resource: fs).apply + fs.permission_manager.acl.save + allow(Hyrax::TimeService).to receive(:time_in_utc).and_call_original + end + end + trait :public do transient do visibility_setting { Hydra::AccessControls::AccessRight::VISIBILITY_TEXT_VALUE_PUBLIC } diff --git a/spec/factories/hyrax_work.rb b/spec/factories/hyrax_work.rb index a28a4bca3b..f168b14b21 100644 --- a/spec/factories/hyrax_work.rb +++ b/spec/factories/hyrax_work.rb @@ -43,9 +43,8 @@ after(:create) do |work, _evaluator| allow(Hyrax::TimeService).to receive(:time_in_utc).and_return(10.days.ago) Hyrax::LeaseManager.new(resource: work).apply - allow(Hyrax::TimeService).to receive(:time_in_utc).and_call_original - work.permission_manager.acl.save + allow(Hyrax::TimeService).to receive(:time_in_utc).and_call_original end end diff --git a/spec/jobs/lease_expiry_job_spec.rb b/spec/jobs/lease_expiry_job_spec.rb index 24a0bea94e..ecab0c0e32 100644 --- a/spec/jobs/lease_expiry_job_spec.rb +++ b/spec/jobs/lease_expiry_job_spec.rb @@ -2,48 +2,85 @@ RSpec.describe LeaseExpiryJob, :clean_repo do subject { described_class } - let(:past_date) { 2.days.ago } + context 'with Valkyrie resources' do + let(:leased_work) { valkyrie_create(:hyrax_work, :under_lease) } + let(:work_with_expired_lease) { valkyrie_create(:hyrax_work, :with_expired_enforced_lease) } + let(:file_set_with_expired_lease) { valkyrie_create(:hyrax_file_set, :with_expired_enforced_lease) } - let(:leased_work) { create(:leased_work) } - - let!(:work_with_expired_lease) do - build(:work, lease_expiration_date: past_date.to_s, visibility_during_lease: 'open', visibility_after_lease: 'restricted').tap do |work| - work.save(validate: false) + describe '#records_with_expired_leases' do + it 'returns all records with expired leases' do + records = [work_with_expired_lease.id, file_set_with_expired_lease.id] + expect(described_class.new.records_with_expired_leases.map(&:id)).to eq(records) + end end - end - let!(:file_set_with_expired_lease) do - build(:file_set, lease_expiration_date: past_date.to_s, visibility_during_lease: 'open', visibility_after_lease: 'restricted').tap do |file_set| - file_set.save(validate: false) + describe '#perform' do + it 'expires leases on works with expired leases' do + expect(work_with_expired_lease.visibility).to eq('open') + described_class.new.perform + reloaded = Hyrax.query_service.find_by(id: work_with_expired_lease.id) + expect(reloaded.visibility).to eq('authenticated') + end + + it 'expires leases on filesets with expired leases' do + expect(file_set_with_expired_lease.visibility).to eq('open') + described_class.new.perform + reloaded = Hyrax.query_service.find_by(id: file_set_with_expired_lease.id) + expect(reloaded.visibility).to eq('authenticated') + end + + it 'does not expire leases that are still in effect' do + expect(leased_work.visibility).to eq('open') + described_class.new.perform + reloaded = Hyrax.query_service.find_by(id: leased_work.id) + expect(reloaded.visibility).to eq('open') + end end end - describe '#records_with_expired_leases' do - it 'returns all records with expired leases' do - records = described_class.new.records_with_expired_leases - expect(records.map(&:id)) - .to contain_exactly(work_with_expired_lease.id, - file_set_with_expired_lease.id) + context 'with ActiveFedora objects', :active_fedora do + let(:past_date) { 2.days.ago } + let(:leased_work) { create(:leased_work) } + + let!(:work_with_expired_lease) do + build(:work, lease_expiration_date: past_date.to_s, visibility_during_lease: 'open', visibility_after_lease: 'restricted').tap do |work| + work.save(validate: false) + end end - end - describe '#perform' do - it 'expires leases on works with expired leases' do - described_class.new.perform - work_with_expired_lease.reload - expect(work_with_expired_lease.visibility).to eq('restricted') + let!(:file_set_with_expired_lease) do + build(:file_set, lease_expiration_date: past_date.to_s, visibility_during_lease: 'open', visibility_after_lease: 'restricted').tap do |file_set| + file_set.save(validate: false) + end end - it 'expires leases on filesets with expired leases' do - described_class.new.perform - file_set_with_expired_lease.reload - expect(file_set_with_expired_lease.visibility).to eq('restricted') + describe '#records_with_expired_leases' do + it 'returns all records with expired leases' do + records = described_class.new.records_with_expired_leases + expect(records.map(&:id)) + .to contain_exactly(work_with_expired_lease.id, + file_set_with_expired_lease.id) + end end - it 'does not expire leases that are still in effect' do - described_class.new.perform - leased_work.reload - expect(leased_work.visibility).to eq('open') + describe '#perform' do + it 'expires leases on works with expired leases' do + described_class.new.perform + work_with_expired_lease.reload + expect(work_with_expired_lease.visibility).to eq('restricted') + end + + it 'expires leases on filesets with expired leases' do + described_class.new.perform + file_set_with_expired_lease.reload + expect(file_set_with_expired_lease.visibility).to eq('restricted') + end + + it 'does not expire leases that are still in effect' do + described_class.new.perform + leased_work.reload + expect(leased_work.visibility).to eq('open') + end end end end