| 1 |
from odoo import tools |
| 2 |
from odoo.exceptions import UserError |
| 3 |
from odoo.tests.common import tagged |
| 4 |
from odoo.addons.account.tests.test_account_move_send import TestAccountMoveSendCommon |
| 5 |
from odoo.addons.l10n_hu_edi.tests.common import L10nHuEdiTestCommon |
| 6 |
from odoo.addons.l10n_hu_edi.models.l10n_hu_edi_connection import L10nHuEdiConnection, L10nHuEdiConnectionError |
| 7 |
|
| 8 |
from unittest import skipIf, mock |
| 9 |
import contextlib |
| 10 |
from datetime import timedelta |
| 11 |
|
| 12 |
TEST_CRED = {} |
| 13 |
last_invoice = {'INV/2024/': 20, 'RINV/2024/': 12} |
| 14 |
with contextlib.suppress(ImportError): |
| 15 |
# Private credentials.py. Sorry, we can't share this file. |
| 16 |
from .credentials import TEST_CRED, last_invoice |
| 17 |
|
| 18 |
|
| 19 |
@tagged('external_l10n', 'external', 'post_install', '-at_install', '-standard', '-post_install_l10n') |
| 20 |
@skipIf(not TEST_CRED, 'no NAV credentials') |
| 21 |
class L10nHuEdiTestFlowsLive(L10nHuEdiTestCommon, TestAccountMoveSendCommon): |
| 22 |
""" Test the Hungarian EDI flows with the NAV test servers. """ |
| 23 |
|
| 24 |
# === Overrides === # |
| 25 |
|
| 26 |
@classmethod |
| 27 |
def write_edi_credentials(cls): |
| 28 |
# OVERRIDE |
| 29 |
return cls.company_data['company'].write({**TEST_CRED}) |
| 30 |
|
| 31 |
# === Tests === # |
| 32 |
|
| 33 |
def test_send_invoice_and_credit_note(self): |
| 34 |
invoice = self.create_invoice_simple() |
| 35 |
with self.set_invoice_name(invoice, 'INV/2024/'): |
| 36 |
invoice.action_post() |
| 37 |
send_and_print = self.create_send_and_print(invoice) |
| 38 |
self.assertTrue(send_and_print.extra_edi_checkboxes and send_and_print.extra_edi_checkboxes.get('hu_nav_30', {}).get('checked')) |
| 39 |
self.assertFalse(invoice._l10n_hu_edi_check_invoices()) |
| 40 |
send_and_print.action_send_and_print() |
| 41 |
self.assertRecordValues(invoice, [{'l10n_hu_edi_state': 'confirmed', 'l10n_hu_invoice_chain_index': -1}]) |
| 42 |
|
| 43 |
credit_note = self.create_reversal(invoice) |
| 44 |
with self.set_invoice_name(credit_note, 'RINV/2024/'): |
| 45 |
credit_note.action_post() |
| 46 |
send_and_print = self.create_send_and_print(credit_note) |
| 47 |
self.assertTrue(send_and_print.extra_edi_checkboxes and send_and_print.extra_edi_checkboxes.get('hu_nav_30', {}).get('checked')) |
| 48 |
self.assertFalse(credit_note._l10n_hu_edi_check_invoices()) |
| 49 |
send_and_print.action_send_and_print() |
| 50 |
self.assertRecordValues(credit_note, [{'l10n_hu_edi_state': 'confirmed', 'l10n_hu_invoice_chain_index': 1}]) |
| 51 |
|
| 52 |
cancel_wizard = self.env['l10n_hu_edi.cancellation'].with_context({"default_invoice_id": credit_note.id}).create({ |
| 53 |
'code': 'ERRATIC_DATA', |
| 54 |
'reason': 'Some reason...', |
| 55 |
}) |
| 56 |
cancel_wizard.button_request_cancel() |
| 57 |
self.assertRecordValues(credit_note, [{'l10n_hu_edi_state': 'cancel_pending', 'l10n_hu_invoice_chain_index': 1}]) |
| 58 |
|
| 59 |
def test_send_advance_final_invoice(self): |
| 60 |
# Skip if sale is not installed |
| 61 |
if 'sale_line_ids' not in self.env['account.move.line']: |
| 62 |
self.skipTest('Sale module not installed, skipping advance invoice tests.') |
| 63 |
|
| 64 |
sale_order, advance_invoice = self.create_advance_invoice() |
| 65 |
with self.set_invoice_name(advance_invoice, 'INV/2024/'): |
| 66 |
advance_invoice.action_post() |
| 67 |
send_and_print = self.create_send_and_print(advance_invoice) |
| 68 |
self.assertTrue(send_and_print.extra_edi_checkboxes and send_and_print.extra_edi_checkboxes.get('hu_nav_30', {}).get('checked')) |
| 69 |
self.assertFalse(advance_invoice._l10n_hu_edi_check_invoices()) |
| 70 |
send_and_print.action_send_and_print() |
| 71 |
self.assertRecordValues(advance_invoice, [{'l10n_hu_edi_state': 'confirmed', 'l10n_hu_invoice_chain_index': -1}]) |
| 72 |
|
| 73 |
self.env['account.payment.register'].with_context(active_ids=advance_invoice.ids, active_model='account.move').create({})._create_payments() |
| 74 |
|
| 75 |
final_invoice = self.create_final_invoice(sale_order) |
| 76 |
with self.set_invoice_name(final_invoice, 'INV/2024/'): |
| 77 |
final_invoice.action_post() |
| 78 |
send_and_print = self.create_send_and_print(final_invoice) |
| 79 |
self.assertTrue(send_and_print.extra_edi_checkboxes and send_and_print.extra_edi_checkboxes.get('hu_nav_30', {}).get('checked')) |
| 80 |
self.assertFalse(final_invoice._l10n_hu_edi_check_invoices()) |
| 81 |
send_and_print.action_send_and_print() |
| 82 |
self.assertRecordValues(final_invoice, [{'l10n_hu_edi_state': 'confirmed', 'l10n_hu_invoice_chain_index': -1}]) |
| 83 |
|
| 84 |
def test_send_invoice_complex_huf(self): |
| 85 |
invoice = self.create_invoice_complex_huf() |
| 86 |
with self.set_invoice_name(invoice, 'INV/2024/'): |
| 87 |
invoice.action_post() |
| 88 |
send_and_print = self.create_send_and_print(invoice) |
| 89 |
self.assertTrue(send_and_print.extra_edi_checkboxes and send_and_print.extra_edi_checkboxes.get('hu_nav_30', {}).get('checked')) |
| 90 |
self.assertFalse(invoice._l10n_hu_edi_check_invoices()) |
| 91 |
send_and_print.action_send_and_print() |
| 92 |
self.assertRecordValues(invoice, [{'l10n_hu_edi_state': 'confirmed', 'l10n_hu_invoice_chain_index': -1}]) |
| 93 |
|
| 94 |
def test_send_invoice_complex_eur(self): |
| 95 |
invoice = self.create_invoice_complex_eur() |
| 96 |
with self.set_invoice_name(invoice, 'INV/2024/'): |
| 97 |
invoice.action_post() |
| 98 |
send_and_print = self.create_send_and_print(invoice) |
| 99 |
self.assertTrue(send_and_print.extra_edi_checkboxes and send_and_print.extra_edi_checkboxes.get('hu_nav_30', {}).get('checked')) |
| 100 |
self.assertFalse(invoice._l10n_hu_edi_check_invoices()) |
| 101 |
send_and_print.action_send_and_print() |
| 102 |
self.assertRecordValues(invoice, [{'l10n_hu_edi_state': 'confirmed', 'l10n_hu_invoice_chain_index': -1}]) |
| 103 |
|
| 104 |
def test_timeout_recovery_fail(self): |
| 105 |
invoice = self.create_invoice_simple() |
| 106 |
invoice.action_post() |
| 107 |
|
| 108 |
send_and_print = self.create_send_and_print(invoice) |
| 109 |
self.assertTrue(send_and_print.extra_edi_checkboxes and send_and_print.extra_edi_checkboxes.get('hu_nav_30', {}).get('checked')) |
| 110 |
self.assertFalse(invoice._l10n_hu_edi_check_invoices()) |
| 111 |
with self.patch_call_nav_endpoint('manageInvoice', make_request=False), contextlib.suppress(UserError): |
| 112 |
send_and_print.action_send_and_print() |
| 113 |
|
| 114 |
self.assertRecordValues(invoice, [{'l10n_hu_edi_state': 'send_timeout', 'l10n_hu_invoice_chain_index': -1}]) |
| 115 |
|
| 116 |
# Set the send time 7 minutes in the past so the timeout recovery mechanism triggers. |
| 117 |
invoice.l10n_hu_edi_send_time -= timedelta(minutes=7) |
| 118 |
with contextlib.suppress(UserError): |
| 119 |
invoice.l10n_hu_edi_button_update_status() |
| 120 |
self.assertRecordValues(invoice, [{'l10n_hu_edi_state': 'rejected', 'l10n_hu_invoice_chain_index': 0}]) |
| 121 |
|
| 122 |
def test_timeout_recovery_success(self): |
| 123 |
invoice = self.create_invoice_simple() |
| 124 |
with self.set_invoice_name(invoice, 'INV/2024/'): |
| 125 |
invoice.action_post() |
| 126 |
send_and_print = self.create_send_and_print(invoice) |
| 127 |
self.assertTrue(send_and_print.extra_edi_checkboxes and send_and_print.extra_edi_checkboxes.get('hu_nav_30', {}).get('checked')) |
| 128 |
self.assertFalse(invoice._l10n_hu_edi_check_invoices()) |
| 129 |
with self.patch_call_nav_endpoint('manageInvoice'), contextlib.suppress(UserError): |
| 130 |
send_and_print.action_send_and_print() |
| 131 |
|
| 132 |
self.assertRecordValues(invoice, [{'l10n_hu_edi_state': 'send_timeout', 'l10n_hu_invoice_chain_index': -1}]) |
| 133 |
|
| 134 |
# Set the send time 7 minutes in the past so the timeout recovery mechanism triggers. |
| 135 |
invoice.l10n_hu_edi_send_time -= timedelta(minutes=7) |
| 136 |
invoice.l10n_hu_edi_button_update_status() |
| 137 |
self.assertRecordValues(invoice, [{'l10n_hu_edi_state': 'confirmed', 'l10n_hu_invoice_chain_index': -1}]) |
| 138 |
|
| 139 |
# === Helpers === # |
| 140 |
|
| 141 |
@contextlib.contextmanager |
| 142 |
def set_invoice_name(self, invoice, prefix): |
| 143 |
try: |
| 144 |
last_invoice[prefix] = last_invoice.get(prefix, 0) + 1 |
| 145 |
invoice.name = f'{prefix}{last_invoice[prefix]:05}' |
| 146 |
yield |
| 147 |
finally: |
| 148 |
if invoice.l10n_hu_edi_state not in ['confirmed', 'confirmed_warning', 'cancel_sent', 'cancel_pending', 'cancelled']: |
| 149 |
last_invoice[prefix] -= 1 |
| 150 |
else: |
| 151 |
with tools.file_open('l10n_hu_edi/tests/credentials.py', 'a') as credentials_file: |
| 152 |
credentials_file.write(f'last_invoice = {last_invoice}\n') |
| 153 |
|
| 154 |
@contextlib.contextmanager |
| 155 |
def patch_call_nav_endpoint(self, endpoint, make_request=True): |
| 156 |
""" Patch _call_nav_endpoint in l10n_hu_edi.connection, so that a Timeout is raised on the specified endpoint. |
| 157 |
|
| 158 |
:param endpoint: the endpoint for which to raise a Timeout |
| 159 |
:param make_request bool: If true, will still make the request before raising the timeout. |
| 160 |
""" |
| 161 |
real_call_nav_endpoint = L10nHuEdiConnection._call_nav_endpoint |
| 162 |
|
| 163 |
def mock_call_nav_endpoint(self, mode, service, data, timeout=20): |
| 164 |
if service == endpoint: |
| 165 |
if make_request: |
| 166 |
real_call_nav_endpoint(self, mode, service, data, timeout=timeout) |
| 167 |
raise L10nHuEdiConnectionError('Freeze! This is a timeout!', code='timeout') |
| 168 |
else: |
| 169 |
return real_call_nav_endpoint(self, mode, service, data, timeout=timeout) |
| 170 |
|
| 171 |
with mock.patch.object(L10nHuEdiConnection, '_call_nav_endpoint', new=mock_call_nav_endpoint): |
| 172 |
yield |