コード例 #1
0
ファイル: test_message.py プロジェクト: staser/mailur
def test_binary():
    assert binary('Ответ: 42').as_bytes() == '\n'.join([
        'MIME-Version: 1.0', 'Content-Transfer-Encoding: binary',
        'Content-Type: text/plain; charset="utf-8"', '', 'Ответ: 42'
    ]).encode()

    assert binary('Ответ: 42').as_string() == '\n'.join([
        'MIME-Version: 1.0', 'Content-Transfer-Encoding: base64',
        'Content-Type: text/plain; charset="utf-8"', '',
        '0J7RgtCy0LXRgjogNDI=', ''
    ])
コード例 #2
0
ファイル: test_imap.py プロジェクト: mrthundergod/mailur
def test_multiappend(patch, msgs):
    new = [(None, None, message.binary(str(i)).as_bytes())
           for i in range(0, 10)]
    con = local.client(None)
    with patch('gevent.pool.Pool.spawn') as m:
        m.return_value.value = ''
        con.multiappend(local.SRC, new)
        assert not m.called
        assert len(msgs(local.SRC)) == 10

        con.multiappend(local.SRC, new, batch=3)
        assert m.called
        assert m.call_count
        assert m.call_args_list[0][0][2] == new[0:3]
        assert m.call_args_list[1][0][2] == new[3:6]
        assert m.call_args_list[2][0][2] == new[6:9]
        assert m.call_args_list[3][0][2] == new[9:]

        m.reset_mock()
        con.multiappend(local.SRC, new, batch=5)
        assert m.called
        assert m.call_count
        assert m.call_args_list[0][0][2] == new[0:5]
        assert m.call_args_list[1][0][2] == new[5:]

    con.multiappend(local.SRC, new, batch=3)
    assert len(msgs(local.SRC)) == 20
コード例 #3
0
ファイル: test_imap.py プロジェクト: mrthundergod/mailur
def test_batched_uids(gm_client):
    con = local.client()
    bsize = 25000
    assert [] == con.fetch([str(i) for i in range(1, 100, 2)], 'FLAGS')
    assert [] == con.fetch([str(i) for i in range(1, bsize, 2)], 'FLAGS')

    con.select(local.ALL, readonly=False)
    assert [] == con.store([str(i) for i in range(1, 100, 2)], '+FLAGS', '#')
    assert [] == con.store([str(i) for i in range(1, bsize, 2)], '+FLAGS', '#')

    # with one message
    msg = message.binary('42')
    msg.add_header('Message-Id', message.gen_msgid())
    con.append(local.SRC, None, None, msg.as_bytes())
    con.select(local.SRC, readonly=True)
    assert [b'1 (UID 1 FLAGS (\\Recent))'
            ] == (con.fetch([str(i) for i in range(1, 100, 2)], 'FLAGS'))
    assert [b'1 (UID 1 FLAGS (\\Recent))'
            ] == (con.fetch([str(i) for i in range(1, bsize, 2)], 'FLAGS'))

    con.select(local.SRC, readonly=False)
    assert [b'1 (UID 1 FLAGS (\\Recent #1))'
            ] == (con.store([str(i) for i in range(1, 100, 2)], '+FLAGS',
                            '#1'))
    assert [b'1 (UID 1 FLAGS (\\Recent #1 #2))'
            ] == (con.store([str(i) for i in range(1, bsize, 2)], '+FLAGS',
                            '#2'))

    gm_client.add_emails([{} for i in range(1, 22)], parse=False)
    assert local.parse(batch=10) is None
コード例 #4
0
ファイル: test_imap.py プロジェクト: naspeh/mailur
def test_multiappend(patch, msgs):
    new = [
        (None, None, message.binary(str(i)).as_bytes())
        for i in range(0, 10)
    ]
    con = local.client(None)
    with patch('gevent.pool.Pool.spawn') as m:
        m.return_value.value = ''
        con.multiappend(local.SRC, new)
        assert not m.called
        assert len(msgs(local.SRC)) == 10

        con.multiappend(local.SRC, new, batch=3)
        assert m.called
        assert m.call_count
        assert m.call_args_list[0][0][2] == new[0:3]
        assert m.call_args_list[1][0][2] == new[3:6]
        assert m.call_args_list[2][0][2] == new[6:9]
        assert m.call_args_list[3][0][2] == new[9:]

        m.reset_mock()
        con.multiappend(local.SRC, new, batch=5)
        assert m.called
        assert m.call_count
        assert m.call_args_list[0][0][2] == new[0:5]
        assert m.call_args_list[1][0][2] == new[5:]

    con.multiappend(local.SRC, new, batch=3)
    assert len(msgs(local.SRC)) == 20
