forked from getsentry/sentry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
294 lines (238 loc) · 10.8 KB
/
tests.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# -*- coding: utf-8 -*-
from __future__ import absolute_import
import datetime
import json
import mock
import zlib
from django.conf import settings as django_settings
from django.core.urlresolvers import reverse
from django.test.utils import override_settings
from django.utils import timezone
from gzip import GzipFile
from exam import fixture
from raven import Client
from sentry.models import Group, Event, Project, User
from sentry.testutils import TestCase
from sentry.testutils.helpers import get_auth_header
from sentry.utils.compat import StringIO
from sentry.utils.settings import (
validate_settings, ConfigurationError, import_string)
DEPENDENCY_TEST_DATA = {
"postgresql": ('DATABASES', 'psycopg2.extensions', "database engine", "django.db.backends.postgresql_psycopg2", {
'default': {
'ENGINE': "django.db.backends.postgresql_psycopg2",
'NAME': 'test',
'USER': 'root',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': ''
}
}),
"mysql": ('DATABASES', 'MySQLdb', "database engine", "django.db.backends.mysql", {
'default': {
'ENGINE': "django.db.backends.mysql",
'NAME': 'test',
'USER': 'root',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': ''
}
}),
"oracle": ('DATABASES', 'cx_Oracle', "database engine", "django.db.backends.oracle", {
'default': {
'ENGINE': "django.db.backends.oracle",
'NAME': 'test',
'USER': 'root',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': ''
}
}),
"memcache": ('CACHES', 'memcache', "caching backend", "django.core.cache.backends.memcached.MemcachedCache", {
'default': {
'BACKEND': "django.core.cache.backends.memcached.MemcachedCache",
'LOCATION': '127.0.0.1:11211',
}
}),
"pylibmc": ('CACHES', 'pylibmc', "caching backend", "django.core.cache.backends.memcached.PyLibMCCache", {
'default': {
'BACKEND': "django.core.cache.backends.memcached.PyLibMCCache",
'LOCATION': '127.0.0.1:11211',
}
}),
}
class RavenIntegrationTest(TestCase):
"""
This mocks the test server and specifically tests behavior that would
happen between Raven <--> Sentry over HTTP communication.
"""
def setUp(self):
self.user = User.objects.create(username='coreapi')
self.project = Project.objects.create(owner=self.user, name='Foo', slug='bar')
self.pm = self.project.team.member_set.get_or_create(user=self.user)[0]
self.pk = self.project.key_set.get_or_create(user=self.user)[0]
def sendRemote(self, url, data, headers={}):
# TODO: make this install a temporary handler which raises an assertion error
import logging
sentry_errors = logging.getLogger('sentry.errors')
sentry_errors.addHandler(logging.StreamHandler())
sentry_errors.setLevel(logging.DEBUG)
content_type = headers.pop('Content-Type', None)
headers = dict(('HTTP_' + k.replace('-', '_').upper(), v) for k, v in headers.iteritems())
resp = self.client.post(
reverse('sentry-api-store', args=[self.pk.project_id]),
data=data,
content_type=content_type,
**headers)
self.assertEquals(resp.status_code, 200, resp.content)
@mock.patch('raven.base.Client.send_remote')
def test_basic(self, send_remote):
send_remote.side_effect = self.sendRemote
client = Client(
dsn='http://%s:%s@localhost:8000/%s' % (
self.pk.public_key, self.pk.secret_key, self.pk.project_id)
)
client.capture('Message', message='foo')
send_remote.assert_called_once()
self.assertEquals(Group.objects.count(), 1)
group = Group.objects.get()
self.assertEquals(group.event_set.count(), 1)
instance = group.event_set.get()
self.assertEquals(instance.message, 'foo')
class SentryRemoteTest(TestCase):
@fixture
def path(self):
return reverse('sentry-api-store')
def test_minimal(self):
kwargs = {'message': 'hello'}
resp = self._postWithHeader(kwargs)
assert resp.status_code == 200, resp.content
event_id = json.loads(resp.content)['id']
instance = Event.objects.get(event_id=event_id)
assert instance.message == 'hello'
def test_timestamp(self):
timestamp = timezone.now().replace(microsecond=0, tzinfo=timezone.utc) - datetime.timedelta(hours=1)
kwargs = {u'message': 'hello', 'timestamp': timestamp.strftime('%s.%f')}
resp = self._postWithSignature(kwargs)
self.assertEquals(resp.status_code, 200, resp.content)
instance = Event.objects.get()
self.assertEquals(instance.message, 'hello')
self.assertEquals(instance.datetime, timestamp)
group = instance.group
self.assertEquals(group.first_seen, timestamp)
self.assertEquals(group.last_seen, timestamp)
def test_timestamp_as_iso(self):
timestamp = timezone.now().replace(microsecond=0, tzinfo=timezone.utc) - datetime.timedelta(hours=1)
kwargs = {u'message': 'hello', 'timestamp': timestamp.strftime('%Y-%m-%dT%H:%M:%S.%f')}
resp = self._postWithSignature(kwargs)
self.assertEquals(resp.status_code, 200, resp.content)
instance = Event.objects.get()
self.assertEquals(instance.message, 'hello')
self.assertEquals(instance.datetime, timestamp)
group = instance.group
self.assertEquals(group.first_seen, timestamp)
self.assertEquals(group.last_seen, timestamp)
def test_ungzipped_data(self):
kwargs = {'message': 'hello', 'server_name': 'not_dcramer.local',
'level': 40, 'site': 'not_a_real_site'}
resp = self._postWithSignature(kwargs)
self.assertEquals(resp.status_code, 200)
instance = Event.objects.get()
self.assertEquals(instance.message, 'hello')
self.assertEquals(instance.server_name, 'not_dcramer.local')
self.assertEquals(instance.site, 'not_a_real_site')
self.assertEquals(instance.level, 40)
@override_settings(SENTRY_ALLOW_ORIGIN='getsentry.com')
def test_correct_data_with_get(self):
kwargs = {'message': 'hello', 'server_name': 'not_dcramer.local',
'level': 40, 'site': 'not_a_real_site'}
resp = self._getWithReferer(kwargs)
self.assertEquals(resp.status_code, 200, resp.content)
instance = Event.objects.get()
self.assertEquals(instance.message, 'hello')
self.assertEquals(instance.server_name, 'not_dcramer.local')
self.assertEquals(instance.level, 40)
self.assertEquals(instance.site, 'not_a_real_site')
@override_settings(SENTRY_ALLOW_ORIGIN='getsentry.com')
def test_get_without_referer(self):
kwargs = {'message': 'hello', 'server_name': 'not_dcramer.local',
'level': 40, 'site': 'not_a_real_site'}
resp = self._getWithReferer(kwargs, referer=None, protocol='4')
self.assertEquals(resp.status_code, 400, resp.content)
@override_settings(SENTRY_ALLOW_ORIGIN='*')
def test_get_without_referer_allowed(self):
kwargs = {'message': 'hello', 'server_name': 'not_dcramer.local',
'level': 40, 'site': 'not_a_real_site'}
resp = self._getWithReferer(kwargs, referer=None, protocol='4')
self.assertEquals(resp.status_code, 200, resp.content)
def test_signature(self):
kwargs = {'message': 'hello', 'server_name': 'not_dcramer.local',
'level': 40, 'site': 'not_a_real_site'}
resp = self._postWithSignature(kwargs)
self.assertEquals(resp.status_code, 200, resp.content)
instance = Event.objects.get()
self.assertEquals(instance.message, 'hello')
self.assertEquals(instance.server_name, 'not_dcramer.local')
self.assertEquals(instance.site, 'not_a_real_site')
self.assertEquals(instance.level, 40)
def test_content_encoding_deflate(self):
kwargs = {'message': 'hello'}
message = zlib.compress(json.dumps(kwargs))
key = self.projectkey.public_key
secret = self.projectkey.secret_key
resp = self.client.post(
self.path, message,
content_type='application/octet-stream',
HTTP_CONTENT_ENCODING='deflate',
HTTP_X_SENTRY_AUTH=get_auth_header('_postWithHeader', key, secret),
)
assert resp.status_code == 200, resp.content
event_id = json.loads(resp.content)['id']
instance = Event.objects.get(event_id=event_id)
assert instance.message == 'hello'
def test_content_encoding_gzip(self):
kwargs = {'message': 'hello'}
message = json.dumps(kwargs)
fp = StringIO()
try:
f = GzipFile(fileobj=fp, mode='w')
f.write(message)
finally:
f.close()
key = self.projectkey.public_key
secret = self.projectkey.secret_key
resp = self.client.post(
self.path, fp.getvalue(),
content_type='application/octet-stream',
HTTP_CONTENT_ENCODING='gzip',
HTTP_X_SENTRY_AUTH=get_auth_header('_postWithHeader', key, secret),
)
assert resp.status_code == 200, resp.content
event_id = json.loads(resp.content)['id']
instance = Event.objects.get(event_id=event_id)
assert instance.message == 'hello'
class DepdendencyTest(TestCase):
def raise_import_error(self, package):
def callable(package_name):
if package_name != package:
return import_string(package_name)
raise ImportError("No module named %s" % (package,))
return callable
@mock.patch('django.conf.settings')
@mock.patch('sentry.utils.settings.import_string')
def validate_dependency(self, key, package, dependency_type, dependency,
setting_value, import_string, settings):
import_string.side_effect = self.raise_import_error(package)
with self.settings(**{key: setting_value}):
with self.assertRaises(ConfigurationError):
validate_settings(django_settings)
def test_validate_fails_on_postgres(self):
self.validate_dependency(*DEPENDENCY_TEST_DATA['postgresql'])
def test_validate_fails_on_mysql(self):
self.validate_dependency(*DEPENDENCY_TEST_DATA['mysql'])
def test_validate_fails_on_oracle(self):
self.validate_dependency(*DEPENDENCY_TEST_DATA['oracle'])
def test_validate_fails_on_memcache(self):
self.validate_dependency(*DEPENDENCY_TEST_DATA['memcache'])
def test_validate_fails_on_pylibmc(self):
self.validate_dependency(*DEPENDENCY_TEST_DATA['pylibmc'])