Validate User name presence and handle blank names gracefully
Addresses an issue where User#familiar_name assumed `name` was always present, potentially raising an exception during view rendering. Now User validates name presence, and User#familiar_name handles blank strings without error, in case any existing invalid records exist.
This commit is contained in:
@@ -29,7 +29,7 @@ class Signup::CompletionsControllerTest < ActionDispatch::IntegrationTest
|
||||
assert_response :redirect, "Valid params should redirect"
|
||||
end
|
||||
|
||||
test "create with invalid params" do
|
||||
test "create with blank name" do
|
||||
untenanted do
|
||||
post signup_completion_path, params: {
|
||||
signup: {
|
||||
@@ -38,6 +38,9 @@ class Signup::CompletionsControllerTest < ActionDispatch::IntegrationTest
|
||||
}
|
||||
end
|
||||
|
||||
assert_response :unprocessable_entity, "Invalid params should return unprocessable entity"
|
||||
assert_response :unprocessable_entity
|
||||
assert_select ".txt-negative" do
|
||||
assert_select "li", text: "Full name can't be blank"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -39,6 +39,27 @@ class UserTest < ActiveSupport::TestCase
|
||||
assert_equal "ÉLH", User.new(name: "Éva-Louise Hernández").initials
|
||||
end
|
||||
|
||||
test "name methods handle blank names gracefully" do
|
||||
user = User.new(name: "")
|
||||
assert_equal "", user.familiar_name
|
||||
assert_nil user.first_name
|
||||
assert_nil user.last_name
|
||||
assert_equal "", user.initials
|
||||
end
|
||||
|
||||
test "validates name presence" do
|
||||
user = User.new(account: accounts("37s"), role: "member", name: "")
|
||||
assert_not user.valid?
|
||||
assert_includes user.errors[:name], "can't be blank"
|
||||
|
||||
user.name = " "
|
||||
assert_not user.valid?
|
||||
assert_includes user.errors[:name], "can't be blank"
|
||||
|
||||
user.name = "Victor Cooper"
|
||||
assert user.valid?
|
||||
end
|
||||
|
||||
test "setup?" do
|
||||
user = users(:kevin)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user