diff --git a/auth_oidc/models/auth_oauth_provider.py b/auth_oidc/models/auth_oauth_provider.py index b048459a81..898e4d0f16 100644 --- a/auth_oidc/models/auth_oauth_provider.py +++ b/auth_oidc/models/auth_oauth_provider.py @@ -129,8 +129,12 @@ def _check_expression(self): for this in self: try: this._eval_expression(self.env.user, {}) - except (AttributeError, KeyError, NameError) as e: - raise exceptions.ValidationError("\n".join(e.args)) from e + except (AttributeError, KeyError, NameError, ValueError) as e: + # AttributeError: user object can be accessed via attributes: user.email + # KeyError: token is a dict of dicts + # NameError: only user and token can be used + # ValueError: for inexistant variables or attributes + raise exceptions.ValidationError(e) from e def _eval_expression(self, user, token): self.ensure_one() diff --git a/auth_oidc/tests/test_auth_oidc_auth_code.py b/auth_oidc/tests/test_auth_oidc_auth_code.py index a08d250cef..c89a85eed1 100644 --- a/auth_oidc/tests/test_auth_oidc_auth_code.py +++ b/auth_oidc/tests/test_auth_oidc_auth_code.py @@ -13,7 +13,7 @@ from jose.utils import long_to_base64 import odoo -from odoo.exceptions import AccessDenied +from odoo.exceptions import AccessDenied, ValidationError from odoo.tests import common from odoo.addons.website.tools import MockRequest as _MockRequest @@ -340,3 +340,26 @@ def test_group_expressions_with_token(self): self.assertFalse( group_line._eval_expression(self.env.user, {"groups": ["group-c"]}) ) + + def test_group_expression_with_inexistant_variable(self): + """Test that group expression with inexistant variable fails""" + group_line = self.env.ref("auth_oidc.local_keycloak").group_line_ids[:1] + + with self.assertRaises(ValidationError): + group_line.expression = "inexistant_variable" + + def test_group_expression_with_inexistant_attribute(self): + """Test that group expression with inexistant attribute (on user) fails""" + group_line = self.env.ref("auth_oidc.local_keycloak").group_line_ids[:1] + + with self.assertRaises(ValidationError): + group_line.expression = "user.not_an_attribute" + + def test_realistic_group_expression(self): + """Test that group expression with inexistant attribute (on user) fails""" + group_line = self.env.ref("auth_oidc.local_keycloak").group_line_ids[:1] + + group_line.expression = "user.email == token['mail']" + self.assertTrue( + group_line._eval_expression(self.env.user, {"mail": self.env.user.email}) + )