Changesets can be listed by changeset number.
The Git repository is here.
- Revision:
- 373
- Log:
Initial import of Radiant 0.9.1, which is now packaged as a gem. This is an
import of the tagged 0.9.1 source checked out from GitHub, which isn't quite
the same as the gem distribution - but it doesn't seem to be available in an
archived form and the installed gem already has modifications, so this is
the closest I can get.
- Author:
- rool
- Date:
- Mon Mar 21 13:40:05 +0000 2011
- Size:
- 6152 Bytes
1 | require File.dirname(__FILE__) + '/../spec_helper' |
2 | |
3 | describe User, "validations" do |
4 | dataset :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, 'this must not be longer than 100 characters', 'x' * 101 |
14 | assert_valid :name, 'x' * 100 |
15 | |
16 | assert_invalid :email, 'this must not be longer than 255 characters', ('x' * 247) + '@test.com' |
17 | assert_valid :email, ('x' * 246) + '@test.com' |
18 | end |
19 | |
20 | |
21 | describe "self.unprotected_attributes" do |
22 | it "should be an array of [:name, :email, :login, :password, :password_confirmation, :locale]" do |
23 | # Make sure we clean up after anything set in another spec |
24 | User.instance_variable_set(:@unprotected_attributes, nil) |
25 | User.unprotected_attributes.should == [:name, :email, :login, :password, :password_confirmation, :locale] |
26 | end |
27 | end |
28 | describe "self.unprotected_attributes=" do |
29 | it "should set the @@unprotected_attributes variable to the given array" do |
30 | User.unprotected_attributes = [:password, :email, :other] |
31 | User.unprotected_attributes.should == [:password, :email, :other] |
32 | end |
33 | end |
34 | |
35 | it 'should validate length ranges' do |
36 | { |
37 | :login => 3..40, |
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, ('this must not be longer than %d characters' % range.max), one_over |
45 | assert_invalid field, ('this must be at least %d characters long' % range.min), one_under |
46 | assert_valid field, max, min |
47 | end |
48 | end |
49 | |
50 | it 'should validate length ranges on existing' do |
51 | @user.save.should == true |
52 | { |
53 | :password => 5..40 |
54 | }.each do |field, range| |
55 | max = 'x' * range.max |
56 | min = 'x' * range.min |
57 | one_over = 'x' + max |
58 | one_under = min[1..-1] |
59 | assert_invalid field, ('this must not be longer than %d characters' % range.max), one_over |
60 | assert_invalid field, ('this must be at least %d characters long' % range.min), one_under |
61 | assert_valid field, max, min |
62 | end |
63 | end |
64 | |
65 | it 'should validate presence' do |
66 | [:name, :login, :password, :password_confirmation].each do |field| |
67 | assert_invalid field, 'this must not be blank', '', ' ', nil |
68 | end |
69 | end |
70 | |
71 | it 'should validate numericality' do |
72 | [:id].each do |field| |
73 | assert_valid field, '1', '0' |
74 | assert_invalid field, 'this must be a number', 'abcd', '1,2', '1.3' |
75 | end |
76 | end |
77 | |
78 | it 'should validate confirmation' do |
79 | @user.confirm_password = true |
80 | assert_invalid :password, 'this must match confirmation', 'test' |
81 | end |
82 | |
83 | it 'should validate uniqueness' do |
84 | assert_invalid :login, 'this login is already in use', 'existing' |
85 | end |
86 | |
87 | it 'should validate format' do |
88 | assert_invalid :email, 'this is not a valid e-mail address', '@test.com', 'test@', 'testtest.com', |
89 | 'test@test', 'test me@test.com', 'test@me.c' |
90 | assert_valid :email, '', 'test@test.com' |
91 | end |
92 | end |
93 | |
94 | describe User do |
95 | dataset :users |
96 | |
97 | before :each do |
98 | @user = User.new(user_params) |
99 | @user.confirm_password = false |
100 | end |
101 | |
102 | it 'should confirm the password by default' do |
103 | @user = User.new |
104 | @user.confirm_password?.should == true |
105 | end |
106 | |
107 | it 'should save password encrypted' do |
108 | @user.confirm_password = true |
109 | @user.password_confirmation = @user.password = 'test_password' |
110 | @user.save! |
111 | @user.password.should == @user.sha1('test_password') |
112 | end |
113 | |
114 | it 'should save existing but empty password' do |
115 | @user.save! |
116 | @user.password_confirmation = @user.password = '' |
117 | @user.save! |
118 | @user.password.should == @user.sha1('password') |
119 | end |
120 | |
121 | it 'should save existing but different password' do |
122 | @user.save! |
123 | @user.password_confirmation = @user.password = 'cool beans' |
124 | @user.save! |
125 | @user.password.should == @user.sha1('cool beans') |
126 | end |
127 | |
128 | it 'should save existing but same password' do |
129 | @user.save! && @user.save! |
130 | @user.password.should == @user.sha1('password') |
131 | end |
132 | |
133 | it "should create a salt when encrypting the password" do |
134 | @user.salt.should be_nil |
135 | @user.send(:encrypt_password) |
136 | @user.salt.should_not be_nil |
137 | @user.password.should == @user.sha1('password') |
138 | end |
139 | |
140 | describe ".remember_me" do |
141 | before do |
142 | Radiant::Config.stub!(:[]).with('session_timeout').and_return(2.weeks) |
143 | @user.save |
144 | @user.remember_me |
145 | @user.reload |
146 | end |
147 | |
148 | it "should remember user" do |
149 | @user.session_token.should_not be_nil |
150 | end |
151 | end |
152 | |
153 | describe ".forget_me" do |
154 | |
155 | before do |
156 | Radiant::Config.stub!(:[]).with('session_timeout').and_return(2.weeks) |
157 | @user.save |
158 | @user.remember_me |
159 | end |
160 | |
161 | it "should forget user" do |
162 | @user.forget_me |
163 | @user.session_token.should be_nil |
164 | end |
165 | end |
166 | |
167 | end |
168 | |
169 | describe User, "class methods" do |
170 | dataset :users |
171 | |
172 | it 'should authenticate with correct username and password' do |
173 | expected = users(:existing) |
174 | user = User.authenticate('existing', 'password') |
175 | user.should == expected |
176 | end |
177 | |
178 | it 'should authenticate with correct email and password' do |
179 | expected = users(:existing) |
180 | user = User.authenticate('existing@example.com', 'password') |
181 | user.should == expected |
182 | end |
183 | |
184 | it 'should not authenticate with bad password' do |
185 | User.authenticate('existing', 'bad password').should be_nil |
186 | end |
187 | |
188 | it 'should not authenticate with bad user' do |
189 | User.authenticate('nonexisting', 'password').should be_nil |
190 | end |
191 | end |
192 | |
193 | describe User, "roles" do |
194 | dataset :users |
195 | |
196 | it "should not have a non-existent role" do |
197 | users(:existing).has_role?(:foo).should be_false |
198 | end |
199 | |
200 | it "should not have a role for which the corresponding method returns false" do |
201 | users(:existing).has_role?(:designer).should be_false |
202 | users(:existing).has_role?(:admin).should be_false |
203 | end |
204 | |
205 | it "should have a role for which the corresponding method returns true" do |
206 | users(:designer).has_role?(:designer).should be_true |
207 | users(:admin).has_role?(:admin).should be_true |
208 | end |
209 | end |