From 6cde8c9b0c4220db3b5598a268c197e77258a8cc Mon Sep 17 00:00:00 2001 From: J Guthrie Date: Mon, 5 Nov 2018 15:40:37 +0000 Subject: [PATCH] Changed User model to not allow nil display_name (w/ tests) --- app/models/user.rb | 2 +- test/models/user_test.rb | 12 +++++------- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 0b0d37326..17985e05f 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -88,7 +88,7 @@ class User < ActiveRecord::Base :default_url => "/assets/:class/:attachment/:style.png", :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] validates :display_name, :if => proc { |u| u.display_name_changed? }, :uniqueness => { :case_sensitive => false } diff --git a/test/models/user_test.rb b/test/models/user_test.rb index 0e618cd22..584f5df0e 100644 --- a/test/models/user_test.rb +++ b/test/models/user_test.rb @@ -65,16 +65,13 @@ class UserTest < ActiveSupport::TestCase def test_display_name_length user = build(:user) 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" assert_not user.valid?, "should not allow 2 char name" user.display_name = "" - assert_not user.valid? + assert_not user.valid?, "should not allow blank/0 char name" user.display_name = nil - # Don't understand why it isn't allowing a nil value, - # when the validates statements specifically allow it - # It appears the database does not allow null values - assert_not user.valid? + assert_not user.valid?, "should not allow nil value" end def test_display_name_valid @@ -89,7 +86,8 @@ class UserTest < ActiveSupport::TestCase "aa,", "aa?", "/;.,?", "も対応します/", "#ping", "foo\x1fbar", "foo\x7fbar", "foo\ufffebar", "foo\uffffbar", "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| user = build(:user) user.display_name = display_name -- 2.39.5