Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent long Nation Inapplicability Alternative URLs from breaking the database transaction #9087

Merged
merged 1 commit into from
May 30, 2024
Merged
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
1 change: 1 addition & 0 deletions app/models/nation_inapplicability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class NationInapplicability < ApplicationRecord

validates :nation_id, inclusion: { in: Nation.potentially_inapplicable.map(&:id) }
validates :alternative_url, uri: true, allow_blank: true
validates :alternative_url, length: { maximum: 255 }

attr_accessor :excluded

Expand Down
7 changes: 7 additions & 0 deletions config/locales/en/errors.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
en:
activerecord:
attributes:
publication/nation_inapplicabilities:
alternative_url: "Alternative URL for excluded nation"
errors:
messages:
invalid_date: "is not in the correct format"
Expand All @@ -11,3 +14,7 @@ en:
invalid_date: First published date is not in the correct format
blank: Enter a first published date
inclusion: First published date must be between 1/1/1900 and the present
nation_inapplicability:
attributes:
alternative_url:
too_long: "is too long. Please shorten the URL to less than 255 characters."
13 changes: 13 additions & 0 deletions test/unit/app/models/nation_inapplicability_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ class NationInapplicabilityTest < ActiveSupport::TestCase
assert inapplicability.valid?
end

test "should be valid with 255 character alternative url" do
alternative_url_255_character = "https://1HHGPav0JgJ6r1rJR34wO2Tksnimp6DjWIrJU02iQgcUK6H7he4aWZ5wrtNGOifEHoLO9afMMfNIZxoOTj6BkQE7NcBwY4fvYpCXwCFaBjnXkRqyl3LfFAIJc5GUXz64LGwQvHQHiOkFdP2fk43HkM2Dx6aHoHxdgRHRB7jVzGNLNwUBQtFdjlLv4CBHRTFMnHBtSsskEXhSGlv0TubV2uouqlUkoLSOwC3AJHa4XN1bcD23112.com"
inapplicability = build(:nation_inapplicability, alternative_url: alternative_url_255_character)
assert inapplicability.valid?
end

test "should error with more than 255 character alternative url" do
alternative_url_256_character = "https://1HHGPav0JgJ6r1rJR34wO2Tksnimp6DjWIrJU02iQgcUK6H7he4aWZ5wrtNGOifEHoLO9afMMfNIZxoOTj6BkQE7NcBwY4fvYpCXwCFaBjnXkRqyl3LfFAIJc5GUXz64LGwQvHQHiOkFdP2fk43HkM2Dx6aHoHxdgRHRB7jVzGNLNwUBQtFdjlLv4CBHRTFMnHBtSsskEXhSGlv0TubV2uouqlUkoLSOwC3AJHa4XN1bcD231125.com"
inapplicability = build(:nation_inapplicability, alternative_url: alternative_url_256_character)
assert_not inapplicability.valid?
assert_includes inapplicability.errors.messages[:alternative_url], I18n.t("activerecord.errors.models.nation_inapplicability.attributes.alternative_url.too_long")
end

test "has a virtual attribute to indicate exclusion" do
nation_inapplicability = create(:nation_inapplicability)

Expand Down