Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/tests/units/commands.py
blob: ff6a4b7be6966229daa697b7ee0beb1f901de9c9 (plain)
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
#!/usr/bin/env python
# sugar-lint: disable

from cStringIO import StringIO

from __init__ import tests

import active_document as ad
from active_document import env, volume, SingleVolume, Document, \
        property_command, document_command, directory_command, volume_command, \
        active_property, Request, BlobProperty, Response, CommandsProcessor, \
        CommandNotFound, NotFound, to_int, to_list


class CommandsTest(tests.Test):

    def test_VolumeCommands(self):
        calls = []

        class TestCommandsProcessor(CommandsProcessor):

            @volume_command(method='PROBE')
            def command_1(self, **kwargs):
                calls.append(('command_1', kwargs))

            @volume_command(method='PROBE', cmd='command_2')
            def command_2(self, **kwargs):
                calls.append(('command_2', kwargs))

        cp = TestCommandsProcessor()

        self.call(cp, 'PROBE')
        self.assertRaises(CommandNotFound, self.call, cp, 'PROBE', cmd='command_1')
        self.call(cp, 'PROBE', cmd='command_2')

        self.assertEqual([
            ('command_1', {}),
            ('command_2', {}),
            ],
            calls)

    def test_DirectoryCommands(self):
        calls = []

        class TestCommandsProcessor(CommandsProcessor):

            @directory_command(method='PROBE')
            def command_1(self, **kwargs):
                calls.append(('command_1', kwargs))

            @directory_command(method='PROBE', cmd='command_2')
            def command_2(self, **kwargs):
                calls.append(('command_2', kwargs))

        cp = TestCommandsProcessor()

        self.assertRaises(CommandNotFound, self.call, cp, 'PROBE')
        self.call(cp, 'PROBE', document='testdocument')
        self.call(cp, 'PROBE', document='fakedocument')
        self.assertRaises(CommandNotFound, self.call, cp, 'PROBE', cmd='command_1', document='testdocument')
        self.call(cp, 'PROBE', cmd='command_2', document='testdocument')
        self.call(cp, 'PROBE', cmd='command_2', document='fakedocument')

        self.assertEqual([
            ('command_1', {}),
            ('command_1', {}),
            ('command_2', {}),
            ('command_2', {}),
            ],
            calls)

    def test_DocumentCommands(self):
        calls = []

        class TestCommandsProcessor(CommandsProcessor):

            @document_command(method='PROBE')
            def command_1(self, **kwargs):
                calls.append(('command_1', kwargs))

            @document_command(method='PROBE', cmd='command_2')
            def command_2(self, **kwargs):
                calls.append(('command_2', kwargs))

        class TestDocument(Document):
            pass

        volume = SingleVolume(tests.tmpdir, [TestDocument])
        cp = TestCommandsProcessor(volume)

        self.assertRaises(CommandNotFound, self.call, cp, 'PROBE')
        self.assertRaises(CommandNotFound, self.call, cp, 'PROBE', document='testdocument')
        self.call(cp, 'PROBE', document='testdocument', guid='guid')
        self.call(cp, 'PROBE', document='fakedocument', guid='guid')
        self.assertRaises(CommandNotFound, self.call, cp, 'PROBE', cmd='command_1', document='testdocument', guid='guid')
        self.call(cp, 'PROBE', cmd='command_2', document='testdocument', guid='guid')
        self.call(cp, 'PROBE', cmd='command_2', document='fakedocument', guid='guid')

        self.assertEqual([
            ('command_1', {}),
            ('command_1', {}),
            ('command_2', {}),
            ('command_2', {}),
            ],
            calls)

    def test_PropertyCommands(self):
        calls = []

        class TestCommandsProcessor(CommandsProcessor):

            @property_command(method='PROBE')
            def command_1(self, **kwargs):
                calls.append(('command_1', kwargs))

            @property_command(method='PROBE', cmd='command_2')
            def command_2(self, **kwargs):
                calls.append(('command_2', kwargs))

        class TestDocument(Document):
            pass

        volume = SingleVolume(tests.tmpdir, [TestDocument])
        cp = TestCommandsProcessor(volume)

        self.assertRaises(CommandNotFound, self.call, cp, 'PROBE')
        self.assertRaises(CommandNotFound, self.call, cp, 'PROBE', document='testdocument')
        self.assertRaises(CommandNotFound, self.call, cp, 'PROBE', document='testdocument', guid='guid')
        self.call(cp, 'PROBE', document='testdocument', guid='guid', prop='prop')
        self.call(cp, 'PROBE', document='fakedocument', guid='guid', prop='prop')
        self.assertRaises(CommandNotFound, self.call, cp, 'PROBE', cmd='command_1', document='testdocument', guid='guid', prop='prop')
        self.call(cp, 'PROBE', cmd='command_2', document='testdocument', guid='guid', prop='prop')
        self.call(cp, 'PROBE', cmd='command_2', document='fakedocument', guid='guid', prop='prop')

        self.assertEqual([
            ('command_1', {}),
            ('command_1', {}),
            ('command_2', {}),
            ('command_2', {}),
            ],
            calls)

    def test_ClassDodcumentCommands(self):
        calls = []

        class TestDocument(Document):

            @document_command(method='PROBE')
            def command_1(cls, **kwargs):
                calls.append(('command_1', kwargs))

            @document_command(method='PROBE', cmd='command_2')
            def command_2(cls, **kwargs):
                calls.append(('command_2', kwargs))

        volume = SingleVolume(tests.tmpdir, [TestDocument])
        cp = CommandsProcessor(volume)

        self.assertRaises(CommandNotFound, self.call, cp, 'PROBE')
        self.assertRaises(CommandNotFound, self.call, cp, 'PROBE', document='testdocument')
        self.assertRaises(NotFound, self.call, cp, 'PROBE', document='testdocument', guid='guid')
        volume['testdocument'].create(guid='guid')
        self.call(cp, 'PROBE', document='testdocument', guid='guid')
        self.assertRaises(CommandNotFound, self.call, cp, 'PROBE', document='fakedocument', guid='guid')
        self.assertRaises(CommandNotFound, self.call, cp, 'PROBE', cmd='command_1', document='testdocument', guid='guid')
        self.call(cp, 'PROBE', cmd='command_2', document='testdocument', guid='guid')
        self.assertRaises(CommandNotFound, self.call, cp, 'PROBE', cmd='command_2', document='fakedocument', guid='guid')

        self.assertEqual([
            ('command_1', {}),
            ('command_2', {}),
            ],
            calls)

    def test_ClassPropertyCommands(self):
        calls = []

        class TestDocument(Document):

            @property_command(method='PROBE')
            def command_1(cls, **kwargs):
                calls.append(('command_1', kwargs))

            @property_command(method='PROBE', cmd='command_2')
            def command_2(cls, **kwargs):
                calls.append(('command_2', kwargs))

        volume = SingleVolume(tests.tmpdir, [TestDocument])
        cp = CommandsProcessor(volume)

        self.assertRaises(CommandNotFound, self.call, cp, 'PROBE')
        self.assertRaises(CommandNotFound, self.call, cp, 'PROBE', document='testdocument')
        self.assertRaises(CommandNotFound, self.call, cp, 'PROBE', document='testdocument', prop='prop')
        self.assertRaises(NotFound, self.call, cp, 'PROBE', document='testdocument', guid='guid', prop='prop')
        volume['testdocument'].create(guid='guid')
        self.call(cp, 'PROBE', document='testdocument', guid='guid', prop='prop')
        self.assertRaises(CommandNotFound, self.call, cp, 'PROBE', document='fakedocument', guid='guid', prop='prop')
        self.assertRaises(CommandNotFound, self.call, cp, 'PROBE', cmd='command_1', document='testdocument', guid='guid', prop='prop')
        self.call(cp, 'PROBE', cmd='command_2', document='testdocument', guid='guid', prop='prop')
        self.assertRaises(CommandNotFound, self.call, cp, 'PROBE', cmd='command_2', document='fakedocument', guid='guid', prop='prop')

        self.assertEqual([
            ('command_1', {}),
            ('command_2', {}),
            ],
            calls)

    def test_AccessLevel(self):
        calls = []

        class TestCommandsProcessor(CommandsProcessor):

            @volume_command(method='PROBE', cmd='all')
            def all(self):
                pass

            @volume_command(method='PROBE', cmd='system', access_level=env.ACCESS_SYSTEM)
            def system(self):
                pass

            @volume_command(method='PROBE', cmd='local', access_level=env.ACCESS_LOCAL)
            def local(self):
                pass

            @volume_command(method='PROBE', cmd='remote', access_level=env.ACCESS_REMOTE)
            def remote(self):
                pass

        cp = TestCommandsProcessor()

        self.call(cp, 'PROBE', cmd='all', access_level=env.ACCESS_REMOTE)
        self.call(cp, 'PROBE', cmd='all', access_level=env.ACCESS_LOCAL)
        self.call(cp, 'PROBE', cmd='all', access_level=env.ACCESS_SYSTEM)

        self.call(cp, 'PROBE', cmd='remote', access_level=env.ACCESS_REMOTE)
        self.assertRaises(env.Forbidden, self.call, cp, 'PROBE', cmd='remote', access_level=env.ACCESS_LOCAL)
        self.assertRaises(env.Forbidden, self.call, cp, 'PROBE', cmd='remote', access_level=env.ACCESS_SYSTEM)

        self.assertRaises(env.Forbidden, self.call, cp, 'PROBE', cmd='local', access_level=env.ACCESS_REMOTE)
        self.call(cp, 'PROBE', cmd='local', access_level=env.ACCESS_LOCAL)
        self.assertRaises(env.Forbidden, self.call, cp, 'PROBE', cmd='local', access_level=env.ACCESS_SYSTEM)

        self.assertRaises(env.Forbidden, self.call, cp, 'PROBE', cmd='system', access_level=env.ACCESS_REMOTE)
        self.assertRaises(env.Forbidden, self.call, cp, 'PROBE', cmd='system', access_level=env.ACCESS_LOCAL)
        self.call(cp, 'PROBE', cmd='system', access_level=env.ACCESS_SYSTEM)

    def test_ParentClasses(self):
        calls = []

        class Parent(object):

            @volume_command(method='PROBE')
            def probe(self):
                return 'probe'

        class TestCommandsProcessor(CommandsProcessor, Parent):
            pass

        cp = TestCommandsProcessor()
        self.assertEqual('probe', self.call(cp, 'PROBE'))

    def test_OverrideInChildClass(self):
        calls = []

        class Parent(CommandsProcessor):

            @volume_command(method='PROBE')
            def probe(self):
                return 'probe-1'

            @volume_command(method='COMMON')
            def common(self):
                return 'common'

        class Child(Parent):

            @volume_command(method='PROBE')
            def probe(self):
                return 'probe-2'

            @volume_command(method='PARTICULAR')
            def particular(self):
                return 'particular'

        cp = Child()
        self.assertEqual('probe-2', self.call(cp, 'PROBE'))
        self.assertEqual('common', self.call(cp, 'COMMON'))
        self.assertEqual('particular', self.call(cp, 'PARTICULAR'))

    def test_RequestRead(self):

        class Stream(object):

            def __init__(self, value):
                self.pos = 0
                self.value = value

            def read(self, size):
                assert self.pos + size <= len(self.value)
                result = self.value[self.pos:self.pos + size]
                self.pos += size
                return result

        request = Request()
        request.content_stream = Stream('123')
        request.content_length = len(request.content_stream.value)
        self.assertEqual('123', request.read())
        self.assertEqual('', request.read())
        self.assertEqual('', request.read(10))

        request = Request()
        request.content_stream = Stream('123')
        request.content_length = len(request.content_stream.value)
        self.assertEqual('123', request.read(10))

        request = Request()
        request.content_stream = Stream('123')
        request.content_length = len(request.content_stream.value)
        self.assertEqual('1', request.read(1))
        self.assertEqual('2', request.read(1))
        self.assertEqual('3', request.read())

    def test_Arguments(self):

        class TestCommandsProcessor(CommandsProcessor):

            @volume_command(method='PROBE', arguments={'arg_int': to_int, 'arg_list': to_list})
            def probe(self, arg_int=None, arg_list=None):
                return arg_int, arg_list

        cp = TestCommandsProcessor()

        self.assertEqual((None, None), self.call(cp, 'PROBE'))
        self.assertEqual((-1, [-2, None]), self.call(cp, 'PROBE', arg_int=-1, arg_list=[-2, None]))
        self.assertEqual((4, [' foo', ' bar  ', '  ']), self.call(cp, 'PROBE', arg_int='4', arg_list=' foo, bar  ,  '))
        self.assertEqual((None, ['foo']), self.call(cp, 'PROBE', arg_list='foo'))
        self.assertEqual((None, []), self.call(cp, 'PROBE', arg_list=''))
        self.assertEqual((None, [' ']), self.call(cp, 'PROBE', arg_list=' '))
        self.assertEqual((0, None), self.call(cp, 'PROBE', arg_int=''))
        self.assertRaises(RuntimeError, self.call, cp, 'PROBE', arg_int=' ')
        self.assertRaises(RuntimeError, self.call, cp, 'PROBE', arg_int='foo')

    def test_PassKwargs(self):

        class TestCommandsProcessor(CommandsProcessor):

            @volume_command(method='PROBE')
            def probe(self, arg, request, response, **kwargs):
                return arg, dict(request), dict(response), kwargs

        cp = TestCommandsProcessor()

        self.assertEqual(
                (None, {'method': 'PROBE'}, {}, {}),
                self.call(cp, 'PROBE'))
        self.assertEqual(
                (1, {'method': 'PROBE', 'arg': 1}, {}, {}),
                self.call(cp, 'PROBE', arg=1))
        self.assertEqual(
                (None, {'method': 'PROBE', 'foo': 'bar'}, {}, {}),
                self.call(cp, 'PROBE', foo='bar'))
        self.assertEqual(
                (-2, {'method': 'PROBE', 'foo': 'bar', 'arg': -2}, {}, {}),
                self.call(cp, 'PROBE', foo='bar', arg=-2))

    def test_PrePost(self):

        class ParentCommandsProcessor(CommandsProcessor):

            @ad.volume_command_pre(method='PROBE')
            def command_pre1(self, request):
                request['probe'].append('pre1')

            @ad.volume_command_pre(method='PROBE')
            def command_pre2(self, request):
                request['probe'].append('pre2')

            @ad.volume_command_post(method='PROBE')
            def command_post1(self, request, response, result):
                request['probe'].append('post1')
                response['probe'].append('post1')
                return result + 1

            @ad.volume_command_post(method='PROBE')
            def command_post2(self, request, response, result):
                request['probe'].append('post2')
                response['probe'].append('post2')
                return result + 1

        class TestCommandsProcessor(ParentCommandsProcessor):

            @ad.volume_command_pre(method='PROBE')
            def command_pre3(self, request):
                request['probe'].append('pre3')

            @ad.volume_command_pre(method='PROBE')
            def command_pre4(self, request):
                request['probe'].append('pre4')

            @ad.volume_command(method='PROBE')
            def command(self, request):
                request['probe'].append('cmd')
                response['probe'].append('cmd')
                return 1

            @ad.volume_command_post(method='PROBE')
            def command_post3(self, request, response, result):
                request['probe'].append('post3')
                response['probe'].append('post3')
                return result + 1

            @ad.volume_command_post(method='PROBE')
            def command_post4(self, request, response, result):
                request['probe'].append('post4')
                response['probe'].append('post4')
                return result + 1

        cp = TestCommandsProcessor()

        request = ad.Request(method='PROBE', probe=[])
        response = ad.Response(probe=[])
        self.assertEqual(5, cp.call(request, response))
        self.assertEqual(['pre1', 'pre2', 'pre3', 'pre4', 'cmd', 'post1', 'post2', 'post3', 'post4'], request['probe'])
        self.assertEqual(['cmd', 'post1', 'post2', 'post3', 'post4'], response['probe'])

    def test_PrePostCallbackLess(self):

        class TestCommandsProcessor(CommandsProcessor):

            @ad.volume_command_pre(method='PROBE')
            def command_pre(self, request):
                request['probe'].append('pre')

            def super_call(self, request, response):
                request['probe'].append('cmd')
                response['probe'].append('cmd')
                return 1

            @ad.volume_command_post(method='PROBE')
            def command_post(self, request, response, result):
                request['probe'].append('post')
                response['probe'].append('post')
                return result + 1

        cp = TestCommandsProcessor()

        request = ad.Request(method='PROBE', probe=[])
        response = ad.Response(probe=[])
        self.assertEqual(2, cp.call(request, response))
        self.assertEqual(['pre', 'cmd', 'post'], request['probe'])
        self.assertEqual(['cmd', 'post'], response['probe'])

    def test_SubCall(self):

        class TestCommandsProcessor(CommandsProcessor):

            @ad.volume_command(method='PROBE')
            def command1(self, request):
                return request.call('PROBE', cmd='command2')

            @ad.volume_command(method='PROBE')
            def command2(self, request):
                return {'access_level': request.access_level, 'accept_language': request.accept_language}

        cp = TestCommandsProcessor()

        request = ad.Request(method='PROBE')
        request.access_level = -1
        request.accept_language = 'foo'
        self.assertEqual({
            'access_level': -1,
            'accept_language': 'foo',
            },
            cp.call(request, ad.Response()))

    def call(self, cp, method, document=None, guid=None, prop=None,
            access_level=env.ACCESS_REMOTE, **kwargs):

        class TestRequest(Request):

            content_stream = None
            content_length = 0

        request = TestRequest(**kwargs)
        request['method'] = method
        request.access_level = access_level
        if document:
            request['document'] = document
        if guid:
            request['guid'] = guid
        if prop:
            request['prop'] = prop
        if 'content_stream' in request:
            request.content_stream = request.pop('content_stream')
            request.content_length = len(request.content_stream.getvalue())

        self.response = Response()
        return cp.call(request, self.response)


if __name__ == '__main__':
    tests.main()