|
8 | 8 | from tornado.testing import AsyncHTTPTestCase |
9 | 9 |
|
10 | 10 |
|
| 11 | +@pytest.fixture |
| 12 | +def tornado_testcase(request): |
| 13 | + # Take the unittest class provided by tornado and manually call its setUp |
| 14 | + # and tearDown. |
| 15 | + # |
| 16 | + # The pytest plugins for tornado seem too complicated to use, as they for |
| 17 | + # some reason assume I want to write my tests in async code. |
| 18 | + def inner(app): |
| 19 | + class TestBogus(AsyncHTTPTestCase): |
| 20 | + def get_app(self): |
| 21 | + return app |
| 22 | + |
| 23 | + def bogustest(self): |
| 24 | + # We need to pass a valid test method name to the ctor, so this |
| 25 | + # is the method. It does nothing. |
| 26 | + pass |
| 27 | + |
| 28 | + self = TestBogus("bogustest") |
| 29 | + self.setUp() |
| 30 | + request.addfinalizer(self.tearDown) |
| 31 | + return self |
| 32 | + |
| 33 | + return inner |
| 34 | + |
| 35 | + |
11 | 36 | class CrashingHandler(RequestHandler): |
12 | 37 | def get(self): |
13 | 38 | with configure_scope() as scope: |
14 | 39 | scope.set_tag("foo", 42) |
15 | 40 | 1 / 0 |
16 | 41 |
|
17 | 42 |
|
18 | | -class TestBasic(AsyncHTTPTestCase): |
19 | | - @pytest.fixture(autouse=True) |
20 | | - def initialize(self, sentry_init, capture_events): |
21 | | - sentry_init(integrations=[TornadoIntegration()], send_default_pii=True) |
22 | | - self.events = capture_events() |
23 | | - |
24 | | - def get_app(self): |
25 | | - |
26 | | - return Application([(r"/hi", CrashingHandler)]) |
27 | | - |
28 | | - def test_basic(self): |
29 | | - response = self.fetch( |
30 | | - "/hi?foo=bar", headers={"Cookie": "name=value; name2=value2; name3=value3"} |
31 | | - ) |
32 | | - assert response.code == 500 |
33 | | - |
34 | | - event, = self.events |
35 | | - exception, = event["exception"]["values"] |
36 | | - assert exception["type"] == "ZeroDivisionError" |
37 | | - |
38 | | - request = event["request"] |
39 | | - host = request["headers"]["Host"] |
40 | | - assert event["request"] == { |
41 | | - "env": {"REMOTE_ADDR": "127.0.0.1"}, |
42 | | - "headers": { |
43 | | - "Accept-Encoding": "gzip", |
44 | | - "Connection": "close", |
45 | | - "Host": host, |
46 | | - "Cookie": "name=value; name2=value2; name3=value3", |
47 | | - }, |
48 | | - "cookies": {"name": "value", "name2": "value2", "name3": "value3"}, |
49 | | - "method": "GET", |
50 | | - "query_string": "foo=bar", |
51 | | - "url": "http://{host}/hi".format(host=host), |
52 | | - } |
53 | | - |
54 | | - assert event["tags"] == {"foo": 42} |
55 | | - assert ( |
56 | | - event["transaction"] |
57 | | - == "tests.integrations.tornado.test_tornado.CrashingHandler.get" |
58 | | - ) |
| 43 | +def test_basic(tornado_testcase, sentry_init, capture_events): |
| 44 | + sentry_init(integrations=[TornadoIntegration()], send_default_pii=True) |
| 45 | + events = capture_events() |
| 46 | + client = tornado_testcase(Application([(r"/hi", CrashingHandler)])) |
| 47 | + |
| 48 | + response = client.fetch( |
| 49 | + "/hi?foo=bar", headers={"Cookie": "name=value; name2=value2; name3=value3"} |
| 50 | + ) |
| 51 | + assert response.code == 500 |
| 52 | + |
| 53 | + event, = events |
| 54 | + exception, = event["exception"]["values"] |
| 55 | + assert exception["type"] == "ZeroDivisionError" |
| 56 | + |
| 57 | + request = event["request"] |
| 58 | + host = request["headers"]["Host"] |
| 59 | + assert event["request"] == { |
| 60 | + "env": {"REMOTE_ADDR": "127.0.0.1"}, |
| 61 | + "headers": { |
| 62 | + "Accept-Encoding": "gzip", |
| 63 | + "Connection": "close", |
| 64 | + "Host": host, |
| 65 | + "Cookie": "name=value; name2=value2; name3=value3", |
| 66 | + }, |
| 67 | + "cookies": {"name": "value", "name2": "value2", "name3": "value3"}, |
| 68 | + "method": "GET", |
| 69 | + "query_string": "foo=bar", |
| 70 | + "url": "http://{host}/hi".format(host=host), |
| 71 | + } |
| 72 | + |
| 73 | + assert event["tags"] == {"foo": 42} |
| 74 | + assert ( |
| 75 | + event["transaction"] |
| 76 | + == "tests.integrations.tornado.test_tornado.CrashingHandler.get" |
| 77 | + ) |
| 78 | + |
| 79 | + with configure_scope() as scope: |
| 80 | + assert not scope._tags |
59 | 81 |
|
60 | | - with configure_scope() as scope: |
61 | | - assert not scope._tags |
62 | 82 |
|
| 83 | +def test_400_not_logged(tornado_testcase, sentry_init, capture_events): |
| 84 | + sentry_init(integrations=[TornadoIntegration()]) |
| 85 | + events = capture_events() |
63 | 86 |
|
64 | | -class Test400NotLogged(AsyncHTTPTestCase): |
65 | | - @pytest.fixture(autouse=True) |
66 | | - def initialize(self, sentry_init, capture_events): |
67 | | - sentry_init(integrations=[TornadoIntegration()]) |
68 | | - self.events = capture_events() |
| 87 | + class CrashingHandler(RequestHandler): |
| 88 | + def get(self): |
| 89 | + raise HTTPError(400, "Oops") |
69 | 90 |
|
70 | | - def get_app(self): |
71 | | - class CrashingHandler(RequestHandler): |
72 | | - def get(self): |
73 | | - raise HTTPError(400, "Oops") |
| 91 | + client = tornado_testcase(Application([(r"/", CrashingHandler)])) |
74 | 92 |
|
75 | | - return Application([(r"/", CrashingHandler)]) |
| 93 | + response = client.fetch("/") |
| 94 | + assert response.code == 400 |
76 | 95 |
|
77 | | - def test_400_not_logged(self): |
78 | | - response = self.fetch("/") |
79 | | - assert response.code == 400 |
| 96 | + assert not events |
80 | 97 |
|
81 | | - assert not self.events |
82 | 98 |
|
| 99 | +def test_user_auth(tornado_testcase, sentry_init, capture_events): |
| 100 | + sentry_init(integrations=[TornadoIntegration()], send_default_pii=True) |
| 101 | + events = capture_events() |
83 | 102 |
|
84 | | -class TestUserAuth(AsyncHTTPTestCase): |
85 | | - @pytest.fixture(autouse=True) |
86 | | - def initialize(self, sentry_init, capture_events): |
87 | | - sentry_init(integrations=[TornadoIntegration()], send_default_pii=True) |
88 | | - self.events = capture_events() |
| 103 | + class UserHandler(RequestHandler): |
| 104 | + def get(self): |
| 105 | + 1 / 0 |
89 | 106 |
|
90 | | - def get_app(self): |
91 | | - class UserHandler(RequestHandler): |
92 | | - def get(self): |
93 | | - 1 / 0 |
| 107 | + def get_current_user(self): |
| 108 | + return 42 |
94 | 109 |
|
95 | | - def get_current_user(self): |
96 | | - return 42 |
| 110 | + class NoUserHandler(RequestHandler): |
| 111 | + def get(self): |
| 112 | + 1 / 0 |
97 | 113 |
|
98 | | - class NoUserHandler(RequestHandler): |
99 | | - def get(self): |
100 | | - 1 / 0 |
| 114 | + client = tornado_testcase( |
| 115 | + Application([(r"/auth", UserHandler), (r"/noauth", NoUserHandler)]) |
| 116 | + ) |
101 | 117 |
|
102 | | - return Application([(r"/auth", UserHandler), (r"/noauth", NoUserHandler)]) |
| 118 | + # has user |
| 119 | + response = client.fetch("/auth") |
| 120 | + assert response.code == 500 |
103 | 121 |
|
104 | | - def test_has_user(self): |
105 | | - response = self.fetch("/auth") |
106 | | - assert response.code == 500 |
| 122 | + event, = events |
| 123 | + exception, = event["exception"]["values"] |
| 124 | + assert exception["type"] == "ZeroDivisionError" |
107 | 125 |
|
108 | | - event, = self.events |
109 | | - exception, = event["exception"]["values"] |
110 | | - assert exception["type"] == "ZeroDivisionError" |
| 126 | + assert event["user"] == {"is_authenticated": True} |
111 | 127 |
|
112 | | - assert event["user"] == {"is_authenticated": True} |
| 128 | + events.clear() |
113 | 129 |
|
114 | | - def test_has_no_user(self): |
115 | | - response = self.fetch("/noauth") |
116 | | - assert response.code == 500 |
| 130 | + # has no user |
| 131 | + response = client.fetch("/noauth") |
| 132 | + assert response.code == 500 |
117 | 133 |
|
118 | | - event, = self.events |
119 | | - exception, = event["exception"]["values"] |
120 | | - assert exception["type"] == "ZeroDivisionError" |
| 134 | + event, = events |
| 135 | + exception, = event["exception"]["values"] |
| 136 | + assert exception["type"] == "ZeroDivisionError" |
121 | 137 |
|
122 | | - assert "user" not in event |
| 138 | + assert "user" not in event |
0 commit comments