Skip to content

Commit

Permalink
Handle the case where the admin_set_id is a blank string
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Nov 3, 2016
1 parent cc6cf29 commit a9f5645
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 31 deletions.
4 changes: 2 additions & 2 deletions app/actors/sufia/apply_permission_template_actor.rb
Expand Up @@ -8,8 +8,8 @@ def create(attributes)
protected

def add_edit_users(attributes)
return unless attributes[:admin_set_id]
template = Sufia::PermissionTemplate.find_by(admin_set_id: attributes[:admin_set_id])
return unless attributes[:admin_set_id].present?
template = Sufia::PermissionTemplate.find_by!(admin_set_id: attributes[:admin_set_id])
curation_concern.edit_users = template.access_grants.where(agent_type: 'user', access: 'manage').pluck(:agent_id)
curation_concern.edit_groups = template.access_grants.where(agent_type: 'group', access: 'manage').pluck(:agent_id)
curation_concern.read_users = template.access_grants.where(agent_type: 'user', access: 'view').pluck(:agent_id)
Expand Down
69 changes: 40 additions & 29 deletions spec/actors/sufia/apply_permission_template_actor_spec.rb
Expand Up @@ -17,37 +17,48 @@
let(:permission_template) { create(:permission_template, admin_set_id: admin_set.id) }

describe "create" do
before do
create(:permission_template_access,
permission_template: permission_template,
agent_type: 'user',
agent_id: 'hannah',
access: 'manage')
create(:permission_template_access,
permission_template: permission_template,
agent_type: 'group',
agent_id: 'librarians',
access: 'manage')
create(:permission_template_access,
permission_template: permission_template,
agent_type: 'user',
agent_id: 'gary',
access: 'view')
create(:permission_template_access,
permission_template: permission_template,
agent_type: 'group',
agent_id: 'readers',
access: 'view')
allow(CurationConcerns::Actors::RootActor).to receive(:new).and_return(create_actor)
allow(create_actor).to receive(:create).and_return(true)
context "when admin_set_id is blank" do
let(:attributes) { { admin_set_id: '' } }

it "returns true" do
expect(actor.create(attributes)).to be true
end
end

it "adds the template users to the work" do
actor.create(attributes)
expect(work.edit_users).to include 'hannah'
expect(work.edit_groups).to include 'librarians'
expect(work.read_users).to include 'gary'
expect(work.read_groups).to include 'readers'
context "when admin_set_id is provided" do
let(:attributes) { { admin_set_id: admin_set.id } }
before do
create(:permission_template_access,
permission_template: permission_template,
agent_type: 'user',
agent_id: 'hannah',
access: 'manage')
create(:permission_template_access,
permission_template: permission_template,
agent_type: 'group',
agent_id: 'librarians',
access: 'manage')
create(:permission_template_access,
permission_template: permission_template,
agent_type: 'user',
agent_id: 'gary',
access: 'view')
create(:permission_template_access,
permission_template: permission_template,
agent_type: 'group',
agent_id: 'readers',
access: 'view')
allow(CurationConcerns::Actors::RootActor).to receive(:new).and_return(create_actor)
allow(create_actor).to receive(:create).and_return(true)
end

it "adds the template users to the work" do
expect(actor.create(attributes)).to be true
expect(work.edit_users).to include 'hannah'
expect(work.edit_groups).to include 'librarians'
expect(work.read_users).to include 'gary'
expect(work.read_groups).to include 'readers'
end
end
end
end

0 comments on commit a9f5645

Please sign in to comment.