askrobots / foxray Public

Foxray: a light error tracker for the Sentry SDKs. Runs inside Django (the admin is the UI) or on its own; SQLite or Postgres, nothing else. Not affiliated with Sentry.

0 downloads

foxray/tests/test_django.py
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/foxray.git
HTTPS
https://gitstable.com/git/askrobots/foxray.git
SSH
ssh://git@gitstable.com:2222/askrobots/foxray.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 0.1.2: the first PyPI release (v0.1.1 is already tagged at 05cc354, before the PyPI metadata) 58507e3 4 weeks ago History
test_django.py · 16.3 KB · 357 lines
1"""Foxray as a Django app: ingest views, the in-process SDK transport, the
2real SDK over HTTP, the admin pages, permissions, email alerts, commands."""
3
4import gzip
5import io
6import json
7import os
8import subprocess
9import sys
10import uuid
11from pathlib import Path
12
13import pytest
14
15from tests.sdk_compat import sdk_active, sdk_client
16from django.contrib.auth.models import Permission, User
17from django.core import mail
18from django.core.management import call_command
19from django.test import Client
20
21from foxray.django.runtime import runtime
22from tests.helpers import envelope, make_event
23from tests.sdk_checks import check_sdk_run
24
25ROOT = Path(__file__).resolve().parent.parent
26pytestmark = pytest.mark.django_db(transaction=True)
27
28
29@pytest.fixture
30def rt():
31 return runtime()
32
33
34@pytest.fixture
35def project(rt):
36 return rt.store.create_project("web")
37
38
39@pytest.fixture
40def admin_client():
41 user = User.objects.create_superuser("root", "root@example.com", "pw-for-tests")
42 c = Client()
43 c.force_login(user)
44 return c
45
46
47def post_envelope(client, project, body, **headers):
48 return client.post(f"/foxray/api/{project.id}/envelope/", data=body, content_type="application/x-sentry-envelope",
49 HTTP_X_SENTRY_AUTH=f"Sentry sentry_key={project.public_key}, sentry_version=7", **headers)
50
51
52def test_ingest_view_is_csrf_exempt_and_gzip(project, rt):
53 client = Client(enforce_csrf_checks=True)
54 ev = make_event()
55 r = post_envelope(client, project, gzip.compress(envelope(({"type": "event"}, ev))), HTTP_CONTENT_ENCODING="gzip")
56 assert r.status_code == 200 and r.json() == {"id": ev["event_id"]}
57 assert r["Access-Control-Allow-Origin"] == "*"
58 issue = rt.store.list_issues()[0][0]
59 assert issue.title == "ValueError: boom" and issue.times_seen == 1
60
61
62def test_ingest_view_errors(project, rt):
63 c = Client()
64 body = envelope(({"type": "event"}, make_event()))
65 assert c.post(f"/foxray/api/{project.id}/envelope/", data=body, content_type="text/plain").status_code == 401
66 assert post_envelope(c, project, b"x", HTTP_CONTENT_ENCODING="zstd").status_code == 415
67 assert c.post("/foxray/api/999/envelope/", data=body, content_type="text/plain").status_code == 404
68 assert c.get(f"/foxray/api/{project.id}/envelope/").status_code == 405
69 assert c.options(f"/foxray/api/{project.id}/envelope/").status_code == 200
70 rt.settings.max_body_bytes = 100
71 try:
72 assert post_envelope(c, project, b"x" * 500).status_code == 413
73 finally:
74 rt.settings.max_body_bytes = 10 * 1024 * 1024
75 r = c.post(f"/foxray/api/{project.id}/store/?sentry_key={project.public_key}",
76 data=json.dumps({"message": "legacy"}), content_type="application/json")
77 assert r.status_code == 200
78
79
80def test_rate_limit_through_django(project, rt):
81 rt.store.update_project(project.id, rate_limit=2)
82 c = Client()
83 codes = [post_envelope(c, project, envelope(({"type": "event"}, make_event()))).status_code for _ in range(4)]
84 assert codes == [200, 200, 429, 429]
85 r = post_envelope(c, project, envelope(({"type": "event"}, make_event())))
86 assert r["X-Sentry-Rate-Limits"].endswith(":error;default;security:project") and int(r["Retry-After"]) > 0
87
88
89def test_email_alerts_go_to_admins_then_superusers_then_override(project, rt, settings):
90 c = Client()
91 post_envelope(c, project, envelope(({"type": "event"}, make_event("First"))))
92 assert len(mail.outbox) == 1 and mail.outbox[0].to == ["ops@example.com"]
93 assert mail.outbox[0].subject.startswith("[Foxray] New issue: First")
94 assert "/admin/foxray/issue/" in mail.outbox[0].body
95 settings.ADMINS = []
96 User.objects.create_superuser("boss", "boss@example.com", "x")
97 post_envelope(c, project, envelope(({"type": "event"}, make_event("Second"))))
98 assert mail.outbox[-1].to == ["boss@example.com"]
99 settings.FOXRAY = {**settings.FOXRAY, "ALERT_EMAILS": ["oncall@example.com"]}
100 rt = runtime()
101 post_envelope(c, project, envelope(({"type": "event"}, make_event("Third"))))
102 assert mail.outbox[-1].to == ["oncall@example.com"]
103 # repeats never alert
104 post_envelope(c, project, envelope(({"type": "event"}, make_event("Third"))))
105 assert len(mail.outbox) == 3
106 assert all(a.status == "sent" for a in rt.store.list_alerts())
107
108
109def test_in_process_transport_needs_no_dsn(rt):
110 import sentry_sdk
111
112 from foxray.django import sdk
113 sdk.init(release="app@9")
114 try:
115 assert sdk_active()
116 try:
117 {}["nope"]
118 except KeyError:
119 event_id = sentry_sdk.capture_exception()
120 sentry_sdk.capture_message("hello from inside")
121 sentry_sdk.flush(timeout=10)
122 finally:
123 sdk_client().close(timeout=5) # stops the transport thread
124 sentry_sdk.init() # back to an inactive client
125 default = rt.store.get_project_by_name("default")
126 ev = rt.store.find_event(default.id, event_id)
127 assert ev is not None and ev.payload["release"] == "app@9"
128 titles = {i.title for i in rt.store.list_issues()[0]}
129 assert {"KeyError: 'nope'", "hello from inside"} <= titles
130
131
132def test_in_process_transport_never_raises(caplog):
133 from foxray.django.sdk import FoxrayTransport
134
135 def broken(data):
136 raise RuntimeError("database is down")
137 t = FoxrayTransport(deliver_fn=broken)
138
139 class Env:
140 def serialize(self):
141 return b'{}\n{"type":"event"}\n{"message":"lost"}\n'
142 t.capture_envelope(Env())
143 t.flush(5)
144 assert any(r.getMessage() == "Foxray transport failed" for r in caplog.records)
145
146
147def test_deliver_falls_back_to_logging(caplog, monkeypatch):
148 from foxray.django import sdk
149 from foxray.django import runtime as rt_mod
150
151 def boom():
152 raise RuntimeError("no database")
153 monkeypatch.setattr(rt_mod, "runtime", boom)
154 sdk.deliver(b'{}\n{"type":"event"}\n{"exception":{"values":[{"type":"ValueError","value":"x"}]}}\n')
155 assert any("could not store an event" in r.getMessage() and "ValueError: x" in r.getMessage()
156 for r in caplog.records)
157
158
159
160def _sdk_major():
161 import sentry_sdk
162 return int(sentry_sdk.VERSION.split(".")[0])
163
164
165@pytest.mark.skipif(_sdk_major() < 2, reason="sdk_client.py uses crons and new_scope (2.x); "
166 "test_sdk_any_version covers 1.x over HTTP")
167def test_real_sentry_sdk_over_http_to_django(live_server, rt):
168 project = rt.store.create_project("shop")
169 host = live_server.url.split("//")[1]
170 dsn = f"http://{project.public_key}@{host}/foxray/{project.id}"
171 env = {**os.environ, "PYTHONPATH": str(ROOT)}
172 env.pop("DJANGO_SETTINGS_MODULE", None)
173 out = subprocess.run([sys.executable, str(ROOT / "tests" / "sdk_client.py"), dsn], cwd=ROOT, env=env,
174 capture_output=True, text=True, timeout=60, check=True).stdout
175 ids = json.loads(out.strip().splitlines()[-1])
176 check_sdk_run(rt.store, project, ids)
177
178
179def test_admin_pages(admin_client, project, rt):
180 c = Client()
181 for i in range(3):
182 post_envelope(c, project, envelope(({"type": "event"}, make_event(environment="production", release="1.0"))))
183 post_envelope(c, project, envelope(({"type": "event"}, make_event("TypeError", "other", level="warning"))))
184 post_envelope(c, project, envelope(({"type": "check_in"}, {"monitor_slug": "nightly", "status": "ok",
185 "monitor_config": {"schedule": {"type": "crontab", "value": "0 3 * * *"}}})))
186 rt.store.add_filter(project.id, "environment", "development")
187 r = admin_client.get("/admin/")
188 assert r.status_code == 200
189 page = r.content.decode()
190 assert "Foxray" in page
191 for name in ("Issues", "Events", "Monitors", "Check-ins", "Filters", "Projects", "Alerts", "Storage"):
192 assert name in page, name
193 r = admin_client.get("/admin/foxray/issue/")
194 assert r.status_code == 200 and "ValueError: boom" in r.content.decode()
195 for q in ("?status=all", "?level__exact=warning", "?environment=production", "?release=1.0", "?q=boom",
196 "?o=4", "?status=resolved"):
197 assert admin_client.get("/admin/foxray/issue/" + q).status_code == 200, q
198 assert "TypeError" not in admin_client.get("/admin/foxray/issue/?environment=production").content.decode()
199 issue = [i for i in rt.store.list_issues()[0] if i.title == "ValueError: boom"][0]
200 r = admin_client.get(f"/admin/foxray/issue/{issue.id}/change/")
201 detail = r.content.decode()
202 assert r.status_code == 200 and "app.views in index" in detail and "raise ValueError" in detail
203 assert "production" in detail and "Last 30 days" in detail
204 # actions on the detail page
205 r = admin_client.post(f"/admin/foxray/issue/{issue.id}/change/", {"action": "ignore_count", "count": "10"})
206 assert r.status_code == 302 and rt.store.get_issue(issue.id).ignore_until_count == 13
207 admin_client.post(f"/admin/foxray/issue/{issue.id}/change/", {"action": "reopen"})
208 admin_client.post(f"/admin/foxray/issue/{issue.id}/change/", {"action": "ignore_until", "until": "2030-01-01"})
209 assert rt.store.get_issue(issue.id).ignore_until.year == 2030
210 # bulk actions on the list
211 r = admin_client.post("/admin/foxray/issue/?status=all", {"action": "resolve", "_selected_action": [issue.id]})
212 assert r.status_code == 302 and rt.store.get_issue(issue.id).status == "resolved"
213 admin_client.post("/admin/foxray/issue/?status=all", {"action": "reopen", "_selected_action": [issue.id]})
214 assert rt.store.get_issue(issue.id).status == "unresolved"
215 # the other pages
216 for url in ("/admin/foxray/event/", "/admin/foxray/monitor/", "/admin/foxray/checkin/", "/admin/foxray/filterrule/",
217 "/admin/foxray/project/", f"/admin/foxray/project/{project.id}/change/", "/admin/foxray/alert/",
218 "/admin/foxray/storage/", "/foxray/"):
219 r = admin_client.get(url, follow=True)
220 assert r.status_code == 200, url
221 page = admin_client.get(f"/admin/foxray/project/{project.id}/change/").content.decode()
222 assert f"http://{project.public_key}@testserver/foxray/{project.id}" in page
223 ev = rt.store.latest_event(issue.id)
224 assert admin_client.get(f"/admin/foxray/event/{ev.id}/change/").status_code == 200
225 mon = rt.store.get_monitor(project.id, "nightly")
226 assert "cron 0 3 * * *" in admin_client.get(f"/admin/foxray/monitor/{mon.id}/change/").content.decode()
227 storage = admin_client.get("/admin/foxray/storage/").content.decode()
228 assert "foxray_event" in storage and "Clean up now" in storage
229 r = admin_client.post("/admin/foxray/storage/", follow=True)
230 assert "Cleanup removed" in r.content.decode() or "already running" in r.content.decode()
231 # a bad regex is refused by the filter form
232 r = admin_client.post("/admin/foxray/filterrule/add/", {"project": project.id, "kind": "message",
233 "pattern": "(a+)+", "is_regex": "on", "enabled": "on"})
234 assert r.status_code == 200 and "nested repeats" in r.content.decode()
235
236
237def test_permissions_use_django_auth(project, rt):
238 c = Client()
239 post_envelope(c, project, envelope(({"type": "event"}, make_event())))
240 issue = rt.store.list_issues()[0][0]
241 staff = User.objects.create_user("staff", "s@example.com", "x", is_staff=True)
242 c.force_login(staff)
243 assert c.get("/admin/foxray/issue/").status_code == 403
244 staff.user_permissions.add(Permission.objects.get(codename="view_issue"))
245 staff = User.objects.get(pk=staff.pk)
246 c.force_login(staff)
247 assert c.get("/admin/foxray/issue/").status_code == 200
248 assert c.get(f"/admin/foxray/issue/{issue.id}/change/").status_code == 200
249 assert c.post(f"/admin/foxray/issue/{issue.id}/change/", {"action": "resolve"}).status_code == 403
250 staff.user_permissions.add(Permission.objects.get(codename="change_issue"))
251 c.force_login(User.objects.get(pk=staff.pk))
252 assert c.post(f"/admin/foxray/issue/{issue.id}/change/", {"action": "resolve"}).status_code == 302
253 assert rt.store.get_issue(issue.id).status == "resolved"
254 anon = Client()
255 assert anon.get("/admin/foxray/issue/").status_code == 302 # to the admin login
256
257
258def test_management_commands(project, rt):
259 out = io.StringIO()
260 call_command("foxray_create_project", "webhive", stdout=out)
261 dsn = out.getvalue().strip()
262 assert dsn.startswith("http://") and "@testserver/foxray/" in dsn
263 out = io.StringIO()
264 call_command("foxray_tick", "--force-cleanup", stdout=out)
265 assert "monitor_alerts" in out.getvalue()
266 out = io.StringIO()
267 call_command("foxray_prune", stdout=out)
268 assert '"mode"' in out.getvalue()
269
270
271def test_ingest_never_raises_into_django(project, rt, monkeypatch):
272 def boom(*a, **k):
273 raise RuntimeError("store exploded")
274 monkeypatch.setattr(rt.ingestor, "_process", boom)
275 r = post_envelope(Client(), project, envelope(({"type": "event"}, make_event())))
276 assert r.status_code == 500 and r.json() == {"detail": "internal error"}
277
278
279def test_uuid_project_ids_rejected_cleanly(project):
280 r = Client().post(f"/foxray/api/{uuid.uuid4().hex}/envelope/", data=b"{}", content_type="text/plain")
281 assert r.status_code == 404
282
283
284def test_env_vars_fill_in_settings(monkeypatch, settings):
285 from foxray.django.conf import conf, core_settings
286 monkeypatch.setenv("FOXRAY_MAX_DB_MB", "123")
287 monkeypatch.setenv("FOXRAY_ALERT_EMAILS", "a@example.com, b@example.com")
288 monkeypatch.setenv("FOXRAY_BASE_URL", "https://from-env")
289 monkeypatch.setenv("FOXRAY_AUTO_INIT", "0")
290 assert core_settings().max_db_mb == 123
291 assert conf()["ALERT_EMAILS"] == ["a@example.com", "b@example.com"]
292 assert conf()["AUTO_INIT"] is False
293 assert core_settings().base_url == "http://testserver" # settings.FOXRAY wins
294
295
296def test_auto_init_and_a_left_over_sentry_dsn(monkeypatch, settings):
297 from foxray.django import sdk
298 started = []
299 monkeypatch.setattr(sdk, "init", lambda **kw: started.append(kw))
300 monkeypatch.setattr(sdk, "_running_tests", lambda: False)
301 monkeypatch.setattr(sdk, "_sdk_active", lambda: False)
302 monkeypatch.setenv("SENTRY_DSN", "https://k@old-sentry.example/1")
303 settings.FOXRAY = {}
304 assert sdk.auto_init() is False # SENTRY_DSN: probably a real Sentry
305 settings.FOXRAY = {"AUTO_INIT": True}
306 assert sdk.auto_init() is True # asked for: the left-over DSN doesn't stop it
307 monkeypatch.setattr(sdk, "_sdk_active", lambda: True)
308 assert sdk.auto_init() is False # a real Sentry SDK already running wins
309 assert len(started) == 1
310
311
312def test_admins_as_pairs_or_plain_addresses(project, settings):
313 from foxray.django.runtime import recipients
314 settings.ADMINS = [("Ops", "ops@example.com")] # before Django 6.0
315 assert recipients(project) == ["ops@example.com"]
316 settings.ADMINS = ["ops@example.com"] # Django 6.0 on
317 assert recipients(project) == ["ops@example.com"]
318
319
320def test_alert_sender(settings):
321 from foxray.django.runtime import sender_email
322 assert sender_email() == "foxray@example.com" # the site's SERVER_EMAIL
323 settings.SERVER_EMAIL = "root@localhost" # Django's default: no mailbox
324 settings.DEFAULT_FROM_EMAIL = "Shop <hello@shop.example>"
325 assert sender_email() == "Shop <hello@shop.example>"
326 settings.FOXRAY = {**settings.FOXRAY, "FROM_EMAIL": "errors@shop.example"}
327 assert sender_email() == "errors@shop.example"
328
329
330class MailBackendThatLogsAFailure:
331 """Like a site's own SMTP backend: a failed send is logged, not raised."""
332 def __init__(self, *args, **kwargs):
333 pass
334
335 def send_messages(self, messages):
336 import logging
337 logging.getLogger("shop.mail").error("SMTP login failed: 535 authentication failed")
338 return 0
339
340
341def test_a_failing_alert_email_is_not_stored_as_an_error(project, rt, settings, caplog):
342 """An alert email whose sending fails (and gets logged) must not become a new issue, whose
343 alert fails again, and so on."""
344 import sentry_sdk
345
346 from foxray.django import sdk
347 settings.EMAIL_BACKEND = "tests.test_django.MailBackendThatLogsAFailure"
348 sdk.init()
349 try:
350 post_envelope(Client(), project, envelope(({"type": "event"}, make_event("Checkout broke"))))
351 sentry_sdk.flush(timeout=10)
352 finally:
353 sdk_client().close(timeout=5)
354 sentry_sdk.init()
355 issues = rt.store.list_issues()[0]
356 assert [i.title for i in issues] == ["Checkout broke: boom"]
357 assert any("dropped an error raised inside Foxray" in r.getMessage() for r in caplog.records)