def test_with_invalid_token_request_invalid_request_error_and_error_description_and_uri_and_other_fields( token_cache, browser_mock: BrowserMock): auth = httpx_auth.OAuth2AuthorizationCode("http://provide_code", "http://provide_access_token") tab = browser_mock.add_response( opened_url= "http://provide_code?response_type=code&state=163f0455b3e9cad3ca04254e5a0169553100d3aa0756c7964d897da316a695ffed5b4f46ef305094fd0a88cfe4b55ff257652015e4aa8f87b97513dba440f8de&redirect_uri=http%3A%2F%2Flocalhost%3A5000%2F", reply_url= "http://localhost:5000#error=invalid_request&error_description=desc&error_uri=http://test_url&other=test", ) with pytest.raises(httpx_auth.InvalidGrantRequest) as exception_info: httpx.get("http://authorized_only", auth=auth) assert ( str(exception_info.value) == "invalid_request: desc\nMore information can be found on http://test_url\nAdditional information: {'other': ['test']}" ) tab.assert_failure( "Unable to properly perform authentication: invalid_request: desc\nMore information can be found on http://test_url\nAdditional information: {'other': ['test']}" )
def test_with_invalid_grant_request_no_json(token_cache, httpx_mock: HTTPXMock, browser_mock: BrowserMock): auth = httpx_auth.OAuth2AuthorizationCode("http://provide_code", "http://provide_access_token") tab = browser_mock.add_response( opened_url= "http://provide_code?response_type=code&state=163f0455b3e9cad3ca04254e5a0169553100d3aa0756c7964d897da316a695ffed5b4f46ef305094fd0a88cfe4b55ff257652015e4aa8f87b97513dba440f8de&redirect_uri=http%3A%2F%2Flocalhost%3A5000%2F", reply_url= "http://localhost:5000#code=SplxlOBeZQQYbYS6WxSbIA&state=163f0455b3e9cad3ca04254e5a0169553100d3aa0756c7964d897da316a695ffed5b4f46ef305094fd0a88cfe4b55ff257652015e4aa8f87b97513dba440f8de", ) httpx_mock.add_response(method="POST", url="http://provide_access_token", data="failure", status_code=400) with pytest.raises(httpx_auth.InvalidGrantRequest) as exception_info: httpx.get("http://authorized_only", auth=auth) assert str(exception_info.value) == "failure" tab.assert_success( "You are now authenticated on 163f0455b3e9cad3ca04254e5a0169553100d3aa0756c7964d897da316a695ffed5b4f46ef305094fd0a88cfe4b55ff257652015e4aa8f87b97513dba440f8de. You may close this tab." )
def test_oauth2_authorization_code_flow_get_code_is_expired_after_30_seconds_by_default( token_cache, httpx_mock: HTTPXMock, browser_mock: BrowserMock): auth = httpx_auth.OAuth2AuthorizationCode("http://provide_code", "http://provide_access_token") # Add a token that expires in 29 seconds, so should be considered as expired when issuing the request token_cache._add_token( key= "163f0455b3e9cad3ca04254e5a0169553100d3aa0756c7964d897da316a695ffed5b4f46ef305094fd0a88cfe4b55ff257652015e4aa8f87b97513dba440f8de", token="2YotnFZFEjr1zCsicMWpAA", expiry=httpx_auth.oauth2_tokens._to_expiry(expires_in=29), ) # Meaning a new one will be requested tab = browser_mock.add_response( opened_url= "http://provide_code?response_type=code&state=163f0455b3e9cad3ca04254e5a0169553100d3aa0756c7964d897da316a695ffed5b4f46ef305094fd0a88cfe4b55ff257652015e4aa8f87b97513dba440f8de&redirect_uri=http%3A%2F%2Flocalhost%3A5000%2F", reply_url= "http://localhost:5000#code=SplxlOBeZQQYbYS6WxSbIA&state=163f0455b3e9cad3ca04254e5a0169553100d3aa0756c7964d897da316a695ffed5b4f46ef305094fd0a88cfe4b55ff257652015e4aa8f87b97513dba440f8de", ) httpx_mock.add_response( method="POST", url="http://provide_access_token", json={ "access_token": "2YotnFZFEjr1zCsicMWpAA", "token_type": "example", "expires_in": 3600, "refresh_token": "tGzv3JOkF0XG5Qx2TlKWIA", "example_parameter": "example_value", }, match_content= b"grant_type=authorization_code&redirect_uri=http%3A%2F%2Flocalhost%3A5000%2F&response_type=code&code=SplxlOBeZQQYbYS6WxSbIA", ) httpx_mock.add_response( match_headers={"Authorization": "Bearer 2YotnFZFEjr1zCsicMWpAA"}) # Send a request to this dummy URL with authentication httpx.get("http://authorized_only", auth=auth) tab.assert_success( "You are now authenticated on 163f0455b3e9cad3ca04254e5a0169553100d3aa0756c7964d897da316a695ffed5b4f46ef305094fd0a88cfe4b55ff257652015e4aa8f87b97513dba440f8de. You may close this tab." )
def test_token_url_is_mandatory(): with pytest.raises(Exception) as exception_info: httpx_auth.OAuth2AuthorizationCode("http://test_url", "") assert str(exception_info.value) == "Token URL is mandatory."