Skip to content

Commit

Permalink
Store session and event requests separately
Browse files Browse the repository at this point in the history
Previously the FakeBugsnagServer stored all requests in one list, which
meant mixing session and event requests together. Now we store them
separately so that each can be asserted against individually
  • Loading branch information
imjoehaines committed Dec 6, 2023
1 parent 6338a7e commit fe2d4b4
Show file tree
Hide file tree
Showing 18 changed files with 419 additions and 390 deletions.
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def reset_breadcrumbs():
def bugsnag_server():
server = FakeBugsnagServer(wait_for_duplicate_requests=False)
bugsnag.configure(
endpoint=server.url,
session_endpoint=server.url,
endpoint=server.events_url,
session_endpoint=server.sessions_url,
api_key='3874876376238728937'
)

Expand Down
4 changes: 2 additions & 2 deletions tests/integrations/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
def bugsnag_server():
server = FakeBugsnagServer(wait_for_duplicate_requests=True)
bugsnag.configure(
endpoint=server.url,
session_endpoint=server.url,
endpoint=server.events_url,
session_endpoint=server.sessions_url,
api_key='3874876376238728937'
)

Expand Down
29 changes: 14 additions & 15 deletions tests/integrations/test_asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ class TestASGIMiddleware(IntegrationTest):
def setUp(self):
super(TestASGIMiddleware, self).setUp()
bugsnag.configure(
endpoint=self.server.url,
session_endpoint=self.server.url,
endpoint=self.server.events_url,
session_endpoint=self.server.sessions_url,
asynchronous=False,
api_key='3874876376238728937',
max_breadcrumbs=25,
auto_capture_sessions=False,
)

def test_normal_http_operation(self):
Expand Down Expand Up @@ -50,7 +49,7 @@ async def index(req):
self.assertRaises(ScaryException, lambda: app.get('/'))
self.assertSentReportCount(1)

payload = self.server.received[0]['json_body']
payload = self.server.events_received[0]['json_body']
request = payload['events'][0]['metaData']['request']
self.assertEqual('/', request['path'])
self.assertEqual('GET', request['httpMethod'])
Expand Down Expand Up @@ -88,7 +87,7 @@ async def index(req):
self.assertRaises(ScaryException, lambda: app.get('/'))
self.assertSentReportCount(1)

payload = self.server.received[0]['json_body']
payload = self.server.events_received[0]['json_body']
environment = payload['events'][0]['metaData']['environment']

self.assertEqual('/', environment['path'])
Expand Down Expand Up @@ -120,7 +119,7 @@ async def index(req):
)
self.assertSentReportCount(1)

payload = self.server.received[0]['json_body']
payload = self.server.events_received[0]['json_body']
headers = payload['events'][0]['metaData']['request']['headers']

self.assertEqual('[FILTERED]', headers['authorization'])
Expand All @@ -134,7 +133,7 @@ async def app(scope, recv, send):
self.assertRaises(ScaryException, lambda: app.get('/'))
self.assertSentReportCount(1)

payload = self.server.received[0]['json_body']
payload = self.server.events_received[0]['json_body']
request = payload['events'][0]['metaData']['request']
self.assertEqual('/', request['path'])
self.assertEqual('GET', request['httpMethod'])
Expand Down Expand Up @@ -171,7 +170,7 @@ async def index(req):
self.assertRaises(ScaryException, lambda: app.get('/'))
self.assertSentReportCount(1)

payload = self.server.received[0]['json_body']
payload = self.server.events_received[0]['json_body']
metadata = payload['events'][0]['metaData']
request = metadata['request']
self.assertEqual('/', request['path'])
Expand Down Expand Up @@ -206,7 +205,7 @@ async def app(scope, receive, send):

self.assertSentReportCount(1)

