Skip to content

Commit

Permalink
[REF] tests: refactor debug_mode() to most module needs
Browse files Browse the repository at this point in the history
The default implementation now uses a request mockup, which is more
general than the former implementation.

Part-of: odoo#151597
  • Loading branch information
Gorash authored and rco-odoo committed Mar 15, 2024
1 parent 028fdd8 commit 07452a0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 49 deletions.
52 changes: 18 additions & 34 deletions odoo/addons/test_access_rights/tests/test_feedback.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
# -*- coding: utf-8 -*-
from unittest.mock import Mock

import odoo
from odoo import SUPERUSER_ID, Command
from odoo.exceptions import AccessError
from odoo.tests import TransactionCase
Expand Down Expand Up @@ -172,12 +169,6 @@ def setUpClass(cls):
'val': 0,
}).with_user(cls.user)

def debug_mode(self):
odoo.http._request_stack.push(Mock(db=self.env.cr.dbname, env=self.env, debug=True))
self.addCleanup(odoo.http._request_stack.pop)
self.env.flush_all()
self.env.invalidate_all()

def _make_rule(self, name, domain, global_=False, attr='write'):
res = self.env['ir.rule'].create({
'name': name,
Expand Down Expand Up @@ -206,8 +197,7 @@ def test_local(self):
If you really, really need access, perhaps you can win over your friendly administrator with a batch of freshly baked cookies."""
% (self.user.name, self.user.id, self.record._description, self.record._name))
# debug mode
self.debug_mode()
with self.assertRaises(AccessError) as ctx:
with self.debug_mode(), self.assertRaises(AccessError) as ctx:
self.record.write({'val': 1})
self.assertEqual(
ctx.exception.args[0],
Expand All @@ -223,7 +213,7 @@ def test_local(self):
% (self.user.name, self.user.id, self.record._description, self.record.display_name, self.record._name, self.record.id))

ChildModel = self.env['test_access_right.inherits']
with self.assertRaises(AccessError) as ctx:
with self.debug_mode(), self.assertRaises(AccessError) as ctx:
ChildModel.with_user(self.user).create({'some_id': self.record.id, 'val': 2})
self.assertEqual(
ctx.exception.args[0],
Expand All @@ -241,8 +231,7 @@ def test_local(self):
def test_locals(self):
self._make_rule('rule 0', '[("val", "=", 42)]')
self._make_rule('rule 1', '[("val", "=", 78)]')
self.debug_mode()
with self.assertRaises(AccessError) as ctx:
with self.debug_mode(), self.assertRaises(AccessError) as ctx:
self.record.write({'val': 1})
self.assertEqual(
ctx.exception.args[0],
Expand All @@ -261,8 +250,7 @@ def test_locals(self):
def test_globals_all(self):
self._make_rule('rule 0', '[("val", "=", 42)]', global_=True)
self._make_rule('rule 1', '[("val", "=", 78)]', global_=True)
self.debug_mode()
with self.assertRaises(AccessError) as ctx:
with self.debug_mode(), self.assertRaises(AccessError) as ctx:
self.record.write({'val': 1})
self.assertEqual(
ctx.exception.args[0],
Expand All @@ -284,8 +272,7 @@ def test_globals_any(self):
"""
self._make_rule('rule 0', '[("val", "=", 42)]', global_=True)
self._make_rule('rule 1', '[(1, "=", 1)]', global_=True)
self.debug_mode()
with self.assertRaises(AccessError) as ctx:
with self.debug_mode(), self.assertRaises(AccessError) as ctx:
self.record.write({'val': 1})
self.assertEqual(
ctx.exception.args[0],
Expand All @@ -305,8 +292,7 @@ def test_combination(self):
self._make_rule('rule 1', '[(1, "=", 1)]', global_=True)
self._make_rule('rule 2', '[(0, "=", 1)]')
self._make_rule('rule 3', '[("val", "=", 55)]')
self.debug_mode()
with self.assertRaises(AccessError) as ctx:
with self.debug_mode(), self.assertRaises(AccessError) as ctx:
self.record.write({'val': 1})
self.assertEqual(
ctx.exception.args[0],
Expand All @@ -330,8 +316,7 @@ def test_warn_company_no_access(self):
"""
self._make_rule('rule 0', "[('company_id', '=', user.company_id.id)]")
self._make_rule('rule 1', '[("val", "=", 0)]', global_=True)
self.debug_mode()
with self.assertRaises(AccessError) as ctx:
with self.debug_mode(), self.assertRaises(AccessError) as ctx:
self.record.write({'val': 1})
self.assertEqual(
ctx.exception.args[0],
Expand Down Expand Up @@ -364,8 +349,7 @@ def test_warn_company_no_company_field(self):
self.record.sudo().company_id = self.env['res.company'].create({'name': 'Brosse Inc.'})
self.user.sudo().company_ids = [Command.link(self.record.company_id.id)]
child_record = ChildModel.create({'parent_id': self.record.id}).with_user(self.user)
self.debug_mode()
with self.assertRaises(AccessError) as ctx:
with self.debug_mode(), self.assertRaises(AccessError) as ctx:
_ = child_record.parent_id
self.assertEqual(
ctx.exception.args[0],
Expand All @@ -389,8 +373,7 @@ def test_warn_company_access(self):
self.record.sudo().company_id = self.env['res.company'].create({'name': 'Brosse Inc.'})
self.user.sudo().company_ids = [Command.link(self.record.company_id.id)]
self._make_rule('rule 0', "[('company_id', '=', user.company_id.id)]", attr='read')
self.debug_mode()
with self.assertRaises(AccessError) as ctx:
with self.debug_mode(), self.assertRaises(AccessError) as ctx:
_ = self.record.val
self.assertEqual(
ctx.exception.args[0],
Expand Down Expand Up @@ -429,9 +412,10 @@ def setUpClass(cls):

@mute_logger('odoo.models')
def test_read(self):
self.env.ref('base.group_no_one').write(
{'users': [Command.link(self.user.id)]})
with self.assertRaises(AccessError) as ctx:
self.user.write({
'groups_id': [Command.set([self.env.ref('base.group_user').id])],
})
with self.debug_mode(), self.assertRaises(AccessError) as ctx:
_ = self.record.forbidden

self.assertEqual(
Expand All @@ -446,7 +430,7 @@ def test_read(self):
% self.user.id
)

with self.assertRaises(AccessError) as ctx:
with self.debug_mode(), self.assertRaises(AccessError) as ctx:
_ = self.record.forbidden3

self.assertEqual(
Expand All @@ -462,10 +446,10 @@ def test_read(self):

@mute_logger('odoo.models')
def test_write(self):
self.env.ref('base.group_no_one').write(
{'users': [Command.link(self.user.id)]})

with self.assertRaises(AccessError) as ctx:
self.user.write({
'groups_id': [Command.set([self.env.ref('base.group_user').id])],
})
with self.debug_mode(), self.assertRaises(AccessError) as ctx:
self.record.write({'forbidden': 1, 'forbidden2': 2})

self.assertEqual(
Expand Down
34 changes: 19 additions & 15 deletions odoo/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
from functools import lru_cache
from itertools import zip_longest as izip_longest
from passlib.context import CryptContext
from unittest.mock import patch, _patch
from unittest.mock import patch, _patch, Mock
from xmlrpc import client as xmlrpclib

import requests
Expand All @@ -57,7 +57,7 @@
from odoo.modules.registry import Registry
from odoo.service import security
from odoo.sql_db import BaseCursor, Cursor
from odoo.tools import float_compare, single_email_re, profiler, lower_logging, SQL
from odoo.tools import float_compare, single_email_re, profiler, lower_logging, SQL, DotDict
from odoo.tools.misc import find_in_path, mute_logger

from . import case
Expand Down Expand Up @@ -404,20 +404,24 @@ def with_user(self, login):

@contextmanager
def debug_mode(self):
""" Enable the effects of group 'base.group_no_one'; mainly useful with :class:`Form`. """
origin_user_has_groups = BaseModel.user_has_groups

def user_has_groups(self, groups):
group_set = set(groups.split(','))
if '!base.group_no_one' in group_set:
return False
elif 'base.group_no_one' in group_set:
group_set.remove('base.group_no_one')
return not group_set or origin_user_has_groups(self, ','.join(group_set))
return origin_user_has_groups(self, groups)

with patch('odoo.models.BaseModel.user_has_groups', user_has_groups):
""" Enable the effects of debug mode (in particular for group ``base.group_no_one``). """
request = Mock(
httprequest=Mock(host='localhost'),
db=self.env.cr.dbname,
env=self.env,
session=DotDict(odoo.http.get_default_session(), debug='1'),
)
try:
self.env.flush_all()
self.env.invalidate_all()
odoo.http._request_stack.push(request)
yield
self.env.flush_all()
self.env.invalidate_all()
finally:
popped_request = odoo.http._request_stack.pop()
if popped_request is not request:
raise Exception('Wrong request stack cleanup.')

@contextmanager
def _assertRaises(self, exception, *, msg=None):
Expand Down

0 comments on commit 07452a0

Please sign in to comment.