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

fix: #571 coerce uid to str #572

Merged
merged 1 commit into from
May 7, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion social_django/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Django ORM models for Social Auth"""

from typing import Union

from django.conf import settings
from django.db import models
from django.db.utils import IntegrityError
Expand Down Expand Up @@ -54,7 +56,9 @@ class Meta:
abstract = True

@classmethod
def get_social_auth(cls, provider: str, uid: str):
def get_social_auth(cls, provider: str, uid: Union[str, int]):
if not isinstance(uid, str):
uid = str(uid)
for social in cls.objects.select_related("user").filter(
provider=provider, uid=uid
):
Expand Down
29 changes: 26 additions & 3 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def test_get_social_auth(self):
self.assertEqual(
UserSocialAuth.get_social_auth(provider=usa.provider, uid=usa.uid), usa
)
self.assertIsNone(UserSocialAuth.get_social_auth(provider="a", uid=1))
self.assertIsNone(UserSocialAuth.get_social_auth(provider="a", uid="1"))

# Mixin
self.assertEqual(
Expand All @@ -154,15 +154,38 @@ def test_get_social_auth(self):
usa,
)
self.assertIsNone(
super(AbstractUserSocialAuth, usa).get_social_auth(provider="a", uid=1)
super(AbstractUserSocialAuth, usa).get_social_auth(provider="a", uid="1")
)

# Manager
self.assertEqual(
UserSocialAuth.objects.get_social_auth(provider=usa.provider, uid=usa.uid),
usa,
)
self.assertIsNone(UserSocialAuth.objects.get_social_auth(provider="a", uid=1))
self.assertIsNone(UserSocialAuth.objects.get_social_auth(provider="a", uid="1"))

def test_get_social_auth_int_uid(self):
usa = self.usa
int_uid = int(usa.uid)

# Model
self.assertEqual(
UserSocialAuth.get_social_auth(provider=usa.provider, uid=int_uid), usa
)

# Mixin
self.assertEqual(
super(AbstractUserSocialAuth, usa).get_social_auth(
provider=usa.provider, uid=usa.uid
),
usa,
)

# Manager
self.assertEqual(
UserSocialAuth.get_social_auth(provider=usa.provider, uid=int_uid),
usa,
)

def test_get_social_auth_for_user(self):
qs = UserSocialAuth.get_social_auth_for_user(
Expand Down