Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # -*- coding: UTF-8 -*- | 2 # -*- coding: UTF-8 -*- |
3 # This file is part of Tryton. The COPYRIGHT file at the top level of | 3 # This file is part of Tryton. The COPYRIGHT file at the top level of |
4 # this repository contains the full copyright notices and license terms. | 4 # this repository contains the full copyright notices and license terms. |
5 "Electronic Mail test suite" | 5 "Electronic Mail test suite" |
6 | 6 |
7 import sys, os | 7 import sys, os |
8 DIR = os.path.abspath(os.path.normpath(os.path.join(__file__, | 8 DIR = os.path.abspath(os.path.normpath(os.path.join(__file__, |
9 '..', '..', '..', '..', '..', 'trytond'))) | 9 '..', '..', '..', '..', '..', 'trytond'))) |
10 if os.path.isdir(DIR): | 10 if os.path.isdir(DIR): |
11 sys.path.insert(0, os.path.dirname(DIR)) | 11 sys.path.insert(0, os.path.dirname(DIR)) |
12 import unittest | 12 import unittest |
13 | 13 |
14 from email.mime.multipart import MIMEMultipart | 14 from email.mime.multipart import MIMEMultipart |
15 from email.mime.text import MIMEText | 15 from email.mime.text import MIMEText |
16 from email.mime.base import MIMEBase | |
17 from email.utils import formatdate | 16 from email.utils import formatdate |
18 | 17 |
19 import trytond.tests.test_tryton | 18 import trytond.tests.test_tryton |
20 from trytond.tests.test_tryton import POOL, DB_NAME, USER, CONTEXT, test_view | 19 from trytond.tests.test_tryton import POOL, DB_NAME, USER, CONTEXT, test_view |
21 from trytond.transaction import Transaction | 20 from trytond.transaction import Transaction |
22 | 21 |
23 USER_TYPES = ('owner_user_%s', 'read_user_%s', 'write_user_%s') | 22 USER_TYPES = ('owner_user_%s', 'read_user_%s', 'write_user_%s') |
24 | 23 |
25 | 24 |
26 class ElectronicMailTestCase(unittest.TestCase): | 25 class ElectronicMailTestCase(unittest.TestCase): |
27 """ | 26 """ |
28 Test Electronic Mail Module | 27 Test Electronic Mail Module |
29 """ | 28 """ |
30 | 29 |
31 def setUp(self): | 30 def setUp(self): |
32 trytond.tests.test_tryton.install_module('electronic_mail') | 31 trytond.tests.test_tryton.install_module('electronic_mail') |
33 | 32 |
34 self.mailbox_obj = POOL.get('electronic_mail.mailbox') | 33 self.mailbox_obj = POOL.get('electronic_mail.mailbox') |
35 self.mail_obj = POOL.get('electronic_mail') | 34 self.mail_obj = POOL.get('electronic_mail') |
36 self.header_obj = POOL.get('electronic_mail.header') | 35 self.header_obj = POOL.get('electronic_mail.header') |
37 self.user_obj = POOL.get('res.user') | 36 self.user_obj = POOL.get('res.user') |
38 self.groupsobj = POOL.get('res.group') | 37 self.groups_obj = POOL.get('res.group') |
ced
2010/11/22 16:57:22
missing _
| |
38 self.ir_model_data_obj = POOL.get('ir.model.data') | |
39 | 39 |
40 def create_user(self, name): | 40 def create_user(self, name): |
41 """ | 41 """ |
42 Creates a new user and returns the ID | 42 Creates a new user and returns the ID |
43 """ | 43 """ |
44 email_user_group = self.groupsobj.search( | 44 group_email_admin_id = self.ir_model_data_obj.get_id( |
45 [('name', '=', 'Electronic Mail User')]) | 45 'electronic_mail', 'group_email_admin') |
ced
2010/11/22 16:57:22
Better to search with ir.model.data
| |
46 email_admin_group = self.groupsobj.search( | 46 group_email_user_id = self.ir_model_data_obj.get_id( |
47 [('name', '=', 'Electronic Mail Administrator')]) | 47 'electronic_mail', 'group_email_user') |
48 | 48 |
49 return self.user_obj.create( | 49 return self.user_obj.create( |
50 { | 50 { |
51 'login': name, | 51 'login': name, |
52 'name': name, | 52 'name': name, |
53 'password': name, | 53 'password': name, |
54 'groups': [('set', email_user_group + email_admin_group)] | 54 'groups': [('set', [group_email_admin_id, group_email_user_id])] |
55 }) | 55 }) |
56 | 56 |
57 def create_users(self, no_of_sets=1): | 57 def create_users(self, no_of_sets=1): |
ced
2011/03/23 10:42:16
I don't understand because every users are the sam
| |
58 """ | 58 """ |
59 A set of user is: | 59 A set of user is: |
60 1 Owner | 60 1 Owner |
61 1 Read user | 61 1 Read user |
62 1 Write User | 62 1 Write User |
63 :return: List of tuple of the ID of three users | 63 :return: List of tuple of the ID of three users |
64 """ | 64 """ |
65 created_users = [ ] | 65 created_users = [ ] |
ced
2011/03/23 10:42:16
no space inside []
| |
66 for iteration in xrange(1, no_of_sets + 1): | 66 for iteration in xrange(1, no_of_sets + 1): |
67 created_users.append( | 67 created_users.append( |
68 tuple([ | 68 tuple([ |
ced
2011/03/23 10:42:16
Don't understand
| |
69 self.create_user(user_type % iteration) \ | 69 self.create_user(user_type % iteration) \ |
70 for user_type in USER_TYPES | 70 for user_type in USER_TYPES |
71 ]) | 71 ]) |
72 ) | 72 ) |
73 return created_users | 73 return created_users |
74 | 74 |
75 def test0005views(self): | 75 def test0005views(self): |
76 ''' | 76 ''' |
77 Test views. | 77 Test views. |
78 ''' | 78 ''' |
79 self.assertRaises(Exception, test_view('electronic_mail')) | 79 self.assertRaises(Exception, test_view('electronic_mail')) |
80 | 80 |
81 def test0010mailbox_security(self): | 81 def test0010mailbox_read_rights(self): |
ced
2010/11/22 16:57:22
security -> read access right
| |
82 ''' | 82 ''' |
83 Create and test mailbox security | 83 Read access rights Test |
84 ''' | 84 ''' |
85 with Transaction().start(DB_NAME, USER, CONTEXT) as transaction: | 85 with Transaction().start(DB_NAME, USER, CONTEXT) as transaction: |
86 # Create Users for testing access | 86 # Create Users for testing access |
87 user_set_1, user_set_2 = self.create_users(no_of_sets=2) | 87 user_set_1, user_set_2 = self.create_users(no_of_sets=2) |
88 # Create a mailbox with a user set | 88 # Create a mailbox with a user set |
89 mailbox_1 = self.mailbox_obj.create( | 89 self.mailbox_obj.create( |
90 { | 90 { |
ced
2011/03/23 10:42:16
Indent ???
| |
91 'name': 'Parent Mailbox', | 91 'name': 'Parent Mailbox', |
92 'user': user_set_1[0], | 92 'user': user_set_1[0], |
93 'read_users': [('set', [user_set_1[1]])], | 93 'read_users': [('set', [user_set_1[1]])], |
94 'write_users': [('set', [user_set_1[2]])], | 94 'write_users': [('set', [user_set_1[2]])], |
95 }) | 95 }) |
96 | 96 |
97 # Create a mailbox 2 with RW users of set 1 + set 2 | 97 # Create a mailbox 2 with RW users of set 1 + set 2 |
98 mailbox_2 = self.mailbox_obj.create( | 98 self.mailbox_obj.create( |
99 { | 99 { |
100 'name': 'Child Mailbox', | 100 'name': 'Child Mailbox', |
101 'user': user_set_2[0], | 101 'user': user_set_2[0], |
102 'read_users': [('set', [user_set_1[1], user_set_2[1]])], | 102 'read_users': [('set', [user_set_1[1], user_set_2[1]])], |
103 'write_users': [('set', [user_set_1[2], user_set_2[2]])], | 103 'write_users': [('set', [user_set_1[2], user_set_2[2]])], |
104 }) | 104 }) |
105 | 105 |
106 # Directly test the mailboxes each user has access to | 106 # Directly test the mailboxes each user has access to |
107 expected_results = { | 107 expected_results = { |
108 user_set_1[0]: 1, user_set_2[0]: 1, | 108 user_set_1[0]: 1, user_set_2[0]: 1, |
109 user_set_1[1]: 2, user_set_2[1]: 1, | 109 user_set_1[1]: 2, user_set_2[1]: 1, |
110 user_set_1[2]: 2, user_set_2[2]: 1, | 110 user_set_1[2]: 2, user_set_2[2]: 1, |
111 USER: 2 | 111 USER: 2 |
ced
2011/03/23 10:42:16
,
| |
112 } | 112 } |
113 for user_id in expected_results.keys(): | 113 for user_id, mailbox_count in expected_results.items(): |
ced
2010/11/22 16:57:22
Why not using iteritems
| |
114 with Transaction().set_user(user_id): | 114 with Transaction().set_user(user_id): |
115 self.assertEqual( | 115 self.assertEqual( |
116 self.mailbox_obj.search([], count=True), | 116 self.mailbox_obj.search([], count=True), |
117 expected_results[user_id] | 117 mailbox_count |
118 ) | 118 ) |
119 | 119 |
120 transaction.cursor.rollback() | 120 transaction.cursor.rollback() |
121 | 121 |
122 def test0020mail_create_security(self): | 122 def test0020mail_create_access_rights(self): |
ced
2010/11/22 16:57:22
create access right
| |
123 """ | 123 """ |
124 Create access rights Test | |
125 | |
124 Create Three Users | 126 Create Three Users |
125 user_r, user_w, user_o | 127 user_r, user_w, user_o |
126 | 128 |
127 Create a mailbox with write access to user_w and read access | 129 Create a mailbox with write access to user_w and read access |
128 to user_r and owner as user_o | 130 to user_r and owner as user_o |
129 | 131 |
130 Try various security combinations for create | 132 Try various security combinations for create |
131 """ | 133 """ |
132 with Transaction().start(DB_NAME, USER, CONTEXT) as transaction: | 134 with Transaction().start(DB_NAME, USER, CONTEXT) as transaction: |
133 user_o, user_r, user_w = self.create_users(no_of_sets=1)[0] | 135 user_o, user_r, user_w = self.create_users(no_of_sets=1)[0] |
ced
2011/03/23 10:42:16
safer: (user_o, user_r, user_w), = ...
| |
134 mailbox = self.mailbox_obj.create( | 136 mailbox = self.mailbox_obj.create( |
135 { | 137 { |
136 'name': 'Mailbox', | 138 'name': 'Mailbox', |
137 'user': user_o, | 139 'user': user_o, |
138 'read_users': [('set', [user_r])], | 140 'read_users': [('set', [user_r])], |
139 'write_users': [('set', [user_w])], | 141 'write_users': [('set', [user_w])], |
140 }) | 142 }) |
141 | 143 |
142 # Raise exception when writing a mail with the read user | 144 # Raise exception when writing a mail with the read user |
143 with Transaction().set_user(user_r): | 145 with Transaction().set_user(user_r): |
144 self.failUnlessRaises( | 146 self.failUnlessRaises( |
145 Exception, self.mail_obj.create, | 147 Exception, self.mail_obj.create, |
146 ({ | 148 ({ |
147 'from_': 'Test', | 149 'from_': 'Test', |
148 'mailbox': mailbox | 150 'mailbox': mailbox |
ced
2011/03/23 10:42:16
add ,
| |
149 },)) | 151 },)) |
ced
2011/03/23 10:42:16
remove ,
| |
150 | 152 |
151 # Creating mail with the write user | 153 # Creating mail with the write user |
152 with Transaction().set_user(user_w): | 154 with Transaction().set_user(user_w): |
153 self.assert_( | 155 self.assert_( |
154 self.mail_obj.create({'from_': 'Test', 'mailbox': mailbox}) | 156 self.mail_obj.create({'from_': 'Test', 'mailbox': mailbox}) |
155 ) | 157 ) |
156 | 158 |
157 # Create an email as mailbox owner | 159 # Create an email as mailbox owner |
158 with Transaction().set_user(user_o): | 160 with Transaction().set_user(user_o): |
159 self.assert_( | 161 self.assert_( |
160 self.mail_obj.create({'from_': 'Test', 'mailbox': mailbox}) | 162 self.mail_obj.create({'from_': 'Test', 'mailbox': mailbox}) |
161 ) | 163 ) |
162 | 164 |
163 transaction.cursor.rollback() | 165 transaction.cursor.rollback() |
164 | 166 |
165 def test0030_email_message_extraction(self): | 167 def test0030_email_message_extraction(self): |
166 """The create from email functionality extracts info from | 168 """The create from email functionality extracts info from |
167 an email.message. Test the extraction for multiple types | 169 an email.message. Test the extraction for multiple types |
168 """ | 170 """ |
169 # 1: multipart/alternative | 171 # 1: multipart/alternative |
170 message = MIMEMultipart('alternative') | 172 message = MIMEMultipart('alternative') |
171 | 173 |
172 message['date'] = formatdate() | 174 message['date'] = formatdate() |
173 message = MIMEMultipart('alternative') | |
174 message['Subject'] = "Link" | 175 message['Subject'] = "Link" |
175 message['From'] = "pythonistas<@>openlabs.co.in" | 176 message['From'] = "pythonistas@example.com" |
ced
2010/11/22 16:57:22
You can perhaps use example.com which exists for s
| |
176 message['To'] = "trytonistas@openlabs.co.in" | 177 message['To'] = "trytonistas@example.com" |
177 text = """Hi!\nHow are you?\nHere is the link you wanted: | 178 text = """Hi!\nHow are you?\nHere is the link you wanted: |
178 http://www.python.org | 179 http://www.python.org/ |
ced
2010/11/22 16:57:22
missing ending /
| |
179 http://www.tryton.org | 180 http://www.tryton.org/ |
180 """ | 181 """ |
181 html = """\ | 182 html = """\ |
182 <html> | 183 <html> |
183 <head></head> | 184 <head></head> |
184 <body> | 185 <body> |
185 <p>Hi!<br> | 186 <p>Hi!<br> |
186 How are you?<br> | 187 How are you?<br> |
187 Here is the <a href="http://www.python.org">link</a> you wanted. | 188 Here is the <a href="http://www.python.org">link</a> you wanted. |
188 </p> | 189 </p> |
189 </body> | 190 </body> |
190 </html> | 191 </html> |
191 """ | 192 """ |
192 part1 = MIMEText(text, 'plain') | 193 part1 = MIMEText(text, 'plain') |
193 part2 = MIMEText(html, 'html') | 194 part2 = MIMEText(html, 'html') |
194 message.attach(part1) | 195 message.attach(part1) |
195 message.attach(part2) | 196 message.attach(part2) |
196 | 197 |
197 with Transaction().start(DB_NAME, USER, CONTEXT) as transaction: | 198 with Transaction().start(DB_NAME, USER, CONTEXT): |
198 mailbox = self.mailbox_obj.create( | 199 mailbox = self.mailbox_obj.create( |
199 { | 200 { |
200 'name': 'Mailbox', | 201 'name': 'Mailbox', |
201 'user': USER, | 202 'user': USER, |
202 'read_users': [('set', [USER])], | 203 'read_users': [('set', [USER])], |
203 'write_users': [('set', [USER])], | 204 'write_users': [('set', [USER])], |
204 }) | 205 }) |
205 mail_id = self.mail_obj.create_from_email(message, mailbox) | 206 mail_id = self.mail_obj.create_from_email(message, mailbox) |
206 mail = self.mail_obj.browse(mail_id) | 207 mail = self.mail_obj.browse(mail_id) |
207 | 208 |
208 self.assertEqual(mail.subject, message['Subject']) | 209 self.assertEqual(mail.subject, message['Subject']) |
209 self.assertEqual(mail.from_, message['From']) | 210 self.assertEqual(mail.from_, message['From']) |
210 self.assertEqual(mail.to, message['To']) | 211 self.assertEqual(mail.to, message['To']) |
211 | 212 |
212 | 213 |
213 def suite(): | 214 def suite(): |
214 "Electronic mail test suite" | 215 "Electronic mail test suite" |
215 suite = trytond.tests.test_tryton.suite() | 216 suite = trytond.tests.test_tryton.suite() |
216 suite.addTests(unittest.TestLoader().loadTestsFromTestCase( | 217 suite.addTests(unittest.TestLoader().loadTestsFromTestCase( |
217 ElectronicMailTestCase | 218 ElectronicMailTestCase |
218 ) | 219 ) |
ced
2011/03/23 10:42:16
strange indent
| |
219 ) | 220 ) |
220 return suite | 221 return suite |
221 | 222 |
222 if __name__ == '__main__': | 223 if __name__ == '__main__': |
223 unittest.TextTestRunner(verbosity=2).run(suite()) | 224 unittest.TextTestRunner(verbosity=2).run(suite()) |
LEFT | RIGHT |