def setUp(self): super(ClientTest, self).setUp() self.client = Client(api_key='testing client key', endpoint=self.server.url, project_root=os.path.join(os.getcwd(), 'tests'), asynchronous=False, install_sys_hook=False)
def setUp(self): super(ClientTest, self).setUp() self.client = Client(api_key='testing client key', use_ssl=False, endpoint=self.server.address, asynchronous=False, install_sys_hook=False)
def test_unregister_installed_except_hook(self): # Setup an original except hook def excepthook(*exc_info): pass sys.excepthook = excepthook client = Client() self.assertNotEqual(sys.excepthook, excepthook) client.uninstall_sys_hook() self.assertEqual(sys.excepthook, excepthook)
def wrapped(obj): client = Client(endpoint=obj.server.url, asynchronous=False) handler = client.log_handler() logger = logging.getLogger(__name__) logger.setLevel(logging.INFO) logger.addHandler(handler) try: func(obj, handler, logger) finally: logger.removeHandler(handler)
def wrapped(obj): client = Client(use_ssl=False, endpoint=obj.server.address, api_key='tomatoes', asynchronous=False) handler = client.log_handler() logger = logging.getLogger(__name__) logger.addHandler(handler) try: func(obj, handler, logger) finally: logger.removeHandler(handler)
def test_delivery(self): c = Configuration() self.called = False class FooDelivery: def deliver(foo, config, payload): self.called = True c.configure(delivery=FooDelivery(), api_key='abc') client = Client(c) client.notify(Exception('Oh no')) self.assertTrue(self.called) self.assertSentReportCount(0) del self.called
def test_failed_delivery(self): c = Configuration() self.called = False class FooDelivery: def deliver(foo, config, payload): self.called = True raise ScaryException('something gone wrong') c.configure(delivery=FooDelivery(), api_key='abc') client = Client(c) client.notify(Exception('Oh no')) self.assertTrue(self.called) del self.called
def test_session_tracker_sets_details_from_config(self): client = Client( auto_capture_sessions=True, session_endpoint=self.server.url, asynchronous=False ) client.session_tracker.start_session() client.session_tracker.send_sessions() json_body = self.server.received[0]['json_body'] # Notifier properties notifier = json_body['notifier'] self.assertTrue('name' in notifier) self.assertEqual(notifier['name'], Notification.NOTIFIER_NAME) self.assertTrue('url' in notifier) self.assertEqual(notifier['url'], Notification.NOTIFIER_URL) self.assertTrue('version' in notifier) notifier_version = package_version('bugsnag') or 'unknown' self.assertEqual(notifier['version'], notifier_version) # App properties app = json_body['app'] self.assertTrue('releaseStage' in app) self.assertEqual(app['releaseStage'], client.configuration.get('release_stage')) self.assertTrue('version' in app) self.assertEqual(app['version'], client.configuration.get('app_version')) # Device properties device = json_body['device'] self.assertTrue('hostname' in device) self.assertEqual(device['hostname'], client.configuration.get('hostname')) self.assertTrue('runtimeVersions' in device) self.assertEqual(device['runtimeVersions']['python'], platform.python_version())
def test_multiple_clients_one_excepthook(self): def excepthook(*exc_info): pass sys.excepthook = excepthook client1 = Client(api_key='abc', asynchronous=False, endpoint=self.server.url) Client(api_key='456', asynchronous=False, endpoint=self.server.url, install_sys_hook=False) self.assertEqual(client1, sys.excepthook.bugsnag_client) self.assertEqual(client1.sys_excepthook, excepthook)
def test_installed_except_hook(self): client = Client() # Prevent the existing hook from being called client.sys_excepthook = None self.hooked = None def hooked_except_hook(*exc_info): self.hooked = exc_info client.excepthook = hooked_except_hook try: raise Exception('Testing excepthook notify') except Exception: sys.excepthook(*sys.exc_info()) self.assertEqual(self.hooked[0], Exception)
def test_session_tracker_does_not_send_when_nothing_to_send(self): client = Client( api_key='fff', session_endpoint=self.server.url, asynchronous=False, release_stage="dev", notify_release_stages=["prod"], ) client.session_tracker.send_sessions() self.assertEqual(0, len(self.server.received))
def test_client_metadata_fields(self): client = Client(endpoint=self.server.url, asynchronous=False) handler = client.log_handler(extra_fields={ 'fruit': ['grapes', 'pears'] }) logger = logging.getLogger(__name__) logger.addHandler(handler) logger.error('A wild tomato appeared', extra={ 'grapes': 8, 'pears': 2, 'tomatoes': 1 }) logger.removeHandler(handler) json_body = self.server.received[0]['json_body'] event = json_body['events'][0] self.assertEqual(event['metaData']['fruit'], { 'grapes': 8, 'pears': 2 })
def test_session_middleware_attaches_session_to_notification(self): client = Client(auto_capture_sessions=True, session_endpoint=self.server.url + '/ignore', endpoint=self.server.url, asynchronous=False) client.session_tracker.start_session() client.notify(Exception("Test")) while len(self.server.received) == 0: time.sleep(0.5) json_body = self.server.received[0]['json_body'] event = json_body['events'][0] self.assertTrue('session' in event) session = event['session'] self.assertTrue('id' in session) self.assertTrue('startedAt' in session) self.assertTrue('events' in session) sesevents = session['events'] self.assertTrue('unhandled' in sesevents) self.assertEqual(sesevents['unhandled'], 0) self.assertTrue('handled' in sesevents) self.assertEqual(sesevents['handled'], 1)
def test_client_metadata_fields(self): client = Client(use_ssl=False, endpoint=self.server.address, api_key='new news', asynchronous=False) handler = client.log_handler(extra_fields={ 'fruit': ['grapes', 'pears'] }) logger = logging.getLogger(__name__) logger.addHandler(handler) logger.error('A wild tomato appeared', extra={ 'grapes': 8, 'pears': 2, 'tomatoes': 1 }) logger.removeHandler(handler) json_body = self.server.received[0]['json_body'] event = json_body['events'][0] self.assertEqual(event['metaData']['fruit'], { 'grapes': 8, 'pears': 2 })
def test_session_middleware_attaches_session_to_notification(self): client = Client( auto_capture_sessions=True, session_endpoint=self.server.url + '/ignore', endpoint=self.server.url, asynchronous=False ) client.session_tracker.start_session() client.notify(Exception("Test")) while len(self.server.received) == 0: time.sleep(0.5) json_body = self.server.received[0]['json_body'] event = json_body['events'][0] self.assertTrue('session' in event) session = event['session'] self.assertTrue('id' in session) self.assertTrue('startedAt' in session) self.assertTrue('events' in session) sesevents = session['events'] self.assertTrue('unhandled' in sesevents) self.assertEqual(sesevents['unhandled'], 0) self.assertTrue('handled' in sesevents) self.assertEqual(sesevents['handled'], 1)
def test_session_tracker_send_sessions_sends_sessions(self): client = Client(auto_capture_sessions=True, session_endpoint=self.server.url, asynchronous=False) client.session_tracker.start_session() self.assertEqual(len(client.session_tracker.session_counts), 1) client.session_tracker.send_sessions() self.assertEqual(len(client.session_tracker.session_counts), 0) json_body = self.server.received[0]['json_body'] self.assertTrue('app' in json_body) self.assertTrue('notifier' in json_body) self.assertTrue('device' in json_body) self.assertTrue('sessionCounts' in json_body) self.assertEqual(len(json_body['sessionCounts']), 1)
def test_installed_except_hook_calls_previous_except_hook(self): self.hook_ran = False def excepthook(*exc_info): self.hook_ran = True sys.excepthook = excepthook client = Client(auto_notify=False) # noqa try: raise Exception('Testing excepthook notify') except Exception: sys.excepthook(*sys.exc_info()) self.assertTrue(self.hook_ran)
def test_multiple_clients_different_keys(self): client1 = Client(api_key='abc', asynchronous=False, endpoint=self.server.url) client2 = Client(api_key='456', asynchronous=False, endpoint=self.server.url) client1.notify(ScaryException('foo')) self.assertSentReportCount(1) headers = self.server.received[0]['headers'] client2.notify(ScaryException('bar')) self.assertSentReportCount(2) headers = self.server.received[1]['headers'] self.assertEqual(headers['Bugsnag-Api-Key'], '456')
def test_multiple_clients_different_keys(self): client1 = Client(api_key='abc', asynchronous=False, use_ssl=False, endpoint=self.server.address) client2 = Client(api_key='456', asynchronous=False, use_ssl=False, endpoint=self.server.address) client1.notify(ScaryException('foo')) self.assertSentReportCount(1) json_body = self.server.received[0]['json_body'] client2.notify(ScaryException('bar')) self.assertSentReportCount(2) json_body = self.server.received[1]['json_body'] self.assertEqual(json_body['apiKey'], '456')
def test_multiple_clients_different_keys(self): client1 = Client(api_key='abc', asynchronous=False, use_ssl=False, endpoint=self.server.address) client2 = Client(api_key='456', asynchronous=False, use_ssl=False, endpoint=self.server.address) client1.notify(ScaryException('foo')) self.assertSentReportCount(1) headers = self.server.received[0]['headers'] client2.notify(ScaryException('bar')) self.assertSentReportCount(2) headers = self.server.received[1]['headers'] self.assertEqual(headers['Bugsnag-Api-Key'], '456')
def test_invalid_delivery(self): c = Configuration() c.configure(delivery=44, api_key='abc') client = Client(c) client.notify(Exception('Oh no'))
class ClientTest(IntegrationTest): def setUp(self): super(ClientTest, self).setUp() self.client = Client(api_key='testing client key', endpoint=self.server.url, project_root=os.path.join(os.getcwd(), 'tests'), asynchronous=False, install_sys_hook=False) # Initialisation def test_init_no_configuration(self): client = Client(install_sys_hook=False) self.assertTrue(isinstance(client.configuration, Configuration)) def test_init_configuration(self): configuration = Configuration() client = Client(configuration=configuration, install_sys_hook=False) self.assertEqual(client.configuration, configuration) def test_init_options(self): client = Client(api_key='testing client key', install_sys_hook=False) self.assertEqual(client.configuration.api_key, 'testing client key') # Sending Event def test_notify_exception(self): self.client.notify(Exception('Testing Notify')) self.assertEqual(len(self.server.received), 1) def test_notify_exc_info(self): try: raise Exception('Testing Notify EXC Info') except Exception: self.client.notify_exc_info(*sys.exc_info()) self.assertEqual(len(self.server.received), 1) def test_delivery(self): c = Configuration() self.called = False class FooDelivery: def deliver(foo, config, payload, options={}): self.called = True c.configure(delivery=FooDelivery(), api_key='abc') client = Client(c) client.notify(Exception('Oh no')) self.assertTrue(self.called) self.assertSentReportCount(0) del self.called def test_invalid_delivery(self): c = Configuration() c.configure(delivery=44, api_key='abc') client = Client(c) client.notify(Exception('Oh no')) def test_failed_delivery(self): c = Configuration() self.called = False class FooDelivery: def deliver(foo, config, payload, options={}): self.called = True raise ScaryException('something gone wrong') c.configure(delivery=FooDelivery(), api_key='abc') client = Client(c) client.notify(Exception('Oh no')) self.assertTrue(self.called) del self.called # Capture def test_notify_capture(self): try: with self.client.capture(): raise Exception('Testing Notify Context') except Exception: pass self.assertEqual(len(self.server.received), 1) def test_notify_capture_raises(self): def foo(): with self.client.capture(): raise Exception('Testing Notify Context') self.assertRaises(Exception, foo) self.assertEqual(len(self.server.received), 1) def test_notify_capture_options(self): try: with self.client.capture(section={'key': 'value'}): raise Exception('Testing Notify Context') except Exception: pass self.assertEqual(len(self.server.received), 1) json_body = self.server.received[0]['json_body'] event = json_body['events'][0] self.assertEqual(event['metaData']['section'], {'key': 'value'}) def test_notify_capture_change_severity(self): try: with self.client.capture(severity='info'): raise Exception('Testing Notify Context') except Exception: pass payload = self.server.received[0]['json_body'] event = payload['events'][0] self.assertEqual(event['severity'], "info") self.assertFalse(event['unhandled']) self.assertEqual(event['severityReason'], {"type": "userContextSetSeverity"}) def test_notify_capture_types(self): try: with self.client.capture((ScaryException, )): raise Exception('Testing Notify Capture Types') except Exception: pass self.assertSentReportCount(0) try: with self.client.capture((ScaryException, )): raise ScaryException('Testing Notify Capture Types') except Exception: pass self.assertSentReportCount(1) self.assertExceptionName(0, 0, 'tests.utils.ScaryException') def test_no_exception_capture(self): with self.client.capture(): pass self.assertEqual(len(self.server.received), 0) def test_capture_decorator(self): @self.client.capture def foo(): raise Exception('Testing Capture Function') try: foo() except Exception: pass self.assertSentReportCount(1) def test_capture_decorator_mismatch(self): @self.client.capture def foo(): pass self.assertRaises(TypeError, foo, 'bar') self.assertSentReportCount(1) payload = self.server.received[0]['json_body'] file = payload['events'][0]['exceptions'][0]['stacktrace'][0]['file'] self.assertEqual(file, "test_client.py") def test_capture_decorator_returns_value(self): @self.client.capture def foo(): return "300" self.assertEqual(foo(), "300") def test_capture_decorator_change_severity(self): @self.client.capture(severity='info') def foo(): raise Exception('Testing Capture Function') try: foo(Exception) except Exception: pass payload = self.server.received[0]['json_body'] event = payload['events'][0] self.assertEqual(event['severity'], "info") self.assertFalse(event['unhandled']) self.assertEqual(event['severityReason'], {"type": "userContextSetSeverity"}) def test_capture_decorator_raises(self): @self.client.capture def foo(): raise Exception('Testing Capture Function') self.assertRaises(Exception, foo) self.assertSentReportCount(1) def test_capture_decorator_with_types(self): @self.client.capture((ScaryException, )) def foo(exception_type): raise exception_type('Testing Capture Function with Types') try: foo(Exception) except Exception: pass self.assertSentReportCount(0) try: foo(ScaryException) except Exception: pass self.assertSentReportCount(1) def test_capture_decorator_with_class_method(self): class Test(object): @self.client.capture() def foo(self): raise Exception() try: test = Test() test.foo() except Exception: pass self.assertSentReportCount(1) # Exception Hook def test_exception_hook(self): try: raise Exception('Testing excepthook notify') except Exception: self.client.excepthook(*sys.exc_info()) self.assertEqual(len(self.server.received), 1) event = self.server.received[0]['json_body']['events'][0] self.assertEqual(event['severity'], 'error') def test_exception_hook_disabled(self): self.client.configuration.auto_notify = False try: raise Exception('Testing excepthook notify') except Exception: self.client.excepthook(*sys.exc_info()) self.assertEqual(len(self.server.received), 0) def test_installed_except_hook(self): client = Client() # Prevent the existing hook from being called client.sys_excepthook = None self.hooked = None def hooked_except_hook(*exc_info): self.hooked = exc_info client.excepthook = hooked_except_hook try: raise Exception('Testing excepthook notify') except Exception: sys.excepthook(*sys.exc_info()) self.assertEqual(self.hooked[0], Exception) def test_installed_except_hook_calls_previous_except_hook(self): self.hook_ran = False def excepthook(*exc_info): self.hook_ran = True sys.excepthook = excepthook client = Client(auto_notify=False) # noqa try: raise Exception('Testing excepthook notify') except Exception: sys.excepthook(*sys.exc_info()) self.assertTrue(self.hook_ran) def test_unregister_installed_except_hook(self): # Setup an original except hook def excepthook(*exc_info): pass sys.excepthook = excepthook client = Client() self.assertNotEqual(sys.excepthook, excepthook) client.uninstall_sys_hook() self.assertEqual(sys.excepthook, excepthook) # Multiple Clients def test_multiple_clients_different_keys(self): client1 = Client(api_key='abc', asynchronous=False, endpoint=self.server.url) client2 = Client(api_key='456', asynchronous=False, endpoint=self.server.url) client1.notify(ScaryException('foo')) self.assertSentReportCount(1) headers = self.server.received[0]['headers'] client2.notify(ScaryException('bar')) self.assertSentReportCount(2) headers = self.server.received[1]['headers'] self.assertEqual(headers['Bugsnag-Api-Key'], '456') def test_multiple_clients_one_excepthook(self): def excepthook(*exc_info): pass sys.excepthook = excepthook client1 = Client(api_key='abc', asynchronous=False, endpoint=self.server.url) Client(api_key='456', asynchronous=False, endpoint=self.server.url, install_sys_hook=False) self.assertEqual(client1, sys.excepthook.bugsnag_client) self.assertEqual(client1.sys_excepthook, excepthook) # Session Tracking def test_session_tracker_object_exists(self): client = Client() self.assertTrue(hasattr(client, 'session_tracker'))
class ClientTest(IntegrationTest): def setUp(self): super(ClientTest, self).setUp() self.client = Client(api_key='testing client key', use_ssl=False, endpoint=self.server.address, asynchronous=False, install_sys_hook=False) # Initialisation def test_init_no_configuration(self): client = Client(install_sys_hook=False) self.assertTrue(isinstance(client.configuration, Configuration)) def test_init_configuration(self): configuration = Configuration() client = Client(configuration=configuration, install_sys_hook=False) self.assertEqual(client.configuration, configuration) def test_init_options(self): client = Client(api_key='testing client key', install_sys_hook=False) self.assertEqual(client.configuration.api_key, 'testing client key') # Sending Notification def test_notify_exception(self): self.client.notify(Exception('Testing Notify')) self.assertEqual(len(self.server.received), 1) def test_notify_exc_info(self): try: raise Exception('Testing Notify EXC Info') except Exception: self.client.notify_exc_info(*sys.exc_info()) self.assertEqual(len(self.server.received), 1) def test_delivery(self): c = Configuration() self.called = False class FooDelivery: def deliver(foo, config, payload): self.called = True c.configure(delivery=FooDelivery(), api_key='abc') client = Client(c) client.notify(Exception('Oh no')) self.assertTrue(self.called) self.assertSentReportCount(0) del self.called def test_invalid_delivery(self): c = Configuration() c.configure(delivery=44, api_key='abc') client = Client(c) client.notify(Exception('Oh no')) def test_failed_delivery(self): c = Configuration() self.called = False class FooDelivery: def deliver(foo, config, payload): self.called = True raise ScaryException('something gone wrong') c.configure(delivery=FooDelivery(), api_key='abc') client = Client(c) client.notify(Exception('Oh no')) self.assertTrue(self.called) del self.called # Capture def test_notify_capture(self): try: with self.client.capture(): raise Exception('Testing Notify Context') except Exception: pass self.assertEqual(len(self.server.received), 1) def test_notify_capture_raises(self): def foo(): with self.client.capture(): raise Exception('Testing Notify Context') self.assertRaises(Exception, foo) self.assertEqual(len(self.server.received), 1) def test_notify_capture_options(self): try: with self.client.capture(section={'key': 'value'}): raise Exception('Testing Notify Context') except Exception: pass self.assertEqual(len(self.server.received), 1) json_body = self.server.received[0]['json_body'] event = json_body['events'][0] self.assertEqual(event['metaData']['section'], {'key': 'value'}) def test_notify_capture_types(self): try: with self.client.capture((ScaryException, )): raise Exception('Testing Notify Capture Types') except Exception: pass self.assertSentReportCount(0) try: with self.client.capture((ScaryException, )): raise ScaryException('Testing Notify Capture Types') except Exception: pass self.assertSentReportCount(1) self.assertExceptionName(0, 0, 'tests.utils.ScaryException') def test_no_exception_capture(self): with self.client.capture(): pass self.assertEqual(len(self.server.received), 0) def test_capture_decorator(self): @self.client.capture def foo(): raise Exception('Testing Capture Function') try: foo() except Exception: pass self.assertSentReportCount(1) def test_capture_decorator_returns_value(self): @self.client.capture def foo(): return "300" self.assertEqual(foo(), "300") def test_capture_decorator_raises(self): @self.client.capture def foo(): raise Exception('Testing Capture Function') self.assertRaises(Exception, foo) self.assertSentReportCount(1) def test_capture_decorator_with_types(self): @self.client.capture((ScaryException, )) def foo(exception_type): raise exception_type('Testing Capture Function with Types') try: foo(Exception) except Exception: pass self.assertSentReportCount(0) try: foo(ScaryException) except Exception: pass self.assertSentReportCount(1) def test_capture_decorator_with_class_method(self): class Test(object): @self.client.capture() def foo(self): raise Exception() try: test = Test() test.foo() except Exception: pass self.assertSentReportCount(1) # Exception Hook def test_exception_hook(self): try: raise Exception('Testing excepthook notify') except Exception: self.client.excepthook(*sys.exc_info()) self.assertEqual(len(self.server.received), 1) event = self.server.received[0]['json_body']['events'][0] self.assertEqual(event['severity'], 'error') def test_exception_hook_disabled(self): self.client.configuration.auto_notify = False try: raise Exception('Testing excepthook notify') except Exception: self.client.excepthook(*sys.exc_info()) self.assertEqual(len(self.server.received), 0) def test_installed_except_hook(self): client = Client() # Prevent the existing hook from being called client.sys_excepthook = None self.hooked = None def hooked_except_hook(*exc_info): self.hooked = exc_info client.excepthook = hooked_except_hook try: raise Exception('Testing excepthook notify') except Exception: sys.excepthook(*sys.exc_info()) self.assertEqual(self.hooked[0], Exception) def test_installed_except_hook_calls_previous_except_hook(self): self.hook_ran = False def excepthook(*exc_info): self.hook_ran = True sys.excepthook = excepthook client = Client(auto_notify=False) # noqa try: raise Exception('Testing excepthook notify') except Exception: sys.excepthook(*sys.exc_info()) self.assertTrue(self.hook_ran) def test_unregister_installed_except_hook(self): # Setup an original except hook def excepthook(*exc_info): pass sys.excepthook = excepthook client = Client() self.assertNotEqual(sys.excepthook, excepthook) client.uninstall_sys_hook() self.assertEqual(sys.excepthook, excepthook) # Multiple Clients def test_multiple_clients_different_keys(self): client1 = Client(api_key='abc', asynchronous=False, use_ssl=False, endpoint=self.server.address) client2 = Client(api_key='456', asynchronous=False, use_ssl=False, endpoint=self.server.address) client1.notify(ScaryException('foo')) self.assertSentReportCount(1) json_body = self.server.received[0]['json_body'] client2.notify(ScaryException('bar')) self.assertSentReportCount(2) json_body = self.server.received[1]['json_body'] self.assertEqual(json_body['apiKey'], '456') def test_multiple_clients_one_excepthook(self): def excepthook(*exc_info): pass sys.excepthook = excepthook client1 = Client(api_key='abc', asynchronous=False, use_ssl=False, endpoint=self.server.address) Client(api_key='456', asynchronous=False, use_ssl=False, endpoint=self.server.address, install_sys_hook=False) self.assertEqual(client1, sys.excepthook.bugsnag_client) self.assertEqual(client1.sys_excepthook, excepthook)
import os import logging from rivr.wsgi import WSGIHandler from palaverapi.views import app logger = logging.getLogger('rivr.request') if os.environ.get('BUGSNAG_API_KEY'): from bugsnag import Client client = Client() handler = client.log_handler() handler.setLevel(logging.ERROR) logger.addHandler(handler) else: console = logging.StreamHandler() console.setLevel(logging.ERROR) logger.addHandler(console) wsgi = WSGIHandler(app)
def test_init_options(self): client = Client(api_key='testing client key', install_sys_hook=False) self.assertEqual(client.configuration.api_key, 'testing client key')
class ClientTest(IntegrationTest): def setUp(self): super(ClientTest, self).setUp() self.client = Client(api_key='testing client key', use_ssl=False, endpoint=self.server.address, asynchronous=False, install_sys_hook=False) # Initialisation def test_init_no_configuration(self): client = Client(install_sys_hook=False) self.assertTrue(isinstance(client.configuration, Configuration)) def test_init_configuration(self): configuration = Configuration() client = Client(configuration=configuration, install_sys_hook=False) self.assertEqual(client.configuration, configuration) def test_init_options(self): client = Client(api_key='testing client key', install_sys_hook=False) self.assertEqual(client.configuration.api_key, 'testing client key') # Sending Notification def test_notify_exception(self): self.client.notify(Exception('Testing Notify')) self.assertEqual(len(self.server.received), 1) def test_notify_exc_info(self): try: raise Exception('Testing Notify EXC Info') except Exception: self.client.notify_exc_info(*sys.exc_info()) self.assertEqual(len(self.server.received), 1) def test_delivery(self): c = Configuration() self.called = False class FooDelivery: def deliver(foo, config, payload): self.called = True c.configure(delivery=FooDelivery(), api_key='abc') client = Client(c) client.notify(Exception('Oh no')) self.assertTrue(self.called) self.assertSentReportCount(0) del self.called def test_invalid_delivery(self): c = Configuration() c.configure(delivery=44, api_key='abc') client = Client(c) client.notify(Exception('Oh no')) def test_failed_delivery(self): c = Configuration() self.called = False class FooDelivery: def deliver(foo, config, payload): self.called = True raise ScaryException('something gone wrong') c.configure(delivery=FooDelivery(), api_key='abc') client = Client(c) client.notify(Exception('Oh no')) self.assertTrue(self.called) del self.called # Capture def test_notify_capture(self): try: with self.client.capture(): raise Exception('Testing Notify Context') except Exception: pass self.assertEqual(len(self.server.received), 1) def test_notify_capture_raises(self): def foo(): with self.client.capture(): raise Exception('Testing Notify Context') self.assertRaises(Exception, foo) self.assertEqual(len(self.server.received), 1) def test_notify_capture_options(self): try: with self.client.capture(section={'key': 'value'}): raise Exception('Testing Notify Context') except Exception: pass self.assertEqual(len(self.server.received), 1) json_body = self.server.received[0]['json_body'] event = json_body['events'][0] self.assertEqual(event['metaData']['section'], { 'key': 'value' }) def test_notify_capture_change_severity(self): try: with self.client.capture(severity='info'): raise Exception('Testing Notify Context') except Exception: pass payload = self.server.received[0]['json_body'] event = payload['events'][0] self.assertEqual(event['severity'], "info") self.assertFalse(event['unhandled']) self.assertEqual(event['severityReason'], { "type": "userContextSetSeverity" }) def test_notify_capture_types(self): try: with self.client.capture((ScaryException,)): raise Exception('Testing Notify Capture Types') except Exception: pass self.assertSentReportCount(0) try: with self.client.capture((ScaryException,)): raise ScaryException('Testing Notify Capture Types') except Exception: pass self.assertSentReportCount(1) self.assertExceptionName(0, 0, 'tests.utils.ScaryException') def test_no_exception_capture(self): with self.client.capture(): pass self.assertEqual(len(self.server.received), 0) def test_capture_decorator(self): @self.client.capture def foo(): raise Exception('Testing Capture Function') try: foo() except Exception: pass self.assertSentReportCount(1) def test_capture_decorator_mismatch(self): @self.client.capture def foo(): pass self.assertRaises(TypeError, foo, 'bar') self.assertSentReportCount(1) payload = self.server.received[0]['json_body'] file = payload['events'][0]['exceptions'][0]['stacktrace'][0]['file'] self.assertEqual(file, "test_client.py") def test_capture_decorator_returns_value(self): @self.client.capture def foo(): return "300" self.assertEqual(foo(), "300") def test_capture_decorator_change_severity(self): @self.client.capture(severity='info') def foo(): raise Exception('Testing Capture Function') try: foo(Exception) except Exception: pass payload = self.server.received[0]['json_body'] event = payload['events'][0] self.assertEqual(event['severity'], "info") self.assertFalse(event['unhandled']) self.assertEqual(event['severityReason'], { "type": "userContextSetSeverity" }) def test_capture_decorator_raises(self): @self.client.capture def foo(): raise Exception('Testing Capture Function') self.assertRaises(Exception, foo) self.assertSentReportCount(1) def test_capture_decorator_with_types(self): @self.client.capture((ScaryException,)) def foo(exception_type): raise exception_type('Testing Capture Function with Types') try: foo(Exception) except Exception: pass self.assertSentReportCount(0) try: foo(ScaryException) except Exception: pass self.assertSentReportCount(1) def test_capture_decorator_with_class_method(self): class Test(object): @self.client.capture() def foo(self): raise Exception() try: test = Test() test.foo() except Exception: pass self.assertSentReportCount(1) # Exception Hook def test_exception_hook(self): try: raise Exception('Testing excepthook notify') except Exception: self.client.excepthook(*sys.exc_info()) self.assertEqual(len(self.server.received), 1) event = self.server.received[0]['json_body']['events'][0] self.assertEqual(event['severity'], 'error') def test_exception_hook_disabled(self): self.client.configuration.auto_notify = False try: raise Exception('Testing excepthook notify') except Exception: self.client.excepthook(*sys.exc_info()) self.assertEqual(len(self.server.received), 0) def test_installed_except_hook(self): client = Client() # Prevent the existing hook from being called client.sys_excepthook = None self.hooked = None def hooked_except_hook(*exc_info): self.hooked = exc_info client.excepthook = hooked_except_hook try: raise Exception('Testing excepthook notify') except Exception: sys.excepthook(*sys.exc_info()) self.assertEqual(self.hooked[0], Exception) def test_installed_except_hook_calls_previous_except_hook(self): self.hook_ran = False def excepthook(*exc_info): self.hook_ran = True sys.excepthook = excepthook client = Client(auto_notify=False) # noqa try: raise Exception('Testing excepthook notify') except Exception: sys.excepthook(*sys.exc_info()) self.assertTrue(self.hook_ran) def test_unregister_installed_except_hook(self): # Setup an original except hook def excepthook(*exc_info): pass sys.excepthook = excepthook client = Client() self.assertNotEqual(sys.excepthook, excepthook) client.uninstall_sys_hook() self.assertEqual(sys.excepthook, excepthook) # Multiple Clients def test_multiple_clients_different_keys(self): client1 = Client(api_key='abc', asynchronous=False, use_ssl=False, endpoint=self.server.address) client2 = Client(api_key='456', asynchronous=False, use_ssl=False, endpoint=self.server.address) client1.notify(ScaryException('foo')) self.assertSentReportCount(1) headers = self.server.received[0]['headers'] client2.notify(ScaryException('bar')) self.assertSentReportCount(2) headers = self.server.received[1]['headers'] self.assertEqual(headers['Bugsnag-Api-Key'], '456') def test_multiple_clients_one_excepthook(self): def excepthook(*exc_info): pass sys.excepthook = excepthook client1 = Client(api_key='abc', asynchronous=False, use_ssl=False, endpoint=self.server.address) Client(api_key='456', asynchronous=False, use_ssl=False, endpoint=self.server.address, install_sys_hook=False) self.assertEqual(client1, sys.excepthook.bugsnag_client) self.assertEqual(client1.sys_excepthook, excepthook) # Session Tracking def test_session_tracker_object_exists(self): client = Client() self.assertTrue(hasattr(client, 'session_tracker'))
def test_init_configuration(self): configuration = Configuration() client = Client(configuration=configuration, install_sys_hook=False) self.assertEqual(client.configuration, configuration)
def test_init_no_configuration(self): client = Client(install_sys_hook=False) self.assertTrue(isinstance(client.configuration, Configuration))
def test_session_tracker_object_exists(self): client = Client() self.assertTrue(hasattr(client, 'session_tracker'))