Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds in actor which utilizes service to add permission to collection #357

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# frozen_string_literal: true

module Hyrax::Migrator::Actors
# Add permissions to collections that are already persisted.
class AddCollectionPermissionActor < Hyrax::Migrator::Actors::AbstractActor
aasm do
state :add_collection_permission_initial, initial: true
state :add_collection_permission_succeeded, :add_collection_permission_failed

event :add_collection_permission_initial do
transitions from: %i[add_collection_permission_initial add_collection_permission_failed],
to: :add_collection_permission_initial
end
event :add_collection_permission_failed, after: :post_fail do
transitions from: :add_collection_permission_initial,
to: :add_collection_permission_failed
end
event :add_collection_permission_succeeded, after: :post_success do
transitions from: :add_collection_permission_initial,
to: :add_collection_permission_succeeded
end
end

HAS_CHILD = 'Collection Permission added.'
NO_CHILD = 'No Collection Permission to add.'

##
# Use the PermissionsCreateService to add permissions to collections in Hyrax.
def create(work)
super
if !work.collection?
@message = NO_CHILD
add_collection_permission_succeeded
else
call_service
end
rescue StandardError => e
add_collection_permission_failed
log("failed while adding collection permission to collection work: #{e.message}")
end

private

def call_service
@message = HAS_CHILD
add_collection_permission_initial
update_work(aasm.current_state)
service ? add_collection_permission_succeeded : add_collection_permission_failed
end

#:nocov:
def service
@service ||= Hyrax::Collections::PermissionsCreateService.add_access(collection_id: @work.id, grants: [{agent_type: Hyrax::PermissionTemplateAccess::USER,
agent_id: @user.user_key,
access: Hyrax::PermissionTemplateAccess::MANAGE }])
end
#:nocov:

def post_fail
failed(aasm.current_state, "Work #{@work.pid} failed adding collection permission.", Hyrax::Migrator::Work::FAIL)
end

def post_success
succeeded(aasm.current_state, "Work #{@work.pid} #{@message}", Hyrax::Migrator::Work::SUCCESS)
end
end
end

1 change: 1 addition & 0 deletions lib/hyrax/migrator/middleware/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def actor_stack
Hyrax::Migrator::Actors::ListChildrenActor,
Hyrax::Migrator::Actors::ChildrenAuditActor,
Hyrax::Migrator::Actors::PersistWorkActor,
Hyrax::Migrator::Actors::AddCollectionPermissionActor,
Hyrax::Migrator::Actors::AddRelationshipsActor,
Hyrax::Migrator::Actors::WorkflowMetadataActor,
Hyrax::Migrator::Actors::TerminalActor
Expand Down
122 changes: 122 additions & 0 deletions spec/hyrax/migrator/actors/add_collection_permission_actor_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
# frozen_string_literal: true

RSpec.describe Hyrax::Migrator::Actors::AddCollectionPermissionActor do
let(:actor) { described_class.new }
let(:terminal) { Hyrax::Migrator::Actors::TerminalActor.new }
let(:work) { create(:collection, pid: pid) }
let(:pid) { '3t945r08v' }
let(:config) { Hyrax::Migrator::Configuration.new }
let(:service) { double }

before do
allow(actor).to receive(:config).and_return(config)
end

describe '#create' do
context 'when the work is not a collection' do
before do
allow(actor).to receive(:service).and_return(service)
actor.next_actor = terminal
actor.create(work)
allow(work).to receive(:collection?).and_return(false)
end

it 'goes straight to succeeded' do
expect(work.aasm_state).to eq('add_collection_permission_succeeded')
end
it 'displays the correct message' do
expect(work.status_message).to eq("Work #{work.pid} No Collection Permission to add.")
end
end

context 'when the service succeeds' do
before do
allow(actor).to receive(:service).and_return(service)
allow(service).to receive(:add_access).and_return(true)
actor.next_actor = terminal
allow(work).to receive(:collection?).and_return(true)
end

it 'sets the proper state' do
actor.create(work)
expect(work.aasm_state).to eq('add_collection_permission_succeeded')
end

it 'sets the status message' do
actor.create(work)
expect(work.status_message).to eq("Work #{work.pid} Collection Permission added.")
end

it 'sets the status' do
actor.create(work)
expect(work.status).to eq('success')
end

it 'calls the next actor' do
expect(terminal).to receive(:create)
actor.create(work)
end
end

context 'when the service fails' do
before do
allow(actor).to receive(:service).and_return(service)
allow(service).to receive(:add_access).and_return(false)
actor.next_actor = terminal
end

it 'sets the proper state' do
actor.create(work)
expect(work.aasm_state).to eq('add_relationships_failed')
end

it 'sets the status message' do
actor.create(work)
expect(work.status_message).to eq("Work #{work.pid} failed adding collection permission.")
end

it 'sets the status' do
actor.create(work)
expect(work.status).to eq('fail')
end

it 'does not call the next actor' do
expect(terminal).not_to receive(:create)
actor.create(work)
end
end

context 'when the service raises an exception' do
before do
allow(actor).to receive(:service).and_return(service)
allow(service).to receive(:add_access).and_raise(StandardError)
actor.next_actor = terminal
end

it 'sets the proper state' do
actor.create(work)
expect(work.aasm_state).to eq('add_collection_permission_failed')
end

it 'sets the status message' do
actor.create(work)
expect(work.status_message).to eq("Work #{work.pid} failed adding collection permission.")
end

it 'sets the status' do
actor.create(work)
expect(work.status).to eq('fail')
end

it 'does not call the next actor' do
expect(terminal).not_to receive(:create)
actor.create(work)
end

it 'logs the failure' do
expect(Rails.logger).to receive(:warn)
actor.create(work)
end
end
end
end
1 change: 1 addition & 0 deletions spec/hyrax/migrator/middleware/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
Hyrax::Migrator::Actors::ChildrenAuditActor,
Hyrax::Migrator::Actors::PersistWorkActor,
Hyrax::Migrator::Actors::AddRelationshipsActor,
Hyrax::Migrator::Actors::AddCollectionPermissionActor,
Hyrax::Migrator::Actors::WorkflowMetadataActor,
Hyrax::Migrator::Actors::TerminalActor
]
Expand Down