Skip to content

Commit

Permalink
Merge pull request #6090 from avalonmediasystem/lease_to_solr
Browse files Browse the repository at this point in the history
Move lengthly check for leases into indexing background job
  • Loading branch information
cjcolvar authored Nov 5, 2024
2 parents 066dbaa + 395d713 commit d2df1bc
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
12 changes: 8 additions & 4 deletions app/models/media_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,13 @@ def fill_in_solr_fields_that_need_sections(solr_doc)
solr_doc['all_comments_ssim'] = all_comments
end

def fill_in_solr_fields_needing_leases(solr_doc)
solr_doc['read_access_virtual_group_ssim'] = virtual_read_groups + leases('external').map(&:inherited_read_groups).flatten
solr_doc['read_access_ip_group_ssim'] = collect_ips_for_index(ip_read_groups + leases('ip').map(&:inherited_read_groups).flatten)
solr_doc[Hydra.config.permissions.read.group] ||= []
solr_doc[Hydra.config.permissions.read.group] += solr_doc['read_access_ip_group_ssim']
end

# Enqueue background job to do a full indexing including more costly fields that read from children
def enqueue_long_indexing
MediaObjectIndexingJob.perform_later(id)
Expand All @@ -314,10 +321,6 @@ def to_solr(include_child_fields: false)
solr_doc[ActiveFedora.index_field_mapper.solr_name("workflow_published", :facetable, type: :string)] = published? ? 'Published' : 'Unpublished'
solr_doc[ActiveFedora.index_field_mapper.solr_name("collection", :symbol, type: :string)] = collection.name if collection.present?
solr_doc[ActiveFedora.index_field_mapper.solr_name("unit", :symbol, type: :string)] = collection.unit if collection.present?
solr_doc['read_access_virtual_group_ssim'] = virtual_read_groups + leases('external').map(&:inherited_read_groups).flatten
solr_doc['read_access_ip_group_ssim'] = collect_ips_for_index(ip_read_groups + leases('ip').map(&:inherited_read_groups).flatten)
solr_doc[Hydra.config.permissions.read.group] ||= []
solr_doc[Hydra.config.permissions.read.group] += solr_doc['read_access_ip_group_ssim']
solr_doc["title_ssort"] = self.title
solr_doc["creator_ssort"] = Array(self.creator).join(', ')
solr_doc["date_ingested_ssim"] = self.create_date.strftime "%F" if self.create_date.present?
Expand All @@ -330,6 +333,7 @@ def to_solr(include_child_fields: false)
solr_doc['section_id_ssim'] = section_ids
if include_child_fields
fill_in_solr_fields_that_need_sections(solr_doc)
fill_in_solr_fields_needing_leases(solr_doc)
elsif id.present? # avoid error in test suite
# Fill in other identifier so these values aren't stripped from the solr doc while waiting for the background job
mf_docs = ActiveFedora::SolrService.query("isPartOf_ssim:#{id}", rows: 100_000)
Expand Down
6 changes: 6 additions & 0 deletions spec/controllers/catalog_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
require 'rails_helper'

describe CatalogController do
include ActiveJob::TestHelper