コード例 #5
0
ファイル: test_imap.py プロジェクト: naspeh/mailur
def test_batched_uids(gm_client):
    con = local.client()
    bsize = 25000
    assert [] == con.fetch([str(i) for i in range(1, 100, 2)], 'FLAGS')
    assert [] == con.fetch([str(i) for i in range(1, bsize, 2)], 'FLAGS')

    con.select(local.ALL, readonly=False)
    assert [] == con.store([str(i) for i in range(1, 100, 2)], '+FLAGS', '#')
    assert [] == con.store([str(i) for i in range(1, bsize, 2)], '+FLAGS', '#')

    # with one message
    msg = message.binary('42')
    msg.add_header('Message-Id', message.gen_msgid())
    con.append(local.SRC, None, None, msg.as_bytes())
    con.select(local.SRC, readonly=True)
    assert [b'1 (UID 1 FLAGS (\\Recent))'] == (
        con.fetch([str(i) for i in range(1, 100, 2)], 'FLAGS')
    )
    assert [b'1 (UID 1 FLAGS (\\Recent))'] == (
        con.fetch([str(i) for i in range(1, bsize, 2)], 'FLAGS')
    )

    con.select(local.SRC, readonly=False)
    assert [b'1 (UID 1 FLAGS (\\Recent #1))'] == (
        con.store([str(i) for i in range(1, 100, 2)], '+FLAGS', '#1')
    )
    assert [b'1 (UID 1 FLAGS (\\Recent #1 #2))'] == (
        con.store([str(i) for i in range(1, bsize, 2)], '+FLAGS', '#2')
    )

    gm_client.add_emails([{} for i in range(1, 22)], parse=False)
    assert local.parse(batch=10) is None
コード例 #6
0
ファイル: test_message.py プロジェクト: naspeh/mailur
def test_binary():
    assert binary('Ответ: 42').as_bytes() == '\n'.join([
        'MIME-Version: 1.0',
        'Content-Transfer-Encoding: binary',
        'Content-Type: text/plain; charset="utf-8"',
        '',
        'Ответ: 42'
    ]).encode()

    assert binary('Ответ: 42').as_string() == '\n'.join([
        'MIME-Version: 1.0',
        'Content-Transfer-Encoding: base64',
        'Content-Type: text/plain; charset="utf-8"',
        '',
        '0J7RgtCy0LXRgjogNDI=',
        ''
    ])
コード例 #7
0
ファイル: conftest.py プロジェクト: t-yamamt/mailur
    def add_emails(items=None, *, tag='\\All', fetch=True, parse=True):
        gmail.client()
        if items is None:
            items = [{}]
        gm_client.fetch = [('OK', [])]
        for item in items:
            gm_client.uid += 1
            uid = gm_client.uid
            gid = 100 * uid
            raw = item.get('raw')
            if raw:
                msg = raw
            else:
                txt = item.get('txt', '42')
                msg = message.binary(txt)

                subj = item.get('subj')
                if not subj:
                    subj = 'Subj %s' % uid
                msg.add_header('Subject', subj)

                date = item.get('date')
                if not date:
                    date = gm_client.time + uid
                msg.add_header('Date', formatdate(date))

                mid = item.get('mid')
                if not mid:
                    mid = '<%s@mlr>' % uid
                msg.add_header('Message-ID', mid)

                in_reply_to = item.get('in_reply_to')
                if in_reply_to:
                    msg.add_header('In-Reply-To', in_reply_to)
                refs = item.get('refs')
                if refs:
                    msg.add_header('References', refs)

                msg = msg.as_bytes()
            flags = item.get('flags', '').encode()
            labels = item.get('labels', '').encode()
            res = gm_client.con.append(local.ALL, gmail.MAP_LABELS.get(tag),
                                       None, msg)
            if res[0] != 'OK':
                raise Exception(res)
            gm_client.fetch[0][1].extend([
                (b'1 (X-GM-MSGID %d X-GM-THRID %d X-GM-LABELS (%s) UID %d '
                 b'INTERNALDATE "08-Jul-2017 09:08:30 +0000" FLAGS (%s) '
                 b'BODY[] {%d}' % (gid, gid, labels, uid, flags, len(msg)),
                 msg), b')'
            ])

        if fetch:
            gmail.fetch_folder(tag)
        if parse:
            local.parse()
