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
Provenance
- Commit
1cd9156- Tree
99971cd- Author
Yeray Diaz Diaz <yeraydiazdiaz@gmail.com>- Committer
Seth Michael Larson <sethmichaellarson@gmail.com>- Parent
fd3b69d
1 file changed+39 added−19 removed
modified:tests/client/test_auth.py+39 added-19 removed
17 unmodified lines18 )19 20 21 class MockDispatch(AsyncDispatcher):22+ def __init__(self, auth_header: str = "", status_code: int = 200) -> None:23+ self.auth_header = auth_header24+ self.status_code = status_code25+26 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+ )38 39 40 class MockDigestAuthDispatch(AsyncDispatcher):41 def __init__(52 unmodified lines94 def reset(self) -> None:95 self._response_count = 096 97 91-class MockAuthHeaderDispatch(AsyncDispatcher):92- def __init__(self, auth_header: str) -> None:93- self.auth_header = auth_header94-95- 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)104-105-98 def test_basic_auth():99 url = "https://example.org/"100 auth = ("tomchristie", "password123")101 105 unmodified lines207 assert response.status_code == 200208 assert response.json() == {"auth": None}209 210 211+def 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"'215+216+ with Client(217+ dispatch=MockDispatch(auth_header=auth_header, status_code=200)218+ ) as client:219+ response = client.get(url, auth=auth)220+221+ assert response.status_code == 200222+ assert response.json() == {"auth": None}223+224+225+def 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")228+229+ with Client(dispatch=MockDispatch(auth_header="", status_code=401)) as client:230+ response = client.get(url, auth=auth)231+232+ assert response.status_code == 401233+ assert response.json() == {"auth": None}234+235+236 @pytest.mark.parametrize(237 "algorithm,expected_hash_length,expected_response_length",238 [239 ("MD5", 64, 32),57 unmodified lines297 assert len(digest_data["opaque"]) == 64 + 2298 assert digest_data["algorithm"] == "SHA-256"299 300 301+@pytest.mark.parametrize("qop", ("auth, auth-int", "auth,auth-int", "unknown,auth"))284-def test_digest_auth_qop_including_spaces_and_auth_returns_auth():302+def test_digest_auth_qop_including_spaces_and_auth_returns_auth(qop: str):303 url = "https://example.org/"304 auth = DigestAuth(username="tomchristie", password="password123")305 288- 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)308 309 310 def test_digest_auth_qop_auth_int_not_implemented():40 unmodified lines351 url = "https://example.org/"352 auth = DigestAuth(username="tomchristie", password="password123")353 354 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)