From bcfcfd7c987c09f7373eee7e5b3a078d3a709510 Mon Sep 17 00:00:00 2001 From: Alex Boyd Date: Mon, 22 Jan 2024 19:27:19 +0000 Subject: [PATCH 1/9] Add rspec configs --- .rspec | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .rspec diff --git a/.rspec b/.rspec new file mode 100644 index 00000000..f6f85f5b --- /dev/null +++ b/.rspec @@ -0,0 +1,3 @@ +--require spec_helper +--color +--format documentation \ No newline at end of file From 08bc4c21f8916a7cc4078e3b1cb6bbb43706612b Mon Sep 17 00:00:00 2001 From: Alex Boyd Date: Mon, 22 Jan 2024 19:30:43 +0000 Subject: [PATCH 2/9] Add admin_set factory --- spec/factories/admin_sets.rb | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 spec/factories/admin_sets.rb diff --git a/spec/factories/admin_sets.rb b/spec/factories/admin_sets.rb new file mode 100644 index 00000000..5a61b842 --- /dev/null +++ b/spec/factories/admin_sets.rb @@ -0,0 +1,27 @@ +# from https://github.com/samvera/hyrax/blob/v2.9.6/spec/factories/admin_sets.rb + +FactoryBot.define do + factory :admin_set do + sequence(:title) { |n| ["Title #{n}"] } + + # Given the relationship between permission template and admin set, when + # an admin set is created via a factory, I believe it is appropriate to go ahead and + # create the corresponding permission template + # + # This way, we can go ahead + after(:create) do |admin_set, evaluator| + if evaluator.with_permission_template + attributes = { source_id: admin_set.id } + attributes = evaluator.with_permission_template.merge(attributes) if evaluator.with_permission_template.respond_to?(:merge) + # There is a unique constraint on permission_templates.source_id; I don't want to + # create a permission template if one already exists for this admin_set + create(:permission_template, attributes) unless Hyrax::PermissionTemplate.find_by(source_id: admin_set.id) + end + end + + transient do + # false, true, or Hash with keys for permission_template + with_permission_template { false } + end + end +end \ No newline at end of file From a31edce801d2217fb12b8f98bf32bb08a0a471c1 Mon Sep 17 00:00:00 2001 From: Alex Boyd Date: Mon, 22 Jan 2024 19:31:33 +0000 Subject: [PATCH 3/9] Add gw_works factory --- spec/factories/gw_works.rb | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 spec/factories/gw_works.rb diff --git a/spec/factories/gw_works.rb b/spec/factories/gw_works.rb new file mode 100644 index 00000000..2d5b0dac --- /dev/null +++ b/spec/factories/gw_works.rb @@ -0,0 +1,7 @@ +FactoryBot.define do + factory :gw_work do + id { Noid::Rails::Service.new.mint } + title { [Faker::Book.title] } + visibility { Hydra::AccessControls::AccessRight::VISIBILITY_TEXT_VALUE_PUBLIC } + end +end \ No newline at end of file From e129587a2b7328ccaff98000e58cfa6e075d2fbf Mon Sep 17 00:00:00 2001 From: Alex Boyd Date: Mon, 22 Jan 2024 19:32:27 +0000 Subject: [PATCH 4/9] Add permission_templates factory --- spec/factories/permission_templates.rb | 98 ++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 spec/factories/permission_templates.rb diff --git a/spec/factories/permission_templates.rb b/spec/factories/permission_templates.rb new file mode 100644 index 00000000..e5d20226 --- /dev/null +++ b/spec/factories/permission_templates.rb @@ -0,0 +1,98 @@ +# frozen_string_literal: true +# From https://github.com/samvera/hyrax/blob/v2.9.6/spec/factories/permission_templates.rb +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) } + + 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 + permission_template.source_id = admin_set.id + elsif evaluator.with_collection + source_id = permission_template.source_id + collection = + if source_id.present? + begin + Collection.find(source_id) + rescue ActiveFedora::ObjectNotFoundError + create(:collection, id: source_id) + end + else + create(:collection) + end + permission_template.source_id = collection.id + end + end + + 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) + end + if evaluator.with_active_workflow + workflow = create(:workflow, active: true, permission_template: permission_template) + create(:workflow_action, workflow: workflow) # Need to create a single action that can be taken + end + AccessHelper.create_access(permission_template, 'user', :manage, evaluator.manage_users) if evaluator.manage_users.present? + AccessHelper.create_access(permission_template, 'group', :manage, evaluator.manage_groups) if evaluator.manage_groups.present? + AccessHelper.create_access(permission_template, 'user', :deposit, evaluator.deposit_users) if evaluator.deposit_users.present? + AccessHelper.create_access(permission_template, 'group', :deposit, evaluator.deposit_groups) if evaluator.deposit_groups.present? + AccessHelper.create_access(permission_template, 'user', :view, evaluator.view_users) if evaluator.view_users.present? + AccessHelper.create_access(permission_template, 'group', :view, evaluator.view_groups) if evaluator.view_groups.present? + end + + transient do + with_admin_set { false } + with_collection { false } + with_workflows { false } + with_active_workflow { false } + manage_users { nil } + manage_groups { nil } + deposit_users { nil } + deposit_groups { nil } + view_users { nil } + view_groups { nil } + end + end + + factory :permission_template_access, class: Hyrax::PermissionTemplateAccess do + permission_template + trait :manage do + access { 'manage' } + end + + trait :deposit do + access { 'deposit' } + end + + trait :view do + access { 'view' } + end + end + + # rubocop:disable Lint/ConstantDefinitionInBlock + class AccessHelper + def self.create_access(permission_template_id, agent_type, access, agent_ids) + agent_ids.each do |agent_id| + FactoryBot.create(:permission_template_access, + access, + permission_template: permission_template_id, + agent_type: agent_type, + agent_id: agent_id) + end + end + end + # rubocop:enable Lint/ConstantDefinitionInBlock +end \ No newline at end of file From 7d8f2eff0398c099f141169a88c1ea51841c666f Mon Sep 17 00:00:00 2001 From: Alex Boyd Date: Mon, 22 Jan 2024 19:33:08 +0000 Subject: [PATCH 5/9] Add users factory --- spec/factories/users.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 spec/factories/users.rb diff --git a/spec/factories/users.rb b/spec/factories/users.rb new file mode 100644 index 00000000..7210d3d8 --- /dev/null +++ b/spec/factories/users.rb @@ -0,0 +1,20 @@ +FactoryBot.define do + factory :user do + email { Faker::Internet.email } + password { Faker::Internet.password } + + factory :admin_user do + after :create do |user| + admin_role = Role.find_or_create_by(name: 'admin') + admin_role.users << user + end + end + + factory :content_admin_user do + after :create do |user| + content_admin_role = Role.find_or_create_by(name: 'content-admin') + content_admin_role.users << user + end + end + end +end \ No newline at end of file From 16f7428aaf1d6627831e7a4a73e363a3a761a475 Mon Sep 17 00:00:00 2001 From: Alex Boyd Date: Mon, 22 Jan 2024 19:34:37 +0000 Subject: [PATCH 6/9] Add database_cleaner to rails_helper --- spec/rails_helper.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index b6317b5a..be80b203 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -5,6 +5,7 @@ # Prevent database truncation if the environment is production abort("The Rails environment is running in production mode!") if Rails.env.production? require 'rspec/rails' +require_relative "../spec/support/database_cleaner" # Add additional requires below this line. Rails is not loaded until this point! # Requires supporting ruby files with custom matchers and macros, etc, in From c0afd3af3b8f88969c749fe6e0b238f4b6ac6382 Mon Sep 17 00:00:00 2001 From: Alex Boyd Date: Mon, 22 Jan 2024 19:35:19 +0000 Subject: [PATCH 7/9] Set transactional fixtures to false --- spec/rails_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index be80b203..96f37cc2 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -38,7 +38,7 @@ # If you're not using ActiveRecord, or you'd prefer not to run each of your # examples within a transaction, remove the following line or assign false # instead of true. - config.use_transactional_fixtures = true + config.use_transactional_fixtures = false # You can uncomment this line to turn off ActiveRecord support entirely. # config.use_active_record = false From 56ef4e82674ccd86ee4a8809196e9fd595cd04c1 Mon Sep 17 00:00:00 2001 From: Alex Boyd Date: Mon, 22 Jan 2024 19:36:20 +0000 Subject: [PATCH 8/9] Add capybara and simplecov to spec_helper --- spec/spec_helper.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ce33d66d..18319fd5 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -13,10 +13,18 @@ # it. # # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration + +require 'capybara/rspec' +require 'simplecov' +SimpleCov.start 'rails' + RSpec.configure do |config| # rspec-expectations config goes here. You can use an alternate # assertion/expectation library such as wrong or the stdlib/minitest # assertions if you prefer. + + config.backtrace_exclusion_patterns = [] + config.expect_with :rspec do |expectations| # This option will default to `true` in RSpec 4. It makes the `description` # and `failure_message` of custom matchers include text for helper methods From 725bdc8cd2a96ee5674b82115868a729d4ae20e3 Mon Sep 17 00:00:00 2001 From: Alex Boyd Date: Mon, 22 Jan 2024 19:37:11 +0000 Subject: [PATCH 9/9] Add database_cleaner configs --- spec/support/database_cleaner.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 spec/support/database_cleaner.rb diff --git a/spec/support/database_cleaner.rb b/spec/support/database_cleaner.rb new file mode 100644 index 00000000..5dc4c9b4 --- /dev/null +++ b/spec/support/database_cleaner.rb @@ -0,0 +1,19 @@ +RSpec.configure do |config| + + config.before(:suite) do + DatabaseCleaner.clean_with :truncation, except: %w(ar_internal_metadata) + end + + config.before(:each) do + DatabaseCleaner.strategy = :transaction + end + + config.before(:each) do + DatabaseCleaner.start + end + + config.after(:each) do + DatabaseCleaner.clean + end + +end \ No newline at end of file