コード例 #8
0
ファイル: test_message.py プロジェクト: naspeh/mailur
def test_parts(gm_client, latest, load_email):
    gm_client.add_emails([{'raw': binary('').as_bytes()}])
    m = latest(parsed=True)
    assert not m['meta']['files']
    assert m['meta']['preview'] == ''
    assert m['body'] == ''
    assert m['body_txt'] is None

    raw = '<?xml version="1.0" encoding="UTF-8"?>'
    gm_client.add_emails([{'raw': binary(raw, 'text/html').as_bytes()}])
    m = latest(parsed=True)
    assert m['meta']['preview'] == ''
    assert m['body'] == ''

    gm_client.add_emails([{'raw': binary(' a  b\n   c d').as_bytes()}])
    m = latest(parsed=True)
    assert not m['meta']['files']
    assert m['meta']['preview'] == ' a b c d'
    assert m['body'] == '<p>&nbsp;a&nbsp;&nbsp;b<br>&nbsp;&nbsp;&nbsp;c d</p>'

    raw = (
        '<p>'
        '<img data-style="test" />'
        '<img data-src="test" />'
        '<img data-style="color:blue" style="color:red" />'
        '<img src="test" />'
        '</p>'
    )
    gm_client.add_emails([{'raw': binary(raw, 'text/html').as_bytes()}])
    m = latest(parsed=True)
    assert m['meta']['preview'] == ''
    assert m['body'] == '<p><img><img><img style="color:red"><img></p>'

    msg = binary('', 'application/json')
    msg.add_header('Content-Disposition', 'attachment; filename="1/f/ /.json"')
    gm_client.add_emails([{'raw': msg.as_bytes()}])
    m = latest(parsed=True)
    assert m['meta']['files'] == [
        {
            'filename': '1-f---.json',
            'path': '',
            'size': 0,
            'url': '/raw/5/1-f---.json',
        }
    ]
    assert m['meta']['preview'] == '[1-f---.json]'
    assert m['body'] == ''

    msg = MIMEPart()
    msg.make_related()
    msg.attach(binary(' ', 'text/plain'))
    msg.attach(binary(' ', 'text/html'))
    gm_client.add_emails([{'raw': msg.as_bytes()}])
    m = latest(parsed=True)
    assert not m['meta']['files']
    assert m['meta']['preview'] == ''
    assert m['body'] == ''

    msg = MIMEPart()
    msg.make_alternative()
    msg.attach(binary(' ', 'text/plain'))
    msg.attach(binary(' ', 'text/html'))
    gm_client.add_emails([{'raw': msg.as_bytes()}])
    m = latest(parsed=True)
    assert not m['meta']['files']
    assert m['meta']['preview'] == ''
    assert m['body'] == ''

    msg = MIMEPart()
    msg.make_mixed()
    msg.attach(binary('plain', 'text/plain'))
    msg.attach(binary('<p>html</p>', 'text/html'))
    gm_client.add_emails([{'raw': msg.as_bytes()}])
    m = latest(parsed=True)
    assert not m['meta']['files']
    assert m['meta']['preview'] == 'plain html'
    assert m['body'] == '<p>plain</p><hr><p>html</p>'
    assert m['body_txt'] is None

    msg = MIMEPart()
    msg.make_alternative()
    msg.attach(binary('plain', 'text/plain'))
    msg.attach(binary('<p>html</p>', 'text/html'))
    gm_client.add_emails([{'raw': msg.as_bytes()}])
    m = latest(parsed=True)
    assert not m['meta']['files']
    assert m['meta']['preview'] == 'html'
    assert m['body'] == '<p>html</p>'
    assert m['body_txt'] == 'plain'

    msg = MIMEPart()
    msg.make_alternative()
    msg.attach(binary('plain', 'text/plain'))
    htm = MIMEPart()
    htm.make_related()
    htm.attach(binary('<p>html</p>', 'text/html'))
    msg.attach(htm)
    gm_client.add_emails([{'raw': msg.as_bytes()}])
    m = latest(parsed=True)
    assert not m['meta']['files']
    assert m['meta']['preview'] == 'html'
    assert m['body'] == '<p>html</p>'
    assert m['body_txt'] == 'plain'

    msg1 = MIMEPart()
    msg1.make_mixed()
    msg1.attach(msg)
    msg1.attach(binary('<p>html2</p>', 'text/html'))
    gm_client.add_emails([{'raw': msg1.as_bytes()}])
    m = latest(parsed=True)
    assert not m['meta']['files']
    assert m['meta']['preview'] == 'html html2'
    assert m['body'] == '<p>html</p><hr><p>html2</p>'
    assert m['body_txt'] is None

    msg = MIMEPart()
    msg.make_mixed()
    msg.attach(binary('<br>plain', 'text/plain'))
    msg.attach(binary('<p>html</p>', 'text/html'))
    gm_client.add_emails([{'raw': msg.as_bytes()}])
    m = latest(parsed=True)
    assert not m['meta']['files']
    assert m['meta']['preview'] == '&lt;br&gt;plain html'
    assert m['body'] == '<p>&lt;br&gt;plain</p><hr><p>html</p>'
    assert m['body_txt'] is None

    msg = MIMEPart()
    msg.make_mixed()
    msg.attach(binary('1', 'application/json'))
    msg.attach(binary('2', 'application/json'))
    gm_client.add_emails([{'raw': msg.as_bytes()}])
    m = latest(parsed=True)
    assert m['meta']['files'] == [
        {
            'filename': 'unknown-%s.json' % p,
            'path': p,
            'size': 1,
            'url': '/raw/%s/%s/unknown-%s.json' % (m['uid'], p, p),
        } for p in ('1', '2')
    ]
    assert m['body'] == ''
    assert m['meta']['preview'] == '[unknown-1.json, unknown-2.json]'

    msg1 = MIMEPart()
    msg1.make_mixed()
    msg1.attach(binary('1'))
    msg1.attach(msg)
    gm_client.add_emails([{'raw': msg1.as_bytes()}])
    m = latest(parsed=True)
    assert m['meta']['files'] == [
        {
            'filename': 'unknown-%s.json' % p,
            'path': p,
            'size': 1,
            'url': '/raw/%s/%s/unknown-%s.json' % (m['uid'], p, p),
        } for p in ('2.1', '2.2')
    ]
    assert m['body'] == '<p>1</p>'
    assert m['meta']['preview'] == '1 [unknown-2.1.json, unknown-2.2.json]'

    msg2 = MIMEPart()
    msg2.make_mixed()
    msg2.attach(msg)
    msg2.attach(binary('0'))
    msg2.attach(msg1)
    gm_client.add_emails([{'raw': msg2.as_bytes()}])
    m = latest(parsed=True)
    assert m['meta']['files'] == [
        {
            'filename': 'unknown-%s.json' % p,
            'path': p,
            'size': 1,
            'url': '/raw/%s/%s/unknown-%s.json' % (m['uid'], p, p),
        } for p in ('1.1', '1.2', '3.2.1', '3.2.2')
    ]
    assert m['body'] == '<p>0<br><br>1</p>'

    # test some real emails with attachments
    m = load_email('msg-attachments-one-gmail.txt', 'koi8-r', parsed=True)
    assert m['meta']['files'] == [
        {
            'filename': '20.png',
            'image': True,
            'path': '2',
            'size': 544,
            'url': '/raw/16/2/20.png',
        }
    ]
    assert '<hr>' not in m['body']
    assert 'ответ на тело' in m['body']

    m = load_email('msg-attachments-two-gmail.txt', 'koi8-r', parsed=True)
    assert m['meta']['files'] == [
        {
            'filename': '08.png',
            'image': True,
            'path': '2',
            'size': 553,
            'url': '/raw/17/2/08.png',
        },
        {
            'filename': '09.png',
            'image': True,
            'path': '3',
            'size': 520,
            'url': '/raw/17/3/09.png',
        }
    ]
    assert '<hr>' not in m['body']
    assert 'ответ на тело' in m['body']

    m = load_email('msg-attachments-two-yandex.txt', 'koi8-r', parsed=True)
    assert m['meta']['files'] == [
        {
            'filename': '49.png',
            'image': True,
            'path': '2',
            'size': 482,
            'url': '/raw/18/2/49.png',
        },
        {
            'filename': '50.png',
            'image': True,
            'path': '3',
            'size': 456,
            'url': '/raw/18/3/50.png',
        }
    ]
    assert 'ответ на тело' in m['body']

    m = load_email('msg-attachments-textfile.txt', 'koi8-r', parsed=True)
    assert m['meta']['files'] == [
        {
            'filename': 'Дополнение4.txt',
            'path': '2',
            'size': 11,
            'url': '/raw/19/2/Дополнение4.txt',
        }
    ]
    assert m['body'] == '<p>тест</p>'

    m = load_email('msg-rfc822.txt', parsed=True)
    assert m['meta']['files'] == [
        {
            'filename': 'unknown-2.eml',
            'path': '2',
            'size': 463,
            'url': '/raw/20/2/unknown-2.eml',
        }
    ]

    # test embeds
    m = load_email('msg-embeds-one-gmail.txt', parsed=True)
    assert m['meta']['files'] == [{
        'content-id': '<ii_jcrlk9sk0_16122eb711c529e8>',
        'filename': '50.png',
        'image': True,
        'path': '2',
        'size': 456,
        'url': '/raw/21/2/50.png',
    }]
    src = 'src="/raw/%s/2/50.png"' % m['meta']['origin_uid']
    assert src in m['body']
