~didrocks/unity/altf10

4 by Aaron Bentley
Initial test.
1
from BaseHTTPServer import (
2
    HTTPServer,
3
    BaseHTTPRequestHandler,
4
    )
6 by Aaron Bentley
Use constants.
5
import httplib
4 by Aaron Bentley
Initial test.
6
import os
5 by Aaron Bentley
Actual fake service working.
7
from signal import SIGKILL
13 by Aaron Bentley
Retrieve messages.
8
import simplejson
4 by Aaron Bentley
Initial test.
9
from StringIO import StringIO
3 by Aaron Bentley
Add test framework.
10
from unittest import TestCase
13 by Aaron Bentley
Retrieve messages.
11
from urlparse import urlparse
15 by Aaron Bentley
Test filtering by message-id.
12
from urlparse import parse_qs
3 by Aaron Bentley
Add test framework.
13
5 by Aaron Bentley
Actual fake service working.
14
from testtools import ExpectedException
15
8 by Aaron Bentley
Test message path.
16
from grackle.client import (
17
    GrackleClient,
21 by Aaron Bentley
Test unsupported orders.
18
    UnsupportedOrder,
8 by Aaron Bentley
Test message path.
19
    )
4 by Aaron Bentley
Initial test.
20
21
22 by Aaron Bentley
Order by thread subject.
22
def threaded_messages(messages):
23
    threads = {}
24
    count = 0
25
    pending = []
26
    for message in messages:
27
        if message.get('in_reply_to') is None:
28
            threads[message['message_id']] = [message]
29
            count += 1
30
        else:
31
            pending.append(message)
32
    for message in pending:
33
        threads[message['in_reply_to']].append(message)
34
    return threads.values()
35
36
28 by Aaron Bentley
Extract GrackleStore.
37
class GrackleStore:
5 by Aaron Bentley
Actual fake service working.
38
28 by Aaron Bentley
Extract GrackleStore.
39
    def __init__(self, messages):
12 by Aaron Bentley
Use pipe to ensure we only use HTTP once it's running.
40
        self.messages = messages
26 by Aaron Bentley
Implement an archive namespace.
41
42
    def get_messages(self, archive_id, query_string):
15 by Aaron Bentley
Test filtering by message-id.
43
        query = parse_qs(query_string)
44
        parameters = simplejson.loads(query['parameters'][0])
22 by Aaron Bentley
Order by thread subject.
45
        order = parameters.get('order')
28 by Aaron Bentley
Extract GrackleStore.
46
        messages = self.messages[archive_id]
22 by Aaron Bentley
Order by thread subject.
47
        if order is not None :
48
            if order not in SUPPORTED_ORDERS:
28 by Aaron Bentley
Extract GrackleStore.
49
                raise UnsupportedOrder
23 by Aaron Bentley
Support thread_oldest order.
50
            elif order.startswith('thread_'):
22 by Aaron Bentley
Order by thread subject.
51
                threaded = threaded_messages(messages)
52
                messages = []
23 by Aaron Bentley
Support thread_oldest order.
53
                if order == 'thread_subject':
54
                    threaded.sort(key=lambda t: t[0]['subject'])
55
                if order == 'thread_oldest':
56
                    threaded.sort(key=lambda t: min(m['date'] for m in t))
24 by Aaron Bentley
Support thread_newest threading.
57
                if order == 'thread_newest':
58
                    threaded.sort(key=lambda t: max(m['date'] for m in t))
22 by Aaron Bentley
Order by thread subject.
59
                for thread in threaded:
60
                    messages.extend(thread)
61
            else:
23 by Aaron Bentley
Support thread_oldest order.
62
                messages.sort(key=lambda m: m[order])
22 by Aaron Bentley
Order by thread subject.
63
        messages = [m for m in messages
29 by Aaron Bentley
implement include_hidden.
64
]
65
        new_messages = []
66
        for message in messages:
67
            if (
68
                not parameters['include_hidden']
69
                and message.get('hidden', False)):