payload = self.server.received[0]['json_body']
payload = self.server.events_received[0]['json_body']
metadata = payload['events'][0]['metaData']
request = metadata['request']
self.assertEqual('/', request['path'])
Expand Down Expand Up @@ -242,7 +241,7 @@ async def index(req):
)
self.assertSentReportCount(1)

payload = self.server.received[0]['json_body']
payload = self.server.events_received[0]['json_body']
request = payload['events'][0]['metaData']['request']
self.assertEqual(
'http://testserver/path?password=[FILTERED]',
Expand Down Expand Up @@ -276,7 +275,7 @@ async def index(req):
)
self.assertSentReportCount(1)

payload = self.server.received[0]['json_body']
payload = self.server.events_received[0]['json_body']
request = payload['events'][0]['metaData']['request']
self.assertEqual('/', request['path'])
self.assertEqual('GET', request['httpMethod'])
Expand Down Expand Up @@ -320,7 +319,7 @@ async def index(req):

assert self.sent_report_count == 1

payload = self.server.received[0]['json_body']
payload = self.server.events_received[0]['json_body']

print(payload)
assert len(payload['events'][0]['exceptions']) == 2
Expand Down Expand Up @@ -368,7 +367,7 @@ async def index(req):

assert self.sent_report_count == 1

payload = self.server.received[0]['json_body']
payload = self.server.events_received[0]['json_body']
exception = payload['events'][0]['exceptions'][0]
feature_flags = payload['events'][0]['featureFlags']

Expand All @@ -387,7 +386,7 @@ async def index(req):

assert self.sent_report_count == 2

payload = self.server.received[1]['json_body']
payload = self.server.events_received[1]['json_body']
feature_flags = payload['events'][0]['featureFlags']