describe "#index" do
describe "as an un-authenticated user" do
it "should show results for items that are public and published" do
Expand Down Expand Up @@ -128,6 +130,7 @@
let!(:lti_group) { @controller.user_session[:virtual_groups].first }
it "should show results for items visible to the lti virtual group" do
mo = FactoryBot.create(:published_media_object, visibility: 'private', read_groups: [lti_group])
perform_enqueued_jobs(only: MediaObjectIndexingJob)
get 'index', params: { :q => "read_access_virtual_group_ssim:#{lti_group}" }
expect(response).to be_successful
expect(response).to render_template('catalog/index')
Expand All @@ -141,6 +144,7 @@
@user = login_as 'public'
@ip_address1 = Faker::Internet.ip_v4_address
@mo = FactoryBot.create(:published_media_object, visibility: 'private', read_groups: [@ip_address1])
perform_enqueued_jobs(only: MediaObjectIndexingJob)
end
it "should show no results when no items are visible to the user's IP address" do
get 'index', params: { :q => "" }
Expand All @@ -156,6 +160,7 @@
ip_address2 = Faker::Internet.ip_v4_address
allow_any_instance_of(ActionDispatch::Request).to receive(:remote_ip).and_return(ip_address2)
mo2 = FactoryBot.create(:published_media_object, visibility: 'private', read_groups: [ip_address2+'/30'])
perform_enqueued_jobs(only: MediaObjectIndexingJob)
get 'index', params: { :q => "" }
expect(assigns(:response).documents.count).to be >= 1
expect(assigns(:response).documents.map(&:id)).to include mo2.id
Expand All @@ -164,6 +169,7 @@
ip_address3 = Faker::Internet.ip_v6_address
allow_any_instance_of(ActionDispatch::Request).to receive(:remote_ip).and_return(ip_address3)
mo3 = FactoryBot.create(:published_media_object, visibility: 'private', read_groups: [ip_address3+'/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00'])
perform_enqueued_jobs(only: MediaObjectIndexingJob)
get 'index', params: { :q => "" }
expect(assigns(:response).documents.count).to be >= 1
expect(assigns(:response).documents.map(&:id)).to include mo3.id
Expand Down
13 changes: 11 additions & 2 deletions spec/models/media_object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
require 'cancan/matchers'

describe MediaObject do
include ActiveJob::TestHelper

let(:media_object) { FactoryBot.create(:media_object) }

it 'assigns a noid id' do
Expand Down Expand Up @@ -251,18 +253,21 @@
media_object.read_groups += [Faker::Internet.ip_v4_address]
media_object.publish! "random"
media_object.reload
perform_enqueued_jobs(only: MediaObjectIndexingJob)
expect(subject.can?(:read, media_object)).to be_falsey
end
it 'should be able to read single-ip authorized, published MediaObject' do
media_object.read_groups += [ip_addr]
media_object.publish! "random"
media_object.reload
perform_enqueued_jobs(only: MediaObjectIndexingJob)
expect(subject.can?(:read, media_object)).to be_truthy
end
it 'should be able to read ip-range authorized, published MediaObject' do
media_object.read_groups += ["#{ip_addr}/30"]
media_object.publish! "random"
media_object.reload
perform_enqueued_jobs(only: MediaObjectIndexingJob)
expect(subject.can?(:read, media_object)).to be_truthy
end
end
Expand Down Expand Up @@ -628,13 +633,17 @@
it 'includes virtual group leases in external group facet' do
media_object.governing_policies += [FactoryBot.create(:lease, inherited_read_groups: ['TestGroup'])]
media_object.save!
expect(media_object.to_solr['read_access_virtual_group_ssim']).to include('TestGroup')
media_object.reload
solr_doc = media_object.to_solr(include_child_fields: true)
expect(solr_doc['read_access_virtual_group_ssim']).to include('TestGroup')
end
it 'includes ip group leases in ip group facet' do
ip_addr = Faker::Internet.ip_v4_address
media_object.governing_policies += [FactoryBot.create(:lease, inherited_read_groups: [ip_addr])]
media_object.save!
expect(media_object.to_solr['read_access_ip_group_ssim']).to include(ip_addr)
media_object.reload
solr_doc = media_object.to_solr(include_child_fields: true)
expect(solr_doc['read_access_ip_group_ssim']).to include(ip_addr)
end
it 'indexes modified time for descMetadata subresource' do
expect(DateTime.parse(media_object.to_solr['descMetadata_modified_dtsi'])).to eq DateTime.parse(media_object.descMetadata.record_change_date.first)
Expand Down

0 comments on commit d2df1bc

Please sign in to comment.