From cd257918f7b16bd05219b0d53bda38d95847d583 Mon Sep 17 00:00:00 2001 From: Martin Voigt Date: Thu, 12 Mar 2026 19:11:37 +0100 Subject: [PATCH 1/4] #1256 OAI-PMH endpoint now returns only materials from current space --- app/controllers/oai_controller.rb | 3 ++- config/initializers/oai_provider.rb | 6 ------ test/controllers/oai_controller_test.rb | 17 +++++++++++++++++ test/test_helper.rb | 17 ++++++++++++++--- 4 files changed, 33 insertions(+), 10 deletions(-) diff --git a/app/controllers/oai_controller.rb b/app/controllers/oai_controller.rb index 907be2f97..ee046c16a 100644 --- a/app/controllers/oai_controller.rb +++ b/app/controllers/oai_controller.rb @@ -5,7 +5,8 @@ class OaiController < ApplicationController # GET /oai-pmh def index - provider = TrainingProvider.new + provider = TrainingProvider.new({ provider_context: :instance_based }) + provider.model = OAI::Provider::ActiveRecordWrapper.new(Material.where(visible: true, space: current_space)) response = provider.process_request(oai_params.to_h) # add XSLT prefix diff --git a/config/initializers/oai_provider.rb b/config/initializers/oai_provider.rb index 9926f2469..d2ae2a552 100644 --- a/config/initializers/oai_provider.rb +++ b/config/initializers/oai_provider.rb @@ -21,9 +21,3 @@ class TrainingProvider < OAI::Provider::Base register_format(OAIRDF.instance) end - -Rails.application.config.after_initialize do - TrainingProvider.source_model OAI::Provider::ActiveRecordWrapper.new(Material.where(visible: true)) -rescue ActiveRecord::ActiveRecordError - Rails.logger.debug 'There is no database yet or some other error, so the OAI-PMH endpoint is not configured.' -end diff --git a/test/controllers/oai_controller_test.rb b/test/controllers/oai_controller_test.rb index e8397c814..b55160def 100644 --- a/test/controllers/oai_controller_test.rb +++ b/test/controllers/oai_controller_test.rb @@ -53,6 +53,23 @@ class OaiControllerTest < ActionDispatch::IntegrationTest assert_includes identifiers, @material.doi end + test 'OAI-PMH endpoint respects current space' do + get '/oai-pmh', params: { verb: 'ListRecords', metadataPrefix: 'oai_dc' } + parsed = Nokogiri::XML(@response.body) + titles = parsed.xpath('//dc:title', @ns).map(&:text) + assert_includes titles, materials(:training_material).title + refute_includes titles, materials(:plant_space_material).title + + plant_space = spaces(:plants) + with_host(plant_space.host) do + get '/oai-pmh', params: { verb: 'ListRecords', metadataPrefix: 'oai_dc' } + parsed = Nokogiri::XML(@response.body) + titles = parsed.xpath('//dc:title', @ns).map(&:text) + refute_includes titles, materials(:training_material).title + assert_includes titles, materials(:plant_space_material).title + end + end + test 'OAI ListRecords returns material in rdf format' do get '/oai-pmh', params: { verb: 'ListRecords', metadataPrefix: 'rdf' } assert_response :success diff --git a/test/test_helper.rb b/test/test_helper.rb index ac56d9b0d..96d8ab889 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -96,11 +96,22 @@ def with_net_connection(&block) end def with_host(host, &block) - original_host = @request.host - @request.host = host + if respond_to?(:host=) + # ActionDispatch::IntegrationTest + original_host = self.host + self.host = host + else + # ActionController::TestCase + original_host = @request.headers['HOST'] + @request.headers['HOST'] = host + end block.call ensure - @request.host = original_host + if respond_to?(:host=) + self.host = original_host + else + @request.headers['HOST'] = original_host + end end # reset dictionaries to their default values From c4e913d8edb6c574dd7a030a9c5e9d654e341b24 Mon Sep 17 00:00:00 2001 From: Martin Voigt Date: Fri, 13 Mar 2026 09:03:30 +0100 Subject: [PATCH 2/4] Use intended method for filtering by current space --- app/controllers/oai_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/oai_controller.rb b/app/controllers/oai_controller.rb index ee046c16a..d9e6f8557 100644 --- a/app/controllers/oai_controller.rb +++ b/app/controllers/oai_controller.rb @@ -6,7 +6,7 @@ class OaiController < ApplicationController # GET /oai-pmh def index provider = TrainingProvider.new({ provider_context: :instance_based }) - provider.model = OAI::Provider::ActiveRecordWrapper.new(Material.where(visible: true, space: current_space)) + provider.model = OAI::Provider::ActiveRecordWrapper.new(Material.in_current_space.where(visible: true)) response = provider.process_request(oai_params.to_h) # add XSLT prefix From b3614a82ff336e90db45309f29ad41b0c82c15aa Mon Sep 17 00:00:00 2001 From: Martin Voigt Date: Fri, 13 Mar 2026 09:21:25 +0100 Subject: [PATCH 3/4] Change OAI-PMH test to match that everything is in default space --- test/controllers/oai_controller_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/controllers/oai_controller_test.rb b/test/controllers/oai_controller_test.rb index b55160def..e3f73f299 100644 --- a/test/controllers/oai_controller_test.rb +++ b/test/controllers/oai_controller_test.rb @@ -58,7 +58,7 @@ class OaiControllerTest < ActionDispatch::IntegrationTest parsed = Nokogiri::XML(@response.body) titles = parsed.xpath('//dc:title', @ns).map(&:text) assert_includes titles, materials(:training_material).title - refute_includes titles, materials(:plant_space_material).title + assert_includes titles, materials(:plant_space_material).title plant_space = spaces(:plants) with_host(plant_space.host) do From 3b018d05ec346051b7d1e1219345ae4d7b62b78b Mon Sep 17 00:00:00 2001 From: Martin Voigt Date: Fri, 13 Mar 2026 11:29:39 +0100 Subject: [PATCH 4/4] Use @request.host shortcut again in with_hosts test helper function --- test/test_helper.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/test_helper.rb b/test/test_helper.rb index 96d8ab889..df2d62de6 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -102,15 +102,15 @@ def with_host(host, &block) self.host = host else # ActionController::TestCase - original_host = @request.headers['HOST'] - @request.headers['HOST'] = host + original_host = @request.host + @request.host = host end block.call ensure if respond_to?(:host=) self.host = original_host else - @request.headers['HOST'] = original_host + @request.host = original_host end end