Debian Codegraph @

addons/l10n_hu_edi/tests/test_flows_mocked.py

raw
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2
3 from odoo import tools, Command
4 from odoo.tests.common import tagged
5 from odoo.exceptions import UserError
6 from odoo.addons.account.tests.test_account_move_send import TestAccountMoveSendCommon
7 from odoo.addons.l10n_hu_edi.tests.common import L10nHuEdiTestCommon
8
9 import requests
10 from unittest import mock
11 from freezegun import freeze_time
12 import contextlib
13
14
15 @tagged('post_install_l10n', '-at_install', 'post_install')
16 class L10nHuEdiTestFlowsMocked(L10nHuEdiTestCommon, TestAccountMoveSendCommon):
17 """ Test the Hungarian EDI flows using mocked data from the test servers. """
18 @classmethod
19 def setUpClass(cls):
20 with freeze_time('2024-01-25T15:28:53Z'):
21 super().setUpClass()
22
23 def test_send_invoice_and_credit_note(self):
24 with self.patch_post(), \
25 freeze_time('2024-01-25T15:28:53Z'):
26 invoice = self.create_invoice_simple()
27 invoice.action_post()
28 send_and_print = self.create_send_and_print(invoice, sending_methods=[])
29 self.assertTrue(send_and_print.extra_edi_checkboxes and send_and_print.extra_edi_checkboxes.get('hu_nav_30', {}).get('checked'))
30 self.assertFalse(invoice._l10n_hu_edi_check_invoices())
31 send_and_print.action_send_and_print()
32 self.assertRecordValues(invoice, [{'l10n_hu_edi_state': 'confirmed', 'l10n_hu_invoice_chain_index': -1}])
33
34 credit_note = self.create_reversal(invoice)
35 credit_note.action_post()
36 send_and_print = self.create_send_and_print(credit_note, sending_methods=[])
37 self.assertTrue(send_and_print.extra_edi_checkboxes and send_and_print.extra_edi_checkboxes.get('hu_nav_30', {}).get('checked'))
38 self.assertFalse(credit_note._l10n_hu_edi_check_invoices())
39 send_and_print.action_send_and_print()
40 self.assertRecordValues(credit_note, [{'l10n_hu_edi_state': 'confirmed', 'l10n_hu_invoice_chain_index': 1}])
41
42 def test_send_invoice_warning(self):
43 with tools.file_open('l10n_hu_edi/tests/mocked_requests/queryTransactionStatus_response_warning.xml', 'r') as response_file:
44 response_data = response_file.read()
45 with self.patch_post({'queryTransactionStatus': response_data}), \
46 freeze_time('2024-01-25T15:28:53Z'):
47 invoice = self.create_invoice_simple()
48 invoice.action_post()
49 send_and_print = self.create_send_and_print(invoice, sending_methods=[])
50 self.assertTrue(send_and_print.extra_edi_checkboxes and send_and_print.extra_edi_checkboxes.get('hu_nav_30', {}).get('checked'))
51 self.assertFalse(invoice._l10n_hu_edi_check_invoices())
52 send_and_print.action_send_and_print()
53 self.assertRecordValues(invoice, [{'l10n_hu_edi_state': 'confirmed_warning', 'l10n_hu_invoice_chain_index': -1}])
54
55 def test_send_invoice_error(self):
56 with tools.file_open('l10n_hu_edi/tests/mocked_requests/queryTransactionStatus_response_error.xml', 'r') as response_file:
57 response_data = response_file.read()
58 with self.patch_post({'queryTransactionStatus': response_data}), \
59 freeze_time('2024-01-25T15:28:53Z'):
60 invoice = self.create_invoice_simple()
61 invoice.action_post()
62 send_and_print = self.create_send_and_print(invoice, sending_methods=[])
63 self.assertTrue(send_and_print.extra_edi_checkboxes and send_and_print.extra_edi_checkboxes.get('hu_nav_30', {}).get('checked'))
64 self.assertFalse(invoice._l10n_hu_edi_check_invoices())
65 with contextlib.suppress(UserError):
66 send_and_print.action_send_and_print()
67 self.assertRecordValues(invoice, [{'l10n_hu_edi_state': 'rejected', 'l10n_hu_invoice_chain_index': 0}])
68
69 def test_timeout_recovery_fail(self):
70 with freeze_time('2024-01-25T15:28:53Z'), \
71 self.patch_post({'manageInvoice': requests.Timeout()}):
72 invoice = self.create_invoice_simple()
73 invoice.action_post()
74
75 send_and_print = self.create_send_and_print(invoice, sending_methods=[])
76 self.assertTrue(send_and_print.extra_edi_checkboxes and send_and_print.extra_edi_checkboxes.get('hu_nav_30', {}).get('checked'))
77 self.assertFalse(invoice._l10n_hu_edi_check_invoices())
78 send_and_print.action_send_and_print()
79 self.assertRecordValues(invoice, [{'l10n_hu_edi_state': 'send_timeout', 'l10n_hu_invoice_chain_index': -1}])
80
81 with tools.file_open('l10n_hu_edi/tests/mocked_requests/queryTransactionStatus_response_original.xml', 'r') as response_file:
82 response_data = response_file.read()
83 # Advance 10 minutes so the timeout recovery mechanism triggers.
84 with freeze_time('2024-01-25T15:38:53Z'), \
85 self.patch_post({'queryTransactionStatus': response_data}):
86 with contextlib.suppress(UserError):
87 invoice.l10n_hu_edi_button_update_status()
88 self.assertRecordValues(invoice, [{'l10n_hu_edi_state': 'rejected', 'l10n_hu_invoice_chain_index': 0}])
89
90 def test_timeout_recovery_success(self):
91 with freeze_time('2024-01-25T15:28:53Z'), \
92 self.patch_post({'manageInvoice': requests.Timeout()}):
93 invoice = self.create_invoice_simple()
94 invoice.name = 'INV/2024/00999' # This matches the invoice name in the XML returned by queryTransactionStatus.
95 invoice.action_post()
96
97 send_and_print = self.create_send_and_print(invoice, sending_methods=[])
98 self.assertTrue(send_and_print.extra_edi_checkboxes and send_and_print.extra_edi_checkboxes.get('hu_nav_30', {}).get('checked'))
99 self.assertFalse(invoice._l10n_hu_edi_check_invoices())
100 send_and_print.action_send_and_print()
101 self.assertRecordValues(invoice, [{'l10n_hu_edi_state': 'send_timeout', 'l10n_hu_invoice_chain_index': -1}])
102
103 # This returns an original XML with name INV/2024/00999
104 with tools.file_open('l10n_hu_edi/tests/mocked_requests/queryTransactionStatus_response_original.xml', 'r') as response_file:
105 response_data = response_file.read()
106
107 # Advance 10 minutes so the timeout recovery mechanism triggers.
108 with freeze_time('2024-01-25T15:38:53Z'), \
109 self.patch_post({'queryTransactionStatus': response_data}):
110 invoice.l10n_hu_edi_button_update_status()
111 self.assertRecordValues(invoice, [{'l10n_hu_edi_state': 'confirmed', 'l10n_hu_invoice_chain_index': -1}])
112
113 def test_cancel_invoice_error(self):
114 with freeze_time('2024-01-25T15:28:53Z'):
115 with self.patch_post():
116 invoice, cancel_wizard = self.create_cancel_wizard()
117 self.assertRecordValues(invoice, [{'l10n_hu_edi_state': 'confirmed'}])
118 with tools.file_open('l10n_hu_edi/tests/mocked_requests/queryTransactionStatus_response_error.xml', 'r') as response_file:
119 response_data = response_file.read()
120 with self.patch_post({'queryTransactionStatus': response_data}):
121 with contextlib.suppress(UserError):
122 cancel_wizard.button_request_cancel()
123 self.assertRecordValues(invoice, [{'l10n_hu_edi_state': 'confirmed_warning', 'l10n_hu_invoice_chain_index': -1}])
124
125 def test_cancel_invoice_pending(self):
126 with freeze_time('2024-01-25T15:28:53Z'):
127 with self.patch_post():
128 invoice, cancel_wizard = self.create_cancel_wizard()
129 self.assertRecordValues(invoice, [{'l10n_hu_edi_state': 'confirmed'}])
130 with tools.file_open('l10n_hu_edi/tests/mocked_requests/queryTransactionStatus_response_annulment_pending.xml', 'r') as response_file:
131 response_data = response_file.read()
132 with self.patch_post({'queryTransactionStatus': response_data}):
133 cancel_wizard.button_request_cancel()
134 self.assertRecordValues(invoice, [{'l10n_hu_edi_state': 'cancel_pending', 'l10n_hu_invoice_chain_index': -1}])
135
136 def test_cancel_invoice_done(self):
137 with freeze_time('2024-01-25T15:28:53Z'):
138 with self.patch_post():
139 invoice, cancel_wizard = self.create_cancel_wizard()
140 self.assertRecordValues(invoice, [{'l10n_hu_edi_state': 'confirmed', 'l10n_hu_invoice_chain_index': -1}])
141 with tools.file_open('l10n_hu_edi/tests/mocked_requests/queryTransactionStatus_response_annulment_done.xml', 'r') as response_file:
142 response_data = response_file.read()
143 with self.patch_post({'queryTransactionStatus': response_data}):
144 cancel_wizard.button_request_cancel()
145 self.assertRecordValues(invoice, [{'l10n_hu_edi_state': 'cancelled', 'state': 'cancel', 'l10n_hu_invoice_chain_index': 0}])
146
147 def test_cancel_and_resend(self):
148 """ Test the sending, annulment and re-sending of an invoice + credit note + modif. invoice """
149 with freeze_time('2024-01-25T15:28:53Z'):
150 with self.patch_post():
151 invoice, cancel_wizard = self.create_cancel_wizard()
152 self.assertRecordValues(invoice, [{'l10n_hu_edi_state': 'confirmed', 'l10n_hu_invoice_chain_index': -1}])
153
154 new_invoice = self.create_reversal(invoice, is_modify=True)
155 self.assertRecordValues(new_invoice, [{'debit_origin_id': invoice.id}])
156 new_invoice.action_post()
157 credit_note = invoice.reversal_move_ids
158
159 send_and_print = self.create_send_and_print(credit_note, sending_methods=[])
160 self.assertTrue(send_and_print.extra_edi_checkboxes and send_and_print.extra_edi_checkboxes.get('hu_nav_30', {}).get('checked'))
161 self.assertFalse(credit_note._l10n_hu_edi_check_invoices())
162 send_and_print.action_send_and_print()
163 self.assertRecordValues(credit_note, [{'l10n_hu_edi_state': 'confirmed', 'l10n_hu_invoice_chain_index': 1}])
164
165 send_and_print = self.create_send_and_print(new_invoice, sending_methods=[])
166 self.assertTrue(send_and_print.extra_edi_checkboxes and send_and_print.extra_edi_checkboxes.get('hu_nav_30', {}).get('checked'))
167 self.assertFalse(new_invoice._l10n_hu_edi_check_invoices())
168 send_and_print.action_send_and_print()
169 self.assertRecordValues(new_invoice, [{'l10n_hu_edi_state': 'confirmed', 'l10n_hu_invoice_chain_index': 2}])
170
171 with tools.file_open('l10n_hu_edi/tests/mocked_requests/queryTransactionStatus_response_annulment_done.xml', 'r') as response_file:
172 response_data = response_file.read()
173 with self.patch_post({'queryTransactionStatus': response_data}):
174 cancel_wizard.button_request_cancel()
175 self.assertRecordValues(invoice, [{'l10n_hu_edi_state': 'cancelled', 'state': 'cancel', 'l10n_hu_invoice_chain_index': 0}])
176 self.assertRecordValues(credit_note, [{'l10n_hu_edi_state': 'cancelled', 'state': 'cancel', 'l10n_hu_invoice_chain_index': 0}])
177 self.assertRecordValues(new_invoice, [{'l10n_hu_edi_state': 'cancelled', 'state': 'cancel', 'l10n_hu_invoice_chain_index': 0}])
178
179 (invoice | credit_note | new_invoice).button_draft()
180 invoice.action_post()
181 credit_note.action_post()
182 new_invoice.action_post()
183
184 with self.patch_post():
185 send_and_print = self.create_send_and_print(invoice, sending_methods=[])
186 self.assertTrue(send_and_print.extra_edi_checkboxes and send_and_print.extra_edi_checkboxes.get('hu_nav_30', {}).get('checked'))
187 self.assertFalse(invoice._l10n_hu_edi_check_invoices())
188 send_and_print.action_send_and_print()
189 self.assertRecordValues(invoice, [{'l10n_hu_edi_state': 'confirmed', 'l10n_hu_invoice_chain_index': -1}])
190
191 send_and_print = self.create_send_and_print(credit_note, sending_methods=[])
192 self.assertTrue(send_and_print.extra_edi_checkboxes and send_and_print.extra_edi_checkboxes.get('hu_nav_30', {}).get('checked'))
193 self.assertFalse(credit_note._l10n_hu_edi_check_invoices())
194 send_and_print.action_send_and_print()
195 self.assertRecordValues(credit_note, [{'l10n_hu_edi_state': 'confirmed', 'l10n_hu_invoice_chain_index': 1}])
196
197 send_and_print = self.create_send_and_print(new_invoice, sending_methods=[])
198 self.assertTrue(send_and_print.extra_edi_checkboxes and send_and_print.extra_edi_checkboxes.get('hu_nav_30', {}).get('checked'))
199 self.assertFalse(new_invoice._l10n_hu_edi_check_invoices())
200 send_and_print.action_send_and_print()
201 self.assertRecordValues(new_invoice, [{'l10n_hu_edi_state': 'confirmed', 'l10n_hu_invoice_chain_index': 2}])
202
203 def test_invoice_line_currency_rate_from_sale(self):
204 if self.env['ir.module.module']._get('sale_stock').state == 'installed':
205 self.env.user.group_ids += self.env.ref('sales_team.group_sale_salesman')
206 currency = self.setup_other_currency('HRK', rates=[
207 ('2016-01-01', 3.0),
208 ('2017-01-01', 2.0),
209 ])
210 pricelist = self.env['product.pricelist'].create({
211 'name': 'Foreign pricelist',
212 'currency_id': currency.id,
213 })
214
215 sale_order = self.env['sale.order'].create({
216 'partner_id': self.partner_company.id,
217 'partner_invoice_id': self.partner_company.id,
218 'pricelist_id': pricelist.id,
219 'order_line': [Command.create({
220 'product_id': self.product.id,
221 'product_uom_qty': 1,
222 'price_unit': 600,
223 })],
224 'currency_id': currency.id,
225 'date_order': '2017-01-01',
226 })
227 sale_order.action_confirm()
228
229 delivery = sale_order.picking_ids
230 delivery.button_validate()
231 delivery.date_done = '2016-01-01'
232
233 invoice = sale_order._create_invoices()
234 self.assertRecordValues(invoice.line_ids, [
235 {'amount_currency': -600.00, 'balance': -200.00},
236 {'amount_currency': -162.00, 'balance': -54.00},
237 {'amount_currency': 762.00, 'balance': 254.00},
238 ])
239
240 def test_case_1_invoice_payment_storno(self):
241 inv = self.create_invoice_simple(amount=1000)
242 inv.action_post()
243 operation = inv._l10n_hu_edi_get_operation_type()
244 self.assertEqual(operation, 'CREATE')
245 self.register_payment(inv, 1000)
246 self.create_reversal(inv, is_modify=True)
247 operation = inv.reversal_move_ids._l10n_hu_edi_get_operation_type()
248 self.assertEqual(operation, 'STORNO')
249
250 def test_case_2_modify_then_storno(self):
251 inv = self.create_invoice_simple(amount=1000)
252 inv.action_post()
253 operation = inv._l10n_hu_edi_get_operation_type()
254 self.assertEqual(operation, 'CREATE')
255 mod1 = self.create_reversal(inv, amount=100)
256 mod1.action_post()
257 operation = mod1._l10n_hu_edi_get_operation_type()
258 self.assertEqual(operation, 'MODIFY')
259 storno = self.create_reversal(inv, amount=900)
260 storno.action_post()
261 operation = storno._l10n_hu_edi_get_operation_type()
262 self.assertEqual(operation, 'STORNO')
263
264 def test_case_3_multiple_modifications_then_storno(self):
265 inv = self.create_invoice_simple(amount=1000)
266 inv.action_post()
267 operation = inv._l10n_hu_edi_get_operation_type()
268 self.assertEqual(operation, 'CREATE')
269 mod1 = self.create_reversal(inv, amount=100)
270 mod1.action_post()
271 operation = mod1._l10n_hu_edi_get_operation_type()
272 self.assertEqual(operation, 'MODIFY')
273 mod2 = self.create_reversal(inv, amount=100)
274 mod2.action_post()
275 operation = mod2._l10n_hu_edi_get_operation_type()
276 self.assertEqual(operation, 'MODIFY')
277 storno = self.create_reversal(inv, amount=800)
278 storno.action_post()
279 operation = storno._l10n_hu_edi_get_operation_type()
280 self.assertEqual(operation, 'STORNO')
281
282 def test_case_4_modification_payment_then_storno(self):
283 inv = self.create_invoice_simple(amount=1000)
284 inv.action_post()
285 operation = inv._l10n_hu_edi_get_operation_type()
286 self.assertEqual(operation, 'CREATE')
287 mod1 = self.create_reversal(inv, amount=100)
288 mod1.action_post()
289 operation = mod1._l10n_hu_edi_get_operation_type()
290 self.assertEqual(operation, 'MODIFY')
291 self.register_payment(inv, 900)
292 self.create_reversal(inv, is_modify=True)
293 mod2 = inv.reversal_move_ids
294 mod2.button_draft()
295 mod2.invoice_line_ids[0].write({
296 'price_unit': 900,
297 })
298 mod2.action_post()
299 # Reconcile the outstanding payment line from mod1 with the invoice
300 inv.js_assign_outstanding_line(mod1.line_ids.filtered(lambda l: l.debit == 0).id)
301 operation = mod2._l10n_hu_edi_get_operation_type()
302 self.assertEqual(operation, 'STORNO')
303
304 def test_case_5_debit_note_then_storno(self):
305 inv = self.create_invoice_simple(amount=1000)
306 inv.action_post()
307 operation = inv._l10n_hu_edi_get_operation_type()
308 self.assertEqual(operation, 'CREATE')
309 dn = self.create_debit_note(inv, amount=100)
310 dn.action_post()
311 operation = dn._l10n_hu_edi_get_operation_type()
312 self.assertEqual(operation, 'MODIFY')
313 storno = self.create_reversal(inv, amount=1100)
314 storno.action_post()
315 # Reconcile the outstanding payment line from dn with the storno
316 storno.js_assign_outstanding_line(dn.line_ids.filtered(lambda l: l.credit == 0).id)
317 operation = storno._l10n_hu_edi_get_operation_type()
318 self.assertEqual(operation, 'STORNO')
319
320 # === Helpers === #
321
322 @contextlib.contextmanager
323 def patch_post(self, responses=None):
324 """ Patch requests.Session in l10n_hu_edi.connection.
325
326 :param responses: If specified, a dict {service: response} that gives, for any service,
327 bytes that should be served as response data, or an Exception that should be raised.
328 Otherwise, will use the default responses stored under
329 mocked_requests/{service}_response.xml
330 """
331 test_case = self
332
333 class MockedSession:
334 def post(self, url, data, headers, timeout=None):
335 prod_url = 'https://api.onlineszamla.nav.gov.hu/invoiceService/v3'
336 demo_url = 'https://api-test.onlineszamla.nav.gov.hu/invoiceService/v3'
337 mocked_requests = ['manageInvoice', 'queryTaxpayer', 'tokenExchange', 'queryTransactionStatus', 'queryTransactionList', 'manageAnnulment']
338
339 base_url, __, service = url.rpartition('/')
340 if base_url not in (prod_url, demo_url) or service not in mocked_requests:
341 test_case.fail(f'Invalid POST url: {url}')
342
343 with tools.file_open(f'l10n_hu_edi/tests/mocked_requests/{service}_request.xml', 'rb') as expected_request_file:
344 test_case.assertXmlTreeEqual(
345 test_case.get_xml_tree_from_string(data),
346 test_case.get_xml_tree_from_string(expected_request_file.read()),
347 )
348
349 mock_response = mock.Mock(spec=requests.Response)
350 mock_response.status_code = 200
351 mock_response.headers = ''
352
353 if responses and service in responses:
354 if isinstance(responses[service], Exception):
355 raise responses[service]
356 mock_response.text = responses[service]
357 else:
358 with tools.file_open(f'l10n_hu_edi/tests/mocked_requests/{service}_response.xml', 'r') as response_file:
359 mock_response.text = response_file.read()
360 return mock_response
361
362 def close(self):
363 pass
364
365 with mock.patch('odoo.addons.l10n_hu_edi.models.l10n_hu_edi_connection.requests.Session', side_effect=MockedSession, autospec=True):
366 yield