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.
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_zero_config.py · 4.1 KB · 108 lines
| 1 | """The promise: add the app, add one URL line, migrate. An error in a view |
| 2 | shows up in /admin/ for the superuser, with no other configuration (no DSN, |
| 3 | no FOXRAY settings, no Foxray users).""" |
| 4 | |
| 5 | import os |
| 6 | import subprocess |
| 7 | import sys |
| 8 | import textwrap |
| 9 | from pathlib import Path |
| 10 | |
| 11 | import pytest |
| 12 | |
| 13 | ROOT = Path(__file__).resolve().parent.parent |
| 14 | |
| 15 | SETTINGS = ''' |
| 16 | SECRET_KEY = "zero-config" |
| 17 | DEBUG = False |
| 18 | ALLOWED_HOSTS = ["testserver", "localhost"] |
| 19 | USE_TZ = True |
| 20 | INSTALLED_APPS = [ |
| 21 | "django.contrib.admin", "django.contrib.auth", "django.contrib.contenttypes", |
| 22 | "django.contrib.sessions", "django.contrib.messages", "django.contrib.staticfiles", |
| 23 | "foxray.django", |
| 24 | ] |
| 25 | MIDDLEWARE = [ |
| 26 | "django.middleware.security.SecurityMiddleware", |
| 27 | "django.contrib.sessions.middleware.SessionMiddleware", |
| 28 | "django.middleware.common.CommonMiddleware", |
| 29 | "django.middleware.csrf.CsrfViewMiddleware", |
| 30 | "django.contrib.auth.middleware.AuthenticationMiddleware", |
| 31 | "django.contrib.messages.middleware.MessageMiddleware", |
| 32 | ] |
| 33 | ROOT_URLCONF = "site_urls" |
| 34 | TEMPLATES = [{"BACKEND": "django.template.backends.django.DjangoTemplates", "APP_DIRS": True, |
| 35 | "OPTIONS": {"context_processors": ["django.template.context_processors.request", |
| 36 | "django.contrib.auth.context_processors.auth", |
| 37 | "django.contrib.messages.context_processors.messages"]}}] |
| 38 | DATABASES = {"default": {"ENGINE": "django.db.backends.sqlite3", "NAME": "site.sqlite3"}} |
| 39 | STATIC_URL = "static/" |
| 40 | DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" |
| 41 | ''' |
| 42 | |
| 43 | URLS = ''' |
| 44 | from django.contrib import admin |
| 45 | from django.urls import include, path |
| 46 | |
| 47 | |
| 48 | def compute_total(cart): |
| 49 | return sum(cart) / len(cart) |
| 50 | |
| 51 | |
| 52 | def checkout(request): |
| 53 | return compute_total([]) |
| 54 | |
| 55 | |
| 56 | urlpatterns = [ |
| 57 | path("admin/", admin.site.urls), |
| 58 | path("foxray/", include("foxray.django.urls")), # the one line |
| 59 | path("checkout/", checkout), |
| 60 | ] |
| 61 | ''' |
| 62 | |
| 63 | FLOW = ''' |
| 64 | import os, re, sys |
| 65 | os.environ["DJANGO_SETTINGS_MODULE"] = "site_settings" |
| 66 | import django |
| 67 | django.setup() |
| 68 | from django.core.management import call_command |
| 69 | call_command("migrate", verbosity=0) |
| 70 | |
| 71 | from django.contrib.auth.models import User |
| 72 | from django.test import Client |
| 73 | import sentry_sdk |
| 74 | |
| 75 | _c = sentry_sdk.get_client() if hasattr(sentry_sdk, "get_client") else sentry_sdk.Hub.current.client |
| 76 | assert _c is not None and (_c.is_active() if hasattr(_c, "is_active") else _c.transport is not None), "Foxray did not start the SDK in-process" |
| 77 | client = Client(raise_request_exception=False) |
| 78 | response = client.get("/checkout/") |
| 79 | assert response.status_code == 500, response.status_code |
| 80 | sentry_sdk.flush(timeout=10) |
| 81 | |
| 82 | admin = User.objects.create_superuser("admin", "admin@example.com", "pw") |
| 83 | client.force_login(admin) |
| 84 | index = client.get("/admin/").content.decode() |
| 85 | assert "Foxray" in index and "Issues" in index and "Storage" in index, "no Foxray section in the admin" |
| 86 | listing = client.get("/admin/foxray/issue/").content.decode() |
| 87 | assert "ZeroDivisionError" in listing, listing[:2000] |
| 88 | issue_id = re.search(r"/admin/foxray/issue/(\\d+)/change/", listing).group(1) |
| 89 | detail = client.get(f"/admin/foxray/issue/{issue_id}/change/").content.decode() |
| 90 | assert "compute_total" in detail and "sum(cart) / len(cart)" in detail, "no stack trace" |
| 91 | assert "/checkout/" in detail |
| 92 | projects = client.get("/admin/foxray/project/").content.decode() |
| 93 | assert "default" in projects and "/foxray/1" in projects, "no default project with a DSN" |
| 94 | print("ZERO-CONFIG OK") |
| 95 | ''' |
| 96 | |
| 97 | |
| 98 | @pytest.mark.slow |
| 99 | def test_add_app_migrate_raise_see_it_in_admin(tmp_path): |
| 100 | (tmp_path / "site_settings.py").write_text(textwrap.dedent(SETTINGS)) |
| 101 | (tmp_path / "site_urls.py").write_text(textwrap.dedent(URLS)) |
| 102 | (tmp_path / "flow.py").write_text(textwrap.dedent(FLOW)) |
| 103 | env = {k: v for k, v in os.environ.items() if not k.startswith(("DJANGO_", "FOXRAY_", "SENTRY_"))} |
| 104 | env["PYTHONPATH"] = os.pathsep.join([str(tmp_path), str(ROOT)]) |
| 105 | result = subprocess.run([sys.executable, "flow.py"], cwd=tmp_path, env=env, capture_output=True, text=True, |
| 106 | timeout=120) |
| 107 | assert result.returncode == 0, result.stdout + result.stderr |
| 108 | assert "ZERO-CONFIG OK" in result.stdout |