artists: fix editing invalid urls in artist entries (fix #3720, #3927, #3781)

Convert to an autosave association on urls. This ensures that when we
save the artist we only validate the added urls, not bad urls that we're
trying to remove, and that url validation errors are propagated up to
the artist object.

This also fixes invalid urls being saved in the artist history despite
validation failing (#3720).
This commit is contained in:
evazion
2018-10-04 17:38:44 -05:00
parent c78dece411
commit 03cc3dfa50
4 changed files with 49 additions and 45 deletions

View File

@@ -166,9 +166,19 @@ class ArtistTest < ActiveSupport::TestCase
end
should "not allow invalid urls" do
artist = FactoryBot.create(:artist, :url_string => "blah")
artist = FactoryBot.build(:artist, :url_string => "blah")
assert_equal(false, artist.valid?)
assert_equal([" blah must begin with http:// or https://"], artist.errors[:url])
assert_equal(["'blah' must begin with http:// or https:// "], artist.errors["urls.url"])
end
should "allow fixing invalid urls" do
artist = FactoryBot.build(:artist)
artist.urls << FactoryBot.build(:artist_url, url: "www.example.com", normalized_url: "www.example.com")
artist.save(validate: false)
artist.update(url_string: "http://www.example.com")
assert_equal(true, artist.valid?)
assert_equal("http://www.example.com", artist.urls.map(&:to_s).join)
end
should "make sure old urls are deleted" do

View File

@@ -17,10 +17,17 @@ class ArtistUrlTest < ActiveSupport::TestCase
end
should "allow urls to be marked as inactive" do
url = FactoryBot.create(:artist_url, :url => "-http://monet.com")
url = FactoryBot.create(:artist_url, url: "http://monet.com", is_active: false)
assert_equal("http://monet.com", url.url)
assert_equal("http://monet.com/", url.normalized_url)
refute(url.is_active?)
assert_equal("-http://monet.com", url.to_s)
end
should "disallow invalid urls" do
url = FactoryBot.build(:artist_url, url: "www.example.com")
assert_equal(false, url.valid?)
assert_match(/must begin with http/, url.errors.full_messages.join)
end
should "always add a trailing slash when normalized" do