Skip to content

Commit

Permalink
🧹 Hyrax 5 upgrade additional specs (#2067)
Browse files Browse the repository at this point in the history
* 🧹 More spec & controller fixes

 Ref scientist-softserv/hykuup_knapsack#55

 - Contact form controller and pages controller duplicate some of the
 homepage controller behavior, so corresponding fixes are needed.
 - Update collections factory and collection_ability_spec to match hyrax
 and stop calling `.gid` on a collection_type.
 - Adjust permission_template_form_spec to adjust for removal of method
 `reset_access_controls!` and add a few new expectations instead.

* 🧹 Begin work on Roles Service

Working with Permission Templates has changed, requiring an adjustment
to both logic and specs.

* 🧹 Solr Document Ability Spec

* Rubocop fixes

* Fix typo of collection variable

* Reinstate some collections factory overrides

* Revert Rubocop changes

Caused spec failure in roles_service_spec.

* Fix create_default_admin_set_job_spec

* Restore mistakenly removed capta use

* 🧹 Stanford import specs

We have plans to remove the Stanford import logic, but for now, this
handles the specs that were failing, in an attempt to get CI to pass.

* Make Rubocop happy

* Fix typo
  • Loading branch information
laritakr authored Dec 19, 2023
1 parent 5f83a9a commit 1c04b9e
Show file tree
Hide file tree
Showing 14 changed files with 80 additions and 142 deletions.
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ Rails/ApplicationMailer:
Rails/ApplicationRecord:
Enabled: false

Rails/HasAndBelongsToMany:
Exclude:
- 'app/models/role.rb'

Rails/HasManyOrHasOneDependent:
Exclude:
- 'app/models/endpoint.rb'
Expand Down
42 changes: 22 additions & 20 deletions app/controllers/hyrax/contact_form_controller.rb
Original file line number Diff line number Diff line change
@@ -1,53 +1,55 @@
# frozen_string_literal: true

# OVERRIDE: Hyrax v3.4.0
# - add inject_theme_views method for theming
# - add homepage presenter for access to feature flippers
# - add access to content blocks in the show method
# - add @featured_collection_list to new method
# OVERRIDE: Hyrax v5.0.0
# - adds inject_theme_views method for theming
# - adds homepage presenter for access to feature flippers
# - adds access to content blocks in the show method
# - adds @featured_collection_list to new method
# - adds captcha

module Hyrax
class ContactFormController < ApplicationController
# OVERRIDE: Hyrax v3.4.0 Add for theming
# OVERRIDE: Add for theming
# Adds Hydra behaviors into the application controller
include Blacklight::SearchContext
include Blacklight::AccessControls::Catalog
before_action :build_contact_form
layout 'homepage'

# OVERRIDE: Adding inject theme views method for theming
around_action :inject_theme_views
class_attribute :model_class
self.model_class = Hyrax::ContactForm
before_action :setup_negative_captcha, only: %i[new create]
# OVERRIDE: Hyrax v3.4.0 Add for theming

# OVERRIDE: Add for theming
# The search builder for finding recent documents
# Override of Blacklight::RequestBuilders
def search_builder_class
Hyrax::HomepageSearchBuilder
end

# OVERRIDE: Hyrax v3.4.0 Add for theming
# OVERRIDE: Add for theming
class_attribute :presenter_class
# OVERRIDE: Hyrax v3.4.0 Add for theming
self.presenter_class = Hyrax::HomepagePresenter

helper Hyrax::ContentBlockHelper

def new
# OVERRIDE: Hyrax v3.4.0 Add for theming
# OVERRIDE: Add for theming
@presenter = presenter_class.new(current_ability, collections)
@featured_researcher = ContentBlock.for(:researcher)
@marketing_text = ContentBlock.for(:marketing)
@home_text = ContentBlock.for(:home_text)
@featured_work_list = FeaturedWorkList.new
# OVERRIDE: Hyrax 3.4.0 add @featured_collection_list
@featured_collection_list = FeaturedCollectionList.new
@announcement_text = ContentBlock.for(:announcement)
end

# rubocop:disable Metrics/MethodLength, Metrics/AbcSize
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
def create
# not spam, form is valid, and captcha is valid
# not spam and a valid form
# Override to include captcha
@captcha.values[:category] = params[:contact_form][:category]
@captcha.values[:contact_method] = params[:contact_form][:contact_method]
@captcha.values[:subject] = params[:contact_form][:subject]
Expand All @@ -58,14 +60,13 @@ def create
after_deliver
else
flash.now[:error] = 'Sorry, this message was not sent successfully. ' +
@contact_form.errors.full_messages.map(&:to_s).join(", ") +
"" + @captcha.error
@contact_form.errors.full_messages.map(&:to_s).join(", ")
end
render :new
rescue RuntimeError => exception
handle_create_exception(exception)
end
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength

def handle_create_exception(exception)
logger.error("Contact form failed to send: #{exception.inspect}")
Expand All @@ -90,11 +91,12 @@ def contact_form_params
end

# OVERRIDE: return collections for theming
# Return 6 collections, sorts by title
def collections(rows: 6)
builder = Hyrax::CollectionSearchBuilder.new(self)
.rows(rows)
response = repository.search(builder)
response.documents
Hyrax::CollectionsService.new(self).search_results do |builder|
builder.rows(rows)
builder.merge(sort: "title_ssi")
end
rescue Blacklight::Exceptions::ECONNREFUSED, Blacklight::Exceptions::InvalidRequest
[]
end
Expand Down
20 changes: 10 additions & 10 deletions app/controllers/hyrax/pages_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

# OVERRIDE: Hyrax v3.4.0
# OVERRIDE: Hyrax v5.0.0
# - add inject_theme_views method for theming
# - add homepage presenter for access to feature flippers
# - add access to content blocks in the show method
Expand All @@ -12,15 +12,15 @@ class PagesController < ApplicationController
load_and_authorize_resource class: ContentBlock, except: :show
layout :pages_layout

# OVERRIDE: Hyrax v3.4.0 Add for theming
# OVERRIDE: Add for theming
# Adds Hydra behaviors into the application controller
include Blacklight::SearchContext
include Blacklight::AccessControls::Catalog

# OVERRIDE: Adding inject theme views method for theming
around_action :inject_theme_views

# OVERRIDE: Hyrax v3.4.0 Add for theming
# OVERRIDE: Add for theming
# The search builder for finding recent documents
# Override of Blacklight::RequestBuilders
def search_builder_class
Expand All @@ -29,20 +29,19 @@ def search_builder_class

# OVERRIDE: Hyrax v3.4.0 Add for theming
class_attribute :presenter_class
# OVERRIDE: Hyrax v3.4.0 Add for theming
self.presenter_class = Hyrax::HomepagePresenter

helper Hyrax::ContentBlockHelper

def show
@page = ContentBlock.for(params[:key])
# OVERRIDE: Hyrax v3.4.0 Add for theming

# OVERRIDE: Additional for theming
@presenter = presenter_class.new(current_ability, collections)
@featured_researcher = ContentBlock.for(:researcher)
@marketing_text = ContentBlock.for(:marketing)
@home_text = ContentBlock.for(:home_text)
@featured_work_list = FeaturedWorkList.new
# OVERRIDE here to add featured collection list to show page
@featured_collection_list = FeaturedCollectionList.new
@announcement_text = ContentBlock.for(:announcement)
end
Expand Down Expand Up @@ -87,11 +86,12 @@ def pages_layout
end

# OVERRIDE: return collections for theming
# Return 6 collections, sorts by title
def collections(rows: 6)
builder = Hyrax::CollectionSearchBuilder.new(self)
.rows(rows)
response = repository.search(builder)
response.documents
Hyrax::CollectionsService.new(self).search_results do |builder|
builder.rows(rows)
builder.merge(sort: "title_ssi")
end
rescue Blacklight::Exceptions::ECONNREFUSED, Blacklight::Exceptions::InvalidRequest
[]
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/role.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

class Role < ApplicationRecord
has_many :users, through: :users_roles
has_and_belongs_to_many :users, join_table: :users_roles
has_many :group_roles, dependent: :destroy
has_many :groups, through: :group_roles

Expand Down
4 changes: 2 additions & 2 deletions app/services/roles_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def create_collection_accesses!
agent_id: 'collection_reader'
)

