Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added Backend 'EtsyOAuth2' #874

Merged
merged 2 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 42 additions & 0 deletions social_core/backends/etsy.py
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"],
}
86 changes: 86 additions & 0 deletions social_core/tests/backends/test_etsy.py
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