Skip to content

Commit 2cb718f

Browse files
committed
Add non-empty constraint for UserSocialAuth uid
Any empty uids are surely erroneous, and better to have integrity errors than having users start sharing accounts when logging in.
1 parent 292425c commit 2cb718f

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Generated by Django 5.1.7 on 2025-03-14 12:16
2+
3+
from django.conf import settings
4+
from django.db import migrations, models
5+
6+
7+
class Migration(migrations.Migration):
8+
dependencies = [
9+
("social_django", "0016_alter_usersocialauth_extra_data"),
10+
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
11+
]
12+
13+
operations = [
14+
migrations.AddConstraint(
15+
model_name="usersocialauth",
16+
constraint=models.CheckConstraint(
17+
condition=models.Q(("uid", ""), _negated=True),
18+
name="user_social_auth_uid_required",
19+
),
20+
),
21+
]

social_django/models.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def __str__(self):
4040
return str(self.user)
4141

4242
class Meta:
43+
constraints = [models.CheckConstraint(condition=~models.Q(uid=""), name="user_social_auth_uid_required")]
4344
abstract = True
4445

4546
@classmethod
@@ -67,7 +68,7 @@ def user_model(cls):
6768
class UserSocialAuth(AbstractUserSocialAuth):
6869
"""Social Auth association model"""
6970

70-
class Meta:
71+
class Meta(AbstractUserSocialAuth.Meta):
7172
"""Meta data"""
7273

7374
app_label = "social_django"

tests/test_views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def test_complete(self, mock_request):
4646
def test_disconnect(self, mock_request):
4747
user_model = get_user_model()
4848
user = user_model._default_manager.create_user(username="test", password="pwd")
49-
UserSocialAuth.objects.create(user=user, provider="facebook")
49+
UserSocialAuth.objects.create(user=user, provider="facebook", uid="some-mock-facebook-uid")
5050
self.client.login(username="test", password="pwd")
5151

5252
url = reverse("social:disconnect", kwargs={"backend": "facebook"})

0 commit comments

Comments
 (0)