Skip to content

Commit

Permalink
Prevent long Nation Inapplicability Alternative URLs from breaking th…
Browse files Browse the repository at this point in the history
…e database transaction

URLs which are longer than 255 characters break the database limit, so we are introducing a character limit for URLs to prevent this from happening. Users should seek to shorten their URLs.
  • Loading branch information
minhngocd committed May 29, 2024
1 parent 315cc64 commit 7daf6f9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
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
4 changes: 4 additions & 0 deletions config/locales/en/errors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,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('alternative_url', scope: %w[errors nation_inapplicability])

Check failure on line 34 in test/unit/app/models/nation_inapplicability_test.rb

View workflow job for this annotation

GitHub Actions / Lint Ruby / Run RuboCop

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping. (https://rubystyle.guide#consistent-string-literals)
end

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

Expand Down

0 comments on commit 7daf6f9

Please sign in to comment.