Skip to content

Commit

Permalink
Update a couple spec factories
Browse files Browse the repository at this point in the history
  • Loading branch information
CGillen committed Jan 16, 2025
1 parent a24c413 commit 060b139
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 22 deletions.
6 changes: 6 additions & 0 deletions spec/factories/admin_sets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,11 @@
# false, true, or Hash with keys for permission_template
with_permission_template { false }
end

factory :complete_admin_set do
alternative_title { ['alternative admin set title'] }
creator { %w[moomin snufkin] }
description { ['Before a revolution happens', 'it is perceived as impossible'] }
end
end
end
56 changes: 44 additions & 12 deletions spec/factories/permission_templates.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
# frozen_string_literal:true
# frozen_string_literal: true

FactoryBot.define do

factory :permission_template, class: Hyrax::PermissionTemplate do
# Given that there is a one to one strong relation between permission_template and admin_set,
# with a unique index on the source_id, I don't want to have duplication in source_id
sequence(:source_id) { |n| format('%010d', n) }

trait :with_immediate_release do
release_period { Hyrax::PermissionTemplate::RELEASE_TEXT_VALUE_NO_DELAY }
end

trait :with_delayed_release do
release_period { Hyrax::PermissionTemplate::RELEASE_TEXT_VALUE_6_MONTHS }
end

before(:create) do |permission_template, evaluator|
if evaluator.with_admin_set
source_id = permission_template.source_id
admin_set =
if source_id.present?
begin
AdminSet.find(source_id)
rescue ActiveFedora::ObjectNotFoundError
create(:admin_set, id: source_id)
end
else
create(:admin_set)
end
admin_set = SourceFinder.find_or_create_admin_set(source_id)
permission_template.source_id = admin_set.id
elsif evaluator.with_collection
source_id = permission_template.source_id
Expand All @@ -39,7 +39,7 @@
after(:create) do |permission_template, evaluator|
if evaluator.with_workflows
Hyrax::Workflow::WorkflowImporter.load_workflow_for(permission_template: permission_template)
Sipity::Workflow.activate!(permission_template: permission_template, workflow_id: permission_template.available_workflows.pluck(:id).first)
Sipity::Workflow.activate!(permission_template: permission_template, workflow_id: permission_template.available_workflows.pick(:id))
end
if evaluator.with_active_workflow
workflow = create(:workflow, active: true, permission_template: permission_template)
Expand Down Expand Up @@ -78,4 +78,36 @@ def self.create_access(permission_template_id, agent_type, access, agent_ids)
end
end
end

class SourceFinder
def self.find_or_create_admin_set(source_id)
Hyrax.config.use_valkyrie? ? find_or_create_admin_set_valkyrie(source_id) : find_or_create_admin_set_active_fedora(source_id)
end

def self.find_or_create_admin_set_active_fedora(source_id)
if source_id.present?
begin
AdminSet.find(source_id)
rescue ActiveFedora::ObjectNotFoundError
FactoryBot.create(:admin_set, id: source_id)
end
else
FactoryBot.create(:admin_set)
end
end

def self.find_or_create_admin_set_valkyrie(source_id)
if source_id.present?
begin
Hyrax.query_service.find_by(id: source_id)
rescue Valkyrie::Persistence::ObjectNotFoundError
# Creating an Administrative set with a pre-determined id will not work for all adapters
# so we're letting the adapter assign the id
FactoryBot.valkyrie_create(:hyrax_admin_set)
end
else
FactoryBot.valkyrie_create(:hyrax_admin_set)
end
end
end
end
40 changes: 30 additions & 10 deletions spec/factories/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,50 @@
factory :user do
sequence(:email) { |n| "user#{n}@example.com" }
password { 'password' }
password_confirmation { 'password' }

transient do
# Allow for custom groups when a user is instantiated.
# @example create(:user, groups: 'avacado')
groups { [] }
end

after(:build) do |user, evaluator|
# In case we have the instance but it has not been persisted
::RSpec::Mocks.allow_message(user, :groups).and_return(Array.wrap(evaluator.groups))
# Given that we are stubbing the class, we need to allow for the original to be called
::RSpec::Mocks.allow_message(user.class.group_service, :fetch_groups).and_call_original
# We need to ensure that each instantiation of the admin user behaves as expected.
# This resolves the issue of both the created object being used as well as re-finding the created object.
::RSpec::Mocks.allow_message(user.class.group_service, :fetch_groups).with(user: user).and_return(Array.wrap(evaluator.groups))
User.group_service.add(user: user, groups: evaluator.groups)
end

after(:create, &:confirm)

factory :admin do
groups { ['admin'] }
end

factory :user_with_mail do
after(:create) do |user|
# Create examples of single file successes and failures
(1..10).each do |number|
file = MockFile.new(number.to_s, "Single File #{number}")
User.batch_user.send_message(user, 'File 1 could not be updated. You do not have sufficient privileges to edit it.', file.to_s, false)
User.batch_user.send_message(user, 'File 1 has been saved', file.to_s, false)
end

# Create examples of mulitple file successes and failures
files = []
(1..50).each do |number|
files << MockFile.new(number.to_s, "File #{number}")
end
User.batch_user.send_message(user, 'These files could not be updated. You do not have sufficient privileges to edit them.', 'Batch upload permission denied', false)
User.batch_user.send_message(user, 'These files have been saved', 'Batch upload complete', false)
end
end
end

trait :guest do
guest { true }
end
end

class MockFile
attr_accessor :to_s, :id
def initialize(id, string)
self.id = id
self.to_s = string
end
end

0 comments on commit 060b139

Please sign in to comment.