Files
Anthias/tests/test_sentry.py
Viktor Petersson e211ec848e feat(sentry): add Sentry error tracking to the Django services (#3007)
* feat(sentry): add Sentry error tracking to the Django services

Adds sentry-sdk to the server and viewer dependency groups (plus the
mypy group, since that CI job imports settings) and initialises it in
the shared Django settings module, so anthias-server, anthias-celery,
and the viewer all report crashes.

- The DSN can be overridden via the SENTRY_DSN env var (pointing at
  an operator's own Sentry project), or set to an empty string to
  disable crash reporting entirely.
- environment= mirrors the ENVIRONMENT env var (production default)
  so dev/CI events are filterable in Sentry.
- release= comes from pyproject.toml's [project].version via the
  existing get_anthias_release() helper.
- send_default_pii is gated on the existing analytics_opt_out knob in
  anthias.conf — opted-out devices still report crashes, but without
  request headers / IPs / user data.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(sentry): don't send events from test runs by default

The unit suite is built to run with no external network dependencies
(conftest.py force-mocks Redis for the same reason), and exceptions
raised on purpose by failing tests must not land in the production
Sentry project. Default the DSN to empty under ENVIRONMENT=test or
pytest (reusing the existing argv detector, moved up from the
DATABASES section); an explicit SENTRY_DSN still wins so the
integration stack can opt in deliberately.

Addresses the Copilot review comment on the hardcoded default DSN.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* test(sentry): pin the no-events-under-pytest guarantee

Asserts the client has no DSN, builds no transport, and drops
capture calls when running under pytest, so a future settings.py
refactor can't silently re-enable sending from test runs.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-07 07:41:46 +02:00

22 lines
870 B
Python

"""Regression guard for the Sentry test-mode gate in settings.py.
The unit suite must run with no external network dependencies
(conftest.py force-mocks Redis for the same reason), and exceptions
raised on purpose by failing tests must never reach the production
Sentry project. settings.py defaults the DSN to '' whenever
ENVIRONMENT=test or pytest is detected on argv — this test pins that
behaviour: under pytest the client has no DSN, builds no transport,
and capture calls are dropped.
"""
import sentry_sdk
def test_sentry_does_not_send_under_pytest() -> None:
client = sentry_sdk.get_client()
assert not client.dsn
assert client.transport is None
# capture_message returns the event id when an event is queued
# for sending; None means the event was dropped client-side.
assert sentry_sdk.capture_message('must not be sent') is None