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