2017-06-23 19:53:23 +02:00
|
|
|
# Copyright 2015 Tecnativa - Antonio Espinosa
|
|
|
|
# Copyright 2016 Tecnativa - Sergio Teruel
|
|
|
|
# Copyright 2017 Tecnativa - David Vidal
|
2022-06-06 19:03:27 +02:00
|
|
|
# Copyright 2022 Moduon - Eduardo de Miguel
|
2020-05-09 19:29:26 +02:00
|
|
|
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
|
2022-01-03 16:39:39 +01:00
|
|
|
|
2023-01-26 15:08:20 +01:00
|
|
|
from unittest.mock import patch
|
2020-05-09 19:29:26 +02:00
|
|
|
|
2022-06-06 19:03:27 +02:00
|
|
|
from odoo.exceptions import ValidationError
|
2017-06-23 19:53:23 +02:00
|
|
|
from odoo.tests import common
|
2015-10-06 11:10:40 +02:00
|
|
|
|
|
|
|
|
2017-06-23 19:53:23 +02:00
|
|
|
class TestResPartner(common.TransactionCase):
|
2023-01-31 02:05:31 +01:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
def check_vies(vat_number):
|
|
|
|
return {"valid": vat_number == "ESB87530432"}
|
|
|
|
|
|
|
|
super().setUpClass()
|
|
|
|
cls.company = cls.env.user.company_id
|
|
|
|
cls.company.vat_check_vies = True
|
|
|
|
cls.partner = cls.env["res.partner"].create(
|
2021-12-16 10:56:40 +01:00
|
|
|
{"name": "Test partner", "is_company": True}
|
|
|
|
)
|
2023-01-31 02:05:31 +01:00
|
|
|
cls.vatnumber_path = "odoo.addons.base_vat.models.res_partner.check_vies"
|
|
|
|
|
|
|
|
cls._vies_check_func = check_vies
|
2015-10-06 11:10:40 +02:00
|
|
|
|
2017-06-23 19:53:23 +02:00
|
|
|
def test_validate_vat_vies(self):
|
2023-01-31 02:05:31 +01:00
|
|
|
with patch(self.vatnumber_path, type(self)._vies_check_func):
|
|
|
|
values = {"vat": "ESB87530432", "country_id": self.env.ref("base.be").id}
|
|
|
|
field_onchange = self.partner._onchange_spec()
|
|
|
|
result = self.partner.onchange(
|
|
|
|
values=values,
|
|
|
|
field_name=["vat", "country_id"],
|
|
|
|
field_onchange=field_onchange,
|
|
|
|
)
|
|
|
|
self.assertEqual(result.get("value", {}).get("vies_passed"), True)
|
|
|
|
self.assertEqual(self.partner.vies_passed, False)
|
|
|
|
self.partner.write(values)
|
2017-06-23 19:53:23 +02:00
|
|
|
self.assertEqual(self.partner.vies_passed, True)
|
|
|
|
|
|
|
|
def test_exception_vat_vies(self):
|
2023-01-31 02:05:31 +01:00
|
|
|
with patch(self.vatnumber_path, side_effect=Exception()):
|
|
|
|
values = {"vat": "87530432", "country_id": self.env.ref("base.es").id}
|
|
|
|
field_onchange = self.partner._onchange_spec()
|
|
|
|
result = self.partner.onchange(
|
|
|
|
values=values,
|
|
|
|
field_name=["vat", "country_id"],
|
|
|
|
field_onchange=field_onchange,
|
|
|
|
)
|
|
|
|
self.assertEqual(result.get("value", {}).get("vies_passed"), True)
|
|
|
|
with self.assertRaises(ValidationError):
|
|
|
|
self.partner.write(values)
|
2017-06-23 19:53:23 +02:00
|
|
|
self.assertEqual(self.partner.vies_passed, False)
|
|
|
|
|
|
|
|
def test_no_validate_vat(self):
|
2023-01-26 15:08:20 +01:00
|
|
|
with patch(self.vatnumber_path) as mock_vatnumber:
|
2017-06-23 19:53:23 +02:00
|
|
|
mock_vatnumber.check_vies.return_value = False
|
2023-01-31 02:05:31 +01:00
|
|
|
with self.assertRaises(ValidationError):
|
|
|
|
self.partner.vat = "ESB11"
|
2017-06-23 19:53:23 +02:00
|
|
|
self.assertEqual(self.partner.vies_passed, False)
|
2021-07-03 02:16:21 +02:00
|
|
|
|
|
|
|
def test_validate_simple_vat_vies(self):
|
2023-01-26 15:08:20 +01:00
|
|
|
with patch(self.vatnumber_path) as mock_vatnumber:
|
2021-07-03 02:16:21 +02:00
|
|
|
self.company.vat_check_vies = False
|
|
|
|
mock_vatnumber.check_vies.return_value = False
|
|
|
|
self.partner.vat = "MXGODE561231GR8"
|
2022-06-10 10:55:32 +02:00
|
|
|
self.partner.country_id = self.env.ref("base.mx")
|
2021-07-03 02:16:21 +02:00
|
|
|
self.assertEqual(self.partner.vies_passed, False)
|
2022-06-06 19:03:27 +02:00
|
|
|
|
|
|
|
def test_validate_vies_passed_false_when_vat_set_to_false(self):
|
2023-01-26 15:08:20 +01:00
|
|
|
with patch(self.vatnumber_path) as mock_vatnumber:
|
2022-06-06 19:03:27 +02:00
|
|
|
mock_vatnumber.check_vies.return_value = True
|
|
|
|
self.partner.vat = "ESB87530432"
|
2022-06-10 10:55:32 +02:00
|
|
|
self.partner.country_id = self.env.ref("base.be")
|
2022-06-06 19:03:27 +02:00
|
|
|
self.assertEqual(self.partner.vies_passed, True)
|
|
|
|
self.partner.vat = False
|
|
|
|
self.assertEqual(self.partner.vies_passed, False)
|
|
|
|
|
|
|
|
def test_validate_wrong_vat_shows_simple_message(self):
|
|
|
|
with self.assertRaisesRegex(ValidationError, "does not seem to be valid"):
|
|
|
|
self.partner.vat = "ES11111111A"
|