コード例 #9
0
    def add_email(item, tag):
        gm_client.uid += 1
        uid = gm_client.uid
        gid = item.get('gid', 100 * uid)
        raw = item.get('raw')
        if raw:
            msg = raw
            date = gm_client.time + uid
        else:
            txt = item.get('txt', '42')
            msg = message.binary(txt)

            subj = item.get('subj')
            if 'subj' not in item:
                subj = 'Subj %s' % uid
            msg.add_header('Subject', subj)

            date = item.get('date')
            if not date:
                date = gm_client.time + uid
            msg.add_header('Date', formatdate(date, usegmt=True))

            mid = item.get('mid')
            if not mid:
                mid = '<%s@mlr>' % uid
            msg.add_header('Message-ID', mid)

            draft_id = item.get('draft_id')
            if draft_id:
                msg.add_header('X-Draft-ID', '<%s>' % draft_id)

            in_reply_to = item.get('in_reply_to')
            if in_reply_to:
                msg.add_header('In-Reply-To', in_reply_to)
            refs = item.get('refs')
            if refs:
                msg.add_header('References', refs)
            fr = item.get('from')
            if fr:
                msg.add_header('From', fr)
            to = item.get('to')
            if to:
                msg.add_header('To', to)

            msg = msg.as_bytes()

        folder = local.SRC if tag == '\\All' else local.ALL
        res = con_gmail.append(folder, item.get('flags'), None, msg)
        if res[0] != 'OK':
            raise Exception(res)

        arrived = dt.datetime.fromtimestamp(date)
        arrived = arrived.strftime('%d-%b-%Y %H:%M:%S %z').encode()
        flags = item.get('flags', '').encode()
        labels = item.get('labels', '').encode()

        gm_client.fetch[1][1].append(
            (b'1 (X-GM-MSGID %d UID %d )' % (gid, uid)))
        gm_client.fetch[0][1].extend([
            (b'1 (X-GM-MSGID %d X-GM-THRID %d X-GM-LABELS (%s) UID %d '
             b'INTERNALDATE "%s" FLAGS (%s) '
             b'BODY[] {%d}' %
             (gid, gid, labels, uid, arrived, flags, len(msg)), msg), b')'
        ])
