import hashlib
import json
import os
+import typing
import pytest
@pytest.mark.asyncio
-async def test_basic_auth():
+async def test_basic_auth() -> None:
url = "https://example.org/"
auth = ("tomchristie", "password123")
@pytest.mark.asyncio
-async def test_basic_auth_in_url():
+async def test_basic_auth_in_url() -> None:
url = "https://tomchristie:password123@example.org/"
client = Client(dispatch=MockDispatch())
@pytest.mark.asyncio
-async def test_basic_auth_on_session():
+async def test_basic_auth_on_session() -> None:
url = "https://example.org/"
auth = ("tomchristie", "password123")
@pytest.mark.asyncio
-async def test_custom_auth():
+async def test_custom_auth() -> None:
url = "https://example.org/"
- def auth(request):
+ def auth(request: Request) -> Request:
request.headers["Authorization"] = "Token 123"
return request
@pytest.mark.asyncio
-async def test_netrc_auth():
+async def test_netrc_auth() -> None:
os.environ["NETRC"] = "tests/.netrc"
url = "http://netrcexample.org"
@pytest.mark.asyncio
-async def test_auth_header_has_priority_over_netrc():
+async def test_auth_header_has_priority_over_netrc() -> None:
os.environ["NETRC"] = "tests/.netrc"
url = "http://netrcexample.org"
@pytest.mark.asyncio
-async def test_trust_env_auth():
+async def test_trust_env_auth() -> None:
os.environ["NETRC"] = "tests/.netrc"
url = "http://netrcexample.org"
}
-def test_auth_hidden_url():
+def test_auth_hidden_url() -> None:
url = "http://example-username:example-password@example.org/"
expected = "URL('http://example-username:[secure]@example.org/')"
assert url == URL(url)
@pytest.mark.asyncio
-async def test_auth_hidden_header():
+async def test_auth_hidden_header() -> None:
url = "https://example.org/"
auth = ("example-username", "example-password")
@pytest.mark.asyncio
-async def test_auth_invalid_type():
+async def test_auth_invalid_type() -> None:
url = "https://example.org/"
- client = Client(dispatch=MockDispatch(), auth="not a tuple, not a callable")
+ client = Client(
+ dispatch=MockDispatch(), auth="not a tuple, not a callable", # type: ignore
+ )
with pytest.raises(TypeError):
await client.get(url)
@pytest.mark.asyncio
-async def test_digest_auth_returns_no_auth_if_no_digest_header_in_response():
+async def test_digest_auth_returns_no_auth_if_no_digest_header_in_response() -> None:
url = "https://example.org/"
auth = DigestAuth(username="tomchristie", password="password123")
@pytest.mark.asyncio
-async def test_digest_auth_200_response_including_digest_auth_header():
+async def test_digest_auth_200_response_including_digest_auth_header() -> None:
url = "https://example.org/"
auth = DigestAuth(username="tomchristie", password="password123")
auth_header = 'Digest realm="realm@host.com",qop="auth",nonce="abc",opaque="xyz"'
@pytest.mark.asyncio
-async def test_digest_auth_401_response_without_digest_auth_header():
+async def test_digest_auth_401_response_without_digest_auth_header() -> None:
url = "https://example.org/"
auth = DigestAuth(username="tomchristie", password="password123")
],
)
@pytest.mark.asyncio
-async def test_digest_auth(algorithm, expected_hash_length, expected_response_length):
+async def test_digest_auth(
+ algorithm: str, expected_hash_length: int, expected_response_length: int
+) -> None:
url = "https://example.org/"
auth = DigestAuth(username="tomchristie", password="password123")
response = await client.get(url, auth=auth)
assert response.status_code == 200
- auth = response.json()["auth"]
- assert auth.startswith("Digest ")
+ authorization = typing.cast(dict, response.json())["auth"]
+ scheme, _, fields = authorization.partition(" ")
+ assert scheme == "Digest"
- response_fields = [field.strip() for field in auth[auth.find(" ") :].split(",")]
+ response_fields = [field.strip() for field in fields.split(",")]
digest_data = dict(field.split("=") for field in response_fields)
assert digest_data["username"] == '"tomchristie"'
@pytest.mark.asyncio
-async def test_digest_auth_no_specified_qop():
+async def test_digest_auth_no_specified_qop() -> None:
url = "https://example.org/"
auth = DigestAuth(username="tomchristie", password="password123")
- client = Client(dispatch=MockDigestAuthDispatch(qop=None))
+ client = Client(dispatch=MockDigestAuthDispatch(qop=""))
response = await client.get(url, auth=auth)
assert response.status_code == 200
- auth = response.json()["auth"]
- assert auth.startswith("Digest ")
+ authorization = typing.cast(dict, response.json())["auth"]
+ scheme, _, fields = authorization.partition(" ")
+ assert scheme == "Digest"
- response_fields = [field.strip() for field in auth[auth.find(" ") :].split(",")]
+ response_fields = [field.strip() for field in fields.split(",")]
digest_data = dict(field.split("=") for field in response_fields)
assert "qop" not in digest_data
@pytest.mark.parametrize("qop", ("auth, auth-int", "auth,auth-int", "unknown,auth"))
@pytest.mark.asyncio
-async def test_digest_auth_qop_including_spaces_and_auth_returns_auth(qop: str):
+async def test_digest_auth_qop_including_spaces_and_auth_returns_auth(qop: str) -> None:
url = "https://example.org/"
auth = DigestAuth(username="tomchristie", password="password123")
@pytest.mark.asyncio
-async def test_digest_auth_qop_auth_int_not_implemented():
+async def test_digest_auth_qop_auth_int_not_implemented() -> None:
url = "https://example.org/"
auth = DigestAuth(username="tomchristie", password="password123")
client = Client(dispatch=MockDigestAuthDispatch(qop="auth-int"))
@pytest.mark.asyncio
-async def test_digest_auth_qop_must_be_auth_or_auth_int():
+async def test_digest_auth_qop_must_be_auth_or_auth_int() -> None:
url = "https://example.org/"
auth = DigestAuth(username="tomchristie", password="password123")
client = Client(dispatch=MockDigestAuthDispatch(qop="not-auth"))
@pytest.mark.asyncio
-async def test_digest_auth_incorrect_credentials():
+async def test_digest_auth_incorrect_credentials() -> None:
url = "https://example.org/"
auth = DigestAuth(username="tomchristie", password="password123")
],
)
@pytest.mark.asyncio
-async def test_digest_auth_raises_protocol_error_on_malformed_header(auth_header: str):
+async def test_digest_auth_raises_protocol_error_on_malformed_header(
+ auth_header: str,
+) -> None:
url = "https://example.org/"
auth = DigestAuth(username="tomchristie", password="password123")
client = Client(dispatch=MockDispatch(auth_header=auth_header, status_code=401))