Permalink
Browse files

changed password validation

  • Loading branch information...
1 parent 52bb1d9 commit 8e04d95fcc16d89199690203e0ae56254ee97815 @orenomba committed Aug 8, 2012
Showing with 48 additions and 2 deletions.
  1. +12 −1 app/models/user.rb
  2. +36 −1 spec/models/user_spec.rb
View
@@ -32,9 +32,15 @@ class User < ActiveRecord::Base
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }
validates :name, :email, presence: true, uniqueness: true
+ validates :name, :format => {
+ :with => /\A[a-zA-Z\d_]+\z/,
+ :message => I18n.t("activerecord.errors.messages.invalid")
+ }
validates :email, email_address: true
validates :name, length: { in: 4..50 }
- validates :password, confirmation: true, length: { in: 8..20 }
+ validates :password, length: { in: 8..20 }, :if => :password?
+ validates :password, presence: {on: :create}
+ validates :password, confirmation: true, :if => :password?
class << self
def name?(guess)
@@ -49,4 +55,9 @@ def name?(guess)
return user.name if user
end
end
+
+ private
+ def password?
+ password.present?
+ end
end
@@ -35,7 +35,7 @@
FactoryGirl.build(:user, email: "test").should_not be_valid
end
- describe "on save" do
+ describe "on create" do
it "should have a name" do
FactoryGirl.build(:user, name: " ").should_not be_valid
end
@@ -53,6 +53,11 @@
FactoryGirl.build(:user, name: chars).should_not be_valid
end
+ it "should have a name in a format" do
+ chars = "/" * 8
+ FactoryGirl.build(:user, name: chars).should_not be_valid
+ end
+
it "should have a name no more than 50 letter long" do
chars = "a" * 51
FactoryGirl.build(:user, name: chars).should_not be_valid
@@ -69,6 +74,36 @@
end
end
+ describe "on update" do
+ before(:each) do
+ @user = FactoryGirl.create(:user)
+ end
+
+ it "won't require a password" do
+ @user.password = ""
+ @user.should be_valid
+ end
+
+ describe "if password is set" do
+ before(:each) do
+ @user.password = "x" * 8
+ @user.password_confirmation = @user.password
+ end
+
+ it "should have length" do
+ @user.password = "x" * 3
+ @user.password_confirmation = @user.password
+ @user.should_not be_valid
+ end
+
+ it "should have confirmation" do
+ @user.password = "x" * 8
+ @user.password_confirmation = "a" * 8
+ @user.should_not be_valid
+ end
+ end
+ end
+
describe "name?" do
it "should find name from email" do
saved = FactoryGirl.create(:user, email: "test@domain.local")

0 comments on commit 8e04d95

Please sign in to comment.