Beispiel #1
0
def test_clean_flags(gm_client, msgs, login):
    gm_client.add_emails([{}] * 2)
    local.link_threads(['1', '2'])

    assert [i['flags'] for i in msgs(local.SRC)] == ['', '', '\\Seen #link']
    assert [i['flags'] for i in msgs()] == ['', '#latest', '\\Seen #link']

    con_src = local.client(local.SRC, readonly=False)
    con_all = local.client(local.ALL, readonly=False)

    con_src.store('3', '-FLAGS', '#link \\Seen')
    con_src.store('1', '+FLAGS', '#latest')
    con_src.store('2', '+FLAGS', '#dup')
    con_all.store('3', '-FLAGS', '#link \\Seen')
    con_all.store('2', '-FLAGS', '#latest')
    assert [i['flags'] for i in msgs(local.SRC)] == ['#latest', '#dup', '']
    assert [i['flags'] for i in msgs()] == ['', '', '']
    local.clean_flags()
    assert [i['flags'] for i in msgs(local.SRC)] == ['', '', '\\Seen #link']
    assert [i['flags'] for i in msgs()] == ['', '#latest', '\\Seen #link']

    con_src.store('3', '-FLAGS', '#link \\Seen')
    con_src.store('2', '+FLAGS', '#err')
    con_all.store('3', '-FLAGS', '#link \\Seen')
    con_all.store('2', '-FLAGS', '#latest')
    assert [i['flags'] for i in msgs(local.SRC)] == ['', '#err', '']
    assert [i['flags'] for i in msgs()] == ['', '', '']
    cli.main('clean-flags %s' % login.user1)
    assert [i['flags'] for i in msgs(local.SRC)] == ['', '', '\\Seen #link']
    assert [i['flags'] for i in msgs()] == ['', '#latest', '\\Seen #link']
Beispiel #2
0
def test_cli_idle2(gm_client, msgs, login, patch):
    remote.data_account({
        'username': '******',
        'password': '******',
        'imap_host': 'imap.test.com',
        'smtp_host': 'smtp.test.com'
    })
    assert remote.get_folders() == [{'tag': '\\All'}]

    spawn(lambda: cli.main('sync %s --timeout=300' % login.user1))
    sleep(2)

    gm_client.add_emails([{}] * 4, fetch=False, parse=False)
    gm_client.fetch = [gm_client.fetch[0]]
    sleep(2)
    assert len(msgs(local.SRC)) == 4
    assert len(msgs()) == 4

    gm_client.list = []
    xlist = [('OK', [b'(\\HasNoChildren) "/" INBOX'])] * 10
    with patch.object(gm_client, 'list', xlist):
        spawn(lambda: cli.main('sync %s --timeout=300' % login.user1))
        sleep(2)

        gm_client.add_emails([{'flags': '#inbox'}], fetch=False, parse=False)
        gm_client.fetch = [gm_client.fetch[0]]
        sleep(2)
        assert len(msgs(local.SRC)) == 5
        assert len(msgs()) == 5
        assert len(msgs('INBOX')) == 1
Beispiel #3
0
def test_cli_idle2(gm_client, msgs, login, patch):
    remote.data_account({
        'username': '******',
        'password': '******',
        'imap_host': 'imap.test.com',
        'smtp_host': 'smtp.test.com'
    })
    assert remote.get_folders() == [{'tag': '\\All'}]

    spawn(lambda: cli.main('sync %s --timeout=300' % login.user1))
    sleep(2)

    gm_client.add_emails([{}] * 4, fetch=False, parse=False)
    gm_client.fetch = [gm_client.fetch[0]]
    sleep(2)
    assert len(msgs(local.SRC)) == 4
    assert len(msgs()) == 4

    gm_client.list = []
    xlist = [('OK', [b'(\\HasNoChildren) "/" INBOX'])] * 10
    with patch.object(gm_client, 'list', xlist):
        spawn(lambda: cli.main('sync %s --timeout=300' % login.user1))
        sleep(2)

        gm_client.add_emails([{'flags': '#inbox'}], fetch=False, parse=False)
        gm_client.fetch = [gm_client.fetch[0]]
        sleep(2)
        assert len(msgs(local.SRC)) == 5
        assert len(msgs()) == 5
        assert len(msgs('INBOX')) == 1
