Skip to content

Commit

Permalink
☑️ Fixing tests and factories for roles_service
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyf committed Feb 16, 2024
1 parent 7f12d48 commit c106563
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 173 deletions.
18 changes: 9 additions & 9 deletions app/services/roles_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -233,18 +233,18 @@ def seed_qa_users!

class GrantWorkflowRolesForAllAdminSetsJob < Hyrax::ApplicationJob
def perform
AdminSet.find_each do |admin_set|
Hyrax::Workflow::PermissionGrantor
.grant_default_workflow_roles!(permission_template: admin_set.permission_template)
Hyrax.query_service.find_all_of_model(model: Hyrax.config.admin_set_class).each do |admin_set|
permission_template = Hyrax::PermissionTemplate.find_or_create_by!(source_id: admin_set.id)
Hyrax::Workflow::PermissionGrantor.grant_default_workflow_roles!(permission_template:)
end
end
end

class CreateCollectionAccessesJob < Hyrax::ApplicationJob
# rubocop:disable Metrics/MethodLength
def perform
Collection.find_each do |c|
pt = Hyrax::PermissionTemplate.find_or_create_by!(source_id: c.id)
Hyrax.query_service.find_all_of_model(model: Hyrax.config.collection_class).each do |collection|
pt = Hyrax::PermissionTemplate.find_or_create_by!(source_id: collection.id)
original_access_grants_count = pt.access_grants.count

pt.access_grants.find_or_create_by!(
Expand All @@ -271,7 +271,7 @@ def perform
agent_id: 'collection_reader'
)

pt.reset_access_controls_for(collection: c) if pt.access_grants.count != original_access_grants_count
pt.reset_access_controls_for(collection:) if pt.access_grants.count != original_access_grants_count
end
end
# rubocop:enable Metrics/MethodLength
Expand All @@ -280,8 +280,8 @@ def perform
class CreateAdminSetAccessesJob < Hyrax::ApplicationJob
# rubocop:disable Metrics/MethodLength
def perform
AdminSet.find_each do |as|
pt = Hyrax::PermissionTemplate.find_or_create_by!(source_id: as.id)
Hyrax.query_service.find_all_of_model(model: Hyrax.config.admin_set_class).each do |admin_set|
pt = Hyrax::PermissionTemplate.find_or_create_by!(source_id: admin_set.id)
original_access_grants_count = pt.access_grants.count

pt.access_grants.find_or_create_by!(
Expand All @@ -308,7 +308,7 @@ def perform
agent_id: 'work_editor'
)

pt.reset_access_controls_for(collection: as) if pt.access_grants.count != original_access_grants_count
pt.reset_access_controls_for(collection: admin_set) if pt.access_grants.count != original_access_grants_count
end
end
# rubocop:enable Metrics/MethodLength
Expand Down
9 changes: 9 additions & 0 deletions spec/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,12 @@ In addition to the baseline `FactoryBot.build` and `FactoryBot.create` Hyrax pro

- `FactoryBot.create_valkyrie` :: For creating a Valkyrie-only object, that is never writing the object via ActiveFedora.
- `FactoryBot.json` :: For creating a JSON object; a read of the Hyrax specs shows that this strategy is used as part of other factories.

### AdminSet and PermissionTemplate

The default Permission Template for Hyku is a bit more complicated than Hyrax. When you want a properly created factory of permission template and admin set use the following:

```ruby
let(:permission_template) { FactoryBot.create(:permission_template, with_admin_set: true, source_id: admin_set.id) }
let(:admin_set) { FactoryBot.valkyrie_create(:hyku_admin_set) }
```
3 changes: 1 addition & 2 deletions spec/factories/admin_sets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
# Use: FactoryBot.valkyrie_create(:hyrax_admin_set)
factory :hyrax_admin_set do
transient do
with_permission_template { true }
# We need FactoryBot declaration, otherwise we use the same strategy
# (e.g. FactoryBot.valkyrie_create), and stumble into a nightmare.
user { FactoryBot.create(:user) }
end

before(:create) do |admin_set, evaluator|
before(:create) do |_admin_set, _evaluator|
Hyrax::Group.find_or_create_by!(name: ::Ability.admin_group_name)
end
end
Expand Down
22 changes: 22 additions & 0 deletions spec/factories/collection_types.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

require Hyrax::Engine.root.join("spec/factories/collection_types").to_s

FactoryBot.modify do
# Hyku assumes a collection editor and collection manager
factory :collection_type do
transient do
creator_user { nil }
creator_group { 'collection_editor' }
manager_user { nil }
manager_group { 'collection_manager' }
end

trait :without_default_participants do
creator_user { nil }
creator_group { nil }
manager_user { nil }
manager_group { nil }
end
end
end
2 changes: 1 addition & 1 deletion spec/factories/generic_work_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
FactoryBot.define do
factory :generic_work_resource, parent: :hyrax_work, class: 'GenericWorkResource'

before(:create) do |work, _e|
before(:create) do |_work, _e|
Hyrax::Group.find_or_create_by!(name: ::Ability.admin_group_name)
end
end
4 changes: 2 additions & 2 deletions spec/factories/permission_templates.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Hyrax's lib/hyrax/spec/factories provides the helpful support of the established factories of
# Hyrax, meaning we can then inherit from those
# frozen_string_literal: true

require Hyrax::Engine.root.join("spec/factories/permission_templates").to_s

FactoryBot.modify do
Expand Down
3 changes: 2 additions & 1 deletion spec/hyrax_with_valkyrie_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
)

require 'hyrax/specs/shared_specs/factories/strategies/valkyrie_resource'
require 'hyrax/specs/shared_specs/factories/strategies/json_strategy'
FactoryBot.register_strategy(:valkyrie_create, ValkyrieCreateStrategy)

require 'hyrax/specs/shared_specs/factories/strategies/json_strategy'
FactoryBot.register_strategy(:json, JsonStrategy)
Loading

0 comments on commit c106563

Please sign in to comment.