diff --git a/app/controllers/oai_controller.rb b/app/controllers/oai_controller.rb index 907be2f97..d9e6f8557 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.in_current_space.where(visible: true)) 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..e3f73f299 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 + assert_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..df2d62de6 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.host + @request.host = host + end block.call ensure - @request.host = original_host + if respond_to?(:host=) + self.host = original_host + else + @request.host = original_host + end end # reset dictionaries to their default values