diff --git a/app/models/nation_inapplicability.rb b/app/models/nation_inapplicability.rb index 2ce651b5f91..1b2c2a8370b 100644 --- a/app/models/nation_inapplicability.rb +++ b/app/models/nation_inapplicability.rb @@ -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 diff --git a/config/locales/en/errors.yml b/config/locales/en/errors.yml index e2298f2b2ba..8d568d42b18 100644 --- a/config/locales/en/errors.yml +++ b/config/locales/en/errors.yml @@ -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" @@ -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." diff --git a/test/unit/app/models/nation_inapplicability_test.rb b/test/unit/app/models/nation_inapplicability_test.rb index f989b52129c..4b153085597 100644 --- a/test/unit/app/models/nation_inapplicability_test.rb +++ b/test/unit/app/models/nation_inapplicability_test.rb @@ -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)