Beispiel #4
0
def test_cli_all_flags(gm_client, msgs, login):
    gm_client.add_emails([{}] * 5)
    assert [i['flags'] for i in msgs(local.SRC)] == [''] * 5
    assert [i['flags'] for i in msgs()] == [''] * 5

    con_src = local.client(local.SRC, readonly=False)
    con_all = local.client(local.ALL, readonly=False)

    con_src.store('1:*', '+FLAGS', '#1')
    con_all.store('1:*', '+FLAGS', '#2')
    cli.main('sync-flags %s' % login.user1)
    assert [i['flags'] for i in msgs(local.SRC)] == ['#1'] * 5
    assert [i['flags'] for i in msgs()] == ['#1'] * 5

    con_src.store('1:*', '+FLAGS', '#2')
    con_all.store('1:*', '+FLAGS', '#3')
    cli.main('sync-flags %s --reverse' % login.user1)
    assert [i['flags'] for i in msgs(local.SRC)] == ['#1 #3'] * 5
    assert [i['flags'] for i in msgs()] == ['#1 #3'] * 5
Beispiel #5
0
def test_cli_all_flags(gm_client, msgs, login):
    gm_client.add_emails([{}] * 5)
    assert [i['flags'] for i in msgs(local.SRC)] == [''] * 5
    assert [i['flags'] for i in msgs()] == [''] * 5

    con_src = local.client(local.SRC, readonly=False)
    con_all = local.client(local.ALL, readonly=False)

    con_src.store('1:*', '+FLAGS', '#1')
    con_all.store('1:*', '+FLAGS', '#2')
    cli.main('sync-flags %s' % login.user1)
    assert [i['flags'] for i in msgs(local.SRC)] == ['#1'] * 5
    assert [i['flags'] for i in msgs()] == ['#1'] * 5

    con_src.store('1:*', '+FLAGS', '#2')
    con_all.store('1:*', '+FLAGS', '#3')
    cli.main('sync-flags %s --reverse' % login.user1)
    assert [i['flags'] for i in msgs(local.SRC)] == ['#1 #3'] * 5
    assert [i['flags'] for i in msgs()] == ['#1 #3'] * 5
Beispiel #6
0
def test_general(gm_client, login, msgs, patch):
    stdout = check_output('mlr -h', shell=True)
    assert b'Mailur CLI' in stdout

    stdout = check_output('mlr icons', shell=True)
    assert b'assets/font/icons.less updated\n' in stdout

    gm_client.add_emails([{}, {}], fetch=False, parse=False)
    assert [i['uid'] for i in msgs(local.SRC)] == []
    assert [i['uid'] for i in msgs()] == []

    cli.main('gmail %s --parse' % login.user1)
    assert [i['uid'] for i in msgs(local.SRC)] == ['1', '2']
    assert [i['uid'] for i in msgs()] == ['1', '2']
    local.link_threads(['1', '2'])

    assert [i['uid'] for i in msgs(local.SRC)] == ['1', '2', '3']
    assert [i['uid'] for i in msgs()] == ['1', '2', '3']
    cli.main('update-links %s' % login.user1)
    assert [i['uid'] for i in msgs(local.SRC)] == ['1', '2', '4']
    assert [i['uid'] for i in msgs()] == ['1', '2', '4']

    with patch('mailur.cli.local') as m:
        cli.main('update-metadata %s' % login.user1)
        assert m.save_addrs.called
        assert m.save_msgids.called
        assert m.save_uid_pairs.called
