Renamed helpers to correspond to renamed Controller classes.
[merb_radiant.git] / radiant_specs / test / unit / .svn / text-base / user_test.rb.svn-base
blobd10b942b6be7ecacbe52c05242e5e9abf6800faa
1 require File.dirname(__FILE__) + '/../test_helper'
3 class UserTest < Test::Unit::TestCase
4   
5   fixtures :users
6   test_helper :users, :validations
7   
8   def setup
9     @model = @user = User.new(VALID_USER_PARAMS)
10     @user.confirm_password = false
11   end
12   
13   def teardown
14     @user.destroy unless @user.new_record?
15   end
16   
17   def test_after_initialize
18     @user = User.new
19     assert_equal true, @user.confirm_password?
20   end
21   
22   def test_confirm_password
23     assert_equal false, @user.confirm_password?
24   end
25   
26   def test_validates_length_of
27     assert_invalid :name, '100-character limit', 'x' * 101
28     assert_valid :name, 'x' * 100
29     
30     assert_invalid :email, '255-character limit', ('x' * 247) + '@test.com'
31     assert_valid :email, ('x' * 246) + '@test.com'
32   end
33   
34   def test_validates_length_of__ranges
35     {
36       :login => 3..40,
37       :password => 5..40
38     }.each do |field, range|
39       max = 'x' * range.max
40       min = 'x' * range.min
41       one_over = 'x' + max
42       one_under = min[1..-1]
43       assert_invalid field, ('%d-character limit' % range.max), one_over
44       assert_invalid field, ('%d-character minimum' % range.min), one_under
45       assert_valid field, max, min
46     end
47   end
48   
49   def test_validates_length_of__ranges__on_existing
50     assert @user.save
51     {
52       :password => 5..40
53     }.each do |field, range|
54       max = 'x' * range.max
55       min = 'x' * range.min
56       one_over = 'x' + max
57       one_under = min[1..-1]
58       assert_invalid field, ('%d-character limit' % range.max), one_over
59       assert_invalid field, ('%d-character minimum' % range.min), one_under
60       assert_valid field, max, min
61     end
62   end
63   
64   def test_validates_presence_of
65     [:name, :login, :password, :password_confirmation].each do |field|
66       assert_invalid field, 'required', '', ' ', nil
67     end
68   end
69   
70   def test_validates_numericality_of
71     [:id].each do |field|
72       assert_valid field, '1', '0'
73       assert_invalid field, 'must be a number', 'abcd', '1,2', '1.3'
74     end
75   end
77   def test_validates_confirmation_of
78     @user.confirm_password = true
79     assert_invalid :password, 'must match confirmation', 'test'
80   end
81   
82   def test_validates_uniqueness_of
83     assert_invalid :login, 'login already in use', 'existing'
84   end
86   def test_validates_format_of
87     assert_invalid :email, 'invalid e-mail address', '@test.com', 'test@', 'testtest.com',
88       'test@test', 'test me@test.com', 'test@me.c'
89     assert_valid :email, '', 'test@test.com'
90   end
91   
92   def test_save__password_encrypted
93     @user.confirm_password = true
94     @user.password_confirmation = @user.password = 'test_password'
95     assert @user.save, "Errors: #{@user.errors.inspect}"
96     assert_equal User.sha1('test_password'), @user.password
97   end
98   
99   def test_save__existing_but_empty_password
100     assert @user.save
101     @user.password_confirmation = @user.password = ''
102     assert @user.save, "Errors: #{@user.errors.inspect}"
103     assert_equal User.sha1('coolness'), @user.password
104   end
105   
106   def test_save__existing_but_different_password
107     assert @user.save
108     @user.password_confirmation = @user.password = 'cool beans'
109     assert @user.save, "Errors: #{@user.errors.inspect}"
110     assert_equal User.sha1('cool beans'), @user.password
111   end
112   
113   def test_save__existing_but_same_password
114     assert @user.save && @user.save
115     assert_equal User.sha1('coolness'), @user.password
116   end
118   # Class Methods
119   
120   def test_authenticate
121     expected = users(:existing)
122     user = User.authenticate('existing', 'password')
123     assert_equal expected, user
124   end
126   def test_authenticate__bad_password
127     assert_nil User.authenticate('existing', 'bad password')
128   end
130   def test_authenticate__bad_user
131     assert_nil User.authenticate('nonexisting', 'password')
132   end
134   def test_sha1
135     assert_equal 'a304e6063c3cdc261ae04bf1f938d7d9d579fee5', User.sha1('test')
136   end