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_protocol.py · 5.2 KB · 136 lines
| 1 | """DSNs, auth, envelopes and bodies.""" |
| 2 | |
| 3 | import base64 |
| 4 | import gzip |
| 5 | import json |
| 6 | import zlib |
| 7 | |
| 8 | import pytest |
| 9 | |
| 10 | from foxray.core.auth import extract_public_key, key_matches, parse_auth_header |
| 11 | from foxray.core.dsn import DsnError, make_dsn, parse_dsn |
| 12 | from foxray.core.envelope import PayloadError, decode_body, parse_envelope, parse_store_body |
| 13 | from tests.helpers import envelope |
| 14 | |
| 15 | |
| 16 | def test_parse_dsn_with_path_prefix(): |
| 17 | d = parse_dsn("https://abc123@example.com/foxray/42") |
| 18 | assert (d.public_key, d.host, d.path, d.project_id) == ("abc123", "example.com", "/foxray", "42") |
| 19 | assert d.endpoint() == "https://example.com/foxray/api/42/envelope/" |
| 20 | assert str(d) == "https://abc123@example.com/foxray/42" |
| 21 | |
| 22 | |
| 23 | def test_parse_dsn_with_port_and_secret(): |
| 24 | d = parse_dsn("http://pub:sec@127.0.0.1:9000/7") |
| 25 | assert (d.secret_key, d.port, d.path, d.project_id) == ("sec", 9000, "", "7") |
| 26 | assert d.endpoint("store") == "http://127.0.0.1:9000/api/7/store/" |
| 27 | |
| 28 | |
| 29 | @pytest.mark.parametrize("bad", ["", "ftp://k@h/1", "https://h/1", "https://k@h/"]) |
| 30 | def test_bad_dsns(bad): |
| 31 | with pytest.raises(DsnError): |
| 32 | parse_dsn(bad) |
| 33 | |
| 34 | |
| 35 | def test_make_dsn_round_trip(): |
| 36 | assert make_dsn("https://askrobots.com/foxray/", "k", 3) == "https://k@askrobots.com/foxray/3" |
| 37 | assert make_dsn("http://127.0.0.1:8765", "k", 1) == "http://k@127.0.0.1:8765/1" |
| 38 | assert parse_dsn(make_dsn("https://h.example/x/y", "k", 9)).endpoint() == "https://h.example/x/y/api/9/envelope/" |
| 39 | |
| 40 | |
| 41 | def test_auth_header_parsing(): |
| 42 | h = parse_auth_header("Sentry sentry_key=abc, sentry_version=7, sentry_client=sentry.python/2.0") |
| 43 | assert h == {"sentry_key": "abc", "sentry_version": "7", "sentry_client": "sentry.python/2.0"} |
| 44 | |
| 45 | |
| 46 | def test_key_sources(): |
| 47 | assert extract_public_key({"x-sentry-auth": "Sentry sentry_key=a"}, {}) == "a" |
| 48 | assert extract_public_key({"authorization": "Sentry sentry_key=b, sentry_version=7"}, {}) == "b" |
| 49 | assert extract_public_key({}, {"sentry_key": "c", "sentry_version": "7"}) == "c" |
| 50 | assert extract_public_key({}, {}, {"dsn": "https://d@h/1"}) == "d" |
| 51 | assert extract_public_key({"authorization": "Bearer x"}, {}) is None |
| 52 | assert key_matches("abc", "abc") and not key_matches("abd", "abc") and not key_matches(None, "abc") |
| 53 | |
| 54 | |
| 55 | def test_envelope_with_lengths_multi_item(): |
| 56 | body = envelope(({"type": "event"}, {"message": "a\nb"}), ({"type": "attachment"}, b"\x00\x01\n\x02"), |
| 57 | ({"type": "check_in"}, {"monitor_slug": "m"}), header={"event_id": "e" * 32}) |
| 58 | header, items = parse_envelope(body) |
| 59 | assert header["event_id"] == "e" * 32 |
| 60 | assert [i.type for i in items] == ["event", "attachment", "check_in"] |
| 61 | assert items[0].json()["message"] == "a\nb" |
| 62 | assert items[1].payload == b"\x00\x01\n\x02" |
| 63 | |
| 64 | |
| 65 | def test_envelope_without_lengths_and_without_trailing_newline(): |
| 66 | body = b'{}\n{"type":"event"}\n{"message":"x"}\n{"type":"session"}\n{"sid":"1"}' |
| 67 | _, items = parse_envelope(body) |
| 68 | assert [i.type for i in items] == ["event", "session"] |
| 69 | assert items[1].json() == {"sid": "1"} |
| 70 | |
| 71 | |
| 72 | def test_envelope_empty_payload_and_blank_header(): |
| 73 | _, items = parse_envelope(b'\n{"type":"client_report","length":0}\n\n') |
| 74 | assert items[0].type == "client_report" and items[0].payload == b"" |
| 75 | |
| 76 | |
| 77 | @pytest.mark.parametrize("body", [b"not json\n", b'{}\n{"type":"event","length":99}\n{}', b'{}\n[1]\n{}', |
| 78 | b'{}\n{"type":"event","length":-1}\n']) |
| 79 | def test_bad_envelopes(body): |
| 80 | with pytest.raises(PayloadError) as e: |
| 81 | parse_envelope(body) |
| 82 | assert e.value.status == 400 |
| 83 | |
| 84 | |
| 85 | def test_too_many_items(): |
| 86 | body = envelope(*[({"type": "session"}, {}) for _ in range(5)]) |
| 87 | with pytest.raises(PayloadError) as e: |
| 88 | parse_envelope(body, max_items=3) |
| 89 | assert e.value.status == 413 |
| 90 | |
| 91 | |
| 92 | def test_decode_gzip_deflate_identity(): |
| 93 | raw = b'{"hello": "world"}' * 10 |
| 94 | assert decode_body(gzip.compress(raw), "gzip", 10_000) == raw |
| 95 | assert decode_body(zlib.compress(raw), "deflate", 10_000) == raw |
| 96 | co = zlib.compressobj(wbits=-15) |
| 97 | assert decode_body(co.compress(raw) + co.flush(), "deflate", 10_000) == raw |
| 98 | assert decode_body(raw, None, 10_000) == raw |
| 99 | assert decode_body(raw, "identity", 10_000) == raw |
| 100 | |
| 101 | |
| 102 | def test_unknown_encoding_is_415(): |
| 103 | with pytest.raises(PayloadError) as e: |
| 104 | decode_body(b"x", "zstd", 100) |
| 105 | assert e.value.status == 415 |
| 106 | |
| 107 | |
| 108 | def test_decompression_bomb_is_413(): |
| 109 | bomb = gzip.compress(b"0" * 5_000_000) |
| 110 | with pytest.raises(PayloadError) as e: |
| 111 | decode_body(bomb, "gzip", 1_000_000) |
| 112 | assert e.value.status == 413 |
| 113 | |
| 114 | |
| 115 | def test_corrupt_gzip_is_400(): |
| 116 | with pytest.raises(PayloadError) as e: |
| 117 | decode_body(b"\x1f\x8bnope", "gzip", 100) |
| 118 | assert e.value.status == 400 |
| 119 | |
| 120 | |
| 121 | def test_store_body_plain_and_old_base64_zlib(): |
| 122 | ev = {"message": "hi"} |
| 123 | assert parse_store_body(json.dumps(ev).encode(), 1000) == ev |
| 124 | old = base64.b64encode(zlib.compress(json.dumps(ev).encode())) |
| 125 | assert parse_store_body(old, 1000) == ev |
| 126 | with pytest.raises(PayloadError): |
| 127 | parse_store_body(b"[]", 1000) |
| 128 | |
| 129 | |
| 130 | def test_brotli_when_installed(): |
| 131 | brotli = pytest.importorskip("brotli") |
| 132 | raw = b'{"x": 1}' * 100 |
| 133 | assert decode_body(brotli.compress(raw), "br", 10_000) == raw |
| 134 | with pytest.raises(PayloadError) as e: |
| 135 | decode_body(brotli.compress(b"0" * 100_000), "br", 1000) |
| 136 | assert e.value.status == 413 |