-
-
Notifications
You must be signed in to change notification settings - Fork 540
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added Backend 'EtsyOAuth2' (#874)
* feat: Added Backend 'EtsyOAuth2' * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
34e4746
commit 9d3ba91
Showing
2 changed files
with
128 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from .oauth import BaseOAuth2PKCE | ||
|
||
|
||
class EtsyOAuth2(BaseOAuth2PKCE): | ||
name = "etsy" | ||
ID_KEY = "user_id" | ||
AUTHORIZATION_URL = "https://www.etsy.com/oauth/connect" | ||
ACCESS_TOKEN_URL = "https://api.etsy.com/v3/public/oauth/token" | ||
REFRESH_TOKEN_URL = "https://api.etsy.com/v3/public/oauth/token" | ||
ACCESS_TOKEN_METHOD = "POST" | ||
REQUEST_TOKEN_METHOD = "POST" | ||
SCOPE_SEPARATOR = " " | ||
EXTRA_DATA = [ | ||
("refresh_token", "refresh_token"), | ||
("expires_in", "expires_in"), | ||
("token_type", "token_type"), | ||
("access_token", "access_token"), | ||
# User Data Fields | ||
("primary_email", "primary_email"), | ||
("first_name", "first_name"), | ||
("last_name", "last_name"), | ||
("image_url_75x75", "image_url_75x75"), | ||
] | ||
|
||
def user_data(self, access_token, *args, **kwargs) -> dict: | ||
client_id, _ = self.get_key_and_secret() | ||
user_id = access_token.split(".")[0] | ||
headers = {"Authorization": f"Bearer {access_token}", "x-api-key": client_id} | ||
return self.get_json( | ||
url=f"https://openapi.etsy.com/v3/application/users/{user_id}", | ||
headers=headers, | ||
) | ||
|
||
def get_user_details(self, response): | ||
return { | ||
"user_id": response["user_id"], | ||
"first_name": response["first_name"], | ||
"last_name": response["last_name"], | ||
"email": response["primary_email"], | ||
"image_url_75x75": response["image_url_75x75"], | ||
"username": response["user_id"], | ||
} |
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,86 @@ | ||
import json | ||
|
||
from .oauth import OAuth2PkceS256Test | ||
|
||
|
||
class EtsyOAuth2Mixin: | ||
backend_path = "social_core.backends.etsy.EtsyOAuth2" | ||
access_token_body = json.dumps( | ||
{ | ||
"access_token": "dummy_user_id.dummy_access_token", | ||
"token_type": "bearer", | ||
"expires_in": 3600, | ||
"refresh_token": "dummy_user_id.dummy_refresh_token", | ||
} | ||
) | ||
refresh_token_body = json.dumps( | ||
{ | ||
"access_token": "dummy_user_id.dummy_access_token_refreshed", | ||
"token_type": "bearer", | ||
"expires_in": 3600, | ||
"refresh_token": "dummy_user_id.dummy_refresh_token_refreshed", | ||
} | ||
) | ||
|
||
user_data_url = "https://openapi.etsy.com/v3/application/users/dummy_user_id" | ||
user_data_body = json.dumps( | ||
{ | ||
"user_id": "dummy_user_id", | ||
"primary_email": "amitray@developer.com", | ||
"first_name": "Amit", | ||
"last_name": "Ray", | ||
"image_url_75x75": "http://www.gravatar.com/avatar/af7d968fe79ea45271e3100391824b79.jpg?s=48&d=mm", | ||
} | ||
) | ||
expected_username = "dummy_user_id" | ||
|
||
def test_login(self): | ||
user = self.do_login() | ||
self.assertEqual(len(user.social), 1) | ||
|
||
social = user.social[0] | ||
self.assertEqual(social.uid, "dummy_user_id") | ||
self.assertEqual(social.extra_data["first_name"], "Amit") | ||
self.assertEqual(social.extra_data["last_name"], "Ray") | ||
self.assertEqual(social.extra_data["primary_email"], "amitray@developer.com") | ||
self.assertEqual( | ||
social.extra_data["image_url_75x75"], | ||
"http://www.gravatar.com/avatar/af7d968fe79ea45271e3100391824b79.jpg?s=48&d=mm", | ||
) | ||
self.assertEqual( | ||
social.extra_data["access_token"], "dummy_user_id.dummy_access_token" | ||
) | ||
self.assertEqual(social.extra_data["token_type"], "bearer") | ||
self.assertEqual(social.extra_data["expires_in"], 3600) | ||
self.assertEqual( | ||
social.extra_data["refresh_token"], "dummy_user_id.dummy_refresh_token" | ||
) | ||
|
||
def test_refresh_token(self): | ||
_, social = self.do_refresh_token() | ||
|
||
self.assertEqual(social.uid, "dummy_user_id") | ||
self.assertEqual(social.extra_data["first_name"], "Amit") | ||
self.assertEqual(social.extra_data["last_name"], "Ray") | ||
self.assertEqual(social.extra_data["primary_email"], "amitray@developer.com") | ||
self.assertEqual( | ||
social.extra_data["image_url_75x75"], | ||
"http://www.gravatar.com/avatar/af7d968fe79ea45271e3100391824b79.jpg?s=48&d=mm", | ||
) | ||
self.assertEqual( | ||
social.extra_data["access_token"], | ||
"dummy_user_id.dummy_access_token_refreshed", | ||
) | ||
self.assertEqual(social.extra_data["token_type"], "bearer") | ||
self.assertEqual(social.extra_data["expires_in"], 3600) | ||
self.assertEqual( | ||
social.extra_data["refresh_token"], | ||
"dummy_user_id.dummy_refresh_token_refreshed", | ||
) | ||
|
||
|
||
class EtsyOAuth2TestPkceS256( | ||
EtsyOAuth2Mixin, | ||
OAuth2PkceS256Test, | ||
): | ||
pass |