From e3b8d4bd69924b58287db9c77a5da267e518d466 Mon Sep 17 00:00:00 2001 From: hugues de keyzer Date: Fri, 3 May 2024 15:05:04 +0200 Subject: [PATCH 1/2] [FIX] validate number before subscription request check that the belgian national registry number is valid before validating the subscription request, to avoid creating and posting the capital release request and then having it rolled back. --- .../models/subscription_request.py | 29 ++++++++++++++++--- .../tests/test_cooperator_national_number.py | 27 +++++++++++++++++ 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/l10n_be_cooperator_national_number/models/subscription_request.py b/l10n_be_cooperator_national_number/models/subscription_request.py index 83d9adc2c..0f8a03416 100644 --- a/l10n_be_cooperator_national_number/models/subscription_request.py +++ b/l10n_be_cooperator_national_number/models/subscription_request.py @@ -1,3 +1,5 @@ +from collections import namedtuple + from odoo import _, api, fields, models from odoo.exceptions import UserError @@ -25,19 +27,38 @@ def _compute_require_national_number(self): self.company_id.require_national_number and not self.is_company ) - def get_national_number_from_partner(self, partner): - national_number_id_category = self.env.ref( + @api.model + def _get_be_national_register_number_id_category(self): + return self.env.ref( "l10n_be_partner_identification.l10n_be_national_registry_number_category" - ).id + ) + + @api.model + def get_national_number_from_partner(self, partner): + national_number_id_category = ( + self._get_be_national_register_number_id_category() + ) national_number = partner.id_numbers.filtered( - lambda rec: rec.category_id.id == national_number_id_category + lambda rec: rec.category_id.id == national_number_id_category.id ) return national_number.name + @api.model + def check_be_national_register_number(self, national_number): + national_number_id_category = ( + self._get_be_national_register_number_id_category() + ) + # this function checks the value of id_number.name, not id_number + # directly. + id_number = namedtuple("id_number", ("name"))(national_number) + national_number_id_category.validate_id_number(id_number) + def validate_subscription_request(self): self.ensure_one() if self.require_national_number and not self.national_number: raise UserError(_("National Number is required.")) + if self.national_number: + self.check_be_national_register_number(self.national_number) invoice = super().validate_subscription_request() if not self.is_company: partner = invoice.partner_id diff --git a/l10n_be_cooperator_national_number/tests/test_cooperator_national_number.py b/l10n_be_cooperator_national_number/tests/test_cooperator_national_number.py index 0ca1d6b07..43cbb5d6e 100644 --- a/l10n_be_cooperator_national_number/tests/test_cooperator_national_number.py +++ b/l10n_be_cooperator_national_number/tests/test_cooperator_national_number.py @@ -2,6 +2,8 @@ # Robin Keunen # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +from unittest import mock + from odoo.exceptions import UserError, ValidationError from odoo.tests.common import TransactionCase @@ -9,6 +11,10 @@ NATIONAL_NUMBER = 90010100123 +account_move_action_post = ( + "odoo.addons.account.models.account_move.AccountMove.action_post" +) + class TestCooperatorNationalNumber(TransactionCase, CooperatorTestMixin): @classmethod @@ -73,6 +79,27 @@ def test_no_national_number_provided(self): with self.assertRaises(UserError): subscription_request.validate_subscription_request() + @mock.patch(account_move_action_post) + def test_invalid_national_number_provided(self, account_move_action_post_mock): + """ + Providing an invalid national number should raise a validation error. + """ + self.set_national_number_required() + vals = self.get_dummy_subscription_requests_vals() + subscription_request = self.env["subscription.request"].create(vals) + subscription_request.national_number = "42" + with self.assertRaises(ValidationError): + subscription_request.validate_subscription_request() + # no capital release requests should be created or posted + capital_release_requests = self.env["account.move"].search( + [("subscription_request", "=", subscription_request.id)] + ) + self.assertFalse(capital_release_requests) + # mocking account.move.create() would be better, but if it is called, + # the error is confusing: psycopg2.ProgrammingError: can't adapt type + # 'MagicMock' + account_move_action_post_mock.assert_not_called() + def test_national_number_provided_not_required(self): """Expect no error when a number is given but not required.""" vals = self.get_dummy_subscription_requests_vals() From 22e48d33c7f39664bdda1b5d74f323dca37b23eb Mon Sep 17 00:00:00 2001 From: hugues de keyzer Date: Fri, 3 May 2024 15:08:28 +0200 Subject: [PATCH 2/2] [FIX] adjust copyright statements --- l10n_be_cooperator_national_number/__init__.py | 4 ++++ l10n_be_cooperator_national_number/models/__init__.py | 4 ++++ l10n_be_cooperator_national_number/models/company.py | 7 +++---- .../models/subscription_request.py | 4 ++++ l10n_be_cooperator_national_number/tests/__init__.py | 4 ++++ .../tests/test_cooperator_national_number.py | 6 +++--- 6 files changed, 22 insertions(+), 7 deletions(-) diff --git a/l10n_be_cooperator_national_number/__init__.py b/l10n_be_cooperator_national_number/__init__.py index 0650744f6..d08ddc313 100644 --- a/l10n_be_cooperator_national_number/__init__.py +++ b/l10n_be_cooperator_national_number/__init__.py @@ -1 +1,5 @@ +# SPDX-FileCopyrightText: 2023 Coop IT Easy SC +# +# SPDX-License-Identifier: AGPL-3.0-or-later + from . import models diff --git a/l10n_be_cooperator_national_number/models/__init__.py b/l10n_be_cooperator_national_number/models/__init__.py index 40cf510c0..78eeb2dbc 100644 --- a/l10n_be_cooperator_national_number/models/__init__.py +++ b/l10n_be_cooperator_national_number/models/__init__.py @@ -1,3 +1,7 @@ +# SPDX-FileCopyrightText: 2023 Coop IT Easy SC +# +# SPDX-License-Identifier: AGPL-3.0-or-later + from . import company from . import subscription_request from . import res_partner diff --git a/l10n_be_cooperator_national_number/models/company.py b/l10n_be_cooperator_national_number/models/company.py index 6068d57d2..1a685b507 100644 --- a/l10n_be_cooperator_national_number/models/company.py +++ b/l10n_be_cooperator_national_number/models/company.py @@ -1,7 +1,6 @@ -# Copyright 2019 Coop IT Easy SCRL fs -# Houssine Bakkali -# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). - +# SPDX-FileCopyrightText: 2023 Coop IT Easy SC +# +# SPDX-License-Identifier: AGPL-3.0-or-later from odoo import _, api, fields, models from odoo.exceptions import ValidationError diff --git a/l10n_be_cooperator_national_number/models/subscription_request.py b/l10n_be_cooperator_national_number/models/subscription_request.py index 0f8a03416..ba2747e72 100644 --- a/l10n_be_cooperator_national_number/models/subscription_request.py +++ b/l10n_be_cooperator_national_number/models/subscription_request.py @@ -1,3 +1,7 @@ +# SPDX-FileCopyrightText: 2023 Coop IT Easy SC +# +# SPDX-License-Identifier: AGPL-3.0-or-later + from collections import namedtuple from odoo import _, api, fields, models diff --git a/l10n_be_cooperator_national_number/tests/__init__.py b/l10n_be_cooperator_national_number/tests/__init__.py index 22fec90ed..0caa29a1e 100644 --- a/l10n_be_cooperator_national_number/tests/__init__.py +++ b/l10n_be_cooperator_national_number/tests/__init__.py @@ -1 +1,5 @@ +# SPDX-FileCopyrightText: 2023 Coop IT Easy SC +# +# SPDX-License-Identifier: AGPL-3.0-or-later + from . import test_cooperator_national_number diff --git a/l10n_be_cooperator_national_number/tests/test_cooperator_national_number.py b/l10n_be_cooperator_national_number/tests/test_cooperator_national_number.py index 43cbb5d6e..59853398b 100644 --- a/l10n_be_cooperator_national_number/tests/test_cooperator_national_number.py +++ b/l10n_be_cooperator_national_number/tests/test_cooperator_national_number.py @@ -1,6 +1,6 @@ -# Copyright 2019 Coop IT Easy SCRL fs -# Robin Keunen -# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +# SPDX-FileCopyrightText: 2023 Coop IT Easy SC +# +# SPDX-License-Identifier: AGPL-3.0-or-later from unittest import mock