/
user_spec.rb
338 lines (284 loc) · 11 KB
/
user_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
require File.dirname(__FILE__) + '/../spec_helper'
describe User, "(general properties)" do
before(:each) do
end
it "should belong to a State" do
User.reflect_on_association(:state).macro.should == :belongs_to
end
it "should have many Commitments" do
User.reflect_on_association(:commitments).macro.should == :has_many
end
it "should have many Events through Commitments" do
reflection = User.reflect_on_association(:events)
reflection.macro.should == :has_many
reflection.options.should have_key(:through)
reflection.options[:through].should == :commitments
end
it "should have many Permissions" do
User.reflect_on_association(:permissions).macro.should == :has_many
end
it "should have many Calendars through Permissions" do
reflection = User.reflect_on_association(:calendars)
reflection.macro.should == :has_many
reflection.options.should have_key(:through)
reflection.options[:through].should == :permissions
end
it "should be composed_of an Address" do
aggr = User.reflect_on_aggregation(:address)
aggr.should_not be_nil
aggr.options[:mapping].should == [%w(street street), %w(street2 street2), %w(city city), %w(state_id state), %w(zip zip), %w(coords coords)]
state = mock_model(State, :id => 15, :code => 'NY', :country => mock_model(Country, :code => 'US'))
a = Address.new
Address.should_receive(:new).and_return(a)
u = User.new(:street => '123 Main Street', :street2 => '1st floor', :city => 'Anytown', :zip => 12345, :state => state)
u.address.should == a
end
it "should have a writable flag controlling display of personal information on contact list" do
u = User.new
u.should respond_to(:show_contact)
u.should respond_to(:show_contact=)
end
it "should have country referred through state" do
country = mock_model(Country, :name => 'Ruritania', :code => 'RU')
state = mock_model(State, :name => 'Federal District', :country => country)
user = User.new
user.should respond_to(:country)
user.state = state
user.country.should == user.state.country
end
end
describe User, "(admin?)" do
before(:each) do
@admin = mock_model(Role, :name => 'admin')
@user = mock_model(Role, :name => 'user')
@two = mock_model(Permission, :role => @admin, :calendar => mock_model(Calendar, :id => 2, :name => 'Calendar 2'))
end
it "should return false if user has no admin roles" do
@permissions = [mock_model(Permission, :role => @user, :calendar => mock_model(Calendar, :id => 1, :name => 'Calendar 1')), mock_model(Permission, :role => @user, :calendar => mock_model(Calendar, :id => 2, :name => 'Calendar 2'))]
@permissions.stub!(:find_by_role_id).and_return(nil)
u = User.new
u.permissions << @permissions
u.admin?.should == false
end
it "should return true if user has at least one admin role" do
@permissions = [mock_model(Permission, :role => @user, :calendar => mock_model(Calendar, :id => 1, :name => 'Calendar 1')), @two]
u = User.new
u.permissions << @permissions
u.permissions.stub!(:find_by_role_id).and_return(@two)
u.admin?.should == true
end
end
describe User, "(validations)" do
fixtures :users, :roles, :calendars, :permissions
=begin
it "should require at least one permission" do
user = users(:marnen)
user.should be_valid
Permission.delete(user.permissions.collect{|p| p.id})
user.reload.should_not be_valid
end
=end
it 'should create a user permission for the calendar, when there\'s only one calendar' do
Calendar.delete(calendars(:two).id) # so we only have one calendar
Calendar.count.should == 1
user = User.new(:email => 'johndoe@example.com', :password => 'foo', :password_confirmation => 'foo')
user.save!
user.permissions.should_not be_nil
user.permissions.should_not be_empty
user.permissions[0].user.should == user
user.permissions[0].calendar.should == calendars(:one)
user.permissions[0].role.should == roles(:user)
end
end
describe User, "(instance properties)" do
fixtures :users
describe "<=>" do
it "should be valid" do
User.new.should respond_to(:<=>)
User.method(:<=>).arity.should == 1
end
it "should sort on last name, first name, and e-mail address in that order" do
smith = u(['Smith', 'John', 'jsmith1@aol.com'])
(smith <=> u(['Smith', 'John', 'jsmith2@aol.com'])).should == -1
(smith <=> u(['Jones', 'Robert', 'rj123@gmail.com'])).should == 1
(smith <=> u(['Smith', 'Mary', 'aaa@aaa.com'])).should == -1
end
protected
def u(array)
User.new(:lastname => array[0], :firstname => array[1], :email => array[2])
end
end
describe "to_s" do
it "should return firstname or lastname if only one of these is defined, 'firstname lastname' if both are defined, or e-mail address if no name is defined" do
@user = User.new
@user.email = 'foo@bar.com' # arbitrary
@user.to_s.should == @user.email
@user.firstname = 'f' # arbitrary
@user.to_s.should == @user.firstname
@user.firstname = nil
@user.lastname = 'l' # arbitrary
@user.to_s.should == @user.lastname
@user.firstname = 'f'
@user.to_s.should == @user.firstname << ' ' << @user.lastname
end
it "should take an optional parameter, :first_last or :last_first" do
@user = User.new
lambda{@user.to_s}.should_not raise_error
lambda{@user.to_s :first_last}.should_not raise_error
lambda{@user.to_s :last_first}.should_not raise_error
end
describe nil do
before(:each) do
@user = User.new(:email => 'foo@bar.com', :firstname => 'f', :lastname => 'l')
end
it "should return lastname, firstname if :last_first is specified" do
@user.to_s(:last_first).should == 'l, f'
end
it "should default to :first_last if no order is specified" do
@user.to_s.should == @user.to_s(:first_last)
end
it "should raise an error if format is unrecognized" do
lambda{@user.to_s :bogus}.should raise_error
end
end
end
it "should have a 'feed_key' property initialized to a 32-character string" do
User.find(:first).feed_key.length.should == 32
end
it "should set feed_key on save" do
@u = User.find(:first)
@u.feed_key = nil
@u.reload.feed_key.length.should == 32
end
it "should not overwrite feed_key if already set" do
@u = User.find(:first)
fk = @u.feed_key
@u.reload.feed_key.should == fk
end
it "should properly deal with regenerating feed_key if it's a duplicate" do
@users = User.find(:all)
@one = @users[0]
@two = @users[1]
fk = @two.feed_key
@one.feed_key = fk
@one.reload.feed_key.should_not == fk
end
end
describe User, "(geographical features)" do
fixtures :users, :states, :countries
before(:each) do
@placemark = Geocoding::Placemark.new
@placemark.stub!(:latlon).and_return([1.0, 2.0])
Geocoding::Placemark.stub!(:new).and_return(@placemark)
@placemarks = Geocoding::Placemarks.new('Test Placemarks', Geocoding::GEO_SUCCESS)
@placemarks.stub!(:[]).and_return(@placemark)
Geocoding::Placemarks.stub!(:new).and_return(@placemarks)
Geocoding.stub!(:get).and_return(@placemarks)
Point.stub!(:from_coordinates).and_return(mock_model(Point))
@user = users(:marnen)
end
it "should save coords when successfully encoded" do
@user.should_receive(:save).once
@user.coords
end
it "should have coords (Point)" do
@user.should respond_to(:coords)
@user.coords.should_not be_nil
@user.coords.should be_a_kind_of(Point)
end
it "should not save coords when unsuccessfully encoded" do
Geocoding.should_receive(:get).and_return(false)
@user.should_not_receive(:save)
@user.coords
end
end
describe User, "(authentication structure)" do
fixtures :users, :roles, :permissions
=begin
describe 'being created' do
before do
@user = nil
@creating_user = lambda do
@user = create_user
violated "#{@user.errors.full_messages.to_sentence}" if @user.new_record?
end
end
it 'increments User#count' do
@creating_user.should change(User, :count).by(1)
end
it 'initializes #activation_code' do
@creating_user.call
@user.reload.activation_code.should_not be_nil
end
end
=end
it 'requires password' do
lambda do
u = create_user(:password => nil)
u.errors.on(:password).should_not be_nil
end.should_not change(User, :count)
end
it 'requires password confirmation' do
lambda do
u = create_user(:password_confirmation => nil)
u.errors.on(:password_confirmation).should_not be_nil
end.should_not change(User, :count)
end
it 'requires email' do
lambda do
u = create_user(:email => nil)
u.errors.on(:email).should_not be_nil
end.should_not change(User, :count)
end
it 'resets password' do
users(:quentin).update_attributes(:password => 'new password', :password_confirmation => 'new password')
User.authenticate('quentin@example.com', 'new password').should == users(:quentin)
end
it 'does not rehash password' do
users(:quentin).update_attributes(:email => 'quentin2@example.com')
User.authenticate('quentin2@example.com', 'test').should == users(:quentin)
end
it 'authenticates user' do
User.authenticate('quentin@example.com', 'test').should == users(:quentin)
end
it 'sets remember token' do
users(:quentin).remember_me
users(:quentin).remember_token.should_not be_nil
users(:quentin).remember_token_expires_at.should_not be_nil
end
it 'unsets remember token' do
users(:quentin).remember_me
users(:quentin).remember_token.should_not be_nil
users(:quentin).forget_me
users(:quentin).remember_token.should be_nil
end
it 'remembers me for one week' do
before = 1.week.from_now.utc
users(:quentin).remember_me_for 1.week
after = 1.week.from_now.utc
users(:quentin).remember_token.should_not be_nil
users(:quentin).remember_token_expires_at.should_not be_nil
users(:quentin).remember_token_expires_at.between?(before, after).should be_true
end
it 'remembers me until one week' do
time = 1.week.from_now.utc
users(:quentin).remember_me_until time
users(:quentin).remember_token.should_not be_nil
users(:quentin).remember_token_expires_at.should_not be_nil
users(:quentin).remember_token_expires_at.should == time
end
it 'remembers me default two weeks' do
before = 2.weeks.from_now.utc
users(:quentin).remember_me
after = 2.weeks.from_now.utc
users(:quentin).remember_token.should_not be_nil
users(:quentin).remember_token_expires_at.should_not be_nil
users(:quentin).remember_token_expires_at.between?(before, after).should be_true
end
protected
def create_user(options = {})
record = User.new({ :id => 1, :email => 'quire@example.com', :password => 'quire', :password_confirmation => 'quire' }.merge(options))
record.save
record
end
end