Skip to content

Commit

Permalink
Don't mint handles unless minimum requirements are met
Browse files Browse the repository at this point in the history
We only want to mint and update handles for items that are in 'dl' and are published.
  • Loading branch information
Tom Johnson committed Nov 23, 2017
1 parent 35ec446 commit 371d612
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 8 deletions.
4 changes: 4 additions & 0 deletions app/actors/hyrax/actors/handle_assurance_actor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ def update(env)
#
# @return [Boolean] true
def ensure_handle(object:)
return true unless
object.displays_in.include?('dl') &&
object.to_sipity_entity.try(:workflow_state_name) == 'published'

if object.identifier.empty?
HandleRegisterJob.perform_later(object)
else
Expand Down
50 changes: 43 additions & 7 deletions spec/actors/hyrax/actors/handle_assurance_actor_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
require 'rails_helper'

RSpec.describe Hyrax::Actors::HandleAssuranceActor do
RSpec.describe Hyrax::Actors::HandleAssuranceActor, :clean, :workflow do
subject(:actor) { described_class.new(next_actor) }
let(:next_actor) { Hyrax::Actors::Terminator.new }
let(:object) { create(:pdf) }
let(:user) { User.new }
let(:object) { actor_create(:published_pdf, displays_in: ['dl'], user: user) }
let(:user) { create(:user) }

let(:env) do
Hyrax::Actors::Environment.new(object, Ability.new(user), {})
Expand All @@ -30,9 +30,28 @@ def update(*)
end

describe '#create' do
it 'enqueues a job' do
before do
object
ActiveJob::Base.queue_adapter = :test
end

context 'before published' do
let(:object) { actor_create(:pdf, displays_in: ['dl'], user: user) }

it 'does not enqueue a job' do
expect { middleware.create(env) }.not_to enqueue_job
end
end

context 'when published but not in DL' do
let(:object) { actor_create(:published_pdf, displays_in: ['trove'], user: user) }

it 'does not enqueue a job' do
expect { middleware.create(env) }.not_to enqueue_job
end
end

it 'enqueues a job' do
expect { middleware.create(env) }
.to enqueue_job.with(object)
end
Expand All @@ -41,7 +60,6 @@ def update(*)
let(:next_actor) { failing_middleware.new }

it 'does not equeue a job' do
ActiveJob::Base.queue_adapter = :test
expect { middleware.create(env) }.not_to enqueue_job
end
end
Expand Down Expand Up @@ -76,9 +94,28 @@ def update(*)
end

describe '#update' do
it 'enqueues a job' do
before do
object
ActiveJob::Base.queue_adapter = :test
end

context 'before published' do
let(:object) { actor_create(:pdf, displays_in: ['dl'], user: user) }

it 'does not enqueue a job' do
expect { middleware.update(env) }.not_to enqueue_job
end
end

context 'when published but not in DL' do
let(:object) { actor_create(:published_pdf, displays_in: ['trove'], user: user) }

it 'does not enqueue a job' do
expect { middleware.update(env) }.not_to enqueue_job
end
end

it 'enqueues a job' do
expect { middleware.create(env) }
.to enqueue_job.with(object)
end
Expand All @@ -87,7 +124,6 @@ def update(*)
let(:next_actor) { failing_middleware.new }

it 'does not equeue a job' do
ActiveJob::Base.queue_adapter = :test
expect { middleware.create(env) }.not_to enqueue_job
end
end
Expand Down
18 changes: 18 additions & 0 deletions spec/factories/pdf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,24 @@
title [FFaker::Book.title]
visibility Hydra::AccessControls::AccessRight::VISIBILITY_TEXT_VALUE_PUBLIC
displays_in ['nowhere']

transient do
user nil
end

factory :published_pdf do
after(:create) do |work, evaluator|
action_info = Hyrax::WorkflowActionInfo.new(work, evaluator.user)
scope = action_info.entity.workflow
action = PowerConverter
.convert_to_sipity_action("publish", scope: scope) { nil }

Hyrax::Workflow::WorkflowActionService
.run(subject: action_info,
action: action,
comment: 'Published by :published_pdf factory in `after_create` hook.')
end
end
end

factory :populated_pdf, class: Pdf do
Expand Down
15 changes: 14 additions & 1 deletion spec/support/build_strategies/actor_create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def result(evaluation)
evaluator = evaluation.instance_variable_get(:@attribute_assigner)
.instance_variable_get(:@evaluator)

ability = Ability.new(evaluator.user)
ability = Ability.new(user(evaluator))
env = Hyrax::Actors::Environment.new(instance, ability, {})

evaluation.notify(:before_create, instance)
Expand All @@ -22,4 +22,17 @@ def result(evaluation)
evaluation.notify(:after_actor_create, instance)
end
end

private

def user(evaluator)
user = evaluator.user

if user.nil? || user.new_record?
raise 'You must pass a created depositing user to use the `actor_create` ' /
"build strategy; received: #{user}"
end

user
end
end

0 comments on commit 371d612

Please sign in to comment.