You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
4.0 KiB

  1. # Copyright 2016-2017 Versada <https://versada.eu/>
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. import logging
  4. import sys
  5. import unittest
  6. import raven
  7. from odoo import exceptions
  8. from .. import initialize_raven
  9. from ..logutils import OdooSentryHandler
  10. def log_handler_by_class(logger, handler_cls):
  11. for handler in logger.handlers:
  12. if isinstance(handler, handler_cls):
  13. yield handler
  14. def remove_logging_handler(logger_name, handler_cls):
  15. '''Removes handlers of specified classes from a :class:`logging.Logger`
  16. with a given name.
  17. :param string logger_name: name of the logger
  18. :param handler_cls: class of the handler to remove. You can pass a tuple of
  19. classes to catch several classes
  20. '''
  21. logger = logging.getLogger(logger_name)
  22. for handler in log_handler_by_class(logger, handler_cls):
  23. logger.removeHandler(handler)
  24. class InMemoryClient(raven.Client):
  25. '''A :class:`raven.Client` subclass which simply stores events in a list.
  26. Extended based on the one found in raven-python to avoid additional testing
  27. dependencies: https://git.io/vyGO3
  28. '''
  29. def __init__(self, **kwargs):
  30. self.events = []
  31. super(InMemoryClient, self).__init__(**kwargs)
  32. def is_enabled(self):
  33. return True
  34. def send(self, **kwargs):
  35. self.events.append(kwargs)
  36. def has_event(self, event_level, event_msg):
  37. for event in self.events:
  38. if (event.get('level') == event_level and
  39. event.get('message') == event_msg):
  40. return True
  41. return False
  42. class TestClientSetup(unittest.TestCase):
  43. def setUp(self):
  44. super(TestClientSetup, self).setUp()
  45. self.logger = logging.getLogger(__name__)
  46. # Sentry is enabled by default, so the default handler will be added
  47. # when the module is loaded. After that, subsequent calls to
  48. # setup_logging will not re-add our handler. We explicitly remove
  49. # OdooSentryHandler handler so we can test with our in-memory client.
  50. remove_logging_handler('', OdooSentryHandler)
  51. def assertEventCaptured(self, client, event_level, event_msg):
  52. self.assertTrue(
  53. client.has_event(event_level, event_msg),
  54. msg='Event: "%s" was not captured' % event_msg
  55. )
  56. def assertEventNotCaptured(self, client, event_level, event_msg):
  57. self.assertFalse(
  58. client.has_event(event_level, event_msg),
  59. msg='Event: "%s" was captured' % event_msg
  60. )
  61. def test_initialize_raven_sets_dsn(self):
  62. config = {
  63. 'sentry_enabled': True,
  64. 'sentry_dsn': 'http://public:secret@example.com/1',
  65. }
  66. client = initialize_raven(config, client_cls=InMemoryClient)
  67. self.assertEqual(client.remote.base_url, 'http://example.com')
  68. def test_capture_event(self):
  69. config = {
  70. 'sentry_enabled': True,
  71. 'sentry_dsn': 'http://public:secret@example.com/1',
  72. }
  73. level, msg = logging.WARNING, 'Test event, can be ignored'
  74. client = initialize_raven(config, client_cls=InMemoryClient)
  75. self.logger.log(level, msg)
  76. self.assertEventCaptured(client, level, msg)
  77. def test_ignore_exceptions(self):
  78. config = {
  79. 'sentry_enabled': True,
  80. 'sentry_dsn': 'http://public:secret@example.com/1',
  81. 'sentry_ignore_exceptions': 'odoo.exceptions.UserError',
  82. }
  83. level, msg = logging.WARNING, 'Test UserError'
  84. client = initialize_raven(config, client_cls=InMemoryClient)
  85. handlers = list(
  86. log_handler_by_class(logging.getLogger(), OdooSentryHandler)
  87. )
  88. self.assertTrue(handlers)
  89. handler = handlers[0]
  90. try:
  91. raise exceptions.UserError(msg)
  92. except exceptions.UserError:
  93. exc_info = sys.exc_info()
  94. record = logging.LogRecord(
  95. __name__, level, __file__, 42, msg, (), exc_info)
  96. handler.emit(record)
  97. self.assertEventNotCaptured(client, level, msg)