Type-check test_auth.py (#665)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Type-check test_auth.py * Drop request cast
authored by Florimond Manca · committed by GitHub · parent e30ec85 · Browse files · 1 file·+38 added−29 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+38 added−29 removed
- tests:+38 added−29 removed
Provenance
- Commit
49e4d15- Tree
07e8b4d- Author
Florimond Manca <florimond.manca@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
e30ec85
1 file changed+38 added−29 removed
modified:tests/client/test_auth.py+38 added-29 removed
1 import hashlib2 import json3 import os4+import typing5 6 import pytest7 8 from httpx import URL, Client, DigestAuth, ProtocolError, Request, Response75 unmodified lines84 return Response(401, headers=headers, content=b"", request=request)85 86 87 @pytest.mark.asyncio87-async def test_basic_auth():88+async def test_basic_auth() -> None:89 url = "https://example.org/"90 auth = ("tomchristie", "password123")91 92 client = Client(dispatch=MockDispatch())3 unmodified lines96 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}97 98 99 @pytest.mark.asyncio99-async def test_basic_auth_in_url():100+async def test_basic_auth_in_url() -> None:101 url = "https://tomchristie:password123@example.org/"102 103 client = Client(dispatch=MockDispatch())104 response = await client.get(url)2 unmodified lines107 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}108 109 110 @pytest.mark.asyncio110-async def test_basic_auth_on_session():111+async def test_basic_auth_on_session() -> None:112 url = "https://example.org/"113 auth = ("tomchristie", "password123")114 115 client = Client(dispatch=MockDispatch(), auth=auth)3 unmodified lines119 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}120 121 122 @pytest.mark.asyncio122-async def test_custom_auth():123+async def test_custom_auth() -> None:124 url = "https://example.org/"125 125- def auth(request):126+ def auth(request: Request) -> Request:127 request.headers["Authorization"] = "Token 123"128 return request129 130 client = Client(dispatch=MockDispatch())3 unmodified lines134 assert response.json() == {"auth": "Token 123"}135 136 137 @pytest.mark.asyncio137-async def test_netrc_auth():138+async def test_netrc_auth() -> None:139 os.environ["NETRC"] = "tests/.netrc"140 url = "http://netrcexample.org"141 142 client = Client(dispatch=MockDispatch())5 unmodified lines148 }149 150 151 @pytest.mark.asyncio151-async def test_auth_header_has_priority_over_netrc():152+async def test_auth_header_has_priority_over_netrc() -> None:153 os.environ["NETRC"] = "tests/.netrc"154 url = "http://netrcexample.org"155 156 client = Client(dispatch=MockDispatch())3 unmodified lines160 assert response.json() == {"auth": "Override"}161 162 163 @pytest.mark.asyncio163-async def test_trust_env_auth():164+async def test_trust_env_auth() -> None:165 os.environ["NETRC"] = "tests/.netrc"166 url = "http://netrcexample.org"167 168 client = Client(dispatch=MockDispatch(), trust_env=False)10 unmodified lines179 "auth": "Basic ZXhhbXBsZS11c2VybmFtZTpleGFtcGxlLXBhc3N3b3Jk"180 }181 182 182-def test_auth_hidden_url():183+def test_auth_hidden_url() -> None:184 url = "http://example-username:example-password@example.org/"185 expected = "URL('http://example-username:[secure]@example.org/')"186 assert url == URL(url)187 assert expected == repr(URL(url))188 189 190 @pytest.mark.asyncio190-async def test_auth_hidden_header():191+async def test_auth_hidden_header() -> None:192 url = "https://example.org/"193 auth = ("example-username", "example-password")194 195 client = Client(dispatch=MockDispatch())2 unmodified lines198 assert "'authorization': '[secure]'" in str(response.request.headers)199 200 201 @pytest.mark.asyncio201-async def test_auth_invalid_type():202+async def test_auth_invalid_type() -> None:203 url = "https://example.org/"203- client = Client(dispatch=MockDispatch(), auth="not a tuple, not a callable")204+ client = Client(205+ dispatch=MockDispatch(), auth="not a tuple, not a callable", # type: ignore206+ )207 with pytest.raises(TypeError):208 await client.get(url)209 210 211 @pytest.mark.asyncio209-async def test_digest_auth_returns_no_auth_if_no_digest_header_in_response():212+async def test_digest_auth_returns_no_auth_if_no_digest_header_in_response() -> None:213 url = "https://example.org/"214 auth = DigestAuth(username="tomchristie", password="password123")215 216 client = Client(dispatch=MockDispatch())3 unmodified lines220 assert response.json() == {"auth": None}221 222 223 @pytest.mark.asyncio221-async def test_digest_auth_200_response_including_digest_auth_header():224+async def test_digest_auth_200_response_including_digest_auth_header() -> None:225 url = "https://example.org/"226 auth = DigestAuth(username="tomchristie", password="password123")227 auth_header = 'Digest realm="realm@host.com",qop="auth",nonce="abc",opaque="xyz"'228 4 unmodified lines233 assert response.json() == {"auth": None}234 235 236 @pytest.mark.asyncio234-async def test_digest_auth_401_response_without_digest_auth_header():237+async def test_digest_auth_401_response_without_digest_auth_header() -> None:238 url = "https://example.org/"239 auth = DigestAuth(username="tomchristie", password="password123")240 241 client = Client(dispatch=MockDispatch(auth_header="", status_code=401))16 unmodified lines258 ("SHA-512-SESS", 64, 128),259 ],260 )261 @pytest.mark.asyncio259-async def test_digest_auth(algorithm, expected_hash_length, expected_response_length):262+async def test_digest_auth(263+ algorithm: str, expected_hash_length: int, expected_response_length: int264+) -> None:265 url = "https://example.org/"266 auth = DigestAuth(username="tomchristie", password="password123")267 268 client = Client(dispatch=MockDigestAuthDispatch(algorithm=algorithm))269 response = await client.get(url, auth=auth)270 271 assert response.status_code == 200267- auth = response.json()["auth"]268- assert auth.startswith("Digest ")272+ authorization = typing.cast(dict, response.json())["auth"]273+ scheme, _, fields = authorization.partition(" ")274+ assert scheme == "Digest"275 270- response_fields = [field.strip() for field in auth[auth.find(" ") :].split(",")]276+ response_fields = [field.strip() for field in fields.split(",")]277 digest_data = dict(field.split("=") for field in response_fields)278 279 assert digest_data["username"] == '"tomchristie"'280 assert digest_data["realm"] == '"httpx@example.org"'7 unmodified lines288 assert len(digest_data["cnonce"]) == 16 + 2289 290 291 @pytest.mark.asyncio286-async def test_digest_auth_no_specified_qop():292+async def test_digest_auth_no_specified_qop() -> None:293 url = "https://example.org/"294 auth = DigestAuth(username="tomchristie", password="password123")295 290- client = Client(dispatch=MockDigestAuthDispatch(qop=None))296+ client = Client(dispatch=MockDigestAuthDispatch(qop=""))297 response = await client.get(url, auth=auth)298 299 assert response.status_code == 200294- auth = response.json()["auth"]295- assert auth.startswith("Digest ")300+ authorization = typing.cast(dict, response.json())["auth"]301+ scheme, _, fields = authorization.partition(" ")302+ assert scheme == "Digest"303 297- response_fields = [field.strip() for field in auth[auth.find(" ") :].split(",")]304+ response_fields = [field.strip() for field in fields.split(",")]305 digest_data = dict(field.split("=") for field in response_fields)306 307 assert "qop" not in digest_data308 assert "nc" not in digest_data8 unmodified lines317 318 319 @pytest.mark.parametrize("qop", ("auth, auth-int", "auth,auth-int", "unknown,auth"))320 @pytest.mark.asyncio314-async def test_digest_auth_qop_including_spaces_and_auth_returns_auth(qop: str):321+async def test_digest_auth_qop_including_spaces_and_auth_returns_auth(qop: str) -> None:322 url = "https://example.org/"323 auth = DigestAuth(username="tomchristie", password="password123")324 325 client = Client(dispatch=MockDigestAuthDispatch(qop=qop))326 await client.get(url, auth=auth)327 328 329 @pytest.mark.asyncio323-async def test_digest_auth_qop_auth_int_not_implemented():330+async def test_digest_auth_qop_auth_int_not_implemented() -> None:331 url = "https://example.org/"332 auth = DigestAuth(username="tomchristie", password="password123")333 client = Client(dispatch=MockDigestAuthDispatch(qop="auth-int"))334 1 unmodified line336 await client.get(url, auth=auth)337 338 339 @pytest.mark.asyncio333-async def test_digest_auth_qop_must_be_auth_or_auth_int():340+async def test_digest_auth_qop_must_be_auth_or_auth_int() -> None:341 url = "https://example.org/"342 auth = DigestAuth(username="tomchristie", password="password123")343 client = Client(dispatch=MockDigestAuthDispatch(qop="not-auth"))344 1 unmodified line346 await client.get(url, auth=auth)347 348 349 @pytest.mark.asyncio343-async def test_digest_auth_incorrect_credentials():350+async def test_digest_auth_incorrect_credentials() -> None:351 url = "https://example.org/"352 auth = DigestAuth(username="tomchristie", password="password123")353 354 client = Client(dispatch=MockDigestAuthDispatch(send_response_after_attempt=2))12 unmodified lines367 'Digest realm="httpx@example.org", qop="auth,au', # malformed fields list368 ],369 )370 @pytest.mark.asyncio364-async def test_digest_auth_raises_protocol_error_on_malformed_header(auth_header: str):371+async def test_digest_auth_raises_protocol_error_on_malformed_header(372+ auth_header: str,373+) -> None:374 url = "https://example.org/"375 auth = DigestAuth(username="tomchristie", password="password123")376 client = Client(dispatch=MockDispatch(auth_header=auth_header, status_code=401))377 2 unmodified lines