-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle GitHub Refresh Token request errors
- Loading branch information
1 parent
bd536f0
commit 5bc4b44
Showing
3 changed files
with
85 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"access_token": "ghu_XXX", | ||
"expires_in": 28800, | ||
"refresh_token": "ghr_XXX", | ||
"refresh_token_expires_in": 15897600, | ||
"scope": "", | ||
"token_type": "bearer" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
import re | ||
from urllib.parse import urlencode | ||
|
||
import pytest | ||
import respx | ||
from httpx import Response | ||
|
||
from httpx_oauth.clients.github import EMAILS_ENDPOINT, PROFILE_ENDPOINT, GitHubOAuth2 | ||
from httpx_oauth.errors import GetIdEmailError | ||
from httpx_oauth.oauth2 import OAuth2Token, RefreshTokenError | ||
|
||
client = GitHubOAuth2("CLIENT_ID", "CLIENT_SECRET") | ||
|
||
|
@@ -26,6 +28,52 @@ def test_github_oauth2(): | |
emails_response = [{"email": "[email protected]"}] | ||
|
||
|
||
class TestGitHubRefreshToken: | ||
@pytest.mark.asyncio | ||
@respx.mock | ||
async def test_refresh_token(self, load_mock, get_respx_call_args): | ||
request = respx.post(client.refresh_token_endpoint).mock( | ||
return_value=Response(200, json=load_mock("github_success_refresh_token")) | ||
) | ||
access_token = await client.refresh_token("REFRESH_TOKEN") | ||
|
||
url, headers, content = await get_respx_call_args(request) | ||
assert headers["Content-Type"] == "application/x-www-form-urlencoded" | ||
assert headers["Accept"] == "application/json" | ||
assert "grant_type=refresh_token" in content | ||
assert "refresh_token=REFRESH_TOKEN" in content | ||
assert "client_id=CLIENT_ID" in content | ||
assert "client_secret=CLIENT_SECRET" in content | ||
|
||
assert type(access_token) == OAuth2Token | ||
assert "access_token" in access_token | ||
assert "token_type" in access_token | ||
assert access_token.is_expired() is False | ||
|
||
@pytest.mark.asyncio | ||
@respx.mock | ||
async def test_refresh_token_error(self, load_mock): | ||
error_response = { | ||
"error": "bad_refresh_token", | ||
"error_description": "The refresh token passed is incorrect or expired.", | ||
"error_uri": "https://docs.github.com", | ||
} | ||
error_response_encoded = urlencode(error_response) | ||
|
||
respx.post(client.refresh_token_endpoint).mock( | ||
return_value=Response( | ||
200, | ||
headers={"content-type": "application/x-www-form-urlencoded"}, | ||
content=error_response_encoded, | ||
) | ||
) | ||
|
||
with pytest.raises(RefreshTokenError) as excinfo: | ||
await client.refresh_token("REFRESH_TOKEN") | ||
assert isinstance(excinfo.value.args[0], dict) | ||
assert "error" in excinfo.value.args[0] | ||
|
||
|
||
class TestGitHubGetIdEmail: | ||
@pytest.mark.asyncio | ||
@respx.mock | ||
|