-
Notifications
You must be signed in to change notification settings - Fork 41
/
test_notification.py
158 lines (124 loc) · 5.63 KB
/
test_notification.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import inspect
import json
import sys
import unittest
from bugsnag.configuration import Configuration
from bugsnag.notification import Notification
from tests import fixtures
try:
reload # Python 2.x
except NameError:
try:
from importlib import reload # Python 3.4+
except ImportError:
from imp import reload # Python 3.0 - 3.3
class TestNotification(unittest.TestCase):
def test_sanitize(self):
"""
It should sanitize request data
"""
config = Configuration()
notification = Notification(Exception("oops"), config, {},
request={"params": {"password": "secret"}})
notification.add_tab("request", {"arguments": {"password": "secret"}})
payload = json.loads(notification._payload())
request = payload['events'][0]['metaData']['request']
self.assertEqual(request['arguments']['password'], '[FILTERED]')
self.assertEqual(request['params']['password'], '[FILTERED]')
def test_code(self):
"""
It should include code
"""
config = Configuration()
line = inspect.currentframe().f_lineno + 1
notification = Notification(Exception("oops"), config, {})
payload = json.loads(notification._payload())
code = payload['events'][0]['exceptions'][0]['stacktrace'][0]['code']
lvl = " "
self.assertEqual(code[str(line - 3)], lvl + "\"\"\"")
self.assertEqual(code[str(line - 2)], lvl + "config = Configuration()")
self.assertEqual(code[str(line - 1)],
lvl + "line = inspect.currentframe().f_lineno + 1")
self.assertEqual(
code[str(line)],
lvl +
"notification = Notification(Exception(\"oops\"), config, {})"
)
self.assertEqual(code[str(line + 1)], "")
self.assertEqual(code[str(line + 2)],
lvl + "payload = json.loads(notification._payload())")
self.assertEqual(code[str(line + 3)], "")
def test_code_at_start_of_file(self):
config = Configuration()
notification = Notification(fixtures.start_of_file[1], config, {},
traceback=fixtures.start_of_file[2])
payload = json.loads(notification._payload())
code = payload['events'][0]['exceptions'][0]['stacktrace'][0]['code']
self.assertEqual(
{'1': '# flake8: noqa',
'2': 'try:',
'3': ' import sys; raise Exception("start")',
'4': 'except Exception: start_of_file = sys.exc_info()',
'5': '# 4',
'6': '# 5',
'7': '# 6'}, code)
def test_code_at_end_of_file(self):
config = Configuration()
notification = Notification(fixtures.end_of_file[1], config, {},
traceback=fixtures.end_of_file[2])
payload = json.loads(notification._payload())
code = payload['events'][0]['exceptions'][0]['stacktrace'][0]['code']
self.assertEqual(
{'6': '# 5',
'7': '# 6',
'8': '# 7',
'9': '# 8',
'10': 'try:',
'11': ' import sys; raise Exception("end")',
'12': 'except Exception: end_of_file = sys.exc_info()'}, code)
def test_code_turned_off(self):
config = Configuration()
config.send_code = False
notification = Notification(Exception("oops"), config, {},
traceback=fixtures.end_of_file[2])
payload = json.loads(notification._payload())
code = payload['events'][0]['exceptions'][0]['stacktrace'][0]['code']
self.assertEqual(code, None)
def test_no_traceback_exclude_modules(self):
from tests.fixtures import helpers
config = Configuration()
notification = helpers.invoke_exception_on_other_file(config)
payload = json.loads(notification._payload())
exception = payload['events'][0]['exceptions'][0]
first_traceback = exception['stacktrace'][0]
self.assertEqual(first_traceback['file'], 'fixtures/helpers.py')
def test_traceback_exclude_modules(self):
# Make sure samples.py is compiling to pyc
import py_compile
py_compile.compile('./fixtures/helpers.py')
from tests.fixtures import helpers
reload(helpers) # .py variation might be loaded from previous test.
if sys.version_info < (3, 0):
# Python 2.6 & 2.7 returns the cached file on __file__,
# and hence we verify it returns .pyc for these versions
# and the code at _generate_stacktrace() handles that.
self.assertTrue(helpers.__file__.endswith('.pyc'))
config = Configuration()
config.traceback_exclude_modules = [helpers]
notification = helpers.invoke_exception_on_other_file(config)
payload = json.loads(notification._payload())
exception = payload['events'][0]['exceptions'][0]
first_traceback = exception['stacktrace'][0]
self.assertEqual(first_traceback['file'], 'test_notification.py')
def test_device_data(self):
"""
It should include device data
"""
config = Configuration()
config.hostname = 'test_host_name'
config.runtime_versions = {'python': '9.9.9'}
notification = Notification(Exception("oops"), config, {})
payload = json.loads(notification._payload())
device = payload['events'][0]['device']
self.assertEqual('test_host_name', device['hostname'])
self.assertEqual('9.9.9', device['runtimeVersions']['python'])