1 require File.dirname(__FILE__) + '/../spec_helper'
3 describe User, "validations" do
5 test_helper :validations
8 @model = @user = User.new(user_params)
9 @user.confirm_password = false
12 it 'should validate length of' do
13 assert_invalid :name, '100-character limit', 'x' * 101
14 assert_valid :name, 'x' * 100
16 assert_invalid :email, '255-character limit', ('x' * 247) + '@test.com'
17 assert_valid :email, ('x' * 246) + '@test.com'
20 it 'should validate length ranges' do
24 }.each do |field, range|
28 one_under = min[1..-1]
29 assert_invalid field, ('%d-character limit' % range.max), one_over
30 assert_invalid field, ('%d-character minimum' % range.min), one_under
31 assert_valid field, max, min
35 it 'should validate length ranges on existing' do
36 @user.save.should == true
39 }.each do |field, range|
43 one_under = min[1..-1]
44 assert_invalid field, ('%d-character limit' % range.max), one_over
45 assert_invalid field, ('%d-character minimum' % range.min), one_under
46 assert_valid field, max, min
50 it 'should validate presence' do
51 [:name, :login, :password, :password_confirmation].each do |field|
52 assert_invalid field, 'required', '', ' ', nil
56 it 'should validate numericality' do
58 assert_valid field, '1', '0'
59 assert_invalid field, 'must be a number', 'abcd', '1,2', '1.3'
63 it 'should validate confirmation' do
64 @user.confirm_password = true
65 assert_invalid :password, 'must match confirmation', 'test'
68 it 'should validate uniqueness' do
69 assert_invalid :login, 'login already in use', 'existing'
72 it 'should validate format' do
73 assert_invalid :email, 'invalid e-mail address', '@test.com', 'test@', 'testtest.com',
74 'test@test', 'test me@test.com', 'test@me.c'
75 assert_valid :email, '', 'test@test.com'
83 @user = User.new(user_params)
84 @user.confirm_password = false
87 it 'should confirm the password by default' do
89 @user.confirm_password?.should == true
92 it 'should save password encrypted' do
93 @user.confirm_password = true
94 @user.password_confirmation = @user.password = 'test_password'
96 @user.password.should == User.sha1('test_password')
99 it 'should save existing but empty password' do
101 @user.password_confirmation = @user.password = ''
103 @user.password.should == User.sha1('password')
106 it 'should save existing but different password' do
108 @user.password_confirmation = @user.password = 'cool beans'
110 @user.password.should == User.sha1('cool beans')
113 it 'should save existing but same password' do
114 @user.save! && @user.save!
115 @user.password.should == User.sha1('password')
119 describe User, "class methods" do
122 it 'should authenticate with correct username and password' do
123 expected = users(:existing)
124 user = User.authenticate('existing', 'password')
125 user.should == expected
128 it 'should not authenticate with bad password' do
129 User.authenticate('existing', 'bad password').should be_nil
132 it 'should not authenticate with bad user' do
133 User.authenticate('nonexisting', 'password').should be_nil