Skip to content

Commit

Permalink
[IMP] connector_search_engine: check binding model
Browse files Browse the repository at this point in the history
  • Loading branch information
qgroulard committed Dec 1, 2023
1 parent d29f007 commit 02013d1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
11 changes: 11 additions & 0 deletions connector_search_engine/models/se_binding.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@ class SeBinding(models.Model):
),
]

@api.constrains("res_model", "index_id")
def _check_model(self):
for binding in self:
if (
binding.res_model
and binding.res_model != binding.index_id.model_id.model
):
raise ValidationError(
_("Binding model must be equal to the index model")
)

@tools.ormcache()
@api.model
def _get_indexable_model_selection(self):
Expand Down
5 changes: 5 additions & 0 deletions connector_search_engine/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,8 @@ def _get_serializer(self):
class ResPartner(models.Model):
_name = "res.partner"
_inherit = ["res.partner", "se.indexable.record"]


class ResUsers(models.Model):
_name = "res.users"
_inherit = ["res.users", "se.indexable.record"]
10 changes: 9 additions & 1 deletion connector_search_engine/tests/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from odoo_test_helper import FakeModelLoader

from odoo.exceptions import ValidationError
from odoo.tests.common import Form
from odoo.tools import mute_logger

Expand All @@ -25,11 +26,12 @@ def setUpClass(cls):
FakeSeAdapter,
FakeSerializer,
ResPartner,
ResUsers,
SeBackend,
SeIndex,
)

cls.loader.update_registry((ResPartner, SeBackend, SeIndex))
cls.loader.update_registry((ResPartner, ResUsers, SeBackend, SeIndex))
cls.binding_model = cls.env["se.binding"]
cls.se_index_model = cls.env["se.index"]

Expand Down Expand Up @@ -381,3 +383,9 @@ def test_binding_multi_backend_index(self):
self.assertEqual(self.partner_binding.mapped("state"), ["done"])
bindings.delete_record()
self.assertFalse(self.partner_binding.exists())

def test_binding_wrong_model(self):
# Try to add a 'res.users' record to a 'res.partner' index
user = self.env["res.users"].search([], limit=1)
with self.assertRaises(ValidationError):
user._add_to_index(self.se_index)

0 comments on commit 02013d1

Please sign in to comment.