Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions config/initializers/oai_client_patch.rb
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 2 additions & 2 deletions docs/docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 5 additions & 5 deletions test/controllers/static_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/controllers/tess_devise/registrations_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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: {
Expand Down
3 changes: 3 additions & 0 deletions test/integration/omniauth_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
9 changes: 9 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
end

Sidekiq.testing!(:fake)
Sidekiq.default_configuration.logger = Rails.logger
OmniAuth.config.logger = Rails.logger

class ActiveSupport::TestCase
include SchemaHelper
Expand Down Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions test/unit/oai_client_test.rb
Original file line number Diff line number Diff line change
@@ -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 = "<title>caf\u00e9 & tea\x00</title>".freeze

assert_silent do
sanitized = @client.sanitize_xml(xml)
assert_equal "<title>caf\u00e9 &amp; tea?</title>", sanitized
assert_equal Encoding::UTF_8, sanitized.encoding
assert_equal "<title>caf\u00e9 & tea\x00</title>", xml
assert_equal Encoding::UTF_8, xml.encoding
assert_nil @client.sanitize_xml(nil)
end
end
end
Loading