Changed User model to not allow nil display_name (w/ tests)

This commit is contained in:
J Guthrie 2018-11-05 15:40:37 +00:00
parent e091246ffc
commit 6cde8c9b0c
2 changed files with 6 additions and 8 deletions

View file

@ -88,7 +88,7 @@ class User < ActiveRecord::Base
:default_url => "/assets/:class/:attachment/:style.png", :default_url => "/assets/:class/:attachment/:style.png",
:styles => { :large => "100x100>", :small => "50x50>" } :styles => { :large => "100x100>", :small => "50x50>" }
validates :display_name, :presence => true, :allow_nil => true, :length => 3..255, validates :display_name, :presence => true, :length => 3..255,
:exclusion => %w[new terms save confirm confirm-email go_public reset-password forgot-password suspended] :exclusion => %w[new terms save confirm confirm-email go_public reset-password forgot-password suspended]
validates :display_name, :if => proc { |u| u.display_name_changed? }, validates :display_name, :if => proc { |u| u.display_name_changed? },
:uniqueness => { :case_sensitive => false } :uniqueness => { :case_sensitive => false }

View file

@ -65,16 +65,13 @@ class UserTest < ActiveSupport::TestCase
def test_display_name_length def test_display_name_length
user = build(:user) user = build(:user)
user.display_name = "123" user.display_name = "123"
assert user.valid?, " should allow nil display name" assert user.valid?, "should allow 3 char name name"
user.display_name = "12" user.display_name = "12"
assert_not user.valid?, "should not allow 2 char name" assert_not user.valid?, "should not allow 2 char name"
user.display_name = "" user.display_name = ""
assert_not user.valid? assert_not user.valid?, "should not allow blank/0 char name"
user.display_name = nil user.display_name = nil
# Don't understand why it isn't allowing a nil value, assert_not user.valid?, "should not allow nil value"
# when the validates statements specifically allow it
# It appears the database does not allow null values
assert_not user.valid?
end end
def test_display_name_valid def test_display_name_valid
@ -89,7 +86,8 @@ class UserTest < ActiveSupport::TestCase
"aa,", "aa?", "/;.,?", "も対応します/", "#ping", "aa,", "aa?", "/;.,?", "も対応します/", "#ping",
"foo\x1fbar", "foo\x7fbar", "foo\ufffebar", "foo\uffffbar", "foo\x1fbar", "foo\x7fbar", "foo\ufffebar", "foo\uffffbar",
"new", "terms", "save", "confirm", "confirm-email", "new", "terms", "save", "confirm", "confirm-email",
"go_public", "reset-password", "forgot-password", "suspended"] "go_public", "reset-password", "forgot-password", "suspended",
"trailing whitespace ", " leading whitespace"]
ok.each do |display_name| ok.each do |display_name|
user = build(:user) user = build(:user)
user.display_name = display_name user.display_name = display_name