Skip to content

Commit

Permalink
rest_log: fix env isolation in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
simahawk committed May 13, 2022
1 parent d0af50f commit 3c78712
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 28 deletions.
10 changes: 6 additions & 4 deletions rest_log/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

class TestDBLoggingMixin(object):
@staticmethod
def _get_service(class_or_instance):
def _get_service(class_or_instance, collection=None):
# pylint: disable=R7980
class LoggedService(Component):
_inherit = "base.rest.service"
Expand Down Expand Up @@ -49,11 +49,13 @@ def fail(self, how):
# class_or_instance._build_services(class_or_instance, LoggedService)
# TODO: WTH _build_services does not load the component?
LoggedService._build_component(class_or_instance.comp_registry)
return class_or_instance._get_service_component(class_or_instance, "logmycalls")
return class_or_instance._get_service_component(
class_or_instance, "logmycalls", collection=collection
)

@contextlib.contextmanager
def _get_mocked_request(self, httprequest=None, extra_headers=None):
with MockRequest(self.env) as mocked_request:
def _get_mocked_request(self, env=None, httprequest=None, extra_headers=None):
with MockRequest(env or self.env) as mocked_request:
mocked_request.httprequest = httprequest or mocked_request.httprequest
headers = {"Cookie": "IaMaCookie!", "Api-Key": "I_MUST_STAY_SECRET"}
headers.update(extra_headers or {})
Expand Down
47 changes: 23 additions & 24 deletions rest_log/tests/test_db_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from odoo import exceptions
from odoo.tools import mute_logger

from odoo.addons.base_rest.controllers.main import _PseudoCollection
from odoo.addons.base_rest.tests.common import TransactionRestServiceRegistryCase
from odoo.addons.component.tests.common import new_rollbacked_env
from odoo.addons.rest_log import exceptions as log_exceptions # pylint: disable=W7950
Expand Down Expand Up @@ -237,7 +238,6 @@ class TestDBLoggingExceptionBase(
def setUpClass(cls):
super().setUpClass()
cls._setup_registry(cls)
cls.service = cls._get_service(cls)

@classmethod
def tearDownClass(cls):
Expand All @@ -249,44 +249,43 @@ def _test_exception(self, test_type, wrapping_exc, exc_name, severity):
log_model = self.env["rest.log"].sudo()
initial_entries = log_model.search([])
entry_url_from_exc = None
with self._get_mocked_request():
try:
self.service.dispatch("fail", test_type)
except Exception as err:
# Not using `assertRaises` to inspect the exception directly
self.assertTrue(isinstance(err, wrapping_exc))
self.assertEqual(
self.service._get_exception_message(err), "Failed as you wanted!"
)
entry_url_from_exc = err.rest_json_info["log_entry_url"]

with new_rollbacked_env() as env:
log_model = env["rest.log"].sudo()
# Context: we are running in a transaction case which uses savepoints.
# The log machinery is going to rollback the transation when catching errors.
# Hence we need a completely separated env for the service.
with new_rollbacked_env() as new_env:
# Init fake collection w/ new env
collection = _PseudoCollection(self._collection_name, new_env)
service = self._get_service(self, collection=collection)
with self._get_mocked_request(env=new_env):
try:
service.dispatch("fail", test_type)
except Exception as err:
# Not using `assertRaises` to inspect the exception directly
self.assertTrue(isinstance(err, wrapping_exc))
self.assertEqual(
service._get_exception_message(err), "Failed as you wanted!"
)
entry_url_from_exc = err.rest_json_info["log_entry_url"]

with new_rollbacked_env() as new_env:
log_model = new_env["rest.log"].sudo()
entry = log_model.search([]) - initial_entries
expected = {
"collection": self.service._collection,
"collection": service._collection,
"state": "failed",
"result": "null",
"exception_name": exc_name,
"exception_message": "Failed as you wanted!",
"severity": severity,
}
self.assertRecordValues(entry, [expected])
self.assertEqual(entry_url_from_exc, self.service._get_log_entry_url(entry))
self.assertEqual(entry_url_from_exc, service._get_log_entry_url(entry))


class TestDBLoggingExceptionUserError(TestDBLoggingExceptionBase):
@staticmethod
def _get_test_controller(class_or_instance, root_path=None):
# Override to avoid registering twice the same controller route.
# Disclaimer: to run these tests w/ need TransactionCase
# because the handling of the exception will do a savepoint rollback
# which causes SavepointCase to fail.
# When using the transaction case the rest_registry is initliazed
# at every test, same for the test controller.
# This leads to the error
# "Only one REST controller
# can be safely declared for root path /test_controller/"
return super()._get_test_controller(
class_or_instance, root_path="/test_log_exception_user/"
)
Expand Down

0 comments on commit 3c78712

Please sign in to comment.