askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
test_object_rate_limit.py · 4.9 KB · 162 lines
| 1 | from concurrent.futures import ThreadPoolExecutor |
| 2 | |
| 3 | import object_rate_limit |
| 4 | |
| 5 | |
| 6 | def test_rate_limit_blocks_after_configured_limit(tmp_path): |
| 7 | first = object_rate_limit.check_rate_limit( |
| 8 | directory=tmp_path, |
| 9 | identity="ip:203.0.113.10", |
| 10 | limit=2, |
| 11 | window_seconds=60, |
| 12 | now=100.0, |
| 13 | ) |
| 14 | second = object_rate_limit.check_rate_limit( |
| 15 | directory=tmp_path, |
| 16 | identity="ip:203.0.113.10", |
| 17 | limit=2, |
| 18 | window_seconds=60, |
| 19 | now=101.0, |
| 20 | ) |
| 21 | third = object_rate_limit.check_rate_limit( |
| 22 | directory=tmp_path, |
| 23 | identity="ip:203.0.113.10", |
| 24 | limit=2, |
| 25 | window_seconds=60, |
| 26 | now=102.0, |
| 27 | ) |
| 28 | |
| 29 | assert first.allowed is True |
| 30 | assert first.remaining == 1 |
| 31 | assert second.allowed is True |
| 32 | assert second.remaining == 0 |
| 33 | assert third.allowed is False |
| 34 | assert third.retry_after == 58 |
| 35 | |
| 36 | |
| 37 | def test_rate_limit_prunes_expired_entries(tmp_path): |
| 38 | first = object_rate_limit.check_rate_limit( |
| 39 | directory=tmp_path, |
| 40 | identity="ip:203.0.113.10", |
| 41 | limit=1, |
| 42 | window_seconds=10, |
| 43 | now=100.0, |
| 44 | ) |
| 45 | second = object_rate_limit.check_rate_limit( |
| 46 | directory=tmp_path, |
| 47 | identity="ip:203.0.113.10", |
| 48 | limit=1, |
| 49 | window_seconds=10, |
| 50 | now=111.0, |
| 51 | ) |
| 52 | |
| 53 | assert first.allowed is True |
| 54 | assert second.allowed is True |
| 55 | assert second.remaining == 0 |
| 56 | |
| 57 | |
| 58 | def test_rate_limit_keeps_identities_separate(tmp_path): |
| 59 | first = object_rate_limit.check_rate_limit( |
| 60 | directory=tmp_path, |
| 61 | identity="ip:203.0.113.10", |
| 62 | limit=1, |
| 63 | window_seconds=60, |
| 64 | now=100.0, |
| 65 | ) |
| 66 | second = object_rate_limit.check_rate_limit( |
| 67 | directory=tmp_path, |
| 68 | identity="ip:203.0.113.11", |
| 69 | limit=1, |
| 70 | window_seconds=60, |
| 71 | now=101.0, |
| 72 | ) |
| 73 | |
| 74 | assert first.allowed is True |
| 75 | assert second.allowed is True |
| 76 | |
| 77 | |
| 78 | def test_disabled_rate_limit_does_not_create_storage(tmp_path): |
| 79 | result = object_rate_limit.check_rate_limit( |
| 80 | directory=tmp_path / "ratelimit", |
| 81 | identity="ip:203.0.113.10", |
| 82 | limit=0, |
| 83 | window_seconds=60, |
| 84 | now=100.0, |
| 85 | ) |
| 86 | |
| 87 | assert result.allowed is True |
| 88 | assert not (tmp_path / "ratelimit").exists() |
| 89 | |
| 90 | |
| 91 | def test_rate_limit_file_is_safe_under_concurrent_access(tmp_path): |
| 92 | def check(_): |
| 93 | return object_rate_limit.check_rate_limit( |
| 94 | directory=tmp_path, |
| 95 | identity="ip:203.0.113.10", |
| 96 | limit=1000, |
| 97 | window_seconds=60, |
| 98 | ) |
| 99 | |
| 100 | with ThreadPoolExecutor(max_workers=10) as pool: |
| 101 | results = list(pool.map(check, range(50))) |
| 102 | |
| 103 | assert all(result.allowed for result in results) |
| 104 | bucket_files = [ |
| 105 | path |
| 106 | for path in tmp_path.iterdir() |
| 107 | if path.name.endswith(".txt") and not path.name.endswith(".lock") |
| 108 | ] |
| 109 | assert len(bucket_files) == 1 |
| 110 | timestamps = bucket_files[0].read_text().splitlines() |
| 111 | assert len(timestamps) == 50 |
| 112 | for timestamp in timestamps: |
| 113 | float(timestamp) |
| 114 | |
| 115 | |
| 116 | def test_degraded_fails_open_by_default_but_flags_it(tmp_path): |
| 117 | """When storage is unusable, the GLOBAL limiter (fail_closed=False, the |
| 118 | default) must stay up -- a broken ratelimit dir cannot take the whole |
| 119 | site down -- but the result is flagged `degraded` so the caller can log |
| 120 | it. It must NOT silently return a normal allow.""" |
| 121 | # A file where the ratelimit directory is expected -> mkdir/open raises |
| 122 | # OSError inside check_rate_limit. |
| 123 | blocker = tmp_path / "ratelimit" |
| 124 | blocker.write_text("not a directory") |
| 125 | |
| 126 | result = object_rate_limit.check_rate_limit( |
| 127 | directory=blocker, identity="visitor", limit=5, window_seconds=60 |
| 128 | ) |
| 129 | assert result.degraded is True |
| 130 | assert result.allowed is True # fail-open default keeps the site up |
| 131 | |
| 132 | |
| 133 | def test_degraded_fails_closed_when_requested(tmp_path): |
| 134 | """A public-write surface passes fail_closed=True: when the abuse counter |
| 135 | is unavailable, deny rather than admit an unmetered flood.""" |
| 136 | blocker = tmp_path / "ratelimit" |
| 137 | blocker.write_text("not a directory") |
| 138 | |
| 139 | result = object_rate_limit.check_rate_limit( |
| 140 | directory=blocker, identity="visitor", limit=5, window_seconds=60, |
| 141 | fail_closed=True, |
| 142 | ) |
| 143 | assert result.degraded is True |
| 144 | assert result.allowed is False # fail-closed denies the write |
| 145 | |
| 146 | |
| 147 | def test_read_error_does_not_masquerade_as_empty_bucket(tmp_path): |
| 148 | """A genuine OSError reading the bucket must surface as degraded, not as |
| 149 | an empty timestamp list (which would silently fail open regardless of |
| 150 | posture).""" |
| 151 | rate_dir = tmp_path / "ratelimit" |
| 152 | rate_dir.mkdir() |
| 153 | # Make the bucket a directory so read_text() raises OSError (IsADirectory). |
| 154 | key = __import__("hashlib").sha256(b"visitor").hexdigest()[:32] |
| 155 | (rate_dir / f"{key}.txt").mkdir() |
| 156 | |
| 157 | result = object_rate_limit.check_rate_limit( |
| 158 | directory=rate_dir, identity="visitor", limit=5, window_seconds=60, |
| 159 | fail_closed=True, |
| 160 | ) |
| 161 | assert result.degraded is True |
| 162 | assert result.allowed is False |