| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Copyright (c) 2016, Menno Smits |
| 3 |
# Released subject to the New BSD License |
| 4 |
# Please see http://en.wikipedia.org/wiki/BSD_licenses |
| 5 |
|
| 6 |
from unittest.mock import Mock, patch, sentinel |
| 7 |
|
| 8 |
from imapclient.imapclient import ANSWERED, DELETED, DRAFT, FLAGGED, RECENT, SEEN |
| 9 |
|
| 10 |
from .imapclient_test import IMAPClientTest |
| 11 |
|
| 12 |
|
| 13 |
class TestFlagsConsts(IMAPClientTest): |
| 14 |
def test_flags_are_bytes(self): |
| 15 |
for flag in DELETED, SEEN, ANSWERED, FLAGGED, DRAFT, RECENT: |
| 16 |
if not isinstance(flag, bytes): |
| 17 |
self.fail("%r flag is not bytes" % flag) |
| 18 |
|
| 19 |
|
| 20 |
class TestFlags(IMAPClientTest): |
| 21 |
def setUp(self): |
| 22 |
super(TestFlags, self).setUp() |
| 23 |
self.client._command_and_check = Mock() |
| 24 |
|
| 25 |
def test_get(self): |
| 26 |
with patch.object( |
| 27 |
self.client, |
| 28 |
"fetch", |
| 29 |
autospec=True, |
| 30 |
return_value={123: {b"FLAGS": [b"foo", b"bar"]}, 444: {b"FLAGS": [b"foo"]}}, |
| 31 |
): |
| 32 |
out = self.client.get_flags(sentinel.messages) |
| 33 |
self.client.fetch.assert_called_with(sentinel.messages, ["FLAGS"]) |
| 34 |
self.assertEqual(out, {123: [b"foo", b"bar"], 444: [b"foo"]}) |
| 35 |
|
| 36 |
def test_set(self): |
| 37 |
self.check(self.client.set_flags, b"FLAGS") |
| 38 |
|
| 39 |
def test_add(self): |
| 40 |
self.check(self.client.add_flags, b"+FLAGS") |
| 41 |
|
| 42 |
def test_remove(self): |
| 43 |
self.check(self.client.remove_flags, b"-FLAGS") |
| 44 |
|
| 45 |
def check(self, meth, expected_command): |
| 46 |
self._check(meth, expected_command) |
| 47 |
self._check(meth, expected_command, silent=True) |
| 48 |
|
| 49 |
def _check(self, meth, expected_command, silent=False): |
| 50 |
if silent: |
| 51 |
expected_command += b".SILENT" |
| 52 |
|
| 53 |
cc = self.client._command_and_check |
| 54 |
cc.return_value = [ |
| 55 |
b"11 (FLAGS (blah foo) UID 1)", |
| 56 |
b"11 (UID 1 OTHER (dont))", |
| 57 |
b"22 (FLAGS (foo) UID 2)", |
| 58 |
b"22 (UID 2 OTHER (care))", |
| 59 |
] |
| 60 |
resp = meth([1, 2], "foo", silent=silent) |
| 61 |
cc.assert_called_once_with("store", b"1,2", expected_command, "(foo)", uid=True) |
| 62 |
if silent: |
| 63 |
self.assertIsNone(resp) |
| 64 |
else: |
| 65 |
self.assertEqual( |
| 66 |
resp, |
| 67 |
{ |
| 68 |
1: (b"blah", b"foo"), |
| 69 |
2: (b"foo",), |
| 70 |
}, |
| 71 |
) |
| 72 |
|
| 73 |
cc.reset_mock() |
| 74 |
|
| 75 |
|
| 76 |
class TestGmailLabels(IMAPClientTest): |
| 77 |
def setUp(self): |
| 78 |
super(TestGmailLabels, self).setUp() |
| 79 |
self.client._command_and_check = Mock() |
| 80 |
|
| 81 |
def test_get(self): |
| 82 |
with patch.object( |
| 83 |
self.client, |
| 84 |
"fetch", |
| 85 |
autospec=True, |
| 86 |
return_value={ |
| 87 |
123: {b"X-GM-LABELS": [b"foo", b"&AUE-abel"]}, |
| 88 |
444: {b"X-GM-LABELS": [b"foo"]}, |
| 89 |
}, |
| 90 |
): |
| 91 |
out = self.client.get_gmail_labels(sentinel.messages) |
| 92 |
self.client.fetch.assert_called_with(sentinel.messages, [b"X-GM-LABELS"]) |
| 93 |
self.assertEqual(out, {123: ["foo", "Łabel"], 444: ["foo"]}) |
| 94 |
|
| 95 |
def test_set(self): |
| 96 |
self.check(self.client.set_gmail_labels, b"X-GM-LABELS") |
| 97 |
|
| 98 |
def test_add(self): |
| 99 |
self.check(self.client.add_gmail_labels, b"+X-GM-LABELS") |
| 100 |
|
| 101 |
def test_remove(self): |
| 102 |
self.check(self.client.remove_gmail_labels, b"-X-GM-LABELS") |
| 103 |
|
| 104 |
def check(self, meth, expected_command): |
| 105 |
self._check(meth, expected_command) |
| 106 |
self._check(meth, expected_command, silent=True) |
| 107 |
|
| 108 |
def _check(self, meth, expected_command, silent=False): |
| 109 |
if silent: |
| 110 |
expected_command += b".SILENT" |
| 111 |
|
| 112 |
cc = self.client._command_and_check |
| 113 |
cc.return_value = [ |
| 114 |
b'11 (X-GM-LABELS (&AUE-abel "f\\"o\\"o") UID 1)', |
| 115 |
b'22 (X-GM-LABELS ("f\\"o\\"o") UID 2)', |
| 116 |
b"11 (UID 1 FLAGS (dont))", |
| 117 |
b"22 (UID 2 FLAGS (care))", |
| 118 |
] |
| 119 |
resp = meth([1, 2], 'f"o"o', silent=silent) |
| 120 |
cc.assert_called_once_with( |
| 121 |
"store", b"1,2", expected_command, '("f\\"o\\"o")', uid=True |
| 122 |
) |
| 123 |
if silent: |
| 124 |
self.assertIsNone(resp) |
| 125 |
else: |
| 126 |
self.assertEqual( |
| 127 |
resp, |
| 128 |
{ |
| 129 |
1: ["Łabel", 'f"o"o'], |
| 130 |
2: [ |
| 131 |
'f"o"o', |
| 132 |
], |
| 133 |
}, |
| 134 |
) |
| 135 |
|
| 136 |
cc.reset_mock() |