Skip to content

Commit

Permalink
[FIX] validate number before subscription request
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
huguesdk committed May 3, 2024
1 parent a56ffd0 commit 333c772
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
24 changes: 20 additions & 4 deletions l10n_be_cooperator_national_number/models/subscription_request.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from collections import namedtuple

from odoo import _, api, fields, models
from odoo.exceptions import UserError

Expand Down Expand Up @@ -25,19 +27,33 @@ 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_national_registry_number_id_category(self):
return self.env.ref(
"l10n_be_partner_identification.l10n_be_national_registry_number_category"
).id
)

def get_national_number_from_partner(self, partner):
national_number_id_category = self._get_national_registry_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_national_registry_number(self, national_number):
national_number_id_category = self._get_national_registry_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_national_registry_number(self.national_number)
invoice = super().validate_subscription_request()
if not self.is_company:
partner = invoice.partner_id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@
# Robin Keunen <[email protected]>
# 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

from odoo.addons.cooperator.tests.cooperator_test_mixin import CooperatorTestMixin

NATIONAL_NUMBER = 90010100123

account_move_action_post = (
"odoo.addons.account.models.account_move.AccountMove.action_post"
)


class TestCooperatorNationalNumber(TransactionCase, CooperatorTestMixin):
@classmethod
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 333c772

Please sign in to comment.