# Path: bugsnag/wsgi/middleware.py
# class BugsnagMiddleware:
#     """
#     Notifies Bugsnag on any unhandled exception that happens while processing
#     a request in your application.
# 
#     This middleware should be installed before any middlewares that catch
#     the exception and render an error messae.
#     """
# 
#     def __init__(self, application):
#         middleware = bugsnag.configure().internal_middleware
#         if bottle_present:
#             bugsnag.configure().runtime_versions['bottle'] = bottle.__version__
#         middleware.before_notify(add_wsgi_request_data_to_notification)
#         self.application = application
# 
#     def __call__(self, environ, start_response):
#         return WrappedWSGIApp(self.application, environ, start_response)
# 
# Path: bugsnag/breadcrumbs.py
# class BreadcrumbType(Enum):
#     NAVIGATION = 'navigation'
#     REQUEST = 'request'
#     PROCESS = 'process'
#     LOG = 'log'
#     USER = 'user'
#     STATE = 'state'
#     ERROR = 'error'
#     MANUAL = 'manual'
# 
# Path: tests/utils.py
# class IntegrationTest(unittest.TestCase):
# 
#     @classmethod
#     def setUpClass(cls):
#         cls.server = FakeBugsnagServer()
# 
#     def setUp(self):
#         self.server.received = []
# 
#     def tearDown(self):
#         bugsnag.legacy.default_client.uninstall_sys_hook()
#         client = bugsnag.Client()
#         client.configuration.api_key = 'some key'
#         bugsnag.legacy.default_client = client
#         bugsnag.legacy.configuration = client.configuration
# 
#     @classmethod
#     def tearDownClass(cls):
#         cls.server.shutdown()
# 
#     def assertSentReportCount(self, count):
#         self.assertEqual(len(self.server.received), count)
# 
#     def assertExceptionName(self, received_index, event_index, name):
#         json_body = self.server.received[received_index]['json_body']
#         event = json_body['events'][event_index]
#         exception = event['exceptions'][0]
#         self.assertEqual(exception['errorClass'], name)
# 
#     @property
#     def sent_report_count(self) -> int:
#         return len(self.server.received)


import pytest
import bugsnag.event
import bugsnag
from webtest import TestApp
from bugsnag.wsgi.middleware import BugsnagMiddleware
from bugsnag.breadcrumbs import BreadcrumbType
from tests.utils import IntegrationTest
        class MyApp(object):

            def __init__(self, environ, start_response):
                raise SentinelError("oops")

        def callback(event):
            event.set_user(id=event.request.GET['user_id'])

        bugsnag.before_notify(callback)
        app = TestApp(BugsnagMiddleware(MyApp))

        with pytest.raises(SentinelError):
            app.get('/beans?user_id=my_id')

        assert len(self.server.received) == 1
        payload = self.server.received[0]['json_body']
        assert payload['events'][0]['user']['id'] == 'my_id'

        breadcrumbs = payload['events'][0]['breadcrumbs']

        assert len(breadcrumbs) == 1
        assert breadcrumbs[0]['name'] == 'http request'
        assert breadcrumbs[0]['metaData'] == {'to': '/beans'}
        assert breadcrumbs[0]['type'] == BreadcrumbType.NAVIGATION.value

    def test_bugsnag_middleware_leaves_breadcrumb_with_referer(self):
        class CrashOnStartApp(object):
            def __init__(self, environ, start_response):
                raise SentinelError("oops")


# Path: bugsnag/wsgi/middleware.py
# class BugsnagMiddleware:
#     """
#     Notifies Bugsnag on any unhandled exception that happens while processing
#     a request in your application.
# 
#     This middleware should be installed before any middlewares that catch
#     the exception and render an error messae.
#     """
# 
#     def __init__(self, application):
#         middleware = bugsnag.configure().internal_middleware
#         if bottle_present:
#             bugsnag.configure().runtime_versions['bottle'] = bottle.__version__
#         middleware.before_notify(add_wsgi_request_data_to_notification)
#         self.application = application
# 
#     def __call__(self, environ, start_response):
#         return WrappedWSGIApp(self.application, environ, start_response)
# 
# Path: bugsnag/breadcrumbs.py
# class BreadcrumbType(Enum):
#     NAVIGATION = 'navigation'
#     REQUEST = 'request'
#     PROCESS = 'process'
#     LOG = 'log'
#     USER = 'user'
#     STATE = 'state'
#     ERROR = 'error'
#     MANUAL = 'manual'
# 
# Path: tests/utils.py
# class IntegrationTest(unittest.TestCase):
# 
#     @classmethod
#     def setUpClass(cls):
#         cls.server = FakeBugsnagServer()
# 
#     def setUp(self):
#         self.server.received = []
# 
#     def tearDown(self):
#         bugsnag.legacy.default_client.uninstall_sys_hook()
#         client = bugsnag.Client()
#         client.configuration.api_key = 'some key'
#         bugsnag.legacy.default_client = client
#         bugsnag.legacy.configuration = client.configuration
# 
#     @classmethod
#     def tearDownClass(cls):
#         cls.server.shutdown()
# 
#     def assertSentReportCount(self, count):
#         self.assertEqual(len(self.server.received), count)
# 
#     def assertExceptionName(self, received_index, event_index, name):
#         json_body = self.server.received[received_index]['json_body']
#         event = json_body['events'][event_index]
#         exception = event['exceptions'][0]
#         self.assertEqual(exception['errorClass'], name)
# 
#     @property
#     def sent_report_count(self) -> int:
#         return len(self.server.received)


# Path: tests/integrations/test_wsgi.py
import pytest
import bugsnag.event
import bugsnag
from webtest import TestApp
from bugsnag.wsgi.middleware import BugsnagMiddleware
from bugsnag.breadcrumbs import BreadcrumbType
from tests.utils import IntegrationTest

        class MyApp(object):

            def __init__(self, environ, start_response):
                raise SentinelError("oops")

        def callback(event):
            event.set_user(id=event.request.GET['user_id'])

        bugsnag.before_notify(callback)
        app = TestApp(BugsnagMiddleware(MyApp))

        with pytest.raises(SentinelError):
            app.get('/beans?user_id=my_id')

        assert len(self.server.received) == 1
        payload = self.server.received[0]['json_body']
        assert payload['events'][0]['user']['id'] == 'my_id'

        breadcrumbs = payload['events'][0]['breadcrumbs']

        assert len(breadcrumbs) == 1
        assert breadcrumbs[0]['name'] == 'http request'
        assert breadcrumbs[0]['metaData'] == {'to': '/beans'}
        assert breadcrumbs[0]['type'] == BreadcrumbType.NAVIGATION.value

    def test_bugsnag_middleware_leaves_breadcrumb_with_referer(self):
        class CrashOnStartApp(object):
            def __init__(self, environ, start_response):
                raise SentinelError("oops")


        app = TestApp(BugsnagMiddleware(CrashOnStartApp))