70
                continue
71
72
            if ('message_ids' in parameters and
73
                message['message_id'] not in parameters['message_ids']):
74
                continue
75
            message = dict(message)
76
            if 'headers' in parameters:
77
                headers = dict(
78
                    (k, v) for k, v in message['headers'].iteritems()
79
                    if k in parameters['headers'])
80
                message['headers'] = headers
81
            max_body = parameters.get('max_body_length')
82
            if max_body is not None:
83
                message['body'] = message['body'][:max_body]
84
            new_messages.append(message)
85
        messages = new_messages
19 by Aaron Bentley
Implement memo/limit support.
86
        limit = parameters.get('limit', 100)
87
        memo = parameters.get('memo')
88
        message_id_indices = dict(
89
            (m['message_id'], idx) for idx, m in enumerate(messages))
90
        if memo is None:
91
            start = 0
92
        else:
93
            start = message_id_indices[memo.encode('rot13')]
94
        if start > 0:
95
            previous_memo = messages[start - 1]['message_id'].encode('rot13')
96
        else:
97
            previous_memo = None
98
        end = min(start + limit, len(messages))
99
        if end < len(messages):
100
            next_memo = messages[end]['message_id'].encode('rot13')
101
        else:
102
            next_memo = None
103
        messages = messages[start:end]
29 by Aaron Bentley
implement include_hidden.
104
16 by Aaron Bentley
Include next_memo, previous_memo in get_messages response.
105
        response = {
29 by Aaron Bentley
implement include_hidden.
106
            'messages': messages,
19 by Aaron Bentley
Implement memo/limit support.
107
            'next_memo': next_memo,
108
            'previous_memo': previous_memo
16 by Aaron Bentley
Include next_memo, previous_memo in get_messages response.
109
            }
28 by Aaron Bentley
Extract GrackleStore.
110
        return response
111
112
113
114
class ForkedFake:
115
116
    def __init__(self, port, messages=None):
117
        self.pid = None
118
        self.port = port
119
        self.messages = messages
120
        self.read_end, self.write_end = os.pipe()
121
122
    def is_ready(self):
123
        os.write(self.write_end, 'asdf')
124
125
    def __enter__(self):
126
        pid = os.fork()
127
        if pid == 0:
128
            self.start_server()
129
        self.pid = pid
130
        os.read(self.read_end, 1)
131
        return
132
133
    def start_server(self):
134
        service = HTTPServer(('', self.port), FakeGrackleRequestHandler)
135
        service.store = GrackleStore(self.messages)
136
        for archive_id, messages in service.store.messages.iteritems():
137
            for message in messages:
138
                message.setdefault('headers', {})
139
        self.is_ready()
140
        service.serve_forever()
141
142
    def __exit__(self, exc_type, exc_val, traceback):
143
        os.kill(self.pid, SIGKILL)
144
145
146
SUPPORTED_ORDERS = set(
147
    ['date', 'author', 'subject', 'thread_newest', 'thread_oldest',
148
     'thread_subject'])
149
150
151
class FakeGrackleRequestHandler(BaseHTTPRequestHandler):
152
153
    def do_POST(self):
154
        message = self.rfile.read(int(self.headers['content-length']))
155
        if message == 'This is a message':
156
            self.send_response(httplib.CREATED)
157
            self.end_headers()
158
            self.wfile.close()
159
        else:
160
            self.send_error(httplib.BAD_REQUEST)
161
162
    def do_GET(self):
163
        scheme, netloc, path, params, query_string, fragments = (
164
            urlparse(self.path))
165
        parts = path.split('/')
166
        if parts[1] == 'archive':
167
            try:
168
                response = self.server.store.get_messages(
169
                    parts[2], query_string)
170
                self.send_response(httplib.OK)
171
                self.end_headers()
172
                self.wfile.write(simplejson.dumps(response))
173
            except UnsupportedOrder:
174
                self.send_response(httplib.BAD_REQUEST)