Beispiel #7
0
def test_clean_flags(gm_client, msgs, login):
    gm_client.add_emails([{}] * 2)
    local.link_threads(['1', '2'])

    assert [i['flags'] for i in msgs(local.SRC)] == ['', '']
    assert [i['flags'] for i in msgs()] == ['', '']

    con_src = local.client(local.SRC, readonly=False)
    con_all = local.client(local.ALL, readonly=False)

    con_src.store('1', '+FLAGS', '#tag1')
    con_src.store('2', '+FLAGS', '#tag2 #tag3')
    con_all.store('1', '+FLAGS', '#tag1 #tag3')
    con_all.store('2', '+FLAGS', '#tag2')
    assert [i['flags'] for i in msgs(local.SRC)] == ['#tag1', '#tag2 #tag3']
    assert [i['flags'] for i in msgs()] == ['#tag1 #tag3', '#tag2']

    cli.main('clean-flags %s #tag1' % login.user1)
    assert [i['flags'] for i in msgs(local.SRC)] == ['', '#tag2 #tag3']
    assert [i['flags'] for i in msgs()] == ['#tag3', '#tag2']

    cli.main('clean-flags %s #tag2 #tag3' % login.user1)
    assert [i['flags'] for i in msgs(local.SRC)] == ['', '']
    assert [i['flags'] for i in msgs()] == ['', '']
Beispiel #8
0
def test_clean_flags(gm_client, msgs, login):
    gm_client.add_emails([{}] * 2)
    local.link_threads(['1', '2'])

    assert [i['flags'] for i in msgs(local.SRC)] == ['', '']
    assert [i['flags'] for i in msgs()] == ['', '']

    con_src = local.client(local.SRC, readonly=False)
    con_all = local.client(local.ALL, readonly=False)

    con_src.store('1', '+FLAGS', '#tag1')
    con_src.store('2', '+FLAGS', '#tag2 #tag3')
    con_all.store('1', '+FLAGS', '#tag1 #tag3')
    con_all.store('2', '+FLAGS', '#tag2')
    assert [i['flags'] for i in msgs(local.SRC)] == ['#tag1', '#tag2 #tag3']
    assert [i['flags'] for i in msgs()] == ['#tag1 #tag3', '#tag2']

    cli.main('clean-flags %s #tag1' % login.user1)
    assert [i['flags'] for i in msgs(local.SRC)] == ['', '#tag2 #tag3']
    assert [i['flags'] for i in msgs()] == ['#tag3', '#tag2']

    cli.main('clean-flags %s #tag2 #tag3' % login.user1)
    assert [i['flags'] for i in msgs(local.SRC)] == ['', '']
    assert [i['flags'] for i in msgs()] == ['', '']
Beispiel #9
0
def test_cli_idle(gm_client, msgs, login, patch):
    spawn(lambda: cli.main('sync %s --timeout=300' % login.user1))
    sleep(2)

    gm_client.add_emails([{}] * 4, fetch=False, parse=False)
    sleep(2)
    assert len(msgs(local.SRC)) == 4
    assert len(msgs()) == 4

    local.parse('all')

    gm_client.add_emails([{}], fetch=False, parse=False)
    sleep(2)
    assert len(msgs(local.SRC)) == 5
    assert len(msgs()) == 5

    con_src = local.client(local.SRC, readonly=False)
    con_src.store('1:*', '+FLAGS', '#1')
    sleep(2)
    assert [i['flags'] for i in msgs(local.SRC)] == ['#1'] * 5
    assert [i['flags'] for i in msgs()] == ['#1'] * 5
