| 1 |
import re |
| 2 |
from functools import wraps |
| 3 |
from io import BytesIO |
| 4 |
from unittest.mock import MagicMock, call, patch |
| 5 |
|
| 6 |
from odoo.exceptions import UserError |
| 7 |
from odoo.tests import freeze_time, tagged |
| 8 |
from odoo.tools import file_open |
| 9 |
|
| 10 |
from odoo.addons.l10n_tr_nilvera_einvoice.tests.test_xml_ubl_tr_common import TestUBLTRCommon |
| 11 |
|
| 12 |
COMPANY_VAT = '3297552117' |
| 13 |
ERRORENOUS_ALIAS = 'erroneous_alias' |
| 14 |
EINVOICE_PARTNER_VAT = '1729171602' |
| 15 |
EARCHIVE_PARTNER_VAT = '17291716060' |
| 16 |
SERVER_ERROR_ALIAS = 'server_error_alias' |
| 17 |
UNAUTHORIZED_ALIAS = 'unauthorized_alias' |
| 18 |
UUID_INVALID_STATUS = 'uuid_with_invalid_status_code' |
| 19 |
UUID_INVALID_INVOICE = 'uuid_invalid_invoice' |
| 20 |
UUID_VALID_INVOICE = 'uuid_valid_invoice' |
| 21 |
|
| 22 |
|
| 23 |
def mock_requests_request(method, url, *args, **kwargs): |
| 24 |
response = MagicMock() |
| 25 |
response.status_code = 200 |
| 26 |
|
| 27 |
if method == 'GET' and 'Check/TaxNumber' in url: |
| 28 |
if EINVOICE_PARTNER_VAT in url: |
| 29 |
response.json.return_value = [ |
| 30 |
{ |
| 31 |
'DocumentType': 'Invoice', |
| 32 |
'Name': 'urn:mail:[email protected]', |
| 33 |
'TaxNumber': EINVOICE_PARTNER_VAT, |
| 34 |
'Title': 'Salt Bae LLC', |
| 35 |
'Type': 'OZEL', |
| 36 |
}, |
| 37 |
] |
| 38 |
elif COMPANY_VAT in url: |
| 39 |
response.json.return_value = [ |
| 40 |
{ |
| 41 |
'TaxNumber': 'text', |
| 42 |
'Title': 'text', |
| 43 |
'FirstCreatedTime': '2025-06-23', |
| 44 |
'CreationTime': '2025-06-23', |
| 45 |
'DocumentType': 'text', |
| 46 |
'Name': 'text', |
| 47 |
'Type': 'text', |
| 48 |
}, |
| 49 |
] |
| 50 |
elif EARCHIVE_PARTNER_VAT in url: |
| 51 |
response.json.return_value = [] |
| 52 |
|
| 53 |
elif method == 'GET' and (match := re.fullmatch(r'/einvoice/sale/([\w-]+)/Status', url)): |
| 54 |
if match.group(1) == UUID_INVALID_STATUS: |
| 55 |
data = { |
| 56 |
"InvoiceStatus": { |
| 57 |
"Code": "boop", |
| 58 |
"Description": "text", |
| 59 |
"DetailDescription": "text", |
| 60 |
}, |
| 61 |
} |
| 62 |
response.get.side_effect = data.get |
| 63 |
else: |
| 64 |
data = { |
| 65 |
"InvoiceStatus": { |
| 66 |
"Code": "succeed", |
| 67 |
"Description": "text", |
| 68 |
"DetailDescription": "text", |
| 69 |
}, |
| 70 |
} |
| 71 |
response.get.side_effect = data.get |
| 72 |
|
| 73 |
elif method == 'POST' and 'Send/Xml' in url: |
| 74 |
if UNAUTHORIZED_ALIAS in url: |
| 75 |
response.status_code = 401 |
| 76 |
response.text = 'Unauthorized' |
| 77 |
elif SERVER_ERROR_ALIAS in url: |
| 78 |
response.status_code = 500 |
| 79 |
response.text = 'Internal Server Error' |
| 80 |
elif ERRORENOUS_ALIAS in url: |
| 81 |
response.status_code = 422 |
| 82 |
response.json.return_value = { |
| 83 |
"Message": "HATALI ISTEK", |
| 84 |
"Errors": [{ |
| 85 |
"Code": 2000, |
| 86 |
"Description": "Yeterli Kontörünüz Bulunmamaktadır.", |
| 87 |
"Detail": "Yeterli Kontörünüz Bulunmamaktadır. Lütfen Kontör Alımı Yapınız.", |
| 88 |
}], |
| 89 |
} |
| 90 |
else: |
| 91 |
response.json.return_value = { |
| 92 |
"UUID": "00aac88a-576b-4a62-98b5-ed34fe4d187d", |
| 93 |
"InvoiceNumber": "", |
| 94 |
} |
| 95 |
|
| 96 |
elif method == 'GET' and '/einvoice/Purchase' in url: |
| 97 |
if '/xml' in url: |
| 98 |
with file_open('l10n_tr_nilvera_einvoice/tests/test_files/fetching/invoice.xml', 'rb') as xml: |
| 99 |
response = xml.read() |
| 100 |
elif '/pdf' in url: |
| 101 |
with file_open('l10n_tr_nilvera_einvoice/tests/test_files/fetching/invoice.pdf', 'rb') as pdf: |
| 102 |
response = pdf.read() |
| 103 |
else: |
| 104 |
data = { |
| 105 |
'TotalPages': 1, |
| 106 |
'Content': [ |
| 107 |
{ |
| 108 |
'UUID': 'invoice_uuid', |
| 109 |
'CreatedDate': '2026-02-02', |
| 110 |
}, |
| 111 |
], |
| 112 |
} |
| 113 |
response.get.side_effect = data.get |
| 114 |
return response |
| 115 |
|
| 116 |
|
| 117 |
def patch_nilvera_request(function): |
| 118 |
@wraps(function) |
| 119 |
def wrapper(*args, **kwargs): |
| 120 |
with patch( |
| 121 |
'odoo.addons.l10n_tr_nilvera.lib.nilvera_client.NilveraClient.request', |
| 122 |
side_effect=mock_requests_request, |
| 123 |
) as mocked_request: |
| 124 |
# If the test expects the mock as an argument, pass it |
| 125 |
if 'mocked_request' in function.__code__.co_varnames: |
| 126 |
return function(*args, mocked_request, **kwargs) |
| 127 |
return function(*args, **kwargs) |
| 128 |
return wrapper |
| 129 |
|
| 130 |
|
| 131 |
@tagged('post_install_l10n', 'post_install', '-at_install') |
| 132 |
class TestTRNilveraMockedRequests(TestUBLTRCommon): |
| 133 |
|
| 134 |
@classmethod |
| 135 |
@patch_nilvera_request |
| 136 |
def setUpClass(cls): |
| 137 |
super().setUpClass() |
| 138 |
cls.einvoice_partner._check_nilvera_customer() |
| 139 |
cls.earchive_partner._check_nilvera_customer() |
| 140 |
cls.env['account.journal'].create({ |
| 141 |
'name': 'TR Journal', |
| 142 |
'code': 'TRJ', |
| 143 |
'type': 'purchase', |
| 144 |
'company_id': cls.company.id, |
| 145 |
}) |
| 146 |
|
| 147 |
@patch_nilvera_request |
| 148 |
def test_which_service_to_call(self): |
| 149 |
_, invoice = self._generate_invoice_xml(self.einvoice_partner, include_invoice=True) |
| 150 |
|
| 151 |
invoices_data = { |
| 152 |
invoice: {**invoice.read()[0], 'extra_edis': {'tr_nilvera'}}, |
| 153 |
} |
| 154 |
|
| 155 |
with patch('odoo.addons.l10n_tr_nilvera_einvoice.models.account_move.AccountMove._l10n_tr_nilvera_submit_einvoice') as mock_submit_einvoice, \ |
| 156 |
patch('odoo.addons.l10n_tr_nilvera_einvoice.models.account_move.AccountMove._l10n_tr_nilvera_submit_earchive') as mock_submit_earchive: |
| 157 |
self.env['account.move.send']._call_web_service_before_invoice_pdf_render(invoices_data) |
| 158 |
mock_submit_einvoice.assert_called_once() |
| 159 |
mock_submit_earchive.assert_not_called() |
| 160 |
|
| 161 |
# Reset the alias to empty for the next test |
| 162 |
invoice.partner_id.l10n_tr_nilvera_customer_alias_id = self.env['l10n_tr.nilvera.alias'] |
| 163 |
invoice.partner_id.vat = False |
| 164 |
mock_submit_einvoice.reset_mock() |
| 165 |
mock_submit_earchive.reset_mock() |
| 166 |
|
| 167 |
self.env['account.move.send']._call_web_service_before_invoice_pdf_render(invoices_data) |
| 168 |
mock_submit_earchive.assert_called_once() |
| 169 |
mock_submit_einvoice.assert_not_called() |
| 170 |
|
| 171 |
@patch_nilvera_request |
| 172 |
def test_submit_einvoice(self, mocked_request): |
| 173 |
xml, invoice = self._generate_invoice_xml(self.einvoice_partner, include_invoice=True) |
| 174 |
wrapped_xml = BytesIO(xml) |
| 175 |
wrapped_xml.name = 'invoice.xml' |
| 176 |
invoice._l10n_tr_nilvera_submit_einvoice(wrapped_xml, self.einvoice_partner.l10n_tr_nilvera_customer_alias_id.name) |
| 177 |
|
| 178 |
mocked_request.assert_called_once_with( |
| 179 |
'POST', |
| 180 |
'/einvoice/Send/Xml?Alias=urn%3Amail%3Asalt%40bae.com', |
| 181 |
files={'file': ('invoice.xml', wrapped_xml, 'application/xml')}, |
| 182 |
handle_response=False, |
| 183 |
) |
| 184 |
self.assertTrue(invoice.is_move_sent) |
| 185 |
self.assertEqual(invoice.l10n_tr_nilvera_send_status, 'sent') |
| 186 |
|
| 187 |
@patch_nilvera_request |
| 188 |
def test_submit_earchive(self, mocked_request): |
| 189 |
xml, invoice = self._generate_invoice_xml(self.earchive_partner, include_invoice=True) |
| 190 |
wrapped_xml = BytesIO(xml) |
| 191 |
wrapped_xml.name = 'invoice.xml' |
| 192 |
invoice._l10n_tr_nilvera_submit_earchive(wrapped_xml) |
| 193 |
|
| 194 |
mocked_request.assert_called_once_with( |
| 195 |
'POST', |
| 196 |
'/earchive/Send/Xml', |
| 197 |
files={'file': ('invoice.xml', wrapped_xml, 'application/xml')}, |
| 198 |
handle_response=False, |
| 199 |
) |
| 200 |
|
| 201 |
self.assertTrue(invoice.is_move_sent) |
| 202 |
self.assertEqual(invoice.l10n_tr_nilvera_send_status, 'sent') |
| 203 |
|
| 204 |
@patch_nilvera_request |
| 205 |
def test_submit_einvoice_errors(self): |
| 206 |
xml, invoice = self._generate_invoice_xml(self.einvoice_partner, include_invoice=True) |
| 207 |
wrapped_xml = BytesIO(xml) |
| 208 |
wrapped_xml.name = 'invoice.xml' |
| 209 |
error_cases = [ |
| 210 |
(UNAUTHORIZED_ALIAS, "Oops, seems like you're unauthorised to do this. Try another API key with more rights or contact Nilvera."), |
| 211 |
(SERVER_ERROR_ALIAS, "Server error from Nilvera, please try again later."), |
| 212 |
(ERRORENOUS_ALIAS, "The invoice couldn't be sent due to the following errors:\n\n2000 - You do not have sufficient credits:\nYeterli Kontörünüz Bulunmamaktadır. Lütfen Kontör Alımı Yapınız.\n"), |
| 213 |
] |
| 214 |
|
| 215 |
for alias, expected_error in error_cases: |
| 216 |
with self.assertRaises(UserError) as context: |
| 217 |
invoice._l10n_tr_nilvera_submit_einvoice(wrapped_xml, alias) |
| 218 |
self.assertEqual(str(context.exception), expected_error) |
| 219 |
|
| 220 |
@patch_nilvera_request |
| 221 |
def test_fetch_status(self): |
| 222 |
_, invoice = self._generate_invoice_xml(self.einvoice_partner, include_invoice=True) |
| 223 |
invoice._l10n_tr_nilvera_get_submitted_document_status() |
| 224 |
|
| 225 |
self.assertEqual(invoice.l10n_tr_nilvera_send_status, 'succeed') |
| 226 |
|
| 227 |
@patch_nilvera_request |
| 228 |
def test_fetch_invalid_status(self): |
| 229 |
_, invoice = self._generate_invoice_xml(self.einvoice_partner, include_invoice=True) |
| 230 |
invoice.l10n_tr_nilvera_uuid = UUID_INVALID_STATUS |
| 231 |
|
| 232 |
invoice._l10n_tr_nilvera_get_submitted_document_status() |
| 233 |
|
| 234 |
self.assertIn( |
| 235 |
invoice.message_ids[0].preview, |
| 236 |
"The invoice status couldn't be retrieved from Nilvera.", |
| 237 |
) |
| 238 |
|
| 239 |
@freeze_time('2026-02-02T12:00:00') |
| 240 |
@patch_nilvera_request |
| 241 |
def test_fetching_einvoices(self, mocked_request): |
| 242 |
with patch.object(self.env.cr, 'commit', autospec=True): |
| 243 |
self.env['account.move']._l10n_tr_nilvera_get_documents() |
| 244 |
self.env['account.move']._l10n_tr_nilvera_get_documents() |
| 245 |
|
| 246 |
self.assertListEqual(mocked_request.call_args_list, [ |
| 247 |
call( |
| 248 |
'GET', |
| 249 |
'/einvoice/Purchase', |
| 250 |
params={ |
| 251 |
'StatusCode': ['succeed'], |
| 252 |
'StartDate': '2026-01-02', |
| 253 |
'EndDate': '2026-02-02T12:00:00', |
| 254 |
'DateFilterType': 'CreatedDate', |
| 255 |
'SortColumn': 'CreationDateTime', |
| 256 |
'SortType': 'ASC', |
| 257 |
'Page': 1, |
| 258 |
}, |
| 259 |
), |
| 260 |
call('GET', '/einvoice/Purchase/invoice_uuid/xml', params={'StatusCode': ['succeed']}), |
| 261 |
call('GET', '/einvoice/Purchase/invoice_uuid/pdf'), |
| 262 |
call( |
| 263 |
'GET', |
| 264 |
'/einvoice/Purchase', |
| 265 |
params={ |
| 266 |
'StatusCode': ['succeed'], |
| 267 |
'StartDate': '2026-02-02', |
| 268 |
'EndDate': '2026-02-02T12:00:00', |
| 269 |
'DateFilterType': 'CreatedDate', |
| 270 |
'SortColumn': 'CreationDateTime', |
| 271 |
'SortType': 'ASC', |
| 272 |
'Page': 1, |
| 273 |
}, |
| 274 |
), |
| 275 |
]) |
| 276 |
|
| 277 |
invoice = self.env['account.move'].search([('l10n_tr_nilvera_uuid', '=', 'invoice_uuid')]) |
| 278 |
self.assertEqual(len(invoice), 1) |
| 279 |
self.assertListEqual(sorted(invoice.attachment_ids.mapped('mimetype')), ['application/pdf', 'application/xml']) |