From 08ebfe051260f72be326c4ed9fe14e1a8e3b2d9e Mon Sep 17 00:00:00 2001 From: tamsin johnson Date: Tue, 29 Aug 2023 11:00:07 -0700 Subject: [PATCH] add specs for `hyrax:embargo:deactivate_expired` there has been some question about whether this rake task actually releases expired embargoes. this tests that it does, and ensures that it integrates with the UI correctly by checking against `Hyrax::EmbargoHelper` (which lists enforced embargoes for use by views). --- spec/factories/hyrax_work.rb | 5 ++++ spec/tasks/rake_spec.rb | 55 ++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/spec/factories/hyrax_work.rb b/spec/factories/hyrax_work.rb index b9bb41d08d..1213569878 100644 --- a/spec/factories/hyrax_work.rb +++ b/spec/factories/hyrax_work.rb @@ -6,6 +6,11 @@ factory :hyrax_work, class: 'Hyrax::Test::SimpleWork' do trait :under_embargo do association :embargo, factory: :hyrax_embargo + + after(:create) do |work, _e| + Hyrax::EmbargoManager.new(resource: work).apply + work.permission_manager.acl.save + end end trait :with_expired_enforced_embargo do diff --git a/spec/tasks/rake_spec.rb b/spec/tasks/rake_spec.rb index f2f13578b8..8b75e994c2 100644 --- a/spec/tasks/rake_spec.rb +++ b/spec/tasks/rake_spec.rb @@ -2,6 +2,61 @@ require 'rake' RSpec.describe "Rake tasks" do + describe "hyrax:embargo:deactivate_expired", :clean_repo do + let!(:active) do + [FactoryBot.valkyrie_create(:hyrax_work, :under_embargo), + FactoryBot.valkyrie_create(:hyrax_work, :under_embargo)] + end + + let!(:expired) do + [FactoryBot.valkyrie_create(:hyrax_work, :with_expired_enforced_embargo), + FactoryBot.valkyrie_create(:hyrax_work, :with_expired_enforced_embargo)] + end + + before do + load_rake_environment [File.expand_path("../../../lib/tasks/embargo_lease.rake", __FILE__)] + end + + it "updates the persisted work ACLs for expired embargoes" do + expect { run_task 'hyrax:embargo:deactivate_expired' } + .to change { + Hyrax.query_service.find_many_by_ids(ids: expired.map(&:id)) + .map { |work| work.permission_manager.read_groups.to_a } + } + .from([contain_exactly('registered'), contain_exactly('registered')]) + .to([include('public'), include('public')]) + end + + it "updates the persisted work visibility for expired embargoes" do + expect { run_task 'hyrax:embargo:deactivate_expired' } + .to change { + Hyrax.query_service.find_many_by_ids(ids: expired.map(&:id)) + .map(&:visibility) + } + .from(['authenticated', 'authenticated']) + .to(['open', 'open']) + end + + it "does not update visibility for works with active embargoes" do + expect { run_task 'hyrax:embargo:deactivate_expired' } + .not_to change { + Hyrax.query_service.find_many_by_ids(ids: active.map(&:id)) + .map(&:visibility) + } + .from(['authenticated', 'authenticated']) + end + + it "removes the work from Hyrax::EmbargoHelper.assets_under_embargo" do + helper = Class.new { include Hyrax::EmbargoHelper } + + # this helper is the source of truth for listing currently enforced embargoes for the UI + expect { run_task 'hyrax:embargo:deactivate_expired' } + .to change { helper.new.assets_under_embargo } + .from(contain_exactly(*(active + expired).map { |work| have_attributes(id: work.id) })) + .to(contain_exactly(*active.map { |work| have_attributes(id: work.id) })) + end + end + describe "hyrax:user:list_emails" do let!(:user1) { create(:user) } let!(:user2) { create(:user) }