175
                self.wfile.write('Unsupported order')
176
                return
13 by Aaron Bentley
Retrieve messages.
177
5 by Aaron Bentley
Actual fake service working.
178
11 by Aaron Bentley
Start working on GET.
179
def fake_grackle_service(client, messages=None):
180
    if messages is None:
181
        messages = {}
12 by Aaron Bentley
Use pipe to ensure we only use HTTP once it's running.
182
    return ForkedFake(client.port, messages)
11 by Aaron Bentley
Start working on GET.
183
4 by Aaron Bentley
Initial test.
184
3 by Aaron Bentley
Add test framework.
185
class TestPutMessage(TestCase):
186
187
    def test_put_message(self):
12 by Aaron Bentley
Use pipe to ensure we only use HTTP once it's running.
188
        client = GrackleClient('localhost', 8436)
11 by Aaron Bentley
Start working on GET.
189
        with fake_grackle_service(client):
7 by Aaron Bentley
Fix URLs etc.
190
            client.put_message('arch1', 'asdf', StringIO('This is a message'))
5 by Aaron Bentley
Actual fake service working.
191
            with ExpectedException(Exception, 'wtf'):
7 by Aaron Bentley
Fix URLs etc.
192
                client.put_message('arch1', 'asdf',
193
                    StringIO('This is not a message'))
11 by Aaron Bentley
Start working on GET.
194
195
196
class TestGetMessages(TestCase):
197
20 by Aaron Bentley
Support order by date
198
    def assertIDOrder(self, ids, messages):
199
        self.assertEqual(ids, [m['message_id'] for m in messages])
200
19 by Aaron Bentley
Implement memo/limit support.
201
    def assertMessageIDs(self, ids, messages):
20 by Aaron Bentley
Support order by date
202
        self.assertIDOrder(
203
            sorted(ids), sorted(messages, key=lambda m:m['message_id']))
19 by Aaron Bentley
Implement memo/limit support.
204
11 by Aaron Bentley
Start working on GET.
205
    def test_get_messages(self):
206
        client = GrackleClient('localhost', 8435)
207
        with fake_grackle_service(client,
208
            {'baz':
17 by Aaron Bentley
Switch hyphens to underscores.
209
            [{'message_id': 'foo'},
210
             {'message_id': 'bar'}]}):
15 by Aaron Bentley
Test filtering by message-id.
211
            response = client.get_messages('baz')
17 by Aaron Bentley
Switch hyphens to underscores.
212
        self.assertEqual(['bar', 'foo'], sorted(m['message_id'] for m in
16 by Aaron Bentley
Include next_memo, previous_memo in get_messages response.
213
            response['messages']))
214
        self.assertIs(None, response['next_memo'])
215
        self.assertIs(None, response['previous_memo'])
15 by Aaron Bentley
Test filtering by message-id.
216
217
    def test_get_messages_by_id(self):
218
        client = GrackleClient('localhost', 8437)
219
        with fake_grackle_service(client,
220
            {'baz':
17 by Aaron Bentley
Switch hyphens to underscores.
221
            [{'message_id': 'foo'},
222
             {'message_id': 'bar'}]}):
16 by Aaron Bentley
Include next_memo, previous_memo in get_messages response.
223
            response = client.get_messages('baz', message_ids=['foo'])
224
        message, = response['messages']
17 by Aaron Bentley
Switch hyphens to underscores.
225
        self.assertEqual('foo', message['message_id'])
19 by Aaron Bentley
Implement memo/limit support.
226
227
    def test_get_messages_batching(self):
20 by Aaron Bentley
Support order by date
228
        client = GrackleClient('localhost', 8438)
19 by Aaron Bentley
Implement memo/limit support.
229
        with fake_grackle_service(client,
230
            {'baz':
231
            [{'message_id': 'foo'},
232
             {'message_id': 'bar'}]}):
233
            response = client.get_messages('baz', limit=1)
