Skip to content

Commit

Permalink
fix: #571 coerce uid to str
Browse files Browse the repository at this point in the history
  • Loading branch information
i-zhivetiev authored and nijel committed May 7, 2024
1 parent 0b64a56 commit 7d8b751
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
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

0 comments on commit 7d8b751

Please sign in to comment.