c.reset_access_controls! if pt.access_grants.count != original_access_grants_count
pt.reset_access_controls_for(collection: c) if pt.access_grants.count != original_access_grants_count
end
end
# rubocop:enable Metrics/MethodLength
Expand Down Expand Up @@ -165,7 +165,7 @@ def create_admin_set_accesses!
agent_id: 'work_editor'
)

as.reset_access_controls! if pt.access_grants.count != original_access_grants_count
pt.reset_access_controls_for(collection: as) if pt.access_grants.count != original_access_grants_count
end
end
# rubocop:enable Metrics/MethodLength
Expand Down
2 changes: 1 addition & 1 deletion lib/stanford/importer/purl_retriever.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def path

def conn
@conn ||= Faraday.new(url: 'https://purl.stanford.edu') do |faraday|
faraday.adapter :httpclient
faraday.adapter Faraday.default_adapter
faraday.use Faraday::Response::RaiseError # raise exceptions on 40x, 50x responses
end
end
Expand Down
8 changes: 4 additions & 4 deletions spec/abilities/collection_ability_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
let(:ability) { Ability.new(current_user) }
let(:user) { create(:user) }
let(:current_user) { user }
let(:collection_type_gid) { create(:collection_type).gid }
let(:collection_type_gid) { create(:collection_type).to_global_id.to_s }
let(:solr_document) { SolrDocument.new(collection.to_solr) }
let(:id) { collection.id }

Expand Down Expand Up @@ -321,7 +321,7 @@
permission_template: collection.permission_template,
agent_type: 'user',
agent_id: user.user_key)
collection.reset_access_controls!
collection.permission_template.reset_access_controls_for(collection:)
end

it 'allows most abilities' do
Expand Down Expand Up @@ -366,7 +366,7 @@
permission_template: collection.permission_template,
agent_type: 'user',
agent_id: user.user_key)
collection.reset_access_controls!
collection.permission_template.reset_access_controls_for(collection:)
end

it 'allows deposit related abilities' do
Expand Down Expand Up @@ -413,7 +413,7 @@
permission_template: collection.permission_template,
agent_type: 'user',
agent_id: user.user_key)
collection.reset_access_controls!
collection.permission_template.reset_access_controls_for(collection:)
end

it 'allows viewing only ability' do
Expand Down
2 changes: 1 addition & 1 deletion spec/abilities/solr_document_ability_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

# OVERRIDE: add specs for custom ability logic
context 'with Collection solr doc' do
let(:collection_type_gid) { create(:collection_type).gid }
let(:collection_type_gid) { create(:collection_type).to_global_id }
let(:collection) do
create(
:collection_lw,
Expand Down
Loading

0 comments on commit 1c04b9e

Please sign in to comment.