Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require File.dirname(__FILE__) + '/../spec_helper'
- describe User do
- def user_attr
- {
- :login => "foo",
- :email => "foo@bar.ee",
- :first_name => "Foo",
- :last_name => "Bar",
- :password => "secret",
- :password_confirmation => "secret"
- }.extend(HashExtension)
- end
- it "should be valid" do
- User.new(user_attr).should be_valid
- end
- it "should be invalid without login" do
- user = User.new(user_attr.except(:login))
- user.should_not be_valid
- user.errors.on(:login).should == "can't be blank"
- user.login = user_attr[:login]
- user.should be_valid
- end
- it "should be invalid without email" do
- user = User.new(user_attr.except(:email))
- user.should_not be_valid
- user.errors.on(:email).should include("can't be blank")
- user.email = user_attr[:email]
- user.should be_valid
- end
- it "should be invalid without first_name" do
- user = User.new(user_attr.except(:first_name))
- user.should_not be_valid
- user.errors.on(:first_name).should == "can't be blank"
- user.first_name = user_attr[:first_name]
- user.should be_valid
- end
- it "should be invalid without last_name" do
- user = User.new(user_attr.except(:last_name))
- user.should_not be_valid
- user.errors.on(:last_name).should == "can't be blank"
- user.last_name = user_attr[:last_name]
- user.should be_valid
- end
- it "should be invalid with login longer than 16 chars" do
- user = User.new(user_attr.except(:login))
- user.login = "thisisanotherwisevalidloginthatstoolong"
- user.should_not be_valid
- user.errors.on(:login).should == "is too long (maximum is 16 characters)"
- user.login = user_attr[:login]
- user.should be_valid
- end
- it "should be invalid with email longer than 64 chars" do
- user = User.new(user_attr.except(:email))
- user.email = "thisyepisanotherwisevalidemail@thisisquiteabitlongerthanallowed.com"
- user.should_not be_valid
- user.errors.on(:email).should == "is too long (maximum is 64 characters)"
- user.email = user_attr[:email]
- user.should be_valid
- end
- it "should be invalid with first_name longer than 32 chars" do
- user = User.new(user_attr.except(:first_name))
- user.first_name = "thisiswaytoolongnametobeallowedhere"
- user.should_not be_valid
- user.errors.on(:first_name).should == "is too long (maximum is 32 characters)"
- user.first_name = user_attr[:first_name]
- user.should be_valid
- end
- it "should be invalid with last name longer than 32 chars" do
- user = User.new(user_attr.except(:last_name))
- user.last_name = "thisiswaytoolongnametobeallowedhere"
- user.should_not be_valid
- user.errors.on(:last_name).should == "is too long (maximum is 32 characters)"
- user.last_name = user_attr[:last_name]
- user.should be_valid
- end
- it "should only allow valid email address" do
- user = User.new(user_attr.except(:email))
- user.email = "foo"
- user.should_not be_valid
- user.errors.on(:email).should == "is invalid"
- user.email = "foo@bar"
- user.should_not be_valid
- user.errors.on(:email).should == "is invalid"
- user.email = user_attr[:email]
- user.should be_valid
- end
- it "should create password_hash (32) and password_salt (8) when setting password" do
- user = User.new(user_attr.except(:password, :password_confirmation))
- user.password_salt.should == ""
- user.password_hash.should == ""
- user.password = "secret"
- user.password_salt.length.should == 8
- user.password_hash.length.should == 32
- end
- it "should be invalid if password and password_confirm don't match" do
- user = User.new(user_attr.except(:password, :password_confirmation))
- user.password = "foo"
- user.password_confirmation = "bar"
- user.should_not be_valid
- user.errors.on(:password).should == "doesn't match confirmation"
- user.password = user_attr[:password]
- user.password_confirmation = user_attr[:password_confirmation]
- user.should be_valid
- end
- it "should be invalid if password and password_confirmation is empty on create" do
- user = User.new(user_attr.except(:password, :password_confirmation))
- user.should_not be_valid
- user.errors.on(:password).should == "can't be blank"
- user.password = user_attr[:password]
- user.password_confirmation = user_attr[:password_confirmation]
- user.should be_valid
- end
- it "should not allow duplicate email" do
- user1 = User.new(user_attr.except(:login))
- user1.login = "foo1"
- user1.save
- user2 = User.new(user_attr.except(:login))
- user2.login = "foo2"
- user2.should_not be_valid
- user2.errors.on(:email).should == "has already been taken"
- user2.email = "foofoo@bar.ee"
- user2.should be_valid
- user1.destroy
- end
- it "should not allow duplicate login" do
- user1 = User.new(user_attr.except(:email))
- user1.email = "foo1@bar.ee"
- user1.save
- user2 = User.new(user_attr.except(:email))
- user2.email = "foo2@bar.ee"
- user2.should_not be_valid
- user2.errors.on(:login).should == "has already been taken"
- user2.login = "foo2"
- user2.should be_valid
- user2.destroy
- end
- end
Add Comment
Please, Sign In to add comment