yus 1.0.2 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -13
- data/.gitignore +3 -0
- data/.ruby-version +1 -0
- data/.travis.yml +22 -0
- data/0001-Added-support-for-running-yus-via-docker.patch +172 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +84 -0
- data/History.txt +11 -0
- data/Manifest.txt +25 -0
- data/Rakefile +26 -0
- data/bin/yus_add_user +5 -41
- data/bin/yus_delete_user +3 -37
- data/bin/yus_dump +19 -0
- data/bin/yus_grant +8 -32
- data/bin/yus_passwd +5 -41
- data/bin/yus_show +8 -34
- data/bin/yusd +2 -2
- data/data/yus_dump.yml +42 -0
- data/lib/data/yus_dump.yml +42 -0
- data/lib/yus/helpers.rb +74 -0
- data/lib/yus/server.rb +1 -2
- data/lib/yus/session.rb +2 -1
- data/lib/yus/version.rb +3 -0
- data/readme.md +27 -0
- data/second.env +1 -0
- data/yus.gemspec +45 -0
- data/yus_migrate_to_utf_8 +214 -0
- metadata +239 -44
- data/.gemtest +0 -0
- data/README.txt +0 -35
- data/test/suite.rb +0 -15
- data/test/test_entity.rb +0 -238
- data/test/test_privilege.rb +0 -57
- data/test/test_server.rb +0 -127
- data/test/test_session.rb +0 -909
data/test/test_session.rb
DELETED
@@ -1,909 +0,0 @@
|
|
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
|
-
gem "minitest"
|
7
|
-
require "minitest/autorun"
|
8
|
-
require 'flexmock'
|
9
|
-
require 'yus/session'
|
10
|
-
require 'digest/sha2'
|
11
|
-
require 'fileutils'
|
12
|
-
require File.expand_path(File.dirname(__FILE__)+'/helpers.rb')
|
13
|
-
|
14
|
-
module Yus
|
15
|
-
class Session
|
16
|
-
public :touch!
|
17
|
-
end
|
18
|
-
class TestAutoSession < Minitest::Test
|
19
|
-
def setup
|
20
|
-
@config = FlexMock.new
|
21
|
-
@config.should_receive(:session_timeout).and_return { 0.5 }
|
22
|
-
@persistence = FlexMock.new
|
23
|
-
@logger = FlexMock.new
|
24
|
-
@logger.should_receive(:info).and_return {}
|
25
|
-
@logger.should_receive(:debug).and_return {}
|
26
|
-
@needle = FlexMock.new
|
27
|
-
@needle.should_receive(:persistence).and_return { @persistence }
|
28
|
-
@needle.should_receive(:config).and_return { @config }
|
29
|
-
@needle.should_receive(:logger).and_return { @logger }
|
30
|
-
@session = AutoSession.new(@needle, 'domain')
|
31
|
-
end
|
32
|
-
def test_get_entity_preference__no_user
|
33
|
-
@persistence.should_receive(:find_entity, 1).times(1).and_return {}
|
34
|
-
assert_raises(UnknownEntityError) {
|
35
|
-
@session.get_entity_preference('name', 'preference_key', 'domain')
|
36
|
-
}
|
37
|
-
end
|
38
|
-
def test_get_entity_preference__no_preference
|
39
|
-
user = FlexMock.new
|
40
|
-
user.should_receive(:get_preference).and_return { |key, domain|
|
41
|
-
assert_equal('preference_key', key)
|
42
|
-
assert_equal('domain', domain)
|
43
|
-
nil
|
44
|
-
}
|
45
|
-
@persistence.should_receive(:find_entity, 1).times(1).and_return { user }
|
46
|
-
res = nil
|
47
|
-
res = @session.get_entity_preference('name', 'preference_key', 'domain')
|
48
|
-
assert_nil(res)
|
49
|
-
end
|
50
|
-
def test_get_entity_preference__success
|
51
|
-
user = FlexMock.new
|
52
|
-
user.should_receive(:get_preference).and_return { |key, domain|
|
53
|
-
assert_equal('preference_key', key)
|
54
|
-
assert_equal('domain', domain)
|
55
|
-
'value'
|
56
|
-
}
|
57
|
-
@persistence.should_receive(:find_entity, 1).times(1).and_return { user }
|
58
|
-
res = nil
|
59
|
-
res = @session.get_entity_preference('name', 'preference_key', 'domain')
|
60
|
-
assert_equal('value', res)
|
61
|
-
end
|
62
|
-
def test_get_entity_preferences__no_user
|
63
|
-
@persistence.should_receive(:find_entity, 1).times(1).and_return {}
|
64
|
-
assert_raises(UnknownEntityError) {
|
65
|
-
@session.get_entity_preferences('name', ['preference_key'], 'domain')
|
66
|
-
}
|
67
|
-
end
|
68
|
-
def test_get_entity_preferences__no_preference
|
69
|
-
user = FlexMock.new
|
70
|
-
user.should_receive(:get_preference).and_return { |key, domain|
|
71
|
-
assert_equal('preference_key', key)
|
72
|
-
assert_equal('domain', domain)
|
73
|
-
nil
|
74
|
-
}
|
75
|
-
@persistence.should_receive(:find_entity, 1).times(1).and_return { user }
|
76
|
-
res = nil
|
77
|
-
res = @session.get_entity_preferences('name', ['preference_key'], 'domain')
|
78
|
-
assert_equal({'preference_key' => nil}, res)
|
79
|
-
end
|
80
|
-
def test_get_entity_preferences__success
|
81
|
-
user = FlexMock.new
|
82
|
-
user.should_receive(:get_preference).and_return { |key, domain|
|
83
|
-
assert_equal('preference_key', key)
|
84
|
-
assert_equal('domain', domain)
|
85
|
-
'value'
|
86
|
-
}
|
87
|
-
@persistence.should_receive(:find_entity, 1).times(1).and_return { user }
|
88
|
-
res = nil
|
89
|
-
res = @session.get_entity_preferences('name', ['preference_key'], 'domain')
|
90
|
-
assert_equal({'preference_key' => 'value'}, res)
|
91
|
-
end
|
92
|
-
def test_set_entity_preference__no_user
|
93
|
-
@persistence.should_receive(:find_entity).and_return { |name|
|
94
|
-
assert_equal('name', name)
|
95
|
-
nil
|
96
|
-
}
|
97
|
-
assert_raises(UnknownEntityError) {
|
98
|
-
@session.set_entity_preference('name', 'key', 'value')
|
99
|
-
}
|
100
|
-
end
|
101
|
-
def test_set_entity_preference__success
|
102
|
-
entity = FlexMock.new
|
103
|
-
value = nil
|
104
|
-
@persistence.should_receive(:find_entity).and_return { |name|
|
105
|
-
assert_equal('name', name)
|
106
|
-
entity
|
107
|
-
}
|
108
|
-
entity.should_receive(:get_preference).and_return { |key, domain|
|
109
|
-
assert_equal('key', key)
|
110
|
-
value
|
111
|
-
}
|
112
|
-
entity.should_receive(:set_preference, 1).times(1).and_return { |key, val, domain|
|
113
|
-
assert_equal('key', key)
|
114
|
-
assert_equal('value', val)
|
115
|
-
value = val
|
116
|
-
}
|
117
|
-
@persistence.should_receive(:save_entity, 1).times(1).and_return { |user|
|
118
|
-
assert_equal(entity, user)
|
119
|
-
}
|
120
|
-
@session.set_entity_preference('name', 'key', 'value')
|
121
|
-
@session.set_entity_preference('name', 'key', 'other')
|
122
|
-
end
|
123
|
-
def test_create_entity__success
|
124
|
-
@config.should_receive(:digest).and_return { Digest::SHA256 }
|
125
|
-
@persistence.should_receive(:find_entity, 1).times(1).and_return {}
|
126
|
-
@persistence.should_receive(:add_entity, 1).times(1).and_return { |entity|
|
127
|
-
assert_instance_of(Entity, entity)
|
128
|
-
assert_equal('name', entity.name)
|
129
|
-
}
|
130
|
-
@session.create_entity('name', 'pass')
|
131
|
-
end
|
132
|
-
def test_create_entity__duplicate
|
133
|
-
@persistence.should_receive(:find_entity, 1).times(1).and_return { 'something' }
|
134
|
-
assert_raises(DuplicateNameError) {
|
135
|
-
@session.create_entity('name')
|
136
|
-
}
|
137
|
-
end
|
138
|
-
def test_allowed
|
139
|
-
assert_equal(false, @session.allowed?('anything at all'))
|
140
|
-
end
|
141
|
-
def test_entity_allowed__no_user
|
142
|
-
@persistence.should_receive(:find_entity, 1).times(1).and_return {}
|
143
|
-
assert_raises(UnknownEntityError) {
|
144
|
-
@session.entity_allowed?('name', 'action', 'key')
|
145
|
-
}
|
146
|
-
end
|
147
|
-
def test_entity_allowed
|
148
|
-
user = FlexMock.new
|
149
|
-
expecteds = [['action1', nil], ['action2', 'key']]
|
150
|
-
user.should_receive(:allowed?).and_return { |action, key|
|
151
|
-
eact, ekey = expecteds.shift
|
152
|
-
assert_equal(eact, action)
|
153
|
-
assert_equal(ekey, key)
|
154
|
-
action == 'action2'
|
155
|
-
}
|
156
|
-
@persistence.should_receive(:find_entity, 2).times(2).and_return { user }
|
157
|
-
assert_equal(false, @session.entity_allowed?('name', 'action1'))
|
158
|
-
assert_equal(true, @session.entity_allowed?('name', 'action2', 'key'))
|
159
|
-
end
|
160
|
-
def test_reset_entity_password__no_user
|
161
|
-
@persistence.should_receive(:find_entity, 1).times(1).and_return {}
|
162
|
-
assert_raises(UnknownEntityError) {
|
163
|
-
@session.reset_entity_password('name', 'token', 'password')
|
164
|
-
}
|
165
|
-
end
|
166
|
-
def test_reset_entity_password__no_token
|
167
|
-
user = FlexMock.new
|
168
|
-
user.should_receive(:allowed?).and_return { |action, token|
|
169
|
-
assert_equal('reset_password', action)
|
170
|
-
assert_equal('token', token)
|
171
|
-
false
|
172
|
-
}
|
173
|
-
@persistence.should_receive(:find_entity, 1).times(1).and_return { user }
|
174
|
-
assert_raises(NotPrivilegedError) {
|
175
|
-
@session.reset_entity_password('name', 'token', 'password')
|
176
|
-
}
|
177
|
-
end
|
178
|
-
def test_reset_entity_password__success
|
179
|
-
user = FlexMock.new
|
180
|
-
user.should_receive(:allowed?).and_return { |action, token|
|
181
|
-
assert_equal('reset_password', action)
|
182
|
-
assert_equal('token', token)
|
183
|
-
true
|
184
|
-
}
|
185
|
-
user.should_receive(:passhash=, 1).times(1).and_return { |hash|
|
186
|
-
assert_equal(Digest::SHA256.hexdigest('password'), hash)
|
187
|
-
}
|
188
|
-
user.should_receive(:revoke).and_return { |action, token|
|
189
|
-
assert_equal('reset_password', action)
|
190
|
-
assert_equal('token', token)
|
191
|
-
}
|
192
|
-
@config.should_receive(:digest).and_return { Digest::SHA256 }
|
193
|
-
@persistence.should_receive(:find_entity, 1).times(1).and_return { user }
|
194
|
-
@persistence.should_receive(:save_entity, 1).times(1).and_return { |entity|
|
195
|
-
assert_equal(user, entity)
|
196
|
-
}
|
197
|
-
@session.reset_entity_password('name', 'token', 'password')
|
198
|
-
end
|
199
|
-
def test_grant__no_user
|
200
|
-
@persistence.should_receive(:find_entity).and_return { |name|
|
201
|
-
assert_equal('username', name)
|
202
|
-
nil
|
203
|
-
}
|
204
|
-
assert_raises(UnknownEntityError) {
|
205
|
-
@session.grant('username', 'action')
|
206
|
-
}
|
207
|
-
end
|
208
|
-
def test_grant__success_key
|
209
|
-
entity = FlexMock.new
|
210
|
-
entity.should_receive(:grant).and_return { |action, key|
|
211
|
-
assert_equal('action', action)
|
212
|
-
assert_equal('key', key)
|
213
|
-
}
|
214
|
-
@persistence.should_receive(:find_entity).and_return { |name|
|
215
|
-
assert_equal('username', name)
|
216
|
-
entity
|
217
|
-
}
|
218
|
-
@persistence.should_receive(:save_entity, 1).times(1).and_return { |user|
|
219
|
-
assert_equal(entity, user)
|
220
|
-
}
|
221
|
-
@session.grant('username', 'action', 'key')
|
222
|
-
end
|
223
|
-
end
|
224
|
-
class TestEntitySession < Minitest::Test
|
225
|
-
HexDigestSample = 'cleartext'
|
226
|
-
def setup
|
227
|
-
@config = FlexMock.new('config')
|
228
|
-
@config.should_receive(:session_timeout).and_return { 0.5 }
|
229
|
-
@config.should_receive(:token_lifetime).and_return { 0.2 }
|
230
|
-
@digest = FlexMock.new('digest')
|
231
|
-
@digest.should_receive(:hexdigest).and_return { HexDigestSample }
|
232
|
-
@config.should_receive(:digest).by_default.and_return {@digest}
|
233
|
-
@user = FlexMock.new('user')
|
234
|
-
@user.should_receive(:set_token).and_return { 'set_token' }
|
235
|
-
@user.should_receive(:name).and_return { 'name' }
|
236
|
-
@persistence = FlexMock.new('persistence')
|
237
|
-
@persistence.should_receive(:save_entity)
|
238
|
-
@logger = FlexMock.new('logger')
|
239
|
-
@logger.should_receive(:info).and_return {}
|
240
|
-
@logger.should_receive(:debug).and_return {}
|
241
|
-
@needle = FlexMock.new('needle')
|
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_generate_token
|
286
|
-
assert_equal(false, @session.expired?)
|
287
|
-
assert_equal(HexDigestSample, @session.generate_token)
|
288
|
-
end
|
289
|
-
def test_create_entity__duplicate
|
290
|
-
@user.should_receive(:allowed?).and_return { |action, key|
|
291
|
-
assert_equal('edit', action)
|
292
|
-
assert_equal('yus.entities', key)
|
293
|
-
true
|
294
|
-
}
|
295
|
-
@persistence.should_receive(:find_entity, 1).times(1).and_return { 'something' }
|
296
|
-
assert_raises(DuplicateNameError) {
|
297
|
-
@session.create_entity('name')
|
298
|
-
}
|
299
|
-
end
|
300
|
-
def test_destroy
|
301
|
-
assert_equal(false, @session.expired?)
|
302
|
-
@session.destroy!
|
303
|
-
assert_equal(true, @session.expired?)
|
304
|
-
assert_nil(@session.instance_variable_get('@persistence'))
|
305
|
-
assert_nil(@session.instance_variable_get('@user'))
|
306
|
-
end
|
307
|
-
def test_entities__not_allowed
|
308
|
-
@user.should_receive(:allowed?).and_return { |action, key|
|
309
|
-
assert_equal('edit', action)
|
310
|
-
assert_equal('yus.entities', key)
|
311
|
-
false
|
312
|
-
}
|
313
|
-
assert_raises(NotPrivilegedError) {
|
314
|
-
@session.entities
|
315
|
-
}
|
316
|
-
end
|
317
|
-
def test_entities__allowed
|
318
|
-
@user.should_receive(:allowed?).and_return { |action, key|
|
319
|
-
assert_equal('edit', action)
|
320
|
-
assert_equal('yus.entities', key)
|
321
|
-
true
|
322
|
-
}
|
323
|
-
@persistence.should_receive(:entities).and_return { [] }
|
324
|
-
assert_equal([], @session.entities)
|
325
|
-
end
|
326
|
-
def test_find_entity__not_allowed
|
327
|
-
@user.should_receive(:allowed?).and_return { |action, key|
|
328
|
-
assert_equal('edit', action)
|
329
|
-
assert_equal('yus.entities', key)
|
330
|
-
false
|
331
|
-
}
|
332
|
-
assert_raises(NotPrivilegedError) {
|
333
|
-
@session.find_entity('username')
|
334
|
-
}
|
335
|
-
end
|
336
|
-
def test_find_entity__success
|
337
|
-
@persistence.should_receive(:find_entity).and_return { |name|
|
338
|
-
assert_equal('username', name)
|
339
|
-
'found'
|
340
|
-
}
|
341
|
-
@user.should_receive(:allowed?).and_return { |action, key|
|
342
|
-
assert_equal('edit', action)
|
343
|
-
assert_equal('yus.entities', key)
|
344
|
-
true
|
345
|
-
}
|
346
|
-
assert_equal('found', @session.find_entity('username'))
|
347
|
-
end
|
348
|
-
def test_grant__not_allowed
|
349
|
-
@user.should_receive(:allowed?).and_return { |action, key|
|
350
|
-
assert_equal('grant', action)
|
351
|
-
assert_equal('action', key)
|
352
|
-
false
|
353
|
-
}
|
354
|
-
assert_raises(NotPrivilegedError) {
|
355
|
-
@session.grant('username', 'action')
|
356
|
-
}
|
357
|
-
end
|
358
|
-
def test_grant__no_user
|
359
|
-
@persistence.should_receive(:find_entity).and_return { |name|
|
360
|
-
assert_equal('username', name)
|
361
|
-
nil
|
362
|
-
}
|
363
|
-
@user.should_receive(:allowed?).and_return { |action, key|
|
364
|
-
assert_equal('grant', action)
|
365
|
-
assert_equal('action', key)
|
366
|
-
true
|
367
|
-
}
|
368
|
-
assert_raises(UnknownEntityError) {
|
369
|
-
@session.grant('username', 'action')
|
370
|
-
}
|
371
|
-
end
|
372
|
-
def test_grant__success_everything
|
373
|
-
entity = FlexMock.new
|
374
|
-
entity.should_receive(:grant).and_return { |action, key|
|
375
|
-
assert_equal('action', action)
|
376
|
-
assert_equal(:everything, key)
|
377
|
-
}
|
378
|
-
@persistence.should_receive(:find_entity).and_return { |name|
|
379
|
-
assert_equal('username', name)
|
380
|
-
entity
|
381
|
-
}
|
382
|
-
@persistence.should_receive(:save_entity, 1).times(1).and_return { |user|
|
383
|
-
assert_equal(entity, user)
|
384
|
-
}
|
385
|
-
@user.should_receive(:allowed?).and_return { |action, key|
|
386
|
-
assert_equal('grant', action)
|
387
|
-
assert_equal('action', key)
|
388
|
-
true
|
389
|
-
}
|
390
|
-
@session.grant('username', 'action')
|
391
|
-
end
|
392
|
-
def test_grant__success_key
|
393
|
-
entity = FlexMock.new
|
394
|
-
entity.should_receive(:grant).and_return { |action, key|
|
395
|
-
assert_equal('action', action)
|
396
|
-
assert_equal('key', key)
|
397
|
-
}
|
398
|
-
@persistence.should_receive(:find_entity).and_return { |name|
|
399
|
-
assert_equal('username', name)
|
400
|
-
entity
|
401
|
-
}
|
402
|
-
@persistence.should_receive(:save_entity, 1).times(1).and_return { |user|
|
403
|
-
assert_equal(entity, user)
|
404
|
-
}
|
405
|
-
@user.should_receive(:allowed?).and_return { |action, key|
|
406
|
-
assert_equal('grant', action)
|
407
|
-
assert_equal('action', key)
|
408
|
-
true
|
409
|
-
}
|
410
|
-
@session.grant('username', 'action', 'key')
|
411
|
-
end
|
412
|
-
def test_valid__success
|
413
|
-
@user.should_receive(:valid?).and_return { true }
|
414
|
-
assert_equal(true, @session.valid?)
|
415
|
-
end
|
416
|
-
def test_valid__failure
|
417
|
-
@user.should_receive(:valid?).and_return { false }
|
418
|
-
assert_equal(false, @session.valid?)
|
419
|
-
end
|
420
|
-
def test_set_password__not_allowed
|
421
|
-
@user.should_receive(:allowed?).and_return { |action, name|
|
422
|
-
assert_equal('set_password', action)
|
423
|
-
assert_equal('name', name)
|
424
|
-
false
|
425
|
-
}
|
426
|
-
assert_raises(NotPrivilegedError) {
|
427
|
-
@session.set_password('name', 'cleartext')
|
428
|
-
}
|
429
|
-
end
|
430
|
-
def test_set_password__no_user
|
431
|
-
@persistence.should_receive(:find_entity).and_return { |name|
|
432
|
-
assert_equal('username', name)
|
433
|
-
nil
|
434
|
-
}
|
435
|
-
@user.should_receive(:allowed?).and_return { |action, name|
|
436
|
-
assert_equal('set_password', action)
|
437
|
-
assert_equal('username', name)
|
438
|
-
true
|
439
|
-
}
|
440
|
-
assert_raises(UnknownEntityError) {
|
441
|
-
@session.set_password('username', 'cleartext')
|
442
|
-
}
|
443
|
-
end
|
444
|
-
def test_set_password__success
|
445
|
-
entity = FlexMock.new
|
446
|
-
@persistence.should_receive(:find_entity).and_return { |name|
|
447
|
-
assert_equal('username', name)
|
448
|
-
entity
|
449
|
-
}
|
450
|
-
@persistence.should_receive(:save_entity, 1).times(1).and_return { |user|
|
451
|
-
assert_equal(entity, user)
|
452
|
-
}
|
453
|
-
@user.should_receive(:allowed?).and_return { |action, name|
|
454
|
-
assert_equal('set_password', action)
|
455
|
-
assert_equal('username', name)
|
456
|
-
true
|
457
|
-
}
|
458
|
-
@config.should_receive(:digest).and_return { Digest::SHA256 }
|
459
|
-
entity.should_receive(:passhash=).and_return { |hash|
|
460
|
-
assert_equal(Digest::SHA256.hexdigest('cleartext'), hash)
|
461
|
-
}
|
462
|
-
@session.set_password('username', 'cleartext')
|
463
|
-
end
|
464
|
-
def test_rename__not_allowed
|
465
|
-
@user.should_receive(:allowed?).and_return { |action, name|
|
466
|
-
assert_equal('edit', action)
|
467
|
-
assert_equal('yus.entities', name)
|
468
|
-
false
|
469
|
-
}
|
470
|
-
assert_raises(NotPrivilegedError) {
|
471
|
-
@session.rename('oldname', 'newname')
|
472
|
-
}
|
473
|
-
end
|
474
|
-
def test_rename__no_user
|
475
|
-
@persistence.should_receive(:find_entity).and_return { |name|
|
476
|
-
assert_equal('oldname', name)
|
477
|
-
nil
|
478
|
-
}
|
479
|
-
@user.should_receive(:allowed?).and_return { |action, name|
|
480
|
-
assert_equal('edit', action)
|
481
|
-
assert_equal('yus.entities', name)
|
482
|
-
true
|
483
|
-
}
|
484
|
-
assert_raises(UnknownEntityError) {
|
485
|
-
@session.rename('oldname', 'newname')
|
486
|
-
}
|
487
|
-
end
|
488
|
-
def test_rename__duplicate_name
|
489
|
-
entity1 = FlexMock.new
|
490
|
-
entity2 = FlexMock.new
|
491
|
-
entities = {
|
492
|
-
'oldname' => entity1,
|
493
|
-
'newname' => entity2,
|
494
|
-
}
|
495
|
-
@persistence.should_receive(:find_entity).and_return { |name|
|
496
|
-
entities[name]
|
497
|
-
}
|
498
|
-
@user.should_receive(:allowed?).and_return { |action, name|
|
499
|
-
assert_equal('edit', action)
|
500
|
-
assert_equal('yus.entities', name)
|
501
|
-
true
|
502
|
-
}
|
503
|
-
assert_raises(DuplicateNameError) {
|
504
|
-
@session.rename('oldname', 'newname')
|
505
|
-
}
|
506
|
-
end
|
507
|
-
def test_rename__success
|
508
|
-
entity = FlexMock.new
|
509
|
-
entity.should_receive(:revoke, 1).times(1).and_return { |action, item|
|
510
|
-
assert_equal('set_password', action)
|
511
|
-
assert_equal('oldname', item)
|
512
|
-
}
|
513
|
-
entity.should_receive(:grant, 1).times(1).and_return { |action, item|
|
514
|
-
assert_equal('set_password', action)
|
515
|
-
assert_equal('newname', item)
|
516
|
-
}
|
517
|
-
entity.should_receive(:rename).and_return { |newname|
|
518
|
-
assert_equal('newname', newname)
|
519
|
-
}
|
520
|
-
entities = {
|
521
|
-
'oldname' => entity,
|
522
|
-
'newname' => nil,
|
523
|
-
}
|
524
|
-
@persistence.should_receive(:find_entity).and_return { |name|
|
525
|
-
entities[name]
|
526
|
-
}
|
527
|
-
@persistence.should_receive(:save_entity, 1).times(1).and_return { |user|
|
528
|
-
assert_equal(entity, user)
|
529
|
-
}
|
530
|
-
@user.should_receive(:allowed?).and_return { |action, name|
|
531
|
-
assert_equal('edit', action)
|
532
|
-
assert_equal('yus.entities', name)
|
533
|
-
true
|
534
|
-
}
|
535
|
-
@session.rename('oldname', 'newname')
|
536
|
-
end
|
537
|
-
def test_revoke__not_allowed
|
538
|
-
@user.should_receive(:allowed?).and_return { |action, key|
|
539
|
-
assert_equal('grant', action)
|
540
|
-
assert_equal('action', key)
|
541
|
-
false
|
542
|
-
}
|
543
|
-
assert_raises(NotPrivilegedError) {
|
544
|
-
@session.revoke('username', 'action')
|
545
|
-
}
|
546
|
-
end
|
547
|
-
def test_revoke__no_user
|
548
|
-
@persistence.should_receive(:find_entity).and_return { |name|
|
549
|
-
assert_equal('username', name)
|
550
|
-
nil
|
551
|
-
}
|
552
|
-
@user.should_receive(:allowed?).and_return { |action, key|
|
553
|
-
assert_equal('grant', action)
|
554
|
-
assert_equal('action', key)
|
555
|
-
true
|
556
|
-
}
|
557
|
-
assert_raises(UnknownEntityError) {
|
558
|
-
@session.revoke('username', 'action')
|
559
|
-
}
|
560
|
-
end
|
561
|
-
def test_revoke__success_everything
|
562
|
-
entity = FlexMock.new
|
563
|
-
entity.should_receive(:revoke).and_return { |action, key|
|
564
|
-
assert_equal('action', action)
|
565
|
-
assert_equal(:everything, key)
|
566
|
-
}
|
567
|
-
@persistence.should_receive(:find_entity).and_return { |name|
|
568
|
-
assert_equal('username', name)
|
569
|
-
entity
|
570
|
-
}
|
571
|
-
@persistence.should_receive(:save_entity, 1).times(1).and_return { |user|
|
572
|
-
assert_equal(entity, user)
|
573
|
-
}
|
574
|
-
@user.should_receive(:allowed?).and_return { |action, key|
|
575
|
-
assert_equal('grant', action)
|
576
|
-
assert_equal('action', key)
|
577
|
-
true
|
578
|
-
}
|
579
|
-
@session.revoke('username', 'action')
|
580
|
-
end
|
581
|
-
def test_revoke__success_key
|
582
|
-
entity = FlexMock.new
|
583
|
-
entity.should_receive(:revoke).and_return { |action, key|
|
584
|
-
assert_equal('action', action)
|
585
|
-
assert_equal('key', key)
|
586
|
-
}
|
587
|
-
@persistence.should_receive(:find_entity).and_return { |name|
|
588
|
-
assert_equal('username', name)
|
589
|
-
entity
|
590
|
-
}
|
591
|
-
@persistence.should_receive(:save_entity, 1).times(1).and_return { |user|
|
592
|
-
assert_equal(entity, user)
|
593
|
-
}
|
594
|
-
@user.should_receive(:allowed?).and_return { |action, key|
|
595
|
-
assert_equal('grant', action)
|
596
|
-
assert_equal('action', key)
|
597
|
-
true
|
598
|
-
}
|
599
|
-
@session.revoke('username', 'action', 'key')
|
600
|
-
end
|
601
|
-
def test_affiliate__not_allowed
|
602
|
-
@user.should_receive(:allowed?).and_return { |action, key|
|
603
|
-
assert_equal('edit', action)
|
604
|
-
assert_equal('yus.entities', key)
|
605
|
-
false
|
606
|
-
}
|
607
|
-
assert_raises(NotPrivilegedError) {
|
608
|
-
@session.affiliate('name', 'group')
|
609
|
-
}
|
610
|
-
end
|
611
|
-
def test_affiliate__no_user
|
612
|
-
@user.should_receive(:allowed?).and_return { |action, key|
|
613
|
-
assert_equal('edit', action)
|
614
|
-
assert_equal('yus.entities', key)
|
615
|
-
true
|
616
|
-
}
|
617
|
-
@persistence.should_receive(:find_entity).and_return { |name|
|
618
|
-
assert_equal('name', name)
|
619
|
-
nil
|
620
|
-
}
|
621
|
-
assert_raises(UnknownEntityError) {
|
622
|
-
@session.affiliate('name', 'group')
|
623
|
-
}
|
624
|
-
end
|
625
|
-
def test_affiliate__no_group
|
626
|
-
user = FlexMock.new
|
627
|
-
@user.should_receive(:allowed?).and_return { |action, key|
|
628
|
-
assert_equal('edit', action)
|
629
|
-
assert_equal('yus.entities', key)
|
630
|
-
true
|
631
|
-
}
|
632
|
-
names = ['name', 'group']
|
633
|
-
entities = [user, nil]
|
634
|
-
@persistence.should_receive(:find_entity).and_return { |name|
|
635
|
-
assert_equal(names.shift, name)
|
636
|
-
entities.shift
|
637
|
-
}
|
638
|
-
assert_raises(UnknownEntityError) {
|
639
|
-
@session.affiliate('name', 'group')
|
640
|
-
}
|
641
|
-
end
|
642
|
-
def test_affiliate__success
|
643
|
-
@persistence.should_receive(:save_entity).and_return {}
|
644
|
-
user = FlexMock.new
|
645
|
-
group = FlexMock.new
|
646
|
-
@user.should_receive(:allowed?).and_return { |action, key|
|
647
|
-
assert_equal('edit', action)
|
648
|
-
assert_equal('yus.entities', key)
|
649
|
-
true
|
650
|
-
}
|
651
|
-
names = ['name', 'group']
|
652
|
-
entities = [user, group]
|
653
|
-
@persistence.should_receive(:find_entity).and_return { |name|
|
654
|
-
assert_equal(names.shift, name)
|
655
|
-
entities.shift
|
656
|
-
}
|
657
|
-
user.should_receive(:join, 1).times(1).and_return { |arg|
|
658
|
-
assert_equal(group, arg)
|
659
|
-
}
|
660
|
-
@session.affiliate('name', 'group')
|
661
|
-
end
|
662
|
-
def test_disaffiliate__not_allowed
|
663
|
-
@user.should_receive(:allowed?).and_return { |action, key|
|
664
|
-
assert_equal('edit', action)
|
665
|
-
assert_equal('yus.entities', key)
|
666
|
-
false
|
667
|
-
}
|
668
|
-
assert_raises(NotPrivilegedError) {
|
669
|
-
@session.disaffiliate('name', 'group')
|
670
|
-
}
|
671
|
-
end
|
672
|
-
def test_disaffiliate__no_user
|
673
|
-
@user.should_receive(:allowed?).and_return { |action, key|
|
674
|
-
assert_equal('edit', action)
|
675
|
-
assert_equal('yus.entities', key)
|
676
|
-
true
|
677
|
-
}
|
678
|
-
@persistence.should_receive(:find_entity).and_return { |name|
|
679
|
-
assert_equal('name', name)
|
680
|
-
nil
|
681
|
-
}
|
682
|
-
assert_raises(UnknownEntityError) {
|
683
|
-
@session.disaffiliate('name', 'group')
|
684
|
-
}
|
685
|
-
end
|
686
|
-
def test_disaffiliate__no_group
|
687
|
-
user = FlexMock.new
|
688
|
-
@user.should_receive(:allowed?).and_return { |action, key|
|
689
|
-
assert_equal('edit', action)
|
690
|
-
assert_equal('yus.entities', key)
|
691
|
-
true
|
692
|
-
}
|
693
|
-
names = ['name', 'group']
|
694
|
-
entities = [user, nil]
|
695
|
-
@persistence.should_receive(:find_entity).and_return { |name|
|
696
|
-
assert_equal(names.shift, name)
|
697
|
-
entities.shift
|
698
|
-
}
|
699
|
-
assert_raises(UnknownEntityError) {
|
700
|
-
@session.disaffiliate('name', 'group')
|
701
|
-
}
|
702
|
-
end
|
703
|
-
def test_disaffiliate__success
|
704
|
-
@persistence.should_receive(:save_entity).and_return {}
|
705
|
-
user = FlexMock.new
|
706
|
-
group = FlexMock.new
|
707
|
-
@user.should_receive(:allowed?).and_return { |action, key|
|
708
|
-
assert_equal('edit', action)
|
709
|
-
assert_equal('yus.entities', key)
|
710
|
-
true
|
711
|
-
}
|
712
|
-
names = ['name', 'group']
|
713
|
-
entities = [user, group]
|
714
|
-
@persistence.should_receive(:find_entity).and_return { |name|
|
715
|
-
assert_equal(names.shift, name)
|
716
|
-
entities.shift
|
717
|
-
}
|
718
|
-
user.should_receive(:leave, 1).times(1).and_return { |arg|
|
719
|
-
assert_equal(group, arg)
|
720
|
-
}
|
721
|
-
@session.disaffiliate('name', 'group')
|
722
|
-
end
|
723
|
-
def test_set_entity_preference__not_allowed
|
724
|
-
@user.should_receive(:allowed?).and_return { |action, key|
|
725
|
-
assert_equal('edit', action)
|
726
|
-
assert_equal('yus.entities', key)
|
727
|
-
false
|
728
|
-
}
|
729
|
-
assert_raises(NotPrivilegedError) {
|
730
|
-
@session.set_entity_preference('name', 'key', 'value')
|
731
|
-
}
|
732
|
-
end
|
733
|
-
def test_set_entity_preference__no_user
|
734
|
-
@user.should_receive(:allowed?).and_return { |action, key|
|
735
|
-
assert_equal('edit', action)
|
736
|
-
assert_equal('yus.entities', key)
|
737
|
-
true
|
738
|
-
}
|
739
|
-
@persistence.should_receive(:find_entity).and_return { |name|
|
740
|
-
assert_equal('name', name)
|
741
|
-
nil
|
742
|
-
}
|
743
|
-
assert_raises(UnknownEntityError) {
|
744
|
-
@session.set_entity_preference('name', 'key', 'value')
|
745
|
-
}
|
746
|
-
end
|
747
|
-
def test_set_entity_preference__success
|
748
|
-
entity = FlexMock.new
|
749
|
-
@user.should_receive(:allowed?).and_return { |action, key|
|
750
|
-
assert_equal('edit', action)
|
751
|
-
assert_equal('yus.entities', key)
|
752
|
-
true
|
753
|
-
}
|
754
|
-
@persistence.should_receive(:find_entity).and_return { |name|
|
755
|
-
assert_equal('name', name)
|
756
|
-
entity
|
757
|
-
}
|
758
|
-
entity.should_receive(:set_preference, 1).times(1).and_return { |key, val|
|
759
|
-
assert_equal('key', key)
|
760
|
-
assert_equal('value', val)
|
761
|
-
}
|
762
|
-
@persistence.should_receive(:save_entity, 1).times(1).and_return { |user|
|
763
|
-
assert_equal(entity, user)
|
764
|
-
}
|
765
|
-
@session.set_entity_preference('name', 'key', 'value')
|
766
|
-
end
|
767
|
-
def test_set_preference
|
768
|
-
@user.should_receive(:set_preference, 1).times(1).and_return { |key, val, domain|
|
769
|
-
assert_equal('key', key)
|
770
|
-
assert_equal('value', val)
|
771
|
-
assert_equal('domain', domain)
|
772
|
-
}
|
773
|
-
@persistence.should_receive(:save_entity, 1).times(1).and_return { |user|
|
774
|
-
assert_equal(@user, user)
|
775
|
-
}
|
776
|
-
@session.set_preference('key', 'value')
|
777
|
-
end
|
778
|
-
def test_set_preferences
|
779
|
-
@user.should_receive(:set_preference, 2).times(2).and_return { |key, val, domain| }
|
780
|
-
@persistence.should_receive(:save_entity, 1).times(1).and_return { |user|
|
781
|
-
assert_equal(@user, user)
|
782
|
-
}
|
783
|
-
@session.set_preferences({'key1' => 'value1', 'key2' => 'value2'})
|
784
|
-
end
|
785
|
-
def test_get_entity_preference__no_preference
|
786
|
-
@user.should_receive(:get_preference).and_return { |key, domain|
|
787
|
-
assert_equal('preference_key', key)
|
788
|
-
assert_equal('domain', domain)
|
789
|
-
nil
|
790
|
-
}
|
791
|
-
res = nil
|
792
|
-
res = @session.get_preference('preference_key')
|
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
|
-
res = @session.get_preference('preference_key')
|
803
|
-
assert_equal('value', res)
|
804
|
-
end
|
805
|
-
def test_ping
|
806
|
-
assert_equal(true, @session.ping)
|
807
|
-
end
|
808
|
-
end
|
809
|
-
class TestRootSession < Minitest::Test
|
810
|
-
def setup
|
811
|
-
@config = FlexMock.new('config')
|
812
|
-
@config.should_receive(:session_timeout).and_return { 0.5 }
|
813
|
-
@needle = FlexMock.new('needle')
|
814
|
-
@persistence = MockPersistence.new
|
815
|
-
@logger = FlexMock.new('logger')
|
816
|
-
@logger.should_receive(:info).and_return {}
|
817
|
-
@needle.should_receive(:persistence).and_return { @persistence }
|
818
|
-
@needle.should_receive(:logger).and_return { @logger }.by_default
|
819
|
-
@needle.should_receive(:config).and_return { @config }.by_default
|
820
|
-
@config.should_receive(:session_timeout).and_return { 0.5 }
|
821
|
-
@session = RootSession.new(@needle)
|
822
|
-
end
|
823
|
-
def test_valid
|
824
|
-
assert_equal(true, @session.valid?)
|
825
|
-
end
|
826
|
-
def test_allowed
|
827
|
-
assert_equal(true, @session.allowed?('anything'))
|
828
|
-
end
|
829
|
-
def test_name
|
830
|
-
@config.should_receive(:root_name).and_return { 'root_name' }
|
831
|
-
assert_equal('root_name', @session.name)
|
832
|
-
end
|
833
|
-
def test_delete_entity
|
834
|
-
entity_name = 'entity_name'
|
835
|
-
entity = @session.create_entity(entity_name, 'entity_pass')
|
836
|
-
assert_equal(entity, @session.find_entity(entity_name))
|
837
|
-
@session.delete_entity(entity_name)
|
838
|
-
assert_nil(@session.find_entity(entity_name))
|
839
|
-
end
|
840
|
-
def test_last_login
|
841
|
-
entity_name = 'entity_name'
|
842
|
-
entity = @session.create_entity(entity_name, 'entity_pass')
|
843
|
-
domain = 'domain'
|
844
|
-
entity.login(domain)
|
845
|
-
assert_equal(entity, @session.find_entity(entity_name))
|
846
|
-
#require 'pry'; binding.pry
|
847
|
-
assert_in_delta(Time.now.to_f, @session.last_login(entity_name, domain).to_f)
|
848
|
-
end
|
849
|
-
def test_show
|
850
|
-
assert_raises(UnknownEntityError) {
|
851
|
-
@session.show('unkown_name')
|
852
|
-
}
|
853
|
-
entity_name = 'entity_name'
|
854
|
-
@session.create_entity(entity_name, 'entity_pass')
|
855
|
-
assert_kind_of(String, @session.show(entity_name))
|
856
|
-
assert(@session.show(entity_name).index(entity_name) > 0)
|
857
|
-
end
|
858
|
-
|
859
|
-
def setup_for_dump
|
860
|
-
assert_raises(UnknownEntityError) {
|
861
|
-
@session.show('unkown_name')
|
862
|
-
}
|
863
|
-
@entity_name = 'entity_name'
|
864
|
-
entity_name2 = 'second_name'
|
865
|
-
password = 'entity_pass'
|
866
|
-
groupname = 'a_yus_group'
|
867
|
-
@session.create_entity(@entity_name, password)
|
868
|
-
@session.create_entity(entity_name2, password)
|
869
|
-
@session.create_entity(groupname, password)
|
870
|
-
assert_kind_of(String, @session.show(@entity_name))
|
871
|
-
assert(@session.show(@entity_name).index(@entity_name) > 0)
|
872
|
-
assert(@session.show(entity_name2).index(entity_name2) > 0)
|
873
|
-
@session.affiliate(@entity_name, groupname)
|
874
|
-
@session.grant(@entity_name, 'action', 'key')
|
875
|
-
|
876
|
-
@needle = FlexMock.new('needle')
|
877
|
-
@needle.should_receive(:persistence).and_return { @persistence }
|
878
|
-
@needle.should_receive(:config).and_return { @config }
|
879
|
-
@needle.should_receive(:logger).and_return { @logger }
|
880
|
-
end
|
881
|
-
|
882
|
-
def test_dump_to_named_yaml
|
883
|
-
setup_for_dump
|
884
|
-
tmpdir = File.expand_path(File.join(__FILE__, '../tmp'))
|
885
|
-
outFile = File.join(tmpdir, 'yus_dump.yaml')
|
886
|
-
assert(@session.dump_to_yaml(outFile))
|
887
|
-
assert(File.exist?(outFile))
|
888
|
-
assert(File.size(outFile) > 0)
|
889
|
-
dump_content = IO.read(outFile)
|
890
|
-
# puts "#{outFile} is #{File.size(outFile)} bytes lang and contains \n#{dump_content}"
|
891
|
-
refute_nil(dump_content)
|
892
|
-
refute_nil(dump_content.index(@entity_name))
|
893
|
-
FileUtils.rm_rf(tmpdir)
|
894
|
-
skip("Tests saving preferences")
|
895
|
-
end
|
896
|
-
|
897
|
-
def test_dump_to_default
|
898
|
-
setup_for_dump
|
899
|
-
assert(@session.dump_to_yaml)
|
900
|
-
outFile = File.expand_path(File.join(__FILE__, '..', '..', 'data', 'yus_dump.yml'))
|
901
|
-
assert(File.exist?(outFile), "outfile #{outFile} should exist")
|
902
|
-
assert(File.size(outFile) > 0)
|
903
|
-
dump_content = IO.read(outFile)
|
904
|
-
# puts "#{outFile} is #{File.size(outFile)} bytes lang and contains \n#{dump_content}"
|
905
|
-
refute_nil(dump_content)
|
906
|
-
refute_nil(dump_content.index(@entity_name))
|
907
|
-
end
|
908
|
-
end
|
909
|
-
end
|