Improved error checking for long fa names

This commit is contained in:
Dylan Knutson
2023-05-03 15:09:46 -07:00
parent a1c287304b
commit ddd47f7520
2 changed files with 31 additions and 1 deletions

View File

@@ -48,17 +48,33 @@ class Domain::Fa::User < ReduxApplicationRecord
through: :followed_joins,
source: :follower
# FA `name` can be up to 30 chars long,
# `url_name` can be longer.
validates_presence_of(:name, :url_name)
validate do
if name && url_name
expected = self.class.name_to_url_name(name)
if url_name != expected
matches = if name.length >= 30
url_name.starts_with?(expected)
else
url_name == expected
end
if !matches
errors.add(
:name,
"name '#{name}' does not match url_name, expected #{expected} but was #{url_name}"
)
end
end
if url_name && url_name =~ /[A-Z]/
errors.add(
:url_name,
"url_name #{url_name} contains uppercase chars"
)
end
end
after_initialize do

View File

@@ -43,6 +43,20 @@ describe Domain::Fa::User do
user.destroy
end
it "can be created with long names" do
expect(described_class.create({
name: "Rails_the_7-tailed_red-orange_",
url_name: "railsthe7-tailedred-orangekits",
}).errors.full_messages).to be_empty
end
it "cannot be created with uppercase url name" do
expect(described_class.create({
name: "Foo",
url_name: "Foo",
}).errors.full_messages).to_not be_empty
end
it "posts can be moved from one user to another" do
user1 = SpecUtil.create_domain_fa_user(name: "Foo", url_name: "foo")
user2 = SpecUtil.create_domain_fa_user(name: "Bar", url_name: "bar")