Skip to content

Commit 976f0c2

Browse files
committed
Add hypothesis settings to all test cases for increased example coverage
1 parent 009af7b commit 976f0c2

File tree

5 files changed

+206
-9
lines changed

5 files changed

+206
-9
lines changed

tests/test_hypothesis_auth.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import re
1010

1111
import pytest
12-
from hypothesis import assume, given
12+
from hypothesis import assume, given, settings
1313
from hypothesis import strategies as st
1414

1515
from requests.auth import HTTPBasicAuth, HTTPDigestAuth, HTTPProxyAuth, _basic_auth_str
@@ -29,13 +29,15 @@
2929
class TestBasicAuthStrProperties:
3030
"""Property-based tests for _basic_auth_str function."""
3131

32+
@settings(max_examples=1000, deadline=None)
3233
@given(safe_text, safe_text)
3334
def test_basic_auth_str_format(self, username: str, password: str) -> None:
3435
"""_basic_auth_str should return properly formatted Basic auth string."""
3536
result = _basic_auth_str(username, password)
3637
assert isinstance(result, str)
3738
assert result.startswith("Basic ")
3839

40+
@settings(max_examples=1000, deadline=None)
3941
@given(safe_text, safe_text)
4042
def test_basic_auth_str_base64_decodable(self, username: str, password: str) -> None:
4143
"""_basic_auth_str should produce valid base64 encoding."""
@@ -51,6 +53,7 @@ def test_basic_auth_str_base64_decodable(self, username: str, password: str) ->
5153
# If decoding fails, test fails
5254
pytest.fail("Failed to decode base64")
5355

56+
@settings(max_examples=1000, deadline=None)
5457
@given(safe_text, safe_text)
5558
def test_basic_auth_str_contains_credentials(self, username: str, password: str) -> None:
5659
"""_basic_auth_str should encode username and password."""
@@ -60,6 +63,7 @@ def test_basic_auth_str_contains_credentials(self, username: str, password: str)
6063
assert username in decoded
6164
assert password in decoded
6265

66+
@settings(max_examples=1000, deadline=None)
6367
@given(st.text(min_size=1, max_size=50), st.text(min_size=1, max_size=50))
6468
def test_basic_auth_str_deterministic(self, username: str, password: str) -> None:
6569
"""_basic_auth_str should be deterministic."""
@@ -75,6 +79,7 @@ def test_basic_auth_str_deterministic(self, username: str, password: str) -> Non
7579
class TestHTTPBasicAuthProperties:
7680
"""Property-based tests for HTTPBasicAuth class."""
7781

82+
@settings(max_examples=1000, deadline=None)
7883
@given(safe_text, safe_text)
7984
def test_http_basic_auth_creation(self, username: str, password: str) -> None:
8085
"""HTTPBasicAuth should be creatable with username and password."""
@@ -83,6 +88,7 @@ def test_http_basic_auth_creation(self, username: str, password: str) -> None:
8388
assert auth.username == username
8489
assert auth.password == password
8590

91+
@settings(max_examples=1000, deadline=None)
8692
@given(safe_text, safe_text)
8793
def test_http_basic_auth_adds_header(self, username: str, password: str) -> None:
8894
"""HTTPBasicAuth should add Authorization header to request."""
@@ -96,13 +102,15 @@ def test_http_basic_auth_adds_header(self, username: str, password: str) -> None
96102
assert "Authorization" in result.headers
97103
assert result.headers["Authorization"].startswith("Basic ")
98104

105+
@settings(max_examples=1000, deadline=None)
99106
@given(safe_text, safe_text)
100107
def test_http_basic_auth_equality(self, username: str, password: str) -> None:
101108
"""HTTPBasicAuth instances with same credentials should be equal."""
102109
auth1 = HTTPBasicAuth(username, password)
103110
auth2 = HTTPBasicAuth(username, password)
104111
assert auth1 == auth2
105112

