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