234
            self.assertEqual(1, len(response['messages']))
235
            messages = response['messages']
236
            response = client.get_messages(
237
                'baz', limit=1, memo=response['next_memo'])
238
            self.assertEqual(1, len(response['messages']))
239
            messages.extend(response['messages'])
240
            self.assertMessageIDs(['foo', 'bar'], messages)
20 by Aaron Bentley
Support order by date
241
22 by Aaron Bentley
Order by thread subject.
242
    def get_messages_member_order_test(self, key):
20 by Aaron Bentley
Support order by date
243
        client = GrackleClient('localhost', 8439)
244
        with fake_grackle_service(client,
22 by Aaron Bentley
Order by thread subject.
245
                {'baz': [{'message_id': 'foo', key: '2011-03-25'},
246
                 {'message_id': 'bar', key: '2011-03-24'}]}):
20 by Aaron Bentley
Support order by date
247
            response = client.get_messages('baz')
248
            self.assertIDOrder(['foo', 'bar'], response['messages'])
22 by Aaron Bentley
Order by thread subject.
249
            response = client.get_messages('baz', order=key)
20 by Aaron Bentley
Support order by date
250
            self.assertIDOrder(['bar', 'foo'], response['messages'])
21 by Aaron Bentley
Test unsupported orders.
251
22 by Aaron Bentley
Order by thread subject.
252
    def test_get_messages_date_order(self):
253
        self.get_messages_member_order_test('date')
254
255
    def test_get_messages_author_order(self):
256
        self.get_messages_member_order_test('author')
257
258
    def test_get_messages_subject_order(self):
259
        self.get_messages_member_order_test('subject')
260
261
    def test_get_messages_thread_subject_order(self):
262
        client = GrackleClient('localhost', 8439)
263
        with fake_grackle_service(client, {'baz': [
264
            {'message_id': 'bar', 'subject': 'y'},
265
            {'message_id': 'qux', 'subject': 'z'},
266
            {'message_id': 'foo', 'subject': 'x', 'in_reply_to': 'qux'},
267
            ]}):
268
            response = client.get_messages('baz')
269
            self.assertIDOrder(['bar', 'qux', 'foo'], response['messages'])
270
            response = client.get_messages('baz', order='subject')
271
            self.assertIDOrder(['foo', 'bar', 'qux'], response['messages'])
272
            response = client.get_messages('baz', order='thread_subject')
273
            self.assertIDOrder(['bar', 'qux', 'foo'], response['messages'])
274
23 by Aaron Bentley
Support thread_oldest order.
275
    def test_get_messages_thread_oldest_order(self):
276
        client = GrackleClient('localhost', 8439)
277
        with fake_grackle_service(client, {'baz': [
278
            {'message_id': 'bar', 'date': 'x'},
279
            {'message_id': 'qux', 'date': 'z'},
280
            {'message_id': 'foo', 'date': 'y', 'in_reply_to': 'qux'},
281
            ]}):
282
            response = client.get_messages('baz')
283
            self.assertIDOrder(['bar', 'qux', 'foo'], response['messages'])
284
            response = client.get_messages('baz', order='date')
285
            self.assertIDOrder(['bar', 'foo', 'qux'], response['messages'])
286
            response = client.get_messages('baz', order='thread_oldest')
287
            self.assertIDOrder(['bar', 'qux', 'foo'], response['messages'])
288
24 by Aaron Bentley
Support thread_newest threading.
289
    def test_get_messages_thread_newest_order(self):
290
        client = GrackleClient('localhost', 8439)
291
        with fake_grackle_service(client, {'baz': [
292
            {'message_id': 'bar', 'date': 'x'},
293
            {'message_id': 'qux', 'date': 'w'},
294
            {'message_id': 'foo', 'date': 'y', 'in_reply_to': 'bar'},
295
            {'message_id': 'baz', 'date': 'z', 'in_reply_to': 'qux'},
296
            ]}):
297
            response = client.get_messages('baz', order='date')
