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_zero_config.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_zero_config.py · 4.1 KB · 108 lines
1"""The promise: add the app, add one URL line, migrate. An error in a view
2shows up in /admin/ for the superuser, with no other configuration (no DSN,
3no FOXRAY settings, no Foxray users)."""
4
5import os
6import subprocess
7import sys
8import textwrap
9from pathlib import Path
10
11import pytest
12
13ROOT = Path(__file__).resolve().parent.parent
14
15SETTINGS = '''
16SECRET_KEY = "zero-config"
17DEBUG = False
18ALLOWED_HOSTS = ["testserver", "localhost"]
19USE_TZ = True
20INSTALLED_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]
25MIDDLEWARE = [
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]
33ROOT_URLCONF = "site_urls"
34TEMPLATES = [{"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"]}}]
38DATABASES = {"default": {"ENGINE": "django.db.backends.sqlite3", "NAME": "site.sqlite3"}}
39STATIC_URL = "static/"
40DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
41'''
42
43URLS = '''
44from django.contrib import admin
45from django.urls import include, path
46
47
48def compute_total(cart):
49 return sum(cart) / len(cart)
50
51
52def checkout(request):
53 return compute_total([])
54
55
56urlpatterns = [
57 path("admin/", admin.site.urls),
58 path("foxray/", include("foxray.django.urls")), # the one line
59 path("checkout/", checkout),
60]
61'''
62
63FLOW = '''
64import os, re, sys
65os.environ["DJANGO_SETTINGS_MODULE"] = "site_settings"
66import django
67django.setup()
68from django.core.management import call_command
69call_command("migrate", verbosity=0)
70
71from django.contrib.auth.models import User
72from django.test import Client
73import sentry_sdk
74
75_c = sentry_sdk.get_client() if hasattr(sentry_sdk, "get_client") else sentry_sdk.Hub.current.client
76assert _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"
77client = Client(raise_request_exception=False)
78response = client.get("/checkout/")
79assert response.status_code == 500, response.status_code
80sentry_sdk.flush(timeout=10)
81
82admin = User.objects.create_superuser("admin", "admin@example.com", "pw")
83client.force_login(admin)
84index = client.get("/admin/").content.decode()
85assert "Foxray" in index and "Issues" in index and "Storage" in index, "no Foxray section in the admin"
86listing = client.get("/admin/foxray/issue/").content.decode()
87assert "ZeroDivisionError" in listing, listing[:2000]
88issue_id = re.search(r"/admin/foxray/issue/(\\d+)/change/", listing).group(1)
89detail = client.get(f"/admin/foxray/issue/{issue_id}/change/").content.decode()
90assert "compute_total" in detail and "sum(cart) / len(cart)" in detail, "no stack trace"
91assert "/checkout/" in detail
92projects = client.get("/admin/foxray/project/").content.decode()
93assert "default" in projects and "/foxray/1" in projects, "no default project with a DSN"
94print("ZERO-CONFIG OK")
95'''
96
97
98@pytest.mark.slow
99def 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