113+
@settings(max_examples=1000, deadline=None)
106114
@given(safe_text, safe_text, safe_text)
107115
def test_http_basic_auth_inequality(
108116
self, username1: str, username2: str, password: str
@@ -113,6 +121,7 @@ def test_http_basic_auth_inequality(
113121
auth2 = HTTPBasicAuth(username2, password)
114122
assert auth1 != auth2
115123

124+
@settings(max_examples=1000, deadline=None)
116125
@given(safe_text, safe_text)
117126
def test_http_basic_auth_returns_request(self, username: str, password: str) -> None:
118127
"""HTTPBasicAuth should return the request object."""
@@ -125,6 +134,7 @@ def test_http_basic_auth_returns_request(self, username: str, password: str) ->
125134
result = auth(req)
126135
assert result is req
127136

137+
@settings(max_examples=1000, deadline=None)
128138
@given(safe_text, safe_text)
129139
def test_http_basic_auth_ne_operator(self, username: str, password: str) -> None:
130140
"""HTTPBasicAuth __ne__ should work correctly."""
@@ -136,6 +146,7 @@ def test_http_basic_auth_ne_operator(self, username: str, password: str) -> None
136146
class TestHTTPProxyAuthProperties:
137147
"""Property-based tests for HTTPProxyAuth class."""
138148

149+
@settings(max_examples=1000, deadline=None)
139150
@given(safe_text, safe_text)
140151
def test_http_proxy_auth_creation(self, username: str, password: str) -> None:
141152
"""HTTPProxyAuth should be creatable with username and password."""
@@ -144,6 +155,7 @@ def test_http_proxy_auth_creation(self, username: str, password: str) -> None:
144155
assert auth.username == username
145156
assert auth.password == password
146157

158+
@settings(max_examples=1000, deadline=None)
147159
@given(safe_text, safe_text)
148160
def test_http_proxy_auth_adds_header(self, username: str, password: str) -> None:
149161
"""HTTPProxyAuth should add Proxy-Authorization header."""
@@ -157,6 +169,7 @@ def test_http_proxy_auth_adds_header(self, username: str, password: str) -> None
157169
assert "Proxy-Authorization" in result.headers
158170
assert result.headers["Proxy-Authorization"].startswith("Basic ")
159171

172+
@settings(max_examples=1000, deadline=None)
160173
@given(safe_text, safe_text)
161174
def test_http_proxy_auth_is_basic_auth_subclass(
162175
self, username: str, password: str
@@ -169,6 +182,7 @@ def test_http_proxy_auth_is_basic_auth_subclass(
169182
class TestHTTPDigestAuthProperties:
170183
"""Property-based tests for HTTPDigestAuth class."""
171184

185+
@settings(max_examples=1000, deadline=None)
172186
@given(safe_text, safe_text)
173187
def test_http_digest_auth_creation(self, username: str, password: str) -> None:
174188
"""HTTPDigestAuth should be creatable with username and password."""
@@ -177,12 +191,14 @@ def test_http_digest_auth_creation(self, username: str, password: str) -> None:
177191
assert auth.username == username
178192
assert auth.password == password
179193

194+
@settings(max_examples=1000, deadline=None)
180195
@given(safe_text, safe_text)
181196
def test_http_digest_auth_has_thread_local(self, username: str, password: str) -> None:
182197
"""HTTPDigestAuth should have thread-local storage."""
183198
auth = HTTPDigestAuth(username, password)
184199
assert hasattr(auth, "_thread_local")
185200

201+
@settings(max_examples=1000, deadline=None)
186202
@given(safe_text, safe_text)
187203
def test_http_digest_auth_init_per_thread_state(
188204
self, username: str, password: str
@@ -197,13 +213,15 @@ def test_http_digest_auth_init_per_thread_state(
197213
assert hasattr(auth._thread_local, "pos")
198214
assert hasattr(auth._thread_local, "num_401_calls")
199215

216+
@settings(max_examples=1000, deadline=None)
200217
@given(safe_text, safe_text)
201218
def test_http_digest_auth_equality(self, username: str, password: str) -> None:
202219
"""HTTPDigestAuth instances with same credentials should be equal."""
203220
auth1 = HTTPDigestAuth(username, password)
204221
auth2 = HTTPDigestAuth(username, password)
205222
assert auth1 == auth2
206223

224+
@settings(max_examples=1000, deadline=None)
207225
@given(safe_text, safe_text, safe_text)
208226
def test_http_digest_auth_inequality(
209227
self, username1: str, username2: str, password: str
@@ -218,6 +236,7 @@ def test_http_digest_auth_inequality(
218236
class TestAuthInvariants:
219237
"""Test invariants that should hold for authentication classes."""
220238

239+
@settings(max_examples=1000, deadline=None)
221240
@given(safe_text, safe_text)
222241
def test_basic_auth_idempotent(self, username: str, password: str) -> None:
223242
"""Applying HTTPBasicAuth multiple times should be idempotent."""
@@ -237,6 +256,7 @@ def test_basic_auth_idempotent(self, username: str, password: str) -> None:
237256
# Should produce same header
238257
assert auth_header1 == auth_header2
239258

259+
@settings(max_examples=1000, deadline=None)
240260
@given(safe_text, safe_text)
241261
def test_proxy_auth_idempotent(self, username: str, password: str) -> None:
242262
"""Applying HTTPProxyAuth multiple times should be idempotent."""
@@ -256,6 +276,7 @@ def test_proxy_auth_idempotent(self, username: str, password: str) -> None:
256276
# Should produce same header
257277
assert auth_header1 == auth_header2
258278

279+
@settings(max_examples=1000, deadline=None)
259280
@given(safe_text, safe_text)
260281
def test_basic_auth_header_format(self, username: str, password: str) -> None:
261282
"""HTTPBasicAuth should produce correctly formatted header."""
@@ -271,6 +292,7 @@ def test_basic_auth_header_format(self, username: str, password: str) -> None:
271292
# Should match Basic auth format
272293
assert re.match(r"^Basic [A-Za-z0-9+/]+=*$", auth_header)
273294

295+
@settings(max_examples=1000, deadline=None)
274296
@given(safe_text, safe_text)
275297
def test_proxy_auth_header_format(self, username: str, password: str) -> None:
276298
"""HTTPProxyAuth should produce correctly formatted header."""
@@ -286,6 +308,7 @@ def test_proxy_auth_header_format(self, username: str, password: str) -> None:
286308
# Should match Basic auth format
287309
assert re.match(r"^Basic [A-Za-z0-9+/]+=*$", auth_header)
288310

311+
@settings(max_examples=1000, deadline=None)
289312
@given(safe_text, safe_text, safe_text, safe_text)
290313
def test_different_credentials_different_headers(
291314
self, user1: str, pass1: str, user2: str, pass2: str
@@ -316,6 +339,7 @@ def test_different_credentials_different_headers(
316339
class TestAuthHeaderEncoding:
317340
"""Test encoding properties of auth headers."""
318341

342+
@settings(max_examples=1000, deadline=None)
319343
@given(
320344
st.text(
321345
alphabet=st.characters(
@@ -348,6 +372,7 @@ def test_ascii_credentials_always_work(self, username: str, password: str) -> No
348372
assert username.encode("latin1") in decoded
349373
assert password.encode("latin1") in decoded
350374

375+
@settings(max_examples=1000, deadline=None)
351376
@given(safe_text, safe_text)
352377
def test_basic_auth_str_roundtrip(self, username: str, password: str) -> None:
353378
"""Basic auth string should be decodable to recover credentials."""
@@ -365,30 +390,35 @@ def test_basic_auth_str_roundtrip(self, username: str, password: str) -> None:
365390
class TestAuthEquality:
366391
"""Test equality and inequality operations for auth classes."""
367392

393+
@settings(max_examples=1000, deadline=None)
368394
@given(safe_text, safe_text)
369395
def test_basic_auth_equal_to_itself(self, username: str, password: str) -> None:
370396
"""HTTPBasicAuth should be equal to itself."""
371397
auth = HTTPBasicAuth(username, password)
372398
assert auth == auth
373399

400+
@settings(max_examples=1000, deadline=None)
374401
@given(safe_text, safe_text)
375402
def test_proxy_auth_equal_to_itself(self, username: str, password: str) -> None:
376403
"""HTTPProxyAuth should be equal to itself."""
377404
auth = HTTPProxyAuth(username, password)
378405
assert auth == auth
379406

407+
@settings(max_examples=1000, deadline=None)
380408
@given(safe_text, safe_text)
381409
def test_digest_auth_equal_to_itself(self, username: str, password: str) -> None:
382410
"""HTTPDigestAuth should be equal to itself."""
383411
auth = HTTPDigestAuth(username, password)
384412
assert auth == auth
385413

414+
@settings(max_examples=1000, deadline=None)
386415
@given(safe_text, safe_text)
387416
def test_basic_auth_not_equal_to_none(self, username: str, password: str) -> None:
388417
"""HTTPBasicAuth should not be equal to None."""
389418
auth = HTTPBasicAuth(username, password)
390419
assert auth != None # noqa: E711
391420

421+
@settings(max_examples=1000, deadline=None)
392422
@given(safe_text, safe_text)
393423
def test_basic_auth_not_equal_to_other_type(
394424
self, username: str, password: str
@@ -399,6 +429,7 @@ def test_basic_auth_not_equal_to_other_type(
399429
assert auth != 123
400430
assert auth != {}
401431

432+
@settings(max_examples=1000, deadline=None)
402433
@given(safe_text, safe_text)
403434
def test_basic_auth_copy_is_equal(self, username: str, password: str) -> None:
404435
"""A copy of HTTPBasicAuth should be equal to original."""
@@ -410,6 +441,7 @@ def test_basic_auth_copy_is_equal(self, username: str, password: str) -> None:
410441
class TestDigestAuthSpecificProperties:
411442
"""Test properties specific to HTTPDigestAuth."""
412443

444+
@settings(max_examples=1000, deadline=None)
413445
@given(safe_text, safe_text)
414446
def test_digest_auth_nonce_count_starts_at_zero(
415447
self, username: str, password: str
@@ -419,6 +451,7 @@ def test_digest_auth_nonce_count_starts_at_zero(
419451
auth.init_per_thread_state()
420452
assert auth._thread_local.nonce_count == 0
421453

454+
@settings(max_examples=1000, deadline=None)
422455
@given(safe_text, safe_text)
423456
def test_digest_auth_last_nonce_starts_empty(
424457
self, username: str, password: str
@@ -428,20 +461,23 @@ def test_digest_auth_last_nonce_starts_empty(
428461
auth.init_per_thread_state()
429462
assert auth._thread_local.last_nonce == ""
430463

464+
@settings(max_examples=1000, deadline=None)
431465
@given(safe_text, safe_text)
432466
def test_digest_auth_chal_starts_empty(self, username: str, password: str) -> None:
433467
"""HTTPDigestAuth chal should start as empty dict."""
434468
auth = HTTPDigestAuth(username, password)
435469
auth.init_per_thread_state()
436470
assert auth._thread_local.chal == {}
437471

472+
@settings(max_examples=1000, deadline=None)
438473
@given(safe_text, safe_text)
439474
def test_digest_auth_pos_starts_none(self, username: str, password: str) -> None:
440475
"""HTTPDigestAuth pos should start as None."""
441476
auth = HTTPDigestAuth(username, password)
442477
auth.init_per_thread_state()
443478
assert auth._thread_local.pos is None
444479

480+
@settings(max_examples=1000, deadline=None)
445481
@given(safe_text, safe_text)
446482
def test_digest_auth_num_401_calls_starts_none(
447483
self, username: str, password: str
@@ -451,6 +487,7 @@ def test_digest_auth_num_401_calls_starts_none(
451487
auth.init_per_thread_state()
452488
assert auth._thread_local.num_401_calls is None
453489

490+
@settings(max_examples=1000, deadline=None)
454491
@given(safe_text, safe_text)
455492
def test_digest_auth_multiple_init_idempotent(
456493
self, username: str, password: str

0 commit comments

Comments
 (0)