assert feature_flags == [
Expand All @@ -400,7 +399,7 @@ async def index(req):

assert self.sent_report_count == 3

payload = self.server.received[2]['json_body']
payload = self.server.events_received[2]['json_body']
feature_flags = payload['events'][0]['featureFlags']

assert feature_flags == []
29 changes: 15 additions & 14 deletions tests/integrations/test_bottle.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
class TestBottle(IntegrationTest):
def setUp(self):
super(TestBottle, self).setUp()
bugsnag.configure(endpoint=self.server.url,
session_endpoint=self.server.url,
auto_capture_sessions=False,
api_key='3874876376238728937',
asynchronous=False)
bugsnag.configure(
endpoint=self.server.events_url,
session_endpoint=self.server.sessions_url,
api_key='3874876376238728937',
asynchronous=False,
)

def test_routing_error(self):
@route('/beans')
Expand All @@ -26,8 +27,8 @@ def index():
app = TestApp(BugsnagMiddleware(app))

self.assertRaises(Exception, lambda: app.get('/beans?password=123'))
self.assertEqual(1, len(self.server.received))
payload = self.server.received[0]['json_body']
self.assertEqual(1, len(self.server.events_received))
payload = self.server.events_received[0]['json_body']
event = payload['events'][0]
self.assertTrue(event['unhandled'])
self.assertEqual(event['context'], 'GET /beans')
Expand All @@ -54,8 +55,8 @@ def index():
app = TestApp(BugsnagMiddleware(app))

self.assertRaises(Exception, lambda: app.get('/beans'))
self.assertEqual(1, len(self.server.received))
payload = self.server.received[0]['json_body']
self.assertEqual(1, len(self.server.events_received))
payload = self.server.events_received[0]['json_body']
metadata = payload['events'][0]['metaData']
self.assertEqual(metadata['environment']['PATH_INFO'], '/beans')

Expand All @@ -69,9 +70,9 @@ def index(variety):
app = TestApp(BugsnagMiddleware(app))

self.assertRaises(Exception, lambda: app.get('/berries/red'))
self.assertEqual(1, len(self.server.received))
self.assertEqual(1, len(self.server.events_received))

payload = self.server.received[0]['json_body']
payload = self.server.events_received[0]['json_body']
event = payload['events'][0]
self.assertTrue(event['unhandled'])
self.assertEqual(event['context'], 'GET /berries/red')
Expand Down Expand Up @@ -112,7 +113,7 @@ def index():

assert self.sent_report_count == 1

payload = self.server.received[0]['json_body']
payload = self.server.events_received[0]['json_body']
exception = payload['events'][0]['exceptions'][0]
feature_flags = payload['events'][0]['featureFlags']

Expand All @@ -131,7 +132,7 @@ def index():

assert self.sent_report_count == 2

payload = self.server.received[1]['json_body']
payload = self.server.events_received[1]['json_body']
feature_flags = payload['events'][0]['featureFlags']

assert feature_flags == [
Expand All @@ -144,7 +145,7 @@ def index():

assert self.sent_report_count == 3

payload = self.server.received[2]['json_body']
payload = self.server.events_received[2]['json_body']
feature_flags = payload['events'][0]['featureFlags']

assert feature_flags == []
30 changes: 15 additions & 15 deletions tests/integrations/test_celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ def square(x):
assert square.delay(3).get(timeout=1) == 9

with pytest.raises(MissingRequestError):
bugsnag_server.wait_for_request()
bugsnag_server.wait_for_event()

assert len(bugsnag_server.received) == 0
assert len(bugsnag_server.events_received) == 0


def test_app_task_failure(celery_app, celery_worker, bugsnag_server):
Expand All @@ -75,12 +75,12 @@ def divide(x, y):
divide.delay(7, 0)
# other (non-failing) tasks should behave normally
result = cube.delay(3)
bugsnag_server.wait_for_request()
bugsnag_server.wait_for_event()

assert len(bugsnag_server.received) == 1
assert len(bugsnag_server.events_received) == 1
assert result.get(timeout=1) == 27

payload = bugsnag_server.received[0]['json_body']
payload = bugsnag_server.events_received[0]['json_body']
event = payload['events'][0]
exception = event['exceptions'][0]
task = event['metaData']['extra_data']
Expand Down Expand Up @@ -125,8 +125,8 @@ def divide(x, y):
# bugsnag should not suppress the exception
failed_result.get(timeout=1)

bugsnag_server.wait_for_request()
assert len(bugsnag_server.received) == 1
bugsnag_server.wait_for_event()
assert len(bugsnag_server.events_received) == 1
assert result.get(timeout=1) == 27


Expand All @@ -146,9 +146,9 @@ def add(x, y):
result = add.delay(2, 2)

with pytest.raises(MissingRequestError):
bugsnag_server.wait_for_request()
bugsnag_server.wait_for_event()

assert len(bugsnag_server.received) == 0
assert len(bugsnag_server.events_received) == 0
assert result.get(timeout=1) == 4


Expand All @@ -166,11 +166,11 @@ def divide(x, y, **kwargs):
with celery_failure_handler():
divide.delay(2, 0, parts='multi', cache=2)

bugsnag_server.wait_for_request()
bugsnag_server.wait_for_event()

assert len(bugsnag_server.received) == 1
assert len(bugsnag_server.events_received) == 1

payload = bugsnag_server.received[0]['json_body']
payload = bugsnag_server.events_received[0]['json_body']
event = payload['events'][0]
exception = event['exceptions'][0]
task = event['metaData']['extra_data']
Expand Down Expand Up @@ -205,11 +205,11 @@ def oh_no():

with celery_failure_handler():
oh_no.delay()
bugsnag_server.wait_for_request()
bugsnag_server.wait_for_event()

assert len(bugsnag_server.received) == 1
assert len(bugsnag_server.events_received) == 1

payload = bugsnag_server.received[0]['json_body']
payload = bugsnag_server.events_received[0]['json_body']
event = payload['events'][0]

assert len(event['exceptions']) == 3
Expand Down
Loading

0 comments on commit fe2d4b4

Please sign in to comment.