Skip to content

Commit

Permalink
Fix embargo expiry job spec (#6410)
Browse files Browse the repository at this point in the history
* Add valkyrie specs for EmbargoExpiryJob

Marks existing spec as AF only.

Duplicates existing EmbargoExpiryJob specs but with Valkyrie resources.

Spec setup is simplified by not specifying "restricted" visibility, instead expecting the factory
default "authenticated". Additional checks to confirm the initial visibility state are also added.

The embargo traits from the hyrax_work factory are copied to the hyrax_file_set factory.

Saving of the expired embargo ACL is moved to inside the time machine to 10 days ago. Without this
change the solr doc will not contain the embargo related fields, causing
`#records_with_expired_embargoes` to not find those resources.

---------

Co-authored-by: bwatson78 <brad.watson.orlando@gmail.com>
  • Loading branch information
dlpierce and bwatson78 authored Nov 1, 2023
1 parent 77c567f commit 357b5e7
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 31 deletions.
22 changes: 22 additions & 0 deletions spec/factories/hyrax_file_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,28 @@
Hyrax.index_adapter.save(resource: file_set) if evaluator.with_index
end

trait :under_embargo do
association :embargo, factory: :hyrax_embargo

after(:create) do |fs, _e|
Hyrax::EmbargoManager.new(resource: fs).apply
fs.permission_manager.acl.save
end
end

trait :with_expired_enforced_embargo do
after(:build) do |fs, _evaluator|
fs.embargo = FactoryBot.valkyrie_create(:hyrax_embargo, :expired)
end

after(:create) do |fs, _evaluator|
allow(Hyrax::TimeService).to receive(:time_in_utc).and_return(10.days.ago)
Hyrax::EmbargoManager.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 }
Expand Down
3 changes: 1 addition & 2 deletions spec/factories/hyrax_work.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
after(:create) do |work, _evaluator|
allow(Hyrax::TimeService).to receive(:time_in_utc).and_return(10.days.ago)
Hyrax::EmbargoManager.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

Expand Down
97 changes: 68 additions & 29 deletions spec/jobs/embargo_expiry_job_spec.rb
Original file line number Diff line number Diff line change
@@ -1,48 +1,87 @@
# frozen_string_literal: true
RSpec.describe EmbargoExpiryJob, :clean_repo do
subject { described_class }
let(:past_date) { 2.days.ago }
let(:embargoed_work) { create(:embargoed_work) }

let!(:work_with_expired_embargo) do
build(:work, embargo_release_date: past_date.to_s, visibility_during_embargo: 'restricted', visibility_after_embargo: 'open').tap do |work|
work.save(validate: false)
context "with Valkyrie resources" do
let(:embargoed_work) { valkyrie_create(:hyrax_work, :under_embargo) }
let(:work_with_expired_embargo) { valkyrie_create(:hyrax_work, :with_expired_enforced_embargo) }
let(:file_set_with_expired_embargo) { valkyrie_create(:hyrax_file_set, :with_expired_enforced_embargo) }

describe '#records_with_expired_embargos' do
it 'returns all records with expired embargos' do
records = [work_with_expired_embargo.id, file_set_with_expired_embargo.id]
expect(described_class.new.records_with_expired_embargos.map(&:id)).to eq(records)
end
end
end

let!(:file_set_with_expired_embargo) do
build(:file_set, embargo_release_date: past_date.to_s, visibility_during_embargo: 'restricted', visibility_after_embargo: 'open').tap do |file_set|
file_set.save(validate: false)
describe '#perform' do
it 'expires embargos on works with expired embargos' do
expect(work_with_expired_embargo.visibility).to eq('authenticated')
described_class.new.perform
reloaded = Hyrax.query_service.find_by(id: work_with_expired_embargo.id)
expect(reloaded.visibility).to eq('open')
end

it 'expires embargos on file sets with expired embargos' do
expect(file_set_with_expired_embargo.visibility).to eq('authenticated')
described_class.new.perform
reloaded = Hyrax.query_service.find_by(id: file_set_with_expired_embargo.id)
expect(reloaded.visibility).to eq('open')
end

it "Doesn't expire embargos that are still in effect" do
expect(embargoed_work.visibility).to eq('authenticated')
described_class.new.perform
reloaded = Hyrax.query_service.find_by(id: embargoed_work.id)
expect(reloaded.visibility).to eq('authenticated')
end
end
end

describe '#records_with_expired_embargos' do
it 'returns all records with expired embargos' do
records = described_class.new.records_with_expired_embargos
context 'with ActiveFedora objects', :active_fedora do
let(:past_date) { 2.days.ago }
let(:embargoed_work) { create(:embargoed_work) }

expect(records.map(&:id))
.to contain_exactly(work_with_expired_embargo.id,
file_set_with_expired_embargo.id)
let!(:work_with_expired_embargo) do
build(:work, embargo_release_date: past_date.to_s, visibility_during_embargo: 'restricted', visibility_after_embargo: 'open').tap do |work|
work.save(validate: false)
end
end
end

describe '#perform' do
it 'expires embargos on works with expired embargos' do
described_class.new.perform
work_with_expired_embargo.reload
expect(work_with_expired_embargo.visibility).to eq('open')
let!(:file_set_with_expired_embargo) do
build(:file_set, embargo_release_date: past_date.to_s, visibility_during_embargo: 'restricted', visibility_after_embargo: 'open').tap do |file_set|
file_set.save(validate: false)
end
end

it 'expires embargos on file sets with expired embargos' do
described_class.new.perform
file_set_with_expired_embargo.reload
expect(file_set_with_expired_embargo.visibility).to eq('open')
describe '#records_with_expired_embargos' do
it 'returns all records with expired embargos' do
records = described_class.new.records_with_expired_embargos

expect(records.map(&:id))
.to contain_exactly(work_with_expired_embargo.id,
file_set_with_expired_embargo.id)
end
end

it "Doesn't expire embargos that are still in effect" do
described_class.new.perform
embargoed_work.reload
expect(embargoed_work.visibility).to eq('restricted')
describe '#perform' do
it 'expires embargos on works with expired embargos' do
described_class.new.perform
work_with_expired_embargo.reload
expect(work_with_expired_embargo.visibility).to eq('open')
end

it 'expires embargos on file sets with expired embargos' do
described_class.new.perform
file_set_with_expired_embargo.reload
expect(file_set_with_expired_embargo.visibility).to eq('open')
end

it "Doesn't expire embargos that are still in effect" do
described_class.new.perform
embargoed_work.reload
expect(embargoed_work.visibility).to eq('restricted')
end
end
end
end

0 comments on commit 357b5e7

Please sign in to comment.