298
            self.assertIDOrder(
299
                ['qux', 'bar', 'foo', 'baz'], response['messages'])
300
            response = client.get_messages('baz', order='thread_newest')
301
            self.assertIDOrder(
302
                ['bar', 'foo', 'qux', 'baz'], response['messages'])
303
21 by Aaron Bentley
Test unsupported orders.
304
    def test_get_messages_unsupported_order(self):
305
        client = GrackleClient('localhost', 8439)
306
        with fake_grackle_service(client,
307
                {'baz': [{'message_id': 'foo', 'date': '2011-03-25'},
308
                 {'message_id': 'bar', 'date': '2011-03-24'}]}):
309
            with ExpectedException(UnsupportedOrder):
310
                client.get_messages('baz', order='nonsense')
27 by Aaron Bentley
get_messages supports header parameter.
311
312
    def test_get_messages_headers_no_headers(self):
313
        client = GrackleClient('localhost', 8440)
314
        with fake_grackle_service(client,
315
            {'baz': [
316
                {'message_id': 'foo'}
317
            ]}):
318
            response = client.get_messages('baz', headers=[
319
                'Subject', 'Date', 'X-Launchpad-Message-Rationale'])
320
        first_message = response['messages'][0]
321
        self.assertEqual('foo', first_message['message_id'])
322
        self.assertEqual({}, first_message['headers'])
323
324
    def test_get_messages_headers_exclude_headers(self):
29 by Aaron Bentley
implement include_hidden.
325
        client = GrackleClient('localhost', 8441)
27 by Aaron Bentley
get_messages supports header parameter.
326
        with fake_grackle_service(client,
327
            {'baz': [
328
                {'message_id': 'foo', 'headers': {'From': 'me'}}
329
            ]}):
330
            response = client.get_messages('baz', headers=[
331
                'Subject', 'Date', 'X-Launchpad-Message-Rationale'])
332
        first_message = response['messages'][0]
333
        self.assertEqual('foo', first_message['message_id'])
334
        self.assertEqual({}, first_message['headers'])
335
336
    def test_get_messages_headers_include_headers(self):
29 by Aaron Bentley
implement include_hidden.
337
        client = GrackleClient('localhost', 8442)
27 by Aaron Bentley
get_messages supports header parameter.
338
        with fake_grackle_service(client,
339
            {'baz': [
340
                {'message_id': 'foo', 'headers': {'From': 'me', 'To': 'you'}}
341
            ]}):
342
            response = client.get_messages('baz', headers=[
343
                'From', 'To'])
344
        first_message = response['messages'][0]
345
        self.assertEqual('foo', first_message['message_id'])
346
        self.assertEqual({'From': 'me', 'To': 'you'}, first_message['headers'])
28 by Aaron Bentley
Extract GrackleStore.
347
348
    def test_get_messages_max_body_length(self):
29 by Aaron Bentley
implement include_hidden.
349
        client = GrackleClient('localhost', 8443)
28 by Aaron Bentley
Extract GrackleStore.
350
        with fake_grackle_service(client,
351
            {'baz': [
352
                {'message_id': 'foo', 'body': u'abcdefghi'}
353
            ]}):
354
            response = client.get_messages('baz', max_body_length=3)
355
        first_message = response['messages'][0]
356
        self.assertEqual('abc', first_message['body'])
357
29 by Aaron Bentley
implement include_hidden.
358
    def test_include_hidden(self):
359
        client = GrackleClient('localhost', 8444)
360
        with fake_grackle_service(client,
361
            {'baz': [
362
                {'message_id': 'foo', 'hidden': True},
363
                {'message_id': 'bar', 'hidden': False}
364
            ]}):
365
            response = client.get_messages('baz', include_hidden=True)
366
            self.assertMessageIDs(['bar', 'foo'], response['messages'])
367
            response = client.get_messages('baz', include_hidden=False)
368
            self.assertMessageIDs(['bar'], response['messages'])
369