コード例 #10
0
ファイル: test_message.py プロジェクト: staser/mailur
def test_parts(gm_client, latest, load_email):
    gm_client.add_emails([{'raw': binary('').as_bytes()}])
    m = latest(parsed=True)
    assert not m['meta']['files']
    assert m['meta']['preview'] == ''
    assert m['body'] == ''
    assert m['body_txt'] is None

    raw = '<?xml version="1.0" encoding="UTF-8"?>'
    gm_client.add_emails([{'raw': binary(raw, 'text/html').as_bytes()}])
    m = latest(parsed=True)
    assert m['meta']['preview'] == ''
    assert m['body'] == ''

    gm_client.add_emails([{'raw': binary(' a  b\n   c d').as_bytes()}])
    m = latest(parsed=True)
    assert not m['meta']['files']
    assert m['meta']['preview'] == ' a b c d'
    assert m['body'] == '<p>&nbsp;a&nbsp;&nbsp;b<br>&nbsp;&nbsp;&nbsp;c d</p>'

    raw = ('<p>'
           '<img data-style="test" />'
           '<img data-src="test" />'
           '<img data-style="color:blue" style="color:red" />'
           '<img src="test" />'
           '</p>')
    gm_client.add_emails([{'raw': binary(raw, 'text/html').as_bytes()}])
    m = latest(parsed=True)
    assert m['meta']['preview'] == ''
    assert m['body'] == '<p><img><img><img style="color:red"><img></p>'

    msg = binary('', 'application/json')
    msg.add_header('Content-Disposition', 'attachment; filename="1/f/ /.json"')
    gm_client.add_emails([{'raw': msg.as_bytes()}])
    m = latest(parsed=True)
    assert m['meta']['files'] == [{
        'filename': '1-f---.json',
        'path': '',
        'size': 0,
        'url': '/raw/5/1-f---.json',
    }]
    assert m['meta']['preview'] == '[1-f---.json]'
    assert m['body'] == ''

    msg = MIMEPart()
    msg.make_related()
    msg.attach(binary(' ', 'text/plain'))
    msg.attach(binary(' ', 'text/html'))
    gm_client.add_emails([{'raw': msg.as_bytes()}])
    m = latest(parsed=True)
    assert not m['meta']['files']
    assert m['meta']['preview'] == ''
    assert m['body'] == ''

    msg = MIMEPart()
    msg.make_alternative()
    msg.attach(binary(' ', 'text/plain'))
    msg.attach(binary(' ', 'text/html'))
    gm_client.add_emails([{'raw': msg.as_bytes()}])
    m = latest(parsed=True)
    assert not m['meta']['files']
    assert m['meta']['preview'] == ''
    assert m['body'] == ''

    msg = MIMEPart()
    msg.make_mixed()
    msg.attach(binary('plain', 'text/plain'))
    msg.attach(binary('<p>html</p>', 'text/html'))
    gm_client.add_emails([{'raw': msg.as_bytes()}])
    m = latest(parsed=True)
    assert not m['meta']['files']
    assert m['meta']['preview'] == 'plain html'
    assert m['body'] == '<p>plain</p><hr><p>html</p>'
    assert m['body_txt'] is None

    msg = MIMEPart()
    msg.make_alternative()
    msg.attach(binary('plain', 'text/plain'))
    msg.attach(binary('<p>html</p>', 'text/html'))
    gm_client.add_emails([{'raw': msg.as_bytes()}])
    m = latest(parsed=True)
    assert not m['meta']['files']
    assert m['meta']['preview'] == 'html'
    assert m['body'] == '<p>html</p>'
    assert m['body_txt'] == 'plain'

    msg = MIMEPart()
    msg.make_alternative()
    msg.attach(binary('plain', 'text/plain'))
    htm = MIMEPart()
    htm.make_related()
    htm.attach(binary('<p>html</p>', 'text/html'))
    msg.attach(htm)
    gm_client.add_emails([{'raw': msg.as_bytes()}])
    m = latest(parsed=True)
    assert not m['meta']['files']
    assert m['meta']['preview'] == 'html'
    assert m['body'] == '<p>html</p>'
    assert m['body_txt'] == 'plain'

    msg1 = MIMEPart()
    msg1.make_mixed()
    msg1.attach(msg)
    msg1.attach(binary('<p>html2</p>', 'text/html'))
    gm_client.add_emails([{'raw': msg1.as_bytes()}])
    m = latest(parsed=True)
    assert not m['meta']['files']
    assert m['meta']['preview'] == 'html html2'
    assert m['body'] == '<p>html</p><hr><p>html2</p>'
    assert m['body_txt'] is None

    msg = MIMEPart()
    msg.make_mixed()
    msg.attach(binary('<br>plain', 'text/plain'))
    msg.attach(binary('<p>html</p>', 'text/html'))
    gm_client.add_emails([{'raw': msg.as_bytes()}])
    m = latest(parsed=True)
    assert not m['meta']['files']
    assert m['meta']['preview'] == '&lt;br&gt;plain html'
    assert m['body'] == '<p>&lt;br&gt;plain</p><hr><p>html</p>'
    assert m['body_txt'] is None

    msg = MIMEPart()
    msg.make_mixed()
    msg.attach(binary('1', 'application/json'))
    msg.attach(binary('2', 'application/json'))
    gm_client.add_emails([{'raw': msg.as_bytes()}])
    m = latest(parsed=True)
    assert m['meta']['files'] == [{
        'filename':
        'unknown-%s.json' % p,
        'path':
        p,
        'size':
        1,
        'url':
        '/raw/%s/%s/unknown-%s.json' % (m['uid'], p, p),
    } for p in ('1', '2')]
    assert m['body'] == ''
    assert m['meta']['preview'] == '[unknown-1.json, unknown-2.json]'

    msg1 = MIMEPart()
    msg1.make_mixed()
    msg1.attach(binary('1'))
    msg1.attach(msg)
    gm_client.add_emails([{'raw': msg1.as_bytes()}])
    m = latest(parsed=True)
    assert m['meta']['files'] == [{
        'filename':
        'unknown-%s.json' % p,
        'path':
        p,
        'size':
        1,
        'url':
        '/raw/%s/%s/unknown-%s.json' % (m['uid'], p, p),
    } for p in ('2.1', '2.2')]
    assert m['body'] == '<p>1</p>'
    assert m['meta']['preview'] == '1 [unknown-2.1.json, unknown-2.2.json]'

    msg2 = MIMEPart()
    msg2.make_mixed()
    msg2.attach(msg)
    msg2.attach(binary('0'))
    msg2.attach(msg1)
    gm_client.add_emails([{'raw': msg2.as_bytes()}])
    m = latest(parsed=True)
    assert m['meta']['files'] == [{
        'filename':
        'unknown-%s.json' % p,
        'path':
        p,
        'size':
        1,
        'url':
        '/raw/%s/%s/unknown-%s.json' % (m['uid'], p, p),
    } for p in ('1.1', '1.2', '3.2.1', '3.2.2')]
    assert m['body'] == '<p>0<br><br>1</p>'

    # test some real emails with attachments
    m = load_email('msg-attachments-one-gmail.txt', 'koi8-r', parsed=True)
    assert m['meta']['files'] == [{
        'filename': '20.png',
        'image': True,
        'path': '2',
        'size': 544,
        'url': '/raw/16/2/20.png',
    }]
    assert '<hr>' not in m['body']
    assert 'ответ на тело' in m['body']

    m = load_email('msg-attachments-two-gmail.txt', 'koi8-r', parsed=True)
    assert m['meta']['files'] == [{
        'filename': '08.png',
        'image': True,
        'path': '2',
        'size': 553,
        'url': '/raw/17/2/08.png',
    }, {
        'filename': '09.png',
        'image': True,
        'path': '3',
        'size': 520,
        'url': '/raw/17/3/09.png',
    }]
    assert '<hr>' not in m['body']
    assert 'ответ на тело' in m['body']

    m = load_email('msg-attachments-two-yandex.txt', 'koi8-r', parsed=True)
    assert m['meta']['files'] == [{
        'filename': '49.png',
        'image': True,
        'path': '2',
        'size': 482,
        'url': '/raw/18/2/49.png',
    }, {
        'filename': '50.png',
        'image': True,
        'path': '3',
        'size': 456,
        'url': '/raw/18/3/50.png',
    }]
    assert 'ответ на тело' in m['body']

    m = load_email('msg-attachments-textfile.txt', 'koi8-r', parsed=True)
    assert m['meta']['files'] == [{
        'filename': 'Дополнение4.txt',
        'path': '2',
        'size': 11,
        'url': '/raw/19/2/Дополнение4.txt',
    }]
    assert m['body'] == '<p>тест</p>'

    m = load_email('msg-rfc822.txt', parsed=True)
    assert m['meta']['files'] == [{
        'filename': 'unknown-2.eml',
        'path': '2',
        'size': 463,
        'url': '/raw/20/2/unknown-2.eml',
    }]

    # test embeds
    m = load_email('msg-embeds-one-gmail.txt', parsed=True)
    assert m['meta']['files'] == [{
        'content-id': '<ii_jcrlk9sk0_16122eb711c529e8>',
        'filename': '50.png',
        'image': True,
        'path': '2',
        'size': 456,
        'url': '/raw/21/2/50.png',
    }]
    src = 'src="/raw/%s/2/50.png"' % m['meta']['origin_uid']
    assert src in m['body']
