yus 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,836 @@
1
+ #!/usr/bin/env ruby
2
+ # TestSession -- yus -- 02.06.2006 -- hwyss@ywesee.com
3
+
4
+ $: << File.expand_path('../lib', File.dirname(__FILE__))
5
+
6
+ require 'test/unit'
7
+ require 'flexmock'
8
+ require 'yus/session'
9
+ require 'digest/sha2'
10
+
11
+ module Yus
12
+ class Session
13
+ public :touch!
14
+ end
15
+ class TestAutoSession < Test::Unit::TestCase
16
+ def setup
17
+ @config = FlexMock.new
18
+ @config.should_receive(:session_timeout).and_return { 0.5 }
19
+ @persistence = FlexMock.new
20
+ @logger = FlexMock.new
21
+ @logger.should_receive(:info).and_return {}
22
+ @logger.should_receive(:debug).and_return {}
23
+ @needle = FlexMock.new
24
+ @needle.should_receive(:persistence).and_return { @persistence }
25
+ @needle.should_receive(:config).and_return { @config }
26
+ @needle.should_receive(:logger).and_return { @logger }
27
+ @session = AutoSession.new(@needle, 'domain')
28
+ end
29
+ def test_get_entity_preference__no_user
30
+ @persistence.should_receive(:find_entity, 1).times(1).and_return {}
31
+ assert_raises(UnknownEntityError) {
32
+ @session.get_entity_preference('name', 'preference_key', 'domain')
33
+ }
34
+ end
35
+ def test_get_entity_preference__no_preference
36
+ user = FlexMock.new
37
+ user.should_receive(:get_preference).and_return { |key, domain|
38
+ assert_equal('preference_key', key)
39
+ assert_equal('domain', domain)
40
+ nil
41
+ }
42
+ @persistence.should_receive(:find_entity, 1).times(1).and_return { user }
43
+ res = nil
44
+ assert_nothing_raised {
45
+ res = @session.get_entity_preference('name', 'preference_key', 'domain')
46
+ }
47
+ assert_nil(res)
48
+ end
49
+ def test_get_entity_preference__success
50
+ user = FlexMock.new
51
+ user.should_receive(:get_preference).and_return { |key, domain|
52
+ assert_equal('preference_key', key)
53
+ assert_equal('domain', domain)
54
+ 'value'
55
+ }
56
+ @persistence.should_receive(:find_entity, 1).times(1).and_return { user }
57
+ res = nil
58
+ assert_nothing_raised {
59
+ res = @session.get_entity_preference('name', 'preference_key', 'domain')
60
+ }
61
+ assert_equal('value', res)
62
+ end
63
+ def test_get_entity_preferences__no_user
64
+ @persistence.should_receive(:find_entity, 1).times(1).and_return {}
65
+ assert_raises(UnknownEntityError) {
66
+ @session.get_entity_preferences('name', ['preference_key'], 'domain')
67
+ }
68
+ end
69
+ def test_get_entity_preferences__no_preference
70
+ user = FlexMock.new
71
+ user.should_receive(:get_preference).and_return { |key, domain|
72
+ assert_equal('preference_key', key)
73
+ assert_equal('domain', domain)
74
+ nil
75
+ }
76
+ @persistence.should_receive(:find_entity, 1).times(1).and_return { user }
77
+ res = nil
78
+ assert_nothing_raised {
79
+ res = @session.get_entity_preferences('name', ['preference_key'], 'domain')
80
+ }
81
+ assert_equal({'preference_key' => nil}, res)
82
+ end
83
+ def test_get_entity_preferences__success
84
+ user = FlexMock.new
85
+ user.should_receive(:get_preference).and_return { |key, domain|
86
+ assert_equal('preference_key', key)
87
+ assert_equal('domain', domain)
88
+ 'value'
89
+ }
90
+ @persistence.should_receive(:find_entity, 1).times(1).and_return { user }
91
+ res = nil
92
+ assert_nothing_raised {
93
+ res = @session.get_entity_preferences('name', ['preference_key'], 'domain')
94
+ }
95
+ assert_equal({'preference_key' => 'value'}, res)
96
+ end
97
+ def test_set_entity_preference__no_user
98
+ @persistence.should_receive(:find_entity).and_return { |name|
99
+ assert_equal('name', name)
100
+ nil
101
+ }
102
+ assert_raises(UnknownEntityError) {
103
+ @session.set_entity_preference('name', 'key', 'value')
104
+ }
105
+ end
106
+ def test_set_entity_preference__success
107
+ entity = FlexMock.new
108
+ value = nil
109
+ @persistence.should_receive(:find_entity).and_return { |name|
110
+ assert_equal('name', name)
111
+ entity
112
+ }
113
+ entity.should_receive(:get_preference).and_return { |key, domain|
114
+ assert_equal('key', key)
115
+ value
116
+ }
117
+ entity.should_receive(:set_preference, 1).times(1).and_return { |key, val, domain|
118
+ assert_equal('key', key)
119
+ assert_equal('value', val)
120
+ value = val
121
+ }
122
+ @persistence.should_receive(:save_entity, 1).times(1).and_return { |user|
123
+ assert_equal(entity, user)
124
+ }
125
+ @session.set_entity_preference('name', 'key', 'value')
126
+ @session.set_entity_preference('name', 'key', 'other')
127
+ end
128
+ def test_create_entity__success
129
+ @config.should_receive(:digest).and_return { Digest::SHA256 }
130
+ @persistence.should_receive(:find_entity, 1).times(1).and_return {}
131
+ @persistence.should_receive(:add_entity, 1).times(1).and_return { |entity|
132
+ assert_instance_of(Entity, entity)
133
+ assert_equal('name', entity.name)
134
+ }
135
+ @session.create_entity('name', 'pass')
136
+ end
137
+ def test_create_entity__duplicate
138
+ @persistence.should_receive(:find_entity, 1).times(1).and_return { 'something' }
139
+ assert_raises(DuplicateNameError) {
140
+ @session.create_entity('name')
141
+ }
142
+ end
143
+ def test_allowed
144
+ assert_equal(false, @session.allowed?('anything at all'))
145
+ end
146
+ def test_entity_allowed__no_user
147
+ @persistence.should_receive(:find_entity, 1).times(1).and_return {}
148
+ assert_raises(UnknownEntityError) {
149
+ @session.entity_allowed?('name', 'action', 'key')
150
+ }
151
+ end
152
+ def test_entity_allowed
153
+ user = FlexMock.new
154
+ expecteds = [['action1', nil], ['action2', 'key']]
155
+ user.should_receive(:allowed?).and_return { |action, key|
156
+ eact, ekey = expecteds.shift
157
+ assert_equal(eact, action)
158
+ assert_equal(ekey, key)
159
+ action == 'action2'
160
+ }
161
+ @persistence.should_receive(:find_entity, 2).times(2).and_return { user }
162
+ assert_equal(false, @session.entity_allowed?('name', 'action1'))
163
+ assert_equal(true, @session.entity_allowed?('name', 'action2', 'key'))
164
+ end
165
+ def test_reset_entity_password__no_user
166
+ @persistence.should_receive(:find_entity, 1).times(1).and_return {}
167
+ assert_raises(UnknownEntityError) {
168
+ @session.reset_entity_password('name', 'token', 'password')
169
+ }
170
+ end
171
+ def test_reset_entity_password__no_token
172
+ user = FlexMock.new
173
+ user.should_receive(:allowed?).and_return { |action, token|
174
+ assert_equal('reset_password', action)
175
+ assert_equal('token', token)
176
+ false
177
+ }
178
+ @persistence.should_receive(:find_entity, 1).times(1).and_return { user }
179
+ res = nil
180
+ assert_raises(NotPrivilegedError) {
181
+ @session.reset_entity_password('name', 'token', 'password')
182
+ }
183
+ end
184
+ def test_reset_entity_password__success
185
+ user = FlexMock.new
186
+ user.should_receive(:allowed?).and_return { |action, token|
187
+ assert_equal('reset_password', action)
188
+ assert_equal('token', token)
189
+ true
190
+ }
191
+ user.should_receive(:passhash=, 1).times(1).and_return { |hash|
192
+ assert_equal(Digest::SHA256.hexdigest('password'), hash)
193
+ }
194
+ user.should_receive(:revoke).and_return { |action, token|
195
+ assert_equal('reset_password', action)
196
+ assert_equal('token', token)
197
+ }
198
+ @config.should_receive(:digest).and_return { Digest::SHA256 }
199
+ @persistence.should_receive(:find_entity, 1).times(1).and_return { user }
200
+ @persistence.should_receive(:save_entity, 1).times(1).and_return { |entity|
201
+ assert_equal(user, entity)
202
+ }
203
+ assert_nothing_raised {
204
+ @session.reset_entity_password('name', 'token', 'password')
205
+ }
206
+ end
207
+ def test_grant__no_user
208
+ @persistence.should_receive(:find_entity).and_return { |name|
209
+ assert_equal('username', name)
210
+ nil
211
+ }
212
+ assert_raises(UnknownEntityError) {
213
+ @session.grant('username', 'action')
214
+ }
215
+ end
216
+ def test_grant__success_key
217
+ entity = FlexMock.new
218
+ entity.should_receive(:grant).and_return { |action, key|
219
+ assert_equal('action', action)
220
+ assert_equal('key', key)
221
+ }
222
+ @persistence.should_receive(:find_entity).and_return { |name|
223
+ assert_equal('username', name)
224
+ entity
225
+ }
226
+ @persistence.should_receive(:save_entity, 1).times(1).and_return { |user|
227
+ assert_equal(entity, user)
228
+ }
229
+ @session.grant('username', 'action', 'key')
230
+ end
231
+ end
232
+ class TestEntitySession < Test::Unit::TestCase
233
+ def setup
234
+ @config = FlexMock.new
235
+ @config.should_receive(:session_timeout).and_return { 0.5 }
236
+ @user = FlexMock.new
237
+ @persistence = FlexMock.new
238
+ @logger = FlexMock.new
239
+ @logger.should_receive(:info).and_return {}
240
+ @logger.should_receive(:debug).and_return {}
241
+ @needle = FlexMock.new
242
+ @needle.should_receive(:persistence).and_return { @persistence }
243
+ @needle.should_receive(:config).and_return { @config }
244
+ @needle.should_receive(:logger).and_return { @logger }
245
+ @session = EntitySession.new(@needle, @user, 'domain')
246
+ end
247
+ def test_expired
248
+ assert_equal(false, @session.expired?)
249
+ sleep(1)
250
+ assert_equal(true, @session.expired?)
251
+ end
252
+ def test_touch
253
+ sleep(1)
254
+ assert_equal(true, @session.expired?)
255
+ @session.touch!
256
+ assert_equal(false, @session.expired?)
257
+ end
258
+ def test_name
259
+ @user.should_receive(:name).and_return { 'name' }
260
+ assert_equal('name', @session.name)
261
+ end
262
+ def test_create_entity__not_allowed
263
+ @user.should_receive(:allowed?).and_return { |action, key|
264
+ assert_equal('edit', action)
265
+ assert_equal('yus.entities', key)
266
+ false
267
+ }
268
+ assert_raises(NotPrivilegedError) {
269
+ @session.create_entity('name')
270
+ }
271
+ end
272
+ def test_create_entity__success
273
+ @user.should_receive(:allowed?).and_return { |action, key|
274
+ assert_equal('edit', action)
275
+ assert_equal('yus.entities', key)
276
+ true
277
+ }
278
+ @persistence.should_receive(:find_entity, 1).times(1).and_return {}
279
+ @persistence.should_receive(:add_entity, 1).times(1).and_return { |entity|
280
+ assert_instance_of(Entity, entity)
281
+ assert_equal('name', entity.name)
282
+ }
283
+ @session.create_entity('name')
284
+ end
285
+ def test_create_entity__duplicate
286
+ @user.should_receive(:allowed?).and_return { |action, key|
287
+ assert_equal('edit', action)
288
+ assert_equal('yus.entities', key)
289
+ true
290
+ }
291
+ @persistence.should_receive(:find_entity, 1).times(1).and_return { 'something' }
292
+ assert_raises(DuplicateNameError) {
293
+ @session.create_entity('name')
294
+ }
295
+ end
296
+ def test_destroy
297
+ assert_equal(false, @session.expired?)
298
+ @session.destroy!
299
+ assert_equal(true, @session.expired?)
300
+ assert_nil(@session.instance_variable_get('@persistence'))
301
+ assert_nil(@session.instance_variable_get('@user'))
302
+ end
303
+ def test_entities__not_allowed
304
+ @user.should_receive(:allowed?).and_return { |action, key|
305
+ assert_equal('edit', action)
306
+ assert_equal('yus.entities', key)
307
+ false
308
+ }
309
+ assert_raises(NotPrivilegedError) {
310
+ @session.entities
311
+ }
312
+ end
313
+ def test_entities__allowed
314
+ @user.should_receive(:allowed?).and_return { |action, key|
315
+ assert_equal('edit', action)
316
+ assert_equal('yus.entities', key)
317
+ true
318
+ }
319
+ @persistence.should_receive(:entities).and_return { [] }
320
+ assert_nothing_raised {
321
+ assert_equal([], @session.entities)
322
+ }
323
+ end
324
+ def test_find_entity__not_allowed
325
+ @user.should_receive(:allowed?).and_return { |action, key|
326
+ assert_equal('edit', action)
327
+ assert_equal('yus.entities', key)
328
+ false
329
+ }
330
+ assert_raises(NotPrivilegedError) {
331
+ @session.find_entity('username')
332
+ }
333
+ end
334
+ def test_find_entity__success
335
+ @persistence.should_receive(:find_entity).and_return { |name|
336
+ assert_equal('username', name)
337
+ 'found'
338
+ }
339
+ @user.should_receive(:allowed?).and_return { |action, key|
340
+ assert_equal('edit', action)
341
+ assert_equal('yus.entities', key)
342
+ true
343
+ }
344
+ assert_equal('found', @session.find_entity('username'))
345
+ end
346
+ def test_grant__not_allowed
347
+ @user.should_receive(:allowed?).and_return { |action, key|
348
+ assert_equal('grant', action)
349
+ assert_equal('action', key)
350
+ false
351
+ }
352
+ assert_raises(NotPrivilegedError) {
353
+ @session.grant('username', 'action')
354
+ }
355
+ end
356
+ def test_grant__no_user
357
+ @persistence.should_receive(:find_entity).and_return { |name|
358
+ assert_equal('username', name)
359
+ nil
360
+ }
361
+ @user.should_receive(:allowed?).and_return { |action, key|
362
+ assert_equal('grant', action)
363
+ assert_equal('action', key)
364
+ true
365
+ }
366
+ assert_raises(UnknownEntityError) {
367
+ @session.grant('username', 'action')
368
+ }
369
+ end
370
+ def test_grant__success_everything
371
+ entity = FlexMock.new
372
+ entity.should_receive(:grant).and_return { |action, key|
373
+ assert_equal('action', action)
374
+ assert_equal(:everything, key)
375
+ }
376
+ @persistence.should_receive(:find_entity).and_return { |name|
377
+ assert_equal('username', name)
378
+ entity
379
+ }
380
+ @persistence.should_receive(:save_entity, 1).times(1).and_return { |user|
381
+ assert_equal(entity, user)
382
+ }
383
+ @user.should_receive(:allowed?).and_return { |action, key|
384
+ assert_equal('grant', action)
385
+ assert_equal('action', key)
386
+ true
387
+ }
388
+ @session.grant('username', 'action')
389
+ end
390
+ def test_grant__success_key
391
+ entity = FlexMock.new
392
+ entity.should_receive(:grant).and_return { |action, key|
393
+ assert_equal('action', action)
394
+ assert_equal('key', key)
395
+ }
396
+ @persistence.should_receive(:find_entity).and_return { |name|
397
+ assert_equal('username', name)
398
+ entity
399
+ }
400
+ @persistence.should_receive(:save_entity, 1).times(1).and_return { |user|
401
+ assert_equal(entity, user)
402
+ }
403
+ @user.should_receive(:allowed?).and_return { |action, key|
404
+ assert_equal('grant', action)
405
+ assert_equal('action', key)
406
+ true
407
+ }
408
+ @session.grant('username', 'action', 'key')
409
+ end
410
+ def test_valid__success
411
+ @user.should_receive(:valid?).and_return { true }
412
+ assert_equal(true, @session.valid?)
413
+ end
414
+ def test_valid__failure
415
+ @user.should_receive(:valid?).and_return { false }
416
+ assert_equal(false, @session.valid?)
417
+ end
418
+ def test_set_password__not_allowed
419
+ @user.should_receive(:allowed?).and_return { |action, name|
420
+ assert_equal('set_password', action)
421
+ assert_equal('name', name)
422
+ false
423
+ }
424
+ assert_raises(NotPrivilegedError) {
425
+ @session.set_password('name', 'cleartext')
426
+ }
427
+ end
428
+ def test_set_password__no_user
429
+ @persistence.should_receive(:find_entity).and_return { |name|
430
+ assert_equal('username', name)
431
+ nil
432
+ }
433
+ @user.should_receive(:allowed?).and_return { |action, name|
434
+ assert_equal('set_password', action)
435
+ assert_equal('username', name)
436
+ true
437
+ }
438
+ assert_raises(UnknownEntityError) {
439
+ @session.set_password('username', 'cleartext')
440
+ }
441
+ end
442
+ def test_set_password__success
443
+ entity = FlexMock.new
444
+ @persistence.should_receive(:find_entity).and_return { |name|
445
+ assert_equal('username', name)
446
+ entity
447
+ }
448
+ @persistence.should_receive(:save_entity, 1).times(1).and_return { |user|
449
+ assert_equal(entity, user)
450
+ }
451
+ @user.should_receive(:allowed?).and_return { |action, name|
452
+ assert_equal('set_password', action)
453
+ assert_equal('username', name)
454
+ true
455
+ }
456
+ @config.should_receive(:digest).and_return { Digest::SHA256 }
457
+ entity.should_receive(:passhash=).and_return { |hash|
458
+ assert_equal(Digest::SHA256.hexdigest('cleartext'), hash)
459
+ }
460
+ @session.set_password('username', 'cleartext')
461
+ end
462
+ def test_rename__not_allowed
463
+ @user.should_receive(:allowed?).and_return { |action, name|
464
+ assert_equal('edit', action)
465
+ assert_equal('yus.entities', name)
466
+ false
467
+ }
468
+ assert_raises(NotPrivilegedError) {
469
+ @session.rename('oldname', 'newname')
470
+ }
471
+ end
472
+ def test_rename__no_user
473
+ @persistence.should_receive(:find_entity).and_return { |name|
474
+ assert_equal('oldname', name)
475
+ nil
476
+ }
477
+ @user.should_receive(:allowed?).and_return { |action, name|
478
+ assert_equal('edit', action)
479
+ assert_equal('yus.entities', name)
480
+ true
481
+ }
482
+ assert_raises(UnknownEntityError) {
483
+ @session.rename('oldname', 'newname')
484
+ }
485
+ end
486
+ def test_rename__duplicate_name
487
+ entity1 = FlexMock.new
488
+ entity2 = FlexMock.new
489
+ entities = {
490
+ 'oldname' => entity1,
491
+ 'newname' => entity2,
492
+ }
493
+ @persistence.should_receive(:find_entity).and_return { |name|
494
+ entities[name]
495
+ }
496
+ @user.should_receive(:allowed?).and_return { |action, name|
497
+ assert_equal('edit', action)
498
+ assert_equal('yus.entities', name)
499
+ true
500
+ }
501
+ assert_raises(DuplicateNameError) {
502
+ @session.rename('oldname', 'newname')
503
+ }
504
+ end
505
+ def test_rename__success
506
+ entity = FlexMock.new
507
+ entity.should_receive(:revoke, 1).times(1).and_return { |action, item|
508
+ assert_equal('set_password', action)
509
+ assert_equal('oldname', item)
510
+ }
511
+ entity.should_receive(:grant, 1).times(1).and_return { |action, item|
512
+ assert_equal('set_password', action)
513
+ assert_equal('newname', item)
514
+ }
515
+ entity.should_receive(:rename).and_return { |newname|
516
+ assert_equal('newname', newname)
517
+ }
518
+ entities = {
519
+ 'oldname' => entity,
520
+ 'newname' => nil,
521
+ }
522
+ @persistence.should_receive(:find_entity).and_return { |name|
523
+ entities[name]
524
+ }
525
+ @persistence.should_receive(:save_entity, 1).times(1).and_return { |user|
526
+ assert_equal(entity, user)
527
+ }
528
+ @user.should_receive(:allowed?).and_return { |action, name|
529
+ assert_equal('edit', action)
530
+ assert_equal('yus.entities', name)
531
+ true
532
+ }
533
+ @session.rename('oldname', 'newname')
534
+ end
535
+ def test_revoke__not_allowed
536
+ @user.should_receive(:allowed?).and_return { |action, key|
537
+ assert_equal('grant', action)
538
+ assert_equal('action', key)
539
+ false
540
+ }
541
+ assert_raises(NotPrivilegedError) {
542
+ @session.revoke('username', 'action')
543
+ }
544
+ end
545
+ def test_revoke__no_user
546
+ @persistence.should_receive(:find_entity).and_return { |name|
547
+ assert_equal('username', name)
548
+ nil
549
+ }
550
+ @user.should_receive(:allowed?).and_return { |action, key|
551
+ assert_equal('grant', action)
552
+ assert_equal('action', key)
553
+ true
554
+ }
555
+ assert_raises(UnknownEntityError) {
556
+ @session.revoke('username', 'action')
557
+ }
558
+ end
559
+ def test_revoke__success_everything
560
+ entity = FlexMock.new
561
+ entity.should_receive(:revoke).and_return { |action, key|
562
+ assert_equal('action', action)
563
+ assert_equal(:everything, key)
564
+ }
565
+ @persistence.should_receive(:find_entity).and_return { |name|
566
+ assert_equal('username', name)
567
+ entity
568
+ }
569
+ @persistence.should_receive(:save_entity, 1).times(1).and_return { |user|
570
+ assert_equal(entity, user)
571
+ }
572
+ @user.should_receive(:allowed?).and_return { |action, key|
573
+ assert_equal('grant', action)
574
+ assert_equal('action', key)
575
+ true
576
+ }
577
+ @session.revoke('username', 'action')
578
+ end
579
+ def test_revoke__success_key
580
+ entity = FlexMock.new
581
+ entity.should_receive(:revoke).and_return { |action, key|
582
+ assert_equal('action', action)
583
+ assert_equal('key', key)
584
+ }
585
+ @persistence.should_receive(:find_entity).and_return { |name|
586
+ assert_equal('username', name)
587
+ entity
588
+ }
589
+ @persistence.should_receive(:save_entity, 1).times(1).and_return { |user|
590
+ assert_equal(entity, user)
591
+ }
592
+ @user.should_receive(:allowed?).and_return { |action, key|
593
+ assert_equal('grant', action)
594
+ assert_equal('action', key)
595
+ true
596
+ }
597
+ @session.revoke('username', 'action', 'key')
598
+ end
599
+ def test_affiliate__not_allowed
600
+ @user.should_receive(:allowed?).and_return { |action, key|
601
+ assert_equal('edit', action)
602
+ assert_equal('yus.entities', key)
603
+ false
604
+ }
605
+ assert_raises(NotPrivilegedError) {
606
+ @session.affiliate('name', 'group')
607
+ }
608
+ end
609
+ def test_affiliate__no_user
610
+ @user.should_receive(:allowed?).and_return { |action, key|
611
+ assert_equal('edit', action)
612
+ assert_equal('yus.entities', key)
613
+ true
614
+ }
615
+ @persistence.should_receive(:find_entity).and_return { |name|
616
+ assert_equal('name', name)
617
+ nil
618
+ }
619
+ assert_raises(UnknownEntityError) {
620
+ @session.affiliate('name', 'group')
621
+ }
622
+ end
623
+ def test_affiliate__no_group
624
+ user = FlexMock.new
625
+ @user.should_receive(:allowed?).and_return { |action, key|
626
+ assert_equal('edit', action)
627
+ assert_equal('yus.entities', key)
628
+ true
629
+ }
630
+ names = ['name', 'group']
631
+ entities = [user, nil]
632
+ @persistence.should_receive(:find_entity).and_return { |name|
633
+ assert_equal(names.shift, name)
634
+ entities.shift
635
+ }
636
+ assert_raises(UnknownEntityError) {
637
+ @session.affiliate('name', 'group')
638
+ }
639
+ end
640
+ def test_affiliate__success
641
+ @persistence.should_receive(:save_entity).and_return {}
642
+ user = FlexMock.new
643
+ group = FlexMock.new
644
+ @user.should_receive(:allowed?).and_return { |action, key|
645
+ assert_equal('edit', action)
646
+ assert_equal('yus.entities', key)
647
+ true
648
+ }
649
+ names = ['name', 'group']
650
+ entities = [user, group]
651
+ @persistence.should_receive(:find_entity).and_return { |name|
652
+ assert_equal(names.shift, name)
653
+ entities.shift
654
+ }
655
+ user.should_receive(:join, 1).times(1).and_return { |arg|
656
+ assert_equal(group, arg)
657
+ }
658
+ @session.affiliate('name', 'group')
659
+ end
660
+ def test_disaffiliate__not_allowed
661
+ @user.should_receive(:allowed?).and_return { |action, key|
662
+ assert_equal('edit', action)
663
+ assert_equal('yus.entities', key)
664
+ false
665
+ }
666
+ assert_raises(NotPrivilegedError) {
667
+ @session.disaffiliate('name', 'group')
668
+ }
669
+ end
670
+ def test_disaffiliate__no_user
671
+ @user.should_receive(:allowed?).and_return { |action, key|
672
+ assert_equal('edit', action)
673
+ assert_equal('yus.entities', key)
674
+ true
675
+ }
676
+ @persistence.should_receive(:find_entity).and_return { |name|
677
+ assert_equal('name', name)
678
+ nil
679
+ }
680
+ assert_raises(UnknownEntityError) {
681
+ @session.disaffiliate('name', 'group')
682
+ }
683
+ end
684
+ def test_disaffiliate__no_group
685
+ user = FlexMock.new
686
+ @user.should_receive(:allowed?).and_return { |action, key|
687
+ assert_equal('edit', action)
688
+ assert_equal('yus.entities', key)
689
+ true
690
+ }
691
+ names = ['name', 'group']
692
+ entities = [user, nil]
693
+ @persistence.should_receive(:find_entity).and_return { |name|
694
+ assert_equal(names.shift, name)
695
+ entities.shift
696
+ }
697
+ assert_raises(UnknownEntityError) {
698
+ @session.disaffiliate('name', 'group')
699
+ }
700
+ end
701
+ def test_disaffiliate__success
702
+ @persistence.should_receive(:save_entity).and_return {}
703
+ user = FlexMock.new
704
+ group = FlexMock.new
705
+ @user.should_receive(:allowed?).and_return { |action, key|
706
+ assert_equal('edit', action)
707
+ assert_equal('yus.entities', key)
708
+ true
709
+ }
710
+ names = ['name', 'group']
711
+ entities = [user, group]
712
+ @persistence.should_receive(:find_entity).and_return { |name|
713
+ assert_equal(names.shift, name)
714
+ entities.shift
715
+ }
716
+ user.should_receive(:leave, 1).times(1).and_return { |arg|
717
+ assert_equal(group, arg)
718
+ }
719
+ @session.disaffiliate('name', 'group')
720
+ end
721
+ def test_set_entity_preference__not_allowed
722
+ @user.should_receive(:allowed?).and_return { |action, key|
723
+ assert_equal('edit', action)
724
+ assert_equal('yus.entities', key)
725
+ false
726
+ }
727
+ assert_raises(NotPrivilegedError) {
728
+ @session.set_entity_preference('name', 'key', 'value')
729
+ }
730
+ end
731
+ def test_set_entity_preference__no_user
732
+ @user.should_receive(:allowed?).and_return { |action, key|
733
+ assert_equal('edit', action)
734
+ assert_equal('yus.entities', key)
735
+ true
736
+ }
737
+ @persistence.should_receive(:find_entity).and_return { |name|
738
+ assert_equal('name', name)
739
+ nil
740
+ }
741
+ assert_raises(UnknownEntityError) {
742
+ @session.set_entity_preference('name', 'key', 'value')
743
+ }
744
+ end
745
+ def test_set_entity_preference__success
746
+ entity = FlexMock.new
747
+ @user.should_receive(:allowed?).and_return { |action, key|
748
+ assert_equal('edit', action)
749
+ assert_equal('yus.entities', key)
750
+ true
751
+ }
752
+ @persistence.should_receive(:find_entity).and_return { |name|
753
+ assert_equal('name', name)
754
+ entity
755
+ }
756
+ entity.should_receive(:set_preference, 1).times(1).and_return { |key, val|
757
+ assert_equal('key', key)
758
+ assert_equal('value', val)
759
+ }
760
+ @persistence.should_receive(:save_entity, 1).times(1).and_return { |user|
761
+ assert_equal(entity, user)
762
+ }
763
+ @session.set_entity_preference('name', 'key', 'value')
764
+ end
765
+ def test_set_preference
766
+ @user.should_receive(:set_preference, 1).times(1).and_return { |key, val, domain|
767
+ assert_equal('key', key)
768
+ assert_equal('value', val)
769
+ assert_equal('domain', domain)
770
+ }
771
+ @persistence.should_receive(:save_entity, 1).times(1).and_return { |user|
772
+ assert_equal(@user, user)
773
+ }
774
+ @session.set_preference('key', 'value')
775
+ end
776
+ def test_set_preferences
777
+ @user.should_receive(:set_preference, 2).times(2).and_return { |key, val, domain| }
778
+ @persistence.should_receive(:save_entity, 1).times(1).and_return { |user|
779
+ assert_equal(@user, user)
780
+ }
781
+ @session.set_preferences({'key1' => 'value1', 'key2' => 'value2'})
782
+ end
783
+ def test_get_entity_preference__no_preference
784
+ @user.should_receive(:get_preference).and_return { |key, domain|
785
+ assert_equal('preference_key', key)
786
+ assert_equal('domain', domain)
787
+ nil
788
+ }
789
+ res = nil
790
+ assert_nothing_raised {
791
+ res = @session.get_preference('preference_key')
792
+ }
793
+ assert_nil(res)
794
+ end
795
+ def test_get_entity_preference__success
796
+ @user.should_receive(:get_preference).and_return { |key, domain|
797
+ assert_equal('preference_key', key)
798
+ assert_equal('domain', domain)
799
+ 'value'
800
+ }
801
+ res = nil
802
+ assert_nothing_raised {
803
+ res = @session.get_preference('preference_key')
804
+ }
805
+ assert_equal('value', res)
806
+ end
807
+ def test_ping
808
+ assert_equal(true, @session.ping)
809
+ end
810
+ end
811
+ class TestRootSession < Test::Unit::TestCase
812
+ def setup
813
+ @config = FlexMock.new
814
+ @config.should_receive(:session_timeout).and_return { 0.5 }
815
+ @persistence = FlexMock.new
816
+ @logger = FlexMock.new
817
+ @logger.should_receive(:info).and_return {}
818
+ @logger.should_receive(:debug).and_return {}
819
+ @needle = FlexMock.new
820
+ @needle.should_receive(:persistence).and_return { @persistence }
821
+ @needle.should_receive(:config).and_return { @config }
822
+ @needle.should_receive(:logger).and_return { @logger }
823
+ @session = RootSession.new(@needle)
824
+ end
825
+ def test_valid
826
+ assert_equal(true, @session.valid?)
827
+ end
828
+ def test_allowed
829
+ assert_equal(true, @session.allowed?('anything'))
830
+ end
831
+ def test_name
832
+ @config.should_receive(:root_name).and_return { 'root_name' }
833
+ assert_equal('root_name', @session.name)
834
+ end
835
+ end
836
+ end