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_rules.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_rules.py · 5.8 KB · 121 lines
1"""Inbound filters and cron schedules."""
2
3from datetime import datetime, timezone
4
5import pytest
6
7from foxray.core.cron import ScheduleError, add_interval, crontab, next_expected, parse_schedule
8from foxray.core.filters import PatternError, filter_reason, validate_pattern
9from foxray.core.models import FilterRule, Project
10from foxray.core.normalize import normalize_event
11from foxray.core.settings import Settings
12from tests.helpers import make_event
13
14S = Settings()
15UTC = timezone.utc
16
17
18def project(**kw):
19 return Project(id=1, name="p", public_key="k", created_at=datetime.now(UTC), **kw)
20
21
22def rule(kind, pattern, rid=1, **kw):
23 return FilterRule(id=rid, project_id=1, kind=kind, pattern=pattern, is_regex=kw.pop("is_regex", False),
24 environment=kw.pop("environment", ""), release=kw.pop("release", ""),
25 enabled=kw.pop("enabled", True), note="", created_at=datetime.now(UTC))
26
27
28def reason(raw, p=None, rules=()):
29 ev, meta = normalize_event(raw, S)
30 return filter_reason(p or project(), S, list(rules), ev, meta)
31
32
33def test_crawler_filter_by_user_agent():
34 bot = make_event(request={"url": "https://x/", "headers": {"User-Agent": "Mozilla/5.0 (compatible; Googlebot/2.1)"}})
35 human = make_event(request={"url": "https://x/", "headers": {"User-Agent": "Mozilla/5.0 Firefox/120"}})
36 assert reason(bot) == "filter:crawler"
37 assert reason(human) is None
38 assert reason(bot, project(filter_crawlers=False)) is None
39
40
41def test_health_check_filter():
42 assert reason({"message": "x", "transaction": "/healthz"}) == "filter:health-check"
43 assert reason({"message": "x", "request": {"url": "https://x.test/ping/"}}) == "filter:health-check"
44 assert reason({"message": "x", "transaction": "/orders"}) is None
45 assert reason({"message": "x", "transaction": "/up"}, project(health_check_names="/up")) == "filter:health-check"
46
47
48def test_js_noise_is_opt_in():
49 ext = {"platform": "javascript", "exception": {"values": [{"type": "TypeError", "value": "x", "stacktrace":
50 {"frames": [{"abs_path": "chrome-extension://abc/content.js", "function": "f"}]}}]}}
51 local = {"platform": "javascript", "message": "x", "request": {"url": "http://localhost:3000/app"}}
52 assert reason(ext) is None and reason(local) is None
53 on = project(filter_js_noise=True)
54 assert reason(ext, on) == "filter:browser-extension"
55 assert reason(local, on) == "filter:localhost"
56 assert reason({"platform": "python", "message": "x", "request": {"url": "http://localhost/"}}, on) is None
57
58
59def test_rules_by_type_message_environment_release():
60 ev = make_event("django.http.Http404", "No Page matches", environment="development", release="1.0")
61 assert reason(ev, rules=[rule("exception", "Http404")]) == "filter:exception#1"
62 assert reason(ev, rules=[rule("message", r"no page \w+", is_regex=True, rid=2)]) == "filter:message#2"
63 assert reason(ev, rules=[rule("environment", "development", rid=3)]) == "filter:environment#3"
64 assert reason(ev, rules=[rule("release", "1.0", rid=4)]) == "filter:release#4"
65 assert reason(ev, rules=[rule("release", "2.0")]) is None
66 # limited to an environment
67 assert reason(ev, rules=[rule("exception", "Http404", environment="production")]) is None
68 assert reason(ev, rules=[rule("exception", "Http404", environment="development", release="1.0")]) is not None
69 assert reason(ev, rules=[rule("exception", "Http404", enabled=False)]) is None
70
71
72def test_pattern_safety():
73 with pytest.raises(PatternError):
74 validate_pattern("x" * 201, False)
75 with pytest.raises(PatternError):
76 validate_pattern("(a+)+$", True)
77 with pytest.raises(PatternError):
78 validate_pattern("([a-z]*)*", True)
79 with pytest.raises(PatternError):
80 validate_pattern("(unclosed", True)
81 assert validate_pattern(r"Timeout \d+", True)
82 # a bad stored rule never matches (and never raises)
83 assert reason(make_event(), rules=[rule("message", "(a+)+", is_regex=True)]) is None
84
85
86# -- cron ----------------------------------------------------------------
87
88def dt(s):
89 return datetime.fromisoformat(s).replace(tzinfo=UTC)
90
91
92def test_crontab_next_after():
93 assert crontab("0 * * * *").next_after(dt("2026-10-01 10:00:05")) == dt("2026-10-01 11:00:00")
94 assert crontab("*/15 * * * *").next_after(dt("2026-10-01 10:14:59")) == dt("2026-10-01 10:15:00")
95 assert crontab("30 2 * * 1-5").next_after(dt("2026-10-02 03:00:00")) == dt("2026-10-05 02:30:00") # Fri -> Mon
96 assert crontab("0 0 1 jan *").next_after(dt("2026-10-01 00:00:00")) == dt("2027-01-01 00:00:00")
97 assert crontab("@daily").next_after(dt("2026-10-01 00:00:00")) == dt("2026-10-02 00:00:00")
98 assert crontab("0 12 * * sun").next_after(dt("2026-10-01 00:00:00")) == dt("2026-10-04 12:00:00")
99 # day-of-month OR day-of-week when both are restricted
100 assert crontab("0 0 15 * mon").next_after(dt("2026-10-01 00:00:00")) == dt("2026-10-05 00:00:00")
101
102
103def test_crontab_timezone():
104 # 09:00 in Chicago (CDT, UTC-5) on 2026-10-01
105 assert next_expected("crontab", "0 9 * * *", "", "America/Chicago", dt("2026-10-01 12:00:00")) == \
106 dt("2026-10-01 14:00:00")
107
108
109@pytest.mark.parametrize("bad", ["", "* * * *", "61 * * * *", "* * * * mon-xyz", "*/0 * * * *"])
110def test_bad_crontabs(bad):
111 with pytest.raises(ScheduleError):
112 crontab(bad)
113
114
115def test_intervals_and_schedule_parsing():
116 assert add_interval(dt("2026-01-31 00:00:00"), 1, "month") == dt("2026-02-28 00:00:00")
117 assert add_interval(dt("2026-10-01 00:00:00"), 2, "hour") == dt("2026-10-01 02:00:00")
118 assert parse_schedule({"type": "interval", "value": 10, "unit": "minutes"}) == ("interval", "10", "minute")
119 assert parse_schedule({"type": "crontab", "value": "0 * * * *"}) == ("crontab", "0 * * * *", "")
120 with pytest.raises(ScheduleError):
121 parse_schedule({"type": "interval", "value": 0, "unit": "minute"})