~unity-2d-team/unity-2d/Shell-MultiMonitor

« back to all changes in this revision

Viewing changes to grackle/tests/test_client.py

  • Committer: Curtis Hovey
  • Date: 2012-01-30 23:37:48 UTC
  • mto: (6.1.31 trunk)
  • mto: This revision was merged to the branch mainline in revision 45.
  • Revision ID: curtis.hovey@canonical.com-20120130233748-fqh3ylrbvphn7rmk
Added support for display_type == 'text-only'.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
    HTTPServer,
3
3
    BaseHTTPRequestHandler,
4
4
    )
 
5
from email.message import Message
 
6
from email.mime.multipart import MIMEMultipart
 
7
from email.mime.text import MIMEText
5
8
import httplib
6
9
import logging
7
10
import os
22
25
    )
23
26
 
24
27
 
 
28
def make_json_message(text, message_id='foo',
 
29
                      headers=None, attachment_type=None):
 
30
    message = MIMEMultipart()
 
31
    message.attach(MIMEText(text))
 
32
    message['Message-Id'] = message_id
 
33
    if headers is not None:
 
34
        for k, v in headers.items():
 
35
            message[k] = v
 
36
    if attachment_type is not None:
 
37
        attachment = Message()
 
38
        attachment.set_payload('attactment data.')
 
39
        attachment['Content-Type'] = attachment_type
 
40
        attachment['Content-Disposition'] = (
 
41
            'attachment; filename="file.ext"')
 
42
        message.attach(attachment)
 
43
    dict_message = {
 
44
        'message_id': message_id,
 
45
        'headers': dict(message.items()),
 
46
        'body': message.get_payload(),
 
47
        }
 
48
    return dict_message
 
49
 
 
50
 
25
51
def threaded_messages(messages):
26
52
    threads = {}
27
53
    count = 0
92
118
            max_body = parameters.get('max_body_length')
93
119
            if display_type == 'headers-only':
94
120
                del message['body']
 
121
            elif (display_type == 'text-only'
 
122
                  and isinstance(message['body'], list)):
 
123
                text_parts = [
 
124
                    part.get_payload() for part in message['body']
 
125
                    if part.get_content_type() == 'text/plain']
 
126
                message['body'] = '\n'.join(text_parts)
95
127
            elif max_body is not None:
96
128
                message['body'] = message['body'][:max_body]
 
129
            else:
 
130
                # display_type == 'all'
 
131
                pass
97
132
            new_messages.append(message)
98
133
        messages = new_messages
99
134
        limit = parameters.get('limit', 100)
437
472
                client.get_messages('baz', display_type='unknown')
438
473
 
439
474
    def test_display_type_headers_only(self):
440
 
        client = GrackleClient('localhost', 8445)
 
475
        client = GrackleClient('localhost', 8446)
441
476
        with ForkedFakeService.from_client(client,
442
477
            {'baz': [
443
478
                {'message_id': 'foo',
449
484
        self.assertEqual('foo', first_message['message_id'])
450
485
        self.assertEqual({'From': 'me', 'To': 'you'}, first_message['headers'])
451
486
        self.assertNotIn('body', first_message)
 
487
 
 
488
    def test_display_type_text_only(self):
 
489
        client = GrackleClient('localhost', 8446)
 
490
        with ForkedFakeService.from_client(client,
 
491
            {'baz': [
 
492
                make_json_message(
 
493
                    'abcdefghi', message_id='foo',
 
494
                    headers={'From': 'me', 'To': 'you'},
 
495
                    attachment_type='text/x-diff')
 
496
            ]}):
 
497
            response = client.get_messages('baz', display_type='text-only')
 
498
        first_message = response['messages'][0]
 
499
        self.assertEqual('foo', first_message['message_id'])
 
500
        self.assertEqual('me', first_message['headers']['From'])
 
501
        self.assertEqual('you', first_message['headers']['To'])
 
502
        self.assertEqual(first_message['body'], 'abcdefghi')