Renamed helpers to correspond to renamed Controller classes.
[merb_radiant.git] / radiant_specs / spec / models / .svn / text-base / user_spec.rb.svn-base
blob9b20703320e4ff122422e33aa80bf750ebcf4235
1 require File.dirname(__FILE__) + '/../spec_helper'
3 describe User, "validations" do
4   scenario :users
5   test_helper :validations
6   
7   before :each do
8     @model = @user = User.new(user_params)
9     @user.confirm_password = false
10   end
11   
12   it 'should validate length of' do
13     assert_invalid :name, '100-character limit', 'x' * 101
14     assert_valid :name, 'x' * 100
15     
16     assert_invalid :email, '255-character limit', ('x' * 247) + '@test.com'
17     assert_valid :email, ('x' * 246) + '@test.com'
18   end
19   
20   it 'should validate length ranges' do
21     {
22       :login => 3..40,
23       :password => 5..40
24     }.each do |field, range|
25       max = 'x' * range.max
26       min = 'x' * range.min
27       one_over = 'x' + max
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
32     end
33   end
34   
35   it 'should validate length ranges on existing' do
36     @user.save.should == true
37     {
38       :password => 5..40
39     }.each do |field, range|
40       max = 'x' * range.max
41       min = 'x' * range.min
42       one_over = 'x' + max
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
47     end
48   end
49   
50   it 'should validate presence' do
51     [:name, :login, :password, :password_confirmation].each do |field|
52       assert_invalid field, 'required', '', ' ', nil
53     end
54   end
55   
56   it 'should validate numericality' do
57     [:id].each do |field|
58       assert_valid field, '1', '0'
59       assert_invalid field, 'must be a number', 'abcd', '1,2', '1.3'
60     end
61   end
62   
63   it 'should validate confirmation' do
64     @user.confirm_password = true
65     assert_invalid :password, 'must match confirmation', 'test'
66   end
67   
68   it 'should validate uniqueness' do
69     assert_invalid :login, 'login already in use', 'existing'
70   end
71   
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'
76   end
77 end
79 describe User do
80   scenario :users
81   
82   before :each do
83     @user = User.new(user_params)
84     @user.confirm_password = false
85   end
86   
87   it 'should confirm the password by default' do
88     @user = User.new
89     @user.confirm_password?.should == true
90   end
91   
92   it 'should save password encrypted' do
93     @user.confirm_password = true
94     @user.password_confirmation = @user.password = 'test_password'
95     @user.save!
96     @user.password.should == User.sha1('test_password')
97   end
98   
99   it 'should save existing but empty password' do
100     @user.save!
101     @user.password_confirmation = @user.password = ''
102     @user.save!
103     @user.password.should == User.sha1('password')
104   end
105   
106   it 'should save existing but different password' do
107     @user.save!
108     @user.password_confirmation = @user.password = 'cool beans'
109     @user.save!
110     @user.password.should == User.sha1('cool beans')
111   end
112   
113   it 'should save existing but same password' do
114     @user.save! && @user.save!
115     @user.password.should == User.sha1('password')
116   end
119 describe User, "class methods" do
120   scenario :users
121   
122   it 'should authenticate with correct username and password' do
123     expected = users(:existing)
124     user = User.authenticate('existing', 'password')
125     user.should == expected
126   end
127   
128   it 'should not authenticate with bad password' do
129     User.authenticate('existing', 'bad password').should be_nil
130   end
131   
132   it 'should not authenticate with bad user' do
133     User.authenticate('nonexisting', 'password').should be_nil
134   end