From f70b2f5ede42556da2c44da12319aff06a52ff87 Mon Sep 17 00:00:00 2001 From: Holger Brunn Date: Fri, 12 Feb 2021 09:35:46 +0100 Subject: [PATCH] [FIX] base_suspend_security: Pretend to be a list of ints if neccessary --- base_suspend_security/base_suspend_security.py | 13 +++++++++++++ .../tests/test_base_suspend_security.py | 17 +++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/base_suspend_security/base_suspend_security.py b/base_suspend_security/base_suspend_security.py index 03845d6d..03ea76ee 100644 --- a/base_suspend_security/base_suspend_security.py +++ b/base_suspend_security/base_suspend_security.py @@ -14,3 +14,16 @@ class BaseSuspendSecurityUid(int): def __iter__(self): yield super(BaseSuspendSecurityUid, self).__int__() + + def __len__(self): + return 1 + + def __getitem__(self, key): + int_value = super().__int__() + if isinstance(key, slice): + if key.start > 0: + return () + return (int_value,) + if key != 0: + raise IndexError + return int_value diff --git a/base_suspend_security/tests/test_base_suspend_security.py b/base_suspend_security/tests/test_base_suspend_security.py index 64717f04..13ae8afa 100644 --- a/base_suspend_security/tests/test_base_suspend_security.py +++ b/base_suspend_security/tests/test_base_suspend_security.py @@ -2,6 +2,7 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from odoo import exceptions from odoo.tests.common import TransactionCase +from ..base_suspend_security import BaseSuspendSecurityUid class TestBaseSuspendSecurity(TransactionCase): @@ -31,6 +32,15 @@ class TestBaseSuspendSecurity(TransactionCase): self.env['res.users'].browse( self.env['res.users'].suspend_security().env.uid) + def test_base_suspend_security_uid(self): + """ Test corner cases of dunder functions """ + uid = BaseSuspendSecurityUid(42) + self.assertFalse(uid == 42) + self.assertEqual(uid[0], 42) + self.assertFalse(uid[1:]) + with self.assertRaises(IndexError): + self.env['res.users'].browse(uid[1]) + def test_suspend_security_on_search(self): user_without_access = self.env["res.users"].create( dict( @@ -46,3 +56,10 @@ class TestBaseSuspendSecurity(TransactionCase): model.sudo(user_without_access).search([]) # this tests the search model.sudo(user_without_access).suspend_security().search([]) + # be sure we can search suspended uids like ints + partners = self.env['res.partner'].with_context( + active_test=False, + ).search([ + ('user_ids', '=', user_without_access.suspend_security().env.uid), + ]) + self.assertTrue(partners)