-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_attach_other_models.py
More file actions
245 lines (215 loc) · 10.5 KB
/
test_attach_other_models.py
File metadata and controls
245 lines (215 loc) · 10.5 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# coding=utf-8
from destral.transaction import Transaction
from destral import testing
from mock import patch
from base64 import b64decode
from osv.osv import except_osv
class TestAttachOtherModels(testing.OOTestCase):
"""
Test the attachment of other models to the email
"""
def setUp(self):
super(TestAttachOtherModels, self).setUp()
self.txn = Transaction().start(self.database)
self.cursor = self.txn.cursor
self.uid = self.txn.user
pool = self.openerp.pool
# Objectes comuns
self.partner_obj = pool.get('res.partner')
self.pm_tmp_obj = pool.get('poweremail.templates')
self.acc_obj = pool.get('poweremail.core_accounts')
self.mailbox_obj = pool.get('poweremail.mailbox')
self.ir_model_obj = pool.get('ir.model')
self.report_obj = pool.get('ir.actions.report.xml')
# Crear partner nou
self.partner_id = self.partner_obj.create(self.cursor, self.uid, {
'name': 'Test Partner',
'email': 'partner@test.com',
})
# Crear compte d'email nou
self.account_id = self.acc_obj.create(self.cursor, self.uid, {
'name': 'Test account',
'user': self.uid,
'email_id': 'test_account@test.com',
'smtpserver': 'smtp.test.com',
'smtpport': 25,
'company': 'no',
})
# Crear model i report dummy
model_partner_id = self.ir_model_obj.search(
self.cursor, self.uid, [('model', '=', 'res.partner')]
)[0]
self.report_id = self.report_obj.create(self.cursor, self.uid, {
'name': 'Fake Report',
'model': 'res.partner',
'report_name': 'fake.report.partner',
'report_type': 'pdf',
})
# Crear plantilla nova
self.template_id = self.pm_tmp_obj.create(self.cursor, self.uid, {
'name': 'Template Test',
'object_name': model_partner_id,
'lang': 'en_US',
'report_template': self.report_id,
'enforce_from_account': self.account_id,
'template_language': 'mako',
'def_to': 'object.email',
'inline': True,
'def_subject': 'Test subject',
'def_body_text': 'Test body text',
'def_priority': '2',
})
@patch('poweremail.poweremail_template.PY3', True)
@patch('poweremail.poweremail_template.poweremail_templates.create_report',
return_value=("OK_REPORT_PY3", "pdf"))
def test_happy_path_python3_encoding(self, mock_create_report):
"""
Ensure that in Python 3, the report string is encoded before base64.
This tests the PY3 branch added in the patch.
"""
# Set the report template object reference
self.pm_tmp_obj.write(
self.cursor, self.uid, [self.template_id],
{'report_template_object_reference': 'object.id'}
)
tmpl = self.pm_tmp_obj.simple_browse(self.cursor, self.uid, self.template_id)
# Get dynamic attachment
res = self.pm_tmp_obj.get_dynamic_attachment(
self.cursor, self.uid, tmpl, [self.partner_id]
)
# Assert that 'file' exists and is correctly base64-encoded
self.assertIn('file', res)
self.assertEqual(b64decode(res['file']), b'OK_REPORT_PY3')
self.assertEqual(res['extension'], 'pdf')
# Generate mail to ensure full happy path works
mailbox_id = self.pm_tmp_obj.generate_mail(
self.cursor, self.uid, self.template_id, [self.partner_id],
context={'raise_exception': True}
)
mail = self.mailbox_obj.simple_browse(self.cursor, self.uid, mailbox_id)
self.assertNotEqual(mail.folder, 'error')
@patch('poweremail.poweremail_template.poweremail_templates.create_report',
return_value=("OK_REPORT", "pdf"))
def test_happy_path_with_valid_reference(self, mock_create_report):
"""Ensure normal flow works when the reference expression returns a
valid ID."""
self.pm_tmp_obj.write(
self.cursor, self.uid, [self.template_id],
{'report_template_object_reference': 'object.id'}
)
tmpl = self.pm_tmp_obj.simple_browse(self.cursor, self.uid, self.template_id)
res = self.pm_tmp_obj.get_dynamic_attachment(
self.cursor, self.uid, tmpl, [self.partner_id]
)
self.assertIn('file', res)
self.assertEqual(b64decode(res['file']), b'OK_REPORT')
self.assertEqual(res['extension'], 'pdf')
mailbox_id = self.pm_tmp_obj.generate_mail(
self.cursor, self.uid, self.template_id, [self.partner_id],
context={'raise_exception': True}
)
mail = self.mailbox_obj.simple_browse(self.cursor, self.uid, mailbox_id)
self.assertNotEqual(mail.folder, 'error')
def test_reference_returns_record_instead_of_id(self):
"""Ensure exception is raised if expression returns a record instead
of an ID."""
self.pm_tmp_obj.write(
self.cursor, self.uid, [self.template_id],
{'report_template_object_reference': 'object'}
)
tmpl = self.pm_tmp_obj.simple_browse(self.cursor, self.uid,
self.template_id)
with self.assertRaises(except_osv) as error:
self.pm_tmp_obj._get_records_from_report_template_object_reference(
self.cursor, self.uid, tmpl, [self.partner_id]
)
self.assertIn(
u"The expression in 'Reference of the report' field returned an empty value or a value that is not an integer ID.",
error.exception.value
)
@patch('poweremail.poweremail_template.poweremail_templates.create_report',
return_value=("content_main", "pdf"))
def test_main_report_only(self, mock_create_report):
"""Test that main report is generated correctly when no object
reference."""
tmpl = self.pm_tmp_obj.simple_browse(self.cursor, self.uid, self.template_id)
res = self.pm_tmp_obj.get_dynamic_attachment(self.cursor, self.uid, tmpl, [self.partner_id])
self.assertEqual(b64decode(res['file']), b'content_main')
self.assertEqual(res['extension'], 'pdf')
@patch('poweremail.poweremail_template.poweremail_templates.create_report')
def test_create_report_from_report_template_object_reference_reference_with_non_records_ids(
self, mock_create_report):
"""Ensure exception is raised if expression returns non-record IDs."""
self.pm_tmp_obj.write(
self.cursor, self.uid, [self.template_id],
{'report_template_object_reference': 'object.name'}
)
tmpl = self.pm_tmp_obj.simple_browse(self.cursor, self.uid,
self.template_id)
with self.assertRaises(except_osv) as error:
self.pm_tmp_obj.get_dynamic_attachment(self.cursor, self.uid, tmpl,
[self.partner_id])
self.assertIn(
u"The expression in 'Reference of the report' field returned an empty value or a value that is not an integer ID.",
error.exception.value
)
def test_reference_expression_without_object(self):
"""Ensure expressions without 'object' raise an exception."""
self.pm_tmp_obj.write(
self.cursor, self.uid, [self.template_id],
{'report_template_object_reference': 'user.menu_id'}
)
tmpl = self.pm_tmp_obj.simple_browse(self.cursor, self.uid, self.template_id)
with self.assertRaises(except_osv) as error:
self.pm_tmp_obj.get_dynamic_attachment(self.cursor, self.uid, tmpl, [self.partner_id])
self.assertIn(
u"The expression in 'Reference of the report' field must contain the 'object' variable.",
error.exception.value
)
def test_reference_evaluates_to_empty(self):
"""Ensure expression with empty result raises an exception."""
self.pm_tmp_obj.write(
self.cursor, self.uid, [self.template_id],
{'report_template_object_reference': 'object.child_ids'}
)
tmpl = self.pm_tmp_obj.simple_browse(self.cursor, self.uid,
self.template_id)
with self.assertRaises(except_osv) as error:
self.pm_tmp_obj.get_dynamic_attachment(self.cursor, self.uid, tmpl,
[self.partner_id])
self.assertIn(
u"The expression in 'Reference of the report' field returned an empty value or a value that is not an integer ID.",
error.exception.value
)
@patch(
'poweremail.poweremail_template.poweremail_templates._get_records_from_report_template_object_reference',
return_value=[]
)
@patch('poweremail.poweremail_template.poweremail_templates.create_report')
def test_create_report_from_report_template_object_reference_no_reference(
self, mock_get_records, mock_create_report):
"""Ensure exception is raised if no records found from reference."""
self.pm_tmp_obj.write(
self.cursor, self.uid, [self.template_id],
{'report_template_object_reference': 'object.id'}
)
tmpl = self.pm_tmp_obj.simple_browse(self.cursor, self.uid,
self.template_id)
with self.assertRaises(except_osv) as error:
self.pm_tmp_obj.create_report_from_report_template_object_reference_reference(
self.cursor, self.uid, tmpl, [self.partner_id]
)
self.assertIn(
u"No records found evaluating the expression in 'Reference of the report' field.",
error.exception.value
)
@patch('poweremail.poweremail_template.poweremail_templates.create_report',
return_value=("content_from_report_template_object_reference",
"pdf"))
def test_reference_uses_same_report_template(self, mock_create_report):
"""Ensure referenced records use the same template report."""
tmpl = self.pm_tmp_obj.simple_browse(self.cursor, self.uid, self.template_id)
self.pm_tmp_obj.get_dynamic_attachment(self.cursor, self.uid, tmpl, [self.partner_id])
mock_create_report.assert_called_once()
args, kwargs = mock_create_report.call_args
self.assertEqual(args[2].report_template.id, tmpl.report_template.id)