コード例 #11
0
def test_parts(gm_client, latest, load_email):
    msg = binary('1')
    gm_client.add_emails([{'raw': msg.as_bytes()}])
    m = latest(parsed=True)
    assert not m['meta']['files']

    msg = MIMEPart()
    msg.make_mixed()
    msg.attach(binary('1', 'application/json'))
    msg.attach(binary('2', 'application/json'))
    gm_client.add_emails([{'raw': msg.as_bytes()}])
    m = latest(parsed=True)
    assert m['meta']['files'] == [{'path': p, 'size': 1} for p in ('1', '2')]

    msg1 = MIMEPart()
    msg1.make_mixed()
    msg1.attach(binary('1'))
    msg1.attach(msg)
    gm_client.add_emails([{'raw': msg1.as_bytes()}])
    m = latest(parsed=True)
    assert m['meta']['files'] == [{
        'path': p,
        'size': 1
    } for p in ('2.1', '2.2')]

    msg2 = MIMEPart()
    msg2.make_mixed()
    msg2.attach(msg)
    msg2.attach(binary('2'))
    msg2.attach(msg1)
    gm_client.add_emails([{'raw': msg2.as_bytes()}])
    m = latest(parsed=True)
    assert m['meta']['files'] == [{
        'path': p,
        'size': 1
    } for p in ('1.1', '1.2', '3.2.1', '3.2.2')]

    # test some real emails with attachments
    m = load_email('msg-attachments-one-gmail.txt', parsed=True)
    assert m['meta']['files'] == [{
        'filename': '20.png',
        'path': '2',
        'size': 523
    }]

    m = load_email('msg-attachments-two-gmail.txt', parsed=True)
    assert m['meta']['files'] == [{
        'filename': '08.png',
        'path': '2',
        'size': 532
    }, {
        'filename': '09.png',
        'path': '3',
        'size': 503
    }]

    m = load_email('msg-attachments-two-yandex.txt', parsed=True)
    assert m['meta']['files'] == [{
        'filename': '49.png',
        'path': '2',
        'size': 472
    }, {
        'filename': '50.png',
        'path': '3',
        'size': 443
    }]

    # test embeds
    m = load_email('msg-embeds-one-gmail.txt', parsed=True)
    assert m['meta']['files'] == [{
        'content-id': '<ii_jcrlk9sk0_16122eb711c529e8>',
        'filename': '50.png',
        'path': '2',
        'size': 443
    }]
    url = '/raw/%s/2' % m['meta']['origin_uid']
    assert url in m['body']

    m = load_email('msg-embeds-external.txt', parsed=True)
    assert m['meta']['files'] == []
    assert 'data-src="/proxy?url=%2F%2Fwww.gravatar.com' in m['body']
    assert 'data-src="/proxy?url=http%3A%2F%2Fwww.gravatar.com' in m['body']
    assert 'data-src="/proxy?url=https%3A%2F%2Fwww.gravatar.com' in m['body']