1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
#
# Copyright (c) 2014 ThoughtWorks, Inc.
#
# Pixelated is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pixelated is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Pixelated. If not, see <http://www.gnu.org/licenses/>.
import unittest
from pixelated.adapter.pixelated_mail import PixelatedMail
from pixelated.adapter.tag import Tag
import test_helper
class TestPixelatedMail(unittest.TestCase):
mail_dict = {
'body': 'Este \xe9 o corpo',
'header': {
'cc': ['cc@pixelated.org', 'anothercc@pixelated.org'],
'to': ['to@pixelated.org', 'anotherto@pixelated.org'],
'bcc': ['bcc@pixelated.org', 'anotherbcc@pixelated.org'],
'subject': 'Oi'
},
'ident': '',
'tags': ['sent']
}
def test_leap_recent_flag_is_translated_to_inbox_tag(self):
pixelated_mail = PixelatedMail.from_leap_mail(test_helper.leap_mail(leap_flags=['\\Recent']))
self.assertIn(Tag('inbox'), pixelated_mail.tags)
def test_leap_deleted_flag_is_translated_to_trash_tag(self):
pixelated_mail = PixelatedMail.from_leap_mail(test_helper.leap_mail(leap_flags=['\\Deleted']))
self.assertIn(Tag('trash'), pixelated_mail.tags)
def test_leap_draft_flag_is_translated_to_draft_tag(self):
pixelated_mail = PixelatedMail.from_leap_mail(test_helper.leap_mail(leap_flags=['\\Draft']))
self.assertIn(Tag('drafts'), pixelated_mail.tags)
def test_leap_flags_that_are_custom_tags_are_handled(self):
pixelated_mail = PixelatedMail.from_leap_mail(test_helper.leap_mail(extra_flags=['tag_work']))
self.assertIn(Tag('work'), pixelated_mail.tags)
def test_custom_tags_containing_our_prefix_are_handled(self):
pixelated_mail = PixelatedMail.from_leap_mail(test_helper.leap_mail(extra_flags=['tag_tag_work_tag_']))
self.assertIn(Tag('tag_work_tag_'), pixelated_mail.tags)
def test_non_tags_flags_are_ignored(self):
pixelated_mail = PixelatedMail.from_leap_mail(test_helper.leap_mail(leap_flags=['\\Recent'],
extra_flags=['this_is_not_a_tag', 'tag_custom_tag']))
self.assertEquals(set([Tag('custom_tag'), Tag('inbox')]), pixelated_mail.tags)
def test_from_dict(self):
mail = PixelatedMail.from_dict(self.mail_dict)
self.assertEqual(mail.headers['cc'], ['cc@pixelated.org', 'anothercc@pixelated.org'])
self.assertEqual(mail.headers['to'], ['to@pixelated.org', 'anotherto@pixelated.org'])
self.assertEqual(mail.headers['bcc'], ['bcc@pixelated.org', 'anotherbcc@pixelated.org'])
self.assertEqual(mail.headers['subject'], 'Oi')
self.assertEqual(mail.ident, '')
self.assertEqual(mail.tags, ['sent'])
self.assertEqual(mail.body, 'Este \xe9 o corpo')
def test_update_tags_return_a_set_for_current_tags_and_a_set_for_removed(self):
pixelated_mail = PixelatedMail.from_leap_mail(test_helper.leap_mail(leap_flags=[], extra_flags=['tag_custom_1', 'tag_custom_2']))
current_tags, removed_tags = pixelated_mail.update_tags(set([Tag('custom_1'), Tag('custom_3')]))
self.assertEquals(set([Tag('custom_3'), Tag('custom_1')]), current_tags)
self.assertEquals(set([Tag('custom_2')]), removed_tags)
def test_to_mime_multipart(self):
mail = PixelatedMail.from_dict(self.mail_dict)
mime_multipart = mail.to_mime_multipart()
self.assertRegexpMatches(mime_multipart.as_string(), "\nTo: to@pixelated.org, anotherto@pixelated.org\n")
self.assertRegexpMatches(mime_multipart.as_string(), "\nCc: cc@pixelated.org, anothercc@pixelated.org\n")
self.assertRegexpMatches(mime_multipart.as_string(), "\nBcc: bcc@pixelated.org, anotherbcc@pixelated.org\n")
self.assertRegexpMatches(mime_multipart.as_string(), "\nSubject: Oi\n")
self.assertRegexpMatches(mime_multipart.as_string(), "\nEste \xe9 o corpo")
def test_smtp_format(self):
mail = PixelatedMail.from_dict(self.mail_dict)
smtp_format = mail.to_smtp_format(_from='pixelated@org')
self.assertRegexpMatches(smtp_format, "\nFrom: pixelated@org")
|