test_auth.py
This file
Path
tests/client/test_auth.py
69 lines · 1.8 KiB · Python
Last changed by
Start using flake8, fix lint issues (#126)Seth Michael Larsonbefore adoptionc1044ca
History of this fileTake it
Auth (#65)Tom Christiebefore adoption5afa7dd
lines 1–2why, one step back1import json2http3 (#86)Tom Christiebefore adoptionc9747aa
line 3why, one step back3from http3 import (Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 4–6why, one step back4 AsyncDispatcher,5 AsyncRequest,6 AsyncResponse,Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041
line 7why, one step back7 CertTypes,Auth (#65)Tom Christiebefore adoption5afa7dd
line 8why, one step back8 Client,Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041
lines 9–10why, one step back9 TimeoutTypes,10 VerifyTypes,Auth (#65)Tom Christiebefore adoption5afa7dd
lines 11–13why, one step back11)1213Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 14why, one step back14class MockDispatch(AsyncDispatcher):Auth (#65)Tom Christiebefore adoption5afa7dd
lines 15–16why, one step back15 async def send(16 self,Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 17why, one step back17 request: AsyncRequest,Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041
lines 18–20why, one step back18 verify: VerifyTypes = None,19 cert: CertTypes = None,20 timeout: TimeoutTypes = None,Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 21why, one step back21 ) -> AsyncResponse:Drop unused interface on ConnectionStore (#70)Tom Christiebefore adoption3ed0885
line 22why, one step back22 body = json.dumps({"auth": request.headers.get("Authorization")}).encode()Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 23why, one step back23 return AsyncResponse(200, content=body, request=request)Auth (#65)Tom Christiebefore adoption5afa7dd
lines 24–27why, one step back242526def test_basic_auth():27 url = "https://example.org/"Drop unused interface on ConnectionStore (#70)Tom Christiebefore adoption3ed0885
line 28why, one step back28 auth = ("tomchristie", "password123")Auth (#65)Tom Christiebefore adoption5afa7dd
lines 29–33why, one step back2930 with Client(dispatch=MockDispatch()) as client:31 response = client.get(url, auth=auth)3233 assert response.status_code == 200Add response.json()Tom Christiebefore adoption2116d44
line 34why, one step back34 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}Auth (#65)Tom Christiebefore adoption5afa7dd
lines 35–43why, one step back353637def test_basic_auth_in_url():38 url = "https://tomchristie:password123@example.org/"3940 with Client(dispatch=MockDispatch()) as client:41 response = client.get(url)4243 assert response.status_code == 200Add response.json()Tom Christiebefore adoption2116d44
line 44why, one step back44 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}Auth (#65)Tom Christiebefore adoption5afa7dd
lines 45–48why, one step back454647def test_basic_auth_on_session():48 url = "https://example.org/"Drop unused interface on ConnectionStore (#70)Tom Christiebefore adoption3ed0885
line 49why, one step back49 auth = ("tomchristie", "password123")Auth (#65)Tom Christiebefore adoption5afa7dd
lines 50–54why, one step back5051 with Client(dispatch=MockDispatch(), auth=auth) as client:52 response = client.get(url)5354 assert response.status_code == 200Add response.json()Tom Christiebefore adoption2116d44
line 55why, one step back55 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}Auth (#65)Tom Christiebefore adoption5afa7dd
lines 56–61why, one step back565758def test_custom_auth():59 url = "https://example.org/"6061 def auth(request):Drop unused interface on ConnectionStore (#70)Tom Christiebefore adoption3ed0885
line 62why, one step back62 request.headers["Authorization"] = "Token 123"Auth (#65)Tom Christiebefore adoption5afa7dd
lines 63–68why, one step back63 return request6465 with Client(dispatch=MockDispatch()) as client:66 response = client.get(url, auth=auth)6768 assert response.status_code == 200Add response.json()Tom Christiebefore adoption2116d44
line 69why, one step back69 assert response.json() == {"auth": "Token 123"}