Skip to content
Myr

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

1 file+38 added−29 removed

modified:tests/client/test_auth.py+38 added-29 removed
1import hashlib2import json3import os4import typing56import pytest78from httpx import URL, Client, DigestAuth, ProtocolError, Request, Response
75 unmodified lines
84 return Response(401, headers=headers, content=b"", request=request)858687@pytest.mark.asyncio87async def test_basic_auth():88async def test_basic_auth() -> None:89 url = "https://example.org/"90 auth = ("tomchristie", "password123")9192 client = Client(dispatch=MockDispatch())
3 unmodified lines
96 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}979899@pytest.mark.asyncio99async def test_basic_auth_in_url():100async def test_basic_auth_in_url() -> None:101 url = "https://tomchristie:password123@example.org/"102103 client = Client(dispatch=MockDispatch())104 response = await client.get(url)
2 unmodified lines
107 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}108109110@pytest.mark.asyncio110async def test_basic_auth_on_session():111async def test_basic_auth_on_session() -> None:112 url = "https://example.org/"113 auth = ("tomchristie", "password123")114115 client = Client(dispatch=MockDispatch(), auth=auth)
3 unmodified lines
119 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}120121122@pytest.mark.asyncio122async def test_custom_auth():123async def test_custom_auth() -> None:124 url = "https://example.org/"125125 def auth(request):126 def auth(request: Request) -> Request:127 request.headers["Authorization"] = "Token 123"128 return request129130 client = Client(dispatch=MockDispatch())
3 unmodified lines
134 assert response.json() == {"auth": "Token 123"}135136137@pytest.mark.asyncio137async def test_netrc_auth():138async def test_netrc_auth() -> None:139 os.environ["NETRC"] = "tests/.netrc"140 url = "http://netrcexample.org"141142 client = Client(dispatch=MockDispatch())
5 unmodified lines
148 }149150151@pytest.mark.asyncio151async def test_auth_header_has_priority_over_netrc():152async def test_auth_header_has_priority_over_netrc() -> None:153 os.environ["NETRC"] = "tests/.netrc"154 url = "http://netrcexample.org"155156 client = Client(dispatch=MockDispatch())
3 unmodified lines
160 assert response.json() == {"auth": "Override"}161162163@pytest.mark.asyncio163async def test_trust_env_auth():164async def test_trust_env_auth() -> None:165 os.environ["NETRC"] = "tests/.netrc"166 url = "http://netrcexample.org"167168 client = Client(dispatch=MockDispatch(), trust_env=False)
10 unmodified lines
179 "auth": "Basic ZXhhbXBsZS11c2VybmFtZTpleGFtcGxlLXBhc3N3b3Jk"180 }181182182def test_auth_hidden_url():183def 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))188189190@pytest.mark.asyncio190async def test_auth_hidden_header():191async def test_auth_hidden_header() -> None:192 url = "https://example.org/"193 auth = ("example-username", "example-password")194195 client = Client(dispatch=MockDispatch())
2 unmodified lines
198 assert "'authorization': '[secure]'" in str(response.request.headers)199200201@pytest.mark.asyncio201async def test_auth_invalid_type():202async 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)209210211@pytest.mark.asyncio209async def test_digest_auth_returns_no_auth_if_no_digest_header_in_response():212async 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")215216 client = Client(dispatch=MockDispatch())
3 unmodified lines
220 assert response.json() == {"auth": None}221222223@pytest.mark.asyncio221async def test_digest_auth_200_response_including_digest_auth_header():224async 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 lines
233 assert response.json() == {"auth": None}234235236@pytest.mark.asyncio234async def test_digest_auth_401_response_without_digest_auth_header():237async def test_digest_auth_401_response_without_digest_auth_header() -> None:238 url = "https://example.org/"239 auth = DigestAuth(username="tomchristie", password="password123")240241 client = Client(dispatch=MockDispatch(auth_header="", status_code=401))
16 unmodified lines
258 ("SHA-512-SESS", 64, 128),259 ],260)261@pytest.mark.asyncio259async def test_digest_auth(algorithm, expected_hash_length, expected_response_length):262async 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")267268 client = Client(dispatch=MockDigestAuthDispatch(algorithm=algorithm))269 response = await client.get(url, auth=auth)270271 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"275270 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)278279 assert digest_data["username"] == '"tomchristie"'280 assert digest_data["realm"] == '"httpx@example.org"'
7 unmodified lines
288 assert len(digest_data["cnonce"]) == 16 + 2289290291@pytest.mark.asyncio286async def test_digest_auth_no_specified_qop():292async def test_digest_auth_no_specified_qop() -> None:293 url = "https://example.org/"294 auth = DigestAuth(username="tomchristie", password="password123")295290 client = Client(dispatch=MockDigestAuthDispatch(qop=None))296 client = Client(dispatch=MockDigestAuthDispatch(qop=""))297 response = await client.get(url, auth=auth)298299 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"303297 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)306307 assert "qop" not in digest_data308 assert "nc" not in digest_data
8 unmodified lines
317318319@pytest.mark.parametrize("qop", ("auth, auth-int", "auth,auth-int", "unknown,auth"))320@pytest.mark.asyncio314async def test_digest_auth_qop_including_spaces_and_auth_returns_auth(qop: str):321async 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")324325 client = Client(dispatch=MockDigestAuthDispatch(qop=qop))326 await client.get(url, auth=auth)327328329@pytest.mark.asyncio323async def test_digest_auth_qop_auth_int_not_implemented():330async 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 line
336 await client.get(url, auth=auth)337338339@pytest.mark.asyncio333async def test_digest_auth_qop_must_be_auth_or_auth_int():340async 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 line
346 await client.get(url, auth=auth)347348349@pytest.mark.asyncio343async def test_digest_auth_incorrect_credentials():350async def test_digest_auth_incorrect_credentials() -> None:351 url = "https://example.org/"352 auth = DigestAuth(username="tomchristie", password="password123")353354 client = Client(dispatch=MockDigestAuthDispatch(send_response_after_attempt=2))
12 unmodified lines
367 'Digest realm="httpx@example.org", qop="auth,au', # malformed fields list368 ],369)370@pytest.mark.asyncio364async def test_digest_auth_raises_protocol_error_on_malformed_header(auth_header: str):371async 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