Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/tests/units/model/routes.py
blob: f47ed88d78303337e9ea7db883e37caed5d2eab4 (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
#!/usr/bin/env python
# sugar-lint: disable

import os
import json
import time
from email.utils import formatdate
from os.path import exists

from __init__ import tests, src_root

from sugar_network import db, model
from sugar_network.model.user import User
from sugar_network.toolkit.router import Router, Request
from sugar_network.toolkit import coroutine


class RoutesTest(tests.Test):

    def test_StaticFiles(self):
        router = Router(model.FrontRoutes())
        local_path = src_root + '/sugar_network/static/httpdocs/images/missing.png'

        response = []
        reply = router({
            'PATH_INFO': '/static/images/missing.png',
            'REQUEST_METHOD': 'GET',
            },
            lambda status, headers: response.extend([status, dict(headers)]))
        result = file(local_path).read()
        self.assertEqual(result, ''.join([i for i in reply]))
        self.assertEqual([
            '200 OK',
            {
                'last-modified': formatdate(os.stat(local_path).st_mtime, localtime=False, usegmt=True),
                'content-length': str(len(result)),
                'content-type': 'image/png',
                'content-disposition': 'attachment; filename="missing.png"',
                }
            ],
            response)

    def test_Subscribe(self):

        class Document(db.Resource):

            @db.indexed_property(slot=1)
            def prop(self, value):
                return value

        routes = model.FrontRoutes()
        volume = db.Volume('db', [Document], routes.broadcast)
        events = []

        def read_events():
            for event in routes.subscribe(event='!commit'):
                events.append(event)

        job = coroutine.spawn(read_events)
        coroutine.dispatch()
        volume['document'].create({'guid': 'guid', 'prop': 'value1'})
        coroutine.dispatch()
        volume['document'].update('guid', {'prop': 'value2'})
        coroutine.dispatch()
        volume['document'].delete('guid')
        coroutine.dispatch()
        volume['document'].commit()
        coroutine.sleep(.5)
        job.kill()

        self.assertEqual([
            {'guid': 'guid', 'resource': 'document', 'event': 'create'},
            {'guid': 'guid', 'resource': 'document', 'event': 'update'},
            {'guid': 'guid', 'event': 'delete', 'resource': u'document'},
            ],
            events)

    def test_SubscribeWithPong(self):
        routes = model.FrontRoutes()
        for event in routes.subscribe(ping=True):
            break
        self.assertEqual({'event': 'pong'}, event)

    def test_feed(self):
        volume = db.Volume('db', model.RESOURCES)
        routes = model.VolumeRoutes(volume)

        volume['context'].create({
            'guid': 'context',
            'type': 'activity',
            'title': '',
            'summary': '',
            'description': '',
            'dependencies': ['foo', 'bar'],
            })
        volume['implementation'].create({
            'guid': 'implementation',
            'context': 'context',
            'license': 'GPLv3',
            'version': '1',
            'date': 0,
            'stability': 'stable',
            'notes': '',
            'data': {
                'spec': {
                    '*-*': {
                        'commands': {'activity': {'exec': 'true'}},
                        'requires': {'dep': {}, 'sugar': {'restrictions': [['0.88', None]]}},
                        },
                    },
                },
            })

        self.assertEqual({
            'implementations': [
                {
                    'guid': 'implementation',
                    'author': {},
                    'ctime': 0,
                    'data': {
                        'spec': {
                            '*-*': {
                                'commands': {'activity': {'exec': 'true'}},
                                'requires': {'dep': {}, 'sugar': {'restrictions': [['0.88', None]]}},
                                },
                            },
                        },
                    'layer': [],
                    'license': 'GPLv3',
                    'notes': {'en-us': ''},
                    'stability': 'stable',
                    'tags': [],
                    'version': '1',
                    'requires': {'bar': {}, 'foo': {}},
                    },
                ],
            },
            routes.feed(Request(method='GET', path=['context', 'context']), 'foo'))


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