Skip to content
Myr

Additional DigestAuth tests (#334)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Yeray Diaz Diaz · committed by Seth Michael Larson · parent fd3b69d · Browse files · 1 file+39 added−19 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+39 added−19 removed

  • tests:+39 added−19 removed

1 file+39 added−19 removed

modified:tests/client/test_auth.py+39 added-19 removed
17 unmodified lines
18)192021class MockDispatch(AsyncDispatcher):22 def __init__(self, auth_header: str = "", status_code: int = 200) -> None:23 self.auth_header = auth_header24 self.status_code = status_code2526 async def send(27 self,28 request: AsyncRequest,29 verify: VerifyTypes = None,30 cert: CertTypes = None,31 timeout: TimeoutTypes = None,32 ) -> AsyncResponse:33 headers = [("www-authenticate", self.auth_header)] if self.auth_header else []34 body = json.dumps({"auth": request.headers.get("Authorization")}).encode()30 return AsyncResponse(200, content=body, request=request)35 return AsyncResponse(36 self.status_code, headers=headers, content=body, request=request37 )383940class MockDigestAuthDispatch(AsyncDispatcher):41 def __init__(
52 unmodified lines
94 def reset(self) -> None:95 self._response_count = 0969791class MockAuthHeaderDispatch(AsyncDispatcher):92 def __init__(self, auth_header: str) -> None:93 self.auth_header = auth_header9495 async def send(96 self,97 request: AsyncRequest,98 verify: VerifyTypes = None,99 cert: CertTypes = None,100 timeout: TimeoutTypes = None,101 ) -> AsyncResponse:102 headers = [("www-authenticate", self.auth_header)]103 return AsyncResponse(401, headers=headers, content=b"", request=request)10410598def test_basic_auth():99 url = "https://example.org/"100 auth = ("tomchristie", "password123")101
105 unmodified lines
207 assert response.status_code == 200208 assert response.json() == {"auth": None}209210211def test_digest_auth_200_response_including_digest_auth_header_is_returned_as_is():212 url = "https://example.org/"213 auth = DigestAuth(username="tomchristie", password="password123")214 auth_header = 'Digest realm="realm@host.com",qop="auth",nonce="abc",opaque="xyz"'215216 with Client(217 dispatch=MockDispatch(auth_header=auth_header, status_code=200)218 ) as client:219 response = client.get(url, auth=auth)220221 assert response.status_code == 200222 assert response.json() == {"auth": None}223224225def test_digest_auth_401_response_without_digest_auth_header_is_returned_as_is():226 url = "https://example.org/"227 auth = DigestAuth(username="tomchristie", password="password123")228229 with Client(dispatch=MockDispatch(auth_header="", status_code=401)) as client:230 response = client.get(url, auth=auth)231232 assert response.status_code == 401233 assert response.json() == {"auth": None}234235236@pytest.mark.parametrize(237 "algorithm,expected_hash_length,expected_response_length",238 [239 ("MD5", 64, 32),
57 unmodified lines
297 assert len(digest_data["opaque"]) == 64 + 2298 assert digest_data["algorithm"] == "SHA-256"299300301@pytest.mark.parametrize("qop", ("auth, auth-int", "auth,auth-int", "unknown,auth"))284def test_digest_auth_qop_including_spaces_and_auth_returns_auth():302def test_digest_auth_qop_including_spaces_and_auth_returns_auth(qop: str):303 url = "https://example.org/"304 auth = DigestAuth(username="tomchristie", password="password123")305288 with Client(dispatch=MockDigestAuthDispatch(qop="auth, auth-int")) as client:306 with Client(dispatch=MockDigestAuthDispatch(qop=qop)) as client:307 client.get(url, auth=auth)308309310def test_digest_auth_qop_auth_int_not_implemented():
40 unmodified lines
351 url = "https://example.org/"352 auth = DigestAuth(username="tomchristie", password="password123")353354 with pytest.raises(ProtocolError):337 with Client(dispatch=MockAuthHeaderDispatch(auth_header=auth_header)) as client:355 with Client(356 dispatch=MockDispatch(auth_header=auth_header, status_code=401)357 ) as client:358 client.get(url, auth=auth)