From cde7b7c3562614ab929578febfd39f6b45378ba1 Mon Sep 17 00:00:00 2001 From: Joe Haines Date: Thu, 10 Aug 2023 13:04:41 +0100 Subject: [PATCH] Add tests for feature flags in ASGI apps --- tests/integrations/test_asgi.py | 67 +++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/tests/integrations/test_asgi.py b/tests/integrations/test_asgi.py index 7eb6fd80..4cc3106c 100644 --- a/tests/integrations/test_asgi.py +++ b/tests/integrations/test_asgi.py @@ -333,3 +333,70 @@ async def index(req): assert len(exception2['stacktrace']) == 2 assert 'other_func' == exception2['stacktrace'][0]['method'] assert 'index' == exception2['stacktrace'][1]['method'] + + def test_feature_flags_dont_leak_between_requests(self): + count = 0 + app = Starlette() + + async def other_func(): + nonlocal count + count += 1 + bugsnag.add_feature_flag(str(count), 'b') + + if count > 4: + bugsnag.clear_feature_flags() + + raise ScaryException('fell winds!') + + @app.route('/') + async def index(req): + nonlocal count + count += 1 + bugsnag.add_feature_flag(str(count), 'a') + + await other_func() + return PlainTextResponse('pineapple') + + app = TestClient(BugsnagMiddleware(app)) + + with pytest.raises(Exception): + app.get('/') + + assert self.sent_report_count == 1 + + payload = self.server.received[0]['json_body'] + exception = payload['events'][0]['exceptions'][0] + feature_flags = payload['events'][0]['featureFlags'] + + assert exception['errorClass'] == 'tests.utils.ScaryException' + assert exception['message'] == 'fell winds!' + assert exception['stacktrace'][0]['method'] == 'other_func' + assert exception['stacktrace'][1]['method'] == 'index' + + assert feature_flags == [ + {'name': '1', 'variant': 'a'}, + {'name': '2', 'variant': 'b'} + ] + + with pytest.raises(Exception): + app.get('/') + + assert self.sent_report_count == 2 + + payload = self.server.received[1]['json_body'] + feature_flags = payload['events'][0]['featureFlags'] + + assert feature_flags == [ + {'name': '3', 'variant': 'a'}, + {'name': '4', 'variant': 'b'} + ] + + with pytest.raises(Exception): + app.get('/') + + assert self.sent_report_count == 3 + + payload = self.server.received[2]['json_body'] + feature_flags = payload['events'][0]['featureFlags'] + + assert feature_flags == []