Beispiel #10
0
def test_cli_idle(gm_client, msgs, login, patch):
    spawn(lambda: cli.main('sync %s --timeout=300' % login.user1))
    sleep(2)

    gm_client.add_emails([{}] * 4, fetch=False, parse=False)
    sleep(2)
    assert len(msgs(local.SRC)) == 4
    assert len(msgs()) == 4

    local.parse('all')

    gm_client.add_emails([{}], fetch=False, parse=False)
    sleep(2)
    assert len(msgs(local.SRC)) == 5
    assert len(msgs()) == 5

    con_src = local.client(local.SRC, readonly=False)
    con_src.store('1:*', '+FLAGS', '#1')
    sleep(2)
    assert [i['flags'] for i in msgs(local.SRC)] == ['#1'] * 5
    assert [i['flags'] for i in msgs()] == ['#1'] * 5
Beispiel #11
0
def test_cli_idle(gm_client, msgs, login, patch):
    with patch('mailur.gmail.get_credentials') as m:
        m.return_value = login.user2, 'user'
        spawn(lambda: cli.main('sync %s --timeout=300' % login.user1))
        sleep(3)

    gm_client.add_emails([{}] * 4, fetch=False, parse=False)
    sleep(2)
    assert len(msgs(local.SRC)) == 4
    assert len(msgs()) == 4

    local.parse('all')

    gm_client.add_emails([{}], fetch=False, parse=False)
    sleep(1)
    assert len(msgs(local.SRC)) == 5
    assert len(msgs()) == 5

    con_src = local.client(local.SRC, readonly=False)
    con_src.store('1:*', '+FLAGS', '#1')
    sleep(1)
    assert [i['flags'] for i in msgs(local.SRC)] == ['#1'] * 5
    assert [i['flags'] for i in msgs()] == ['#latest #1'] * 5
Beispiel #12
0
def test_general(gm_client, login, msgs, patch, call):
    stdout = check_output('mlr -h', shell=True)
    assert b'Mailur CLI' in stdout

    stdout = check_output('mlr icons', shell=True)
    assert b'assets/font/icons.less updated\n' in stdout

    gm_client.add_emails([{}, {}], fetch=False, parse=False)
    assert [i['uid'] for i in msgs(local.SRC)] == []
    assert [i['uid'] for i in msgs()] == []

    cli.main('remote %s --parse' % login.user1)
    assert [i['uid'] for i in msgs(local.SRC)] == ['1', '2']
    assert [i['uid'] for i in msgs()] == ['1', '2']

    with patch('mailur.cli.local') as m:
        cli.main('metadata %s' % login.user1)
        assert m.update_metadata.called

    cli.main('metadata %s' % login.user1)

    with patch('mailur.cli.remote.fetch_folder') as m:
        cli.main('remote %s' % login.user1)
        opts = {'batch': 1000, 'threads': 2}
        assert m.call_args_list == [
            call(tag='\\All', **opts),
            call(tag='\\Junk', **opts),
            call(tag='\\Trash', **opts),
        ]

        m.reset_mock()
        cli.main('remote %s --tag=\\All' % login.user1)
        assert m.call_args_list == [call(tag='\\All', **opts)]

        m.reset_mock()
        cli.main('remote %s --box=All' % login.user1)
        assert m.call_args_list == [call(box='All', **opts)]
Beispiel #13
0
def test_fetch_and_parse(gm_client, login, msgs, patch, raises):
    stdout = check_output('mlr parse %s' % login.user1, shell=True)
    assert b'## all parsed already' in stdout

    cli.main('remote %s' % login.user1)
    assert len(msgs(local.SRC)) == 0
    assert len(msgs()) == 0

    gm_client.add_emails([{}], fetch=False, parse=False)
    cli.main('remote %s' % login.user1)
    assert len(msgs(local.SRC)) == 1
    assert len(msgs()) == 0

    cli.main('parse %s' % login.user1)
    assert len(msgs(local.SRC)) == 1
    assert len(msgs()) == 1

    gm_client.add_emails([{}], fetch=False, parse=False)
    cli.main('remote %s --parse' % login.user1)
    assert len(msgs(local.SRC)) == 2
    assert len(msgs()) == 2

    assert [i['uid'] for i in msgs(local.SRC)] == ['1', '2']
    assert [i['uid'] for i in msgs()] == ['1', '2']
    cli.main('parse %s all' % login.user1)
    assert [i['uid'] for i in msgs(local.SRC)] == ['1', '2']
    assert [i['uid'] for i in msgs()] == ['3', '4']

    with patch('mailur.remote.fetch') as m, raises(SystemExit):
        m.side_effect = SystemExit
        cli.main('remote %s --parse' % login.user1)
    assert len(msgs(local.SRC)) == 2
    assert len(msgs()) == 2
Beispiel #14
0
def test_general(gm_client, login, msgs, patch, call):
    stdout = check_output('mlr -h', shell=True)
    assert b'Mailur CLI' in stdout

    gm_client.add_emails([{}, {}], fetch=False, parse=False)
    assert [i['uid'] for i in msgs(local.SRC)] == []
    assert [i['uid'] for i in msgs()] == []

    cli.main('%s remote --parse' % login.user1)
    assert [i['uid'] for i in msgs(local.SRC)] == ['1', '2']
    assert [i['uid'] for i in msgs()] == ['1', '2']

    with patch('mailur.cli.local') as m:
        cli.main('%s metadata' % login.user1)
        assert m.update_metadata.called

    cli.main('%s metadata' % login.user1)
    with local.client() as con:
        con.copy(['1', '2'], local.ALL)
        puids = con.search('ALL')
        con.select(local.SRC)
        ouids = con.search('ALL')
        assert len(puids) == (2 * len(ouids))

        cli.main('%s metadata' % login.user1)
        con.select(local.ALL)
        all_puids = con.search('ALL')
        assert len(all_puids) == 4
        local.link_threads(ouids)
        cli.main('%s parse --fix-duplicates' % login.user1)
        cli.main('%s metadata' % login.user1)

        con.select(local.ALL)
        puids = con.search('ALL')
        con.select(local.SRC)
        ouids = con.search('ALL')
        assert len(puids) == len(ouids)
        puids_from_msgs = sorted(local.data_msgs.get())
        puids_from_pairs = sorted(local.data_uidpairs.get().values())
        assert puids_from_msgs == puids_from_pairs

    with patch('mailur.cli.remote.fetch_folder') as m:
        cli.main('%s remote' % login.user1)
        opts = {'batch': 1000, 'threads': 2}
        assert m.call_args_list == [
            call(tag='\\All', **opts),
            call(tag='\\Junk', **opts),
            call(tag='\\Trash', **opts),
        ]

        m.reset_mock()
        cli.main('%s remote --tag=\\All' % login.user1)
        assert m.call_args_list == [call(tag='\\All', **opts)]

        m.reset_mock()
        cli.main('%s remote --box=All' % login.user1)
        assert m.call_args_list == [call(box='All', **opts)]
Beispiel #15
0
def test_cli_idle_gmail(gm_client, msgs, login, patch):
    actions = []

    def fetch(con, uids, fields):
        responces = getattr(gm_client, 'fetch', None)
        if 'CHANGEDSINCE' in fields:
            index = fields.split()[-1]
            if index == '5)':
                return ('OK', [
                    b'4 (X-GM-MSGID 10400 '
                    b'X-GM-LABELS ("\\Inbox" "\\Starred" "mlr/thrid/777") '
                    b'FLAGS (\\Seen) UID 104 MODSEQ (427368))'
                ])
            return ('OK', [])
        elif responces:
            return responces.pop()
        elif 'X-GM-LABELS' in fields:
            if con.current_box != local.SRC:
                return ('OK', [
                    b'1 (X-GM-MSGID 10100 X-GM-LABELS () '
                    b'FLAGS (\\Seen) UID 101 MODSEQ (427368))'
                ])
            return ('OK', [
                (b'2 (X-GM-MSGID 10200 X-GM-LABELS () '
                 b'FLAGS (\\Seen) UID 102 MODSEQ (427368))'),
                (b'3 (X-GM-MSGID 10300 X-GM-LABELS () '
                 b'FLAGS (\\Seen) UID 103 MODSEQ (427368))'),
                (b'4 (X-GM-MSGID 10400 X-GM-LABELS () '
                 b'FLAGS (\\Seen) UID 104 MODSEQ (427368))'),
                (b'6 (X-GM-MSGID 10600 X-GM-LABELS (\\Draft) '
                 b'FLAGS (\\Seen) UID 106 MODSEQ (427368))'),
            ])
        return con._uid('FETCH', uids, fields)

    def search(con, charset, *criteria):
        if 'X-GM-MSGID' in criteria[0]:
            uid = int(criteria[0].split()[-1]) // 100
            return ('OK', [(b'%d' % uid)])
        return con._uid('SEARCH', charset, *criteria)

    def store(con, uids, cmd, flags):
        if 'X-GM-LABELS' in cmd:
            actions.append((uids, cmd, sorted(flags.split())))
            return ('OK', [])
        return con._uid('STORE', uids, cmd, flags)

    gm_client.fake_fetch = fetch
    gm_client.fake_search = search
    gm_client.fake_store = store

    spawn(lambda: cli.main('%s sync --timeout=300' % login.user1))
    sleep(5)

    gm_client.add_emails([{}] * 4, fetch=False, parse=False)
    sleep(3)
    assert len(msgs(local.SRC)) == 4
    assert len(msgs()) == 4

    local.parse('all')

    gm_client.add_emails([{}], fetch=False, parse=False)
    sleep(3)
    assert len(msgs(local.SRC)) == 5
    assert len(msgs()) == 5
    expected_flags = ['', '', '', '\\Flagged \\Seen #inbox', '']
    assert [i['flags'] for i in msgs(local.SRC)] == expected_flags
    assert [i['flags'] for i in msgs()] == expected_flags

    con_src = local.client(local.SRC, readonly=False)
    con_src.store('1:*', '+FLAGS', '#1')
    sleep(3)
    expected_flags = [(f + ' #1').strip() for f in expected_flags]
    assert [i['flags'] for i in msgs(local.SRC)] == expected_flags
    assert [i['flags'] for i in msgs()] == expected_flags

    assert actions == [
        ('101', '-X-GM-LABELS', ['\\Spam']),
        ('101', '+X-GM-LABELS', ['\\Inbox']),  # move to \\All
        ('101', '-X-GM-LABELS', ['\\Trash']),
        ('101', '+X-GM-LABELS', ['\\Inbox']),  # move to \\All
        ('104', '+X-GM-LABELS', ['\\Inbox', '\\Starred']),
        ('104', '+X-GM-LABELS', ['\\Inbox', '\\Starred']),
        ('101', '-X-GM-LABELS', ['\\Spam']),
        ('101', '+X-GM-LABELS', ['\\Inbox']),  # move to \\All
        ('101', '-X-GM-LABELS', ['\\Trash']),
        ('101', '+X-GM-LABELS', ['\\Inbox']),  # move to \\All
    ]

    actions.clear()
    con_src = local.client(local.SRC, readonly=False)
    con_src.store('2', '+FLAGS', '#inbox')
    sleep(3)
    con_src.store('2', '+FLAGS', '#trash')
    sleep(3)
    expected_flags[1] = '#inbox #1 #trash'
    assert [i['flags'] for i in msgs(local.SRC)] == expected_flags
    assert [i['flags'] for i in msgs()] == expected_flags
    assert actions == [
        ('102', '+X-GM-LABELS', ['\\Inbox']),
        ('102', '-X-GM-LABELS', ['\\Inbox']),
        ('102', '+X-GM-LABELS', ['\\Trash']),
    ]