diff --git a/config/initializers/oai_client_patch.rb b/config/initializers/oai_client_patch.rb new file mode 100644 index 000000000..b4e7b6484 --- /dev/null +++ b/config/initializers/oai_client_patch.rb @@ -0,0 +1,18 @@ +# OAI 1.3.0 mutates string literals and response bodies, which Ruby 3.4 warns about. +module OaiClientMutableStrings + private + + def build_uri(verb, opts) + opts = validate_options(verb, opts) + uri = @base + uri.query = "verb=#{verb}" + opts.each_pair { |key, value| uri.query << '&' << externalize(key) << '=' << encode(value) } + uri + end + + def strip_invalid_utf_8_chars(xml) + super(xml&.dup) + end +end + +OAI::Client.prepend(OaiClientMutableStrings) diff --git a/docs/docker.md b/docs/docker.md index 7fcb5c1f6..c5c406fe2 100644 --- a/docs/docker.md +++ b/docs/docker.md @@ -71,11 +71,11 @@ Credentials are in the `.env` file. Use `db` as the server/host (choose PostgreS The full test suite can be run using the following command: - docker compose run test + docker compose run --rm test To run a specific test, you can override the command being passed: - docker compose run test rails test test/models/event_test.rb + docker compose run --rm test rails test test/models/event_test.rb ### Solr diff --git a/test/controllers/static_controller_test.rb b/test/controllers/static_controller_test.rb index 2cfd61f91..c3b6f6cb0 100644 --- a/test/controllers/static_controller_test.rb +++ b/test/controllers/static_controller_test.rb @@ -379,7 +379,7 @@ class StaticControllerTest < ActionController::TestCase end test 'should show community banner if matching community for country' do - Locator.instance.stub(:lookup, { 'country' => { 'iso_code' => 'GB', 'names' => { 'en' => 'United Kingdom' } } }) do + with_country({ 'iso_code' => 'GB', 'names' => { 'en' => 'United Kingdom' } }) do with_settings({ site: { home_page: { communities: true } } }) do get :home assert_response :success @@ -389,7 +389,7 @@ class StaticControllerTest < ActionController::TestCase end test 'should not show community banner if no matching community for country' do - Locator.instance.stub(:lookup, { 'country' => { 'iso_code' => 'SE', 'names' => { 'en' => 'Sweden' } } }) do + with_country({ 'iso_code' => 'SE', 'names' => { 'en' => 'Sweden' } }) do with_settings({ site: { home_page: { communities: true } } }) do get :home assert_response :success @@ -399,7 +399,7 @@ class StaticControllerTest < ActionController::TestCase end test 'should not show community banner if feature disabled' do - Locator.instance.stub(:lookup, { 'country' => { 'iso_code' => 'GB', 'names' => { 'en' => 'United Kingdom' } } }) do + with_country({ 'iso_code' => 'GB', 'names' => { 'en' => 'United Kingdom' } }) do with_settings({ site: { home_page: { communities: false } } }) do get :home assert_response :success @@ -410,13 +410,13 @@ class StaticControllerTest < ActionController::TestCase test 'should not show registration button if disabled for country' do with_settings({ blocked_countries: ['gb'] }) do - Locator.instance.stub(:lookup, { 'country' => { 'iso_code' => 'GB' } }) do + with_country({ 'iso_code' => 'GB' }) do get :home assert_response :success assert_select '.dropdown-item a', text: 'Register', count: 0 end - Locator.instance.stub(:lookup, { 'country' => { 'iso_code' => 'FR' } }) do + with_country({ 'iso_code' => 'FR' }) do get :home assert_response :success assert_select '.dropdown-item a', text: 'Register', count: 1 diff --git a/test/controllers/tess_devise/registrations_controller_test.rb b/test/controllers/tess_devise/registrations_controller_test.rb index 6cc0e42e4..dedcb9f46 100644 --- a/test/controllers/tess_devise/registrations_controller_test.rb +++ b/test/controllers/tess_devise/registrations_controller_test.rb @@ -127,7 +127,7 @@ class RegistrationsControllerTest < ActionController::TestCase test 'should not register user from blocked country' do assert_no_difference('User.count') do - Locator.instance.stub(:lookup, { 'country' => { 'iso_code' => 'GB' } }) do + with_country({ 'iso_code' => 'GB' }) do with_settings({ blocked_countries: ['gb'] }) do post :create, params: { user: { @@ -147,7 +147,7 @@ class RegistrationsControllerTest < ActionController::TestCase test 'should register user if country not blocked' do assert_difference('User.count') do - Locator.instance.stub(:lookup, { 'country' => { 'iso_code' => 'FR' } }) do + with_country({ 'iso_code' => 'FR' }) do with_settings({ blocked_countries: ['gb'] }) do post :create, params: { user: { diff --git a/test/integration/omniauth_test.rb b/test/integration/omniauth_test.rb index 5a1e04c37..c8e853c75 100644 --- a/test/integration/omniauth_test.rb +++ b/test/integration/omniauth_test.rb @@ -479,9 +479,11 @@ class OmniauthTest < ActionDispatch::IntegrationTest original_before_request_phase = OmniAuth.config.before_request_phase original_test_mode = OmniAuth.config.test_mode + original_allowed_hosts = OmniAuth.config.allowed_hosts OmniAuth.config.before_request_phase = before_request_phase OmniAuth.config.test_mode = false + OmniAuth.config.allowed_hosts = ['space2.mytess.training'] with_host('space2.mytess.training') do post '/users/auth/oidc' @@ -494,6 +496,7 @@ class OmniauthTest < ActionDispatch::IntegrationTest ensure OmniAuth.config.before_request_phase = original_before_request_phase OmniAuth.config.test_mode = original_test_mode + OmniAuth.config.allowed_hosts = original_allowed_hosts end end diff --git a/test/test_helper.rb b/test/test_helper.rb index ed091a0a8..58c98b385 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -40,6 +40,8 @@ end Sidekiq.testing!(:fake) +Sidekiq.default_configuration.logger = Rails.logger +OmniAuth.config.logger = Rails.logger class ActiveSupport::TestCase include SchemaHelper @@ -118,6 +120,13 @@ def with_host(host, &block) end end + def with_country(country, &block) + locator = Locator.send(:allocate) + locator.stub(:lookup, { 'country' => country }) do + Locator.stub(:instance, locator, &block) + end + end + # reset dictionaries to their default values def reset_dictionaries dictionaries = TeSS::Config.dictionaries diff --git a/test/unit/oai_client_test.rb b/test/unit/oai_client_test.rb new file mode 100644 index 000000000..4ab9a2ef4 --- /dev/null +++ b/test/unit/oai_client_test.rb @@ -0,0 +1,31 @@ +require 'test_helper' + +class OaiClientTest < ActiveSupport::TestCase + setup do + @client = OAI::Client.new('https://example.org/oai-pmh') + end + + test 'encodes OAI query options without mutating string literals' do + + assert_silent do + uri = @client.send(:build_uri, 'ListRecords', metadata_prefix: 'oai_dc', set: 'a b&c') + assert_equal 'https://example.org/oai-pmh?verb=ListRecords&metadataPrefix=oai_dc&set=a+b%26c', uri.to_s + + uri = @client.send(:build_uri, 'ListRecords', resumption_token: 'next&page') + assert_equal 'verb=ListRecords&resumptionToken=next%26page', uri.query + end + end + + test 'sanitizes frozen XML without changing its content or original encoding' do + xml = "caf\u00e9 & tea\x00".freeze + + assert_silent do + sanitized = @client.sanitize_xml(xml) + assert_equal "caf\u00e9 & tea?", sanitized + assert_equal Encoding::UTF_8, sanitized.encoding + assert_equal "caf\u00e9 & tea\x00", xml + assert_equal Encoding::UTF_8, xml.encoding + assert_nil @client.sanitize_xml(nil) + end + end +end