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.

122 lines
4.1 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. from odoo.tests import TransactionCase
  6. from odoo.tools import config
  7. from odoo import exceptions
  8. from ..hooks import initialize_sentry
  9. from sentry_sdk.transport import HttpTransport
  10. from sentry_sdk.integrations.logging import _IGNORED_LOGGERS
  11. def remove_handler_ignore(handler_name):
  12. """Removes handlers of handlers ignored list.
  13. """
  14. _IGNORED_LOGGERS.discard(handler_name)
  15. class TestException(exceptions.UserError):
  16. pass
  17. class InMemoryTransport(HttpTransport):
  18. """A :class:`sentry_sdk.Hub.transport` subclass which simply stores events in a list.
  19. Extended based on the one found in raven-python to avoid additional testing
  20. dependencies: https://git.io/vyGO3
  21. """
  22. def __init__(self, *args, **kwargs):
  23. self.events = []
  24. def capture_event(self, event, *args, **kwargs):
  25. self.events.append(event)
  26. def has_event(self, event_level, event_msg):
  27. for event in self.events:
  28. if (event.get('level') == event_level and
  29. event.get('logentry', {}).get('message') == event_msg):
  30. return True
  31. return False
  32. def flush(self, *args, **kwargs):
  33. pass
  34. def kill(self, *args, **kwargs):
  35. pass
  36. class TestClientSetup(TransactionCase):
  37. def setUp(self):
  38. super(TestClientSetup, self).setUp()
  39. self.dsn = 'http://public:secret@example.com/1'
  40. config.options['sentry_enabled'] = True
  41. config.options['sentry_dsn'] = self.dsn
  42. self.client = initialize_sentry(config)._client
  43. self.client.transport = InMemoryTransport({'dsn': self.dsn})
  44. self.handler = self.client.integrations['logging']._handler
  45. def log(self, level, msg, exc_info=None):
  46. record = logging.LogRecord(
  47. __name__, level, __file__, 42, msg, (), exc_info)
  48. self.handler.emit(record)
  49. def assertEventCaptured(self, client, event_level, event_msg):
  50. self.assertTrue(
  51. client.transport.has_event(event_level, event_msg),
  52. msg='Event: "%s" was not captured' % event_msg
  53. )
  54. def assertEventNotCaptured(self, client, event_level, event_msg):
  55. self.assertFalse(
  56. client.transport.has_event(event_level, event_msg),
  57. msg='Event: "%s" was captured' % event_msg
  58. )
  59. def test_initialize_raven_sets_dsn(self):
  60. self.assertEqual(self.client.dsn, self.dsn)
  61. def test_capture_event(self):
  62. level, msg = logging.WARNING, 'Test event, can be ignored'
  63. self.log(level, msg)
  64. level = "warning"
  65. self.assertEventCaptured(self.client, level, msg)
  66. def test_capture_event_exc(self):
  67. level, msg = logging.WARNING, 'Test event, can be ignored exception'
  68. try:
  69. raise TestException(msg)
  70. except TestException:
  71. exc_info = sys.exc_info()
  72. self.log(level, msg, exc_info)
  73. level = "warning"
  74. self.assertEventCaptured(self.client, level, msg)
  75. def test_ignore_exceptions(self):
  76. config.options['sentry_ignore_exceptions'] = "odoo.exceptions.UserError"
  77. client = initialize_sentry(config)._client
  78. client.transport = InMemoryTransport({'dsn': self.dsn})
  79. level, msg = logging.WARNING, 'Test exception'
  80. try:
  81. raise exceptions.UserError(msg)
  82. except exceptions.UserError:
  83. exc_info = sys.exc_info()
  84. self.log(level, msg, exc_info)
  85. level = "warning"
  86. self.assertEventNotCaptured(client, level, msg)
  87. def test_exclude_logger(self):
  88. config.options['sentry_enabled'] = True
  89. config.options['sentry_exclude_loggers'] = __name__
  90. client = initialize_sentry(config)._client
  91. client.transport = InMemoryTransport({'dsn': self.dsn})
  92. level, msg = logging.WARNING, 'Test exclude logger %s' % __name__
  93. self.log(level, msg)
  94. level = "warning"
  95. # Revert ignored logger so it doesn't affect other tests
  96. remove_handler_ignore(__name__)
  97. self.assertEventNotCaptured(client, level, msg)