From 2cb718f34a371d0588f4ae8b41979bfcb35174b7 Mon Sep 17 00:00:00 2001 From: Aleksander Vognild Burkow Date: Thu, 21 Mar 2024 17:26:50 +0100 Subject: [PATCH] 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. --- ...ocialauth_user_social_auth_uid_required.py | 21 +++++++++++++++++++ social_django/models.py | 3 ++- tests/test_views.py | 2 +- 3 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 social_django/migrations/0017_usersocialauth_user_social_auth_uid_required.py diff --git a/social_django/migrations/0017_usersocialauth_user_social_auth_uid_required.py b/social_django/migrations/0017_usersocialauth_user_social_auth_uid_required.py new file mode 100644 index 00000000..971f37d0 --- /dev/null +++ b/social_django/migrations/0017_usersocialauth_user_social_auth_uid_required.py @@ -0,0 +1,21 @@ +# Generated by Django 5.1.7 on 2025-03-14 12:16 + +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("social_django", "0016_alter_usersocialauth_extra_data"), + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.AddConstraint( + model_name="usersocialauth", + constraint=models.CheckConstraint( + condition=models.Q(("uid", ""), _negated=True), + name="user_social_auth_uid_required", + ), + ), + ] diff --git a/social_django/models.py b/social_django/models.py index cc65c613..70f9d995 100644 --- a/social_django/models.py +++ b/social_django/models.py @@ -40,6 +40,7 @@ def __str__(self): return str(self.user) class Meta: + constraints = [models.CheckConstraint(condition=~models.Q(uid=""), name="user_social_auth_uid_required")] abstract = True @classmethod @@ -67,7 +68,7 @@ def user_model(cls): class UserSocialAuth(AbstractUserSocialAuth): """Social Auth association model""" - class Meta: + class Meta(AbstractUserSocialAuth.Meta): """Meta data""" app_label = "social_django" diff --git a/tests/test_views.py b/tests/test_views.py index 1a3de5e0..db10f3b5 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -46,7 +46,7 @@ def test_complete(self, mock_request): def test_disconnect(self, mock_request): user_model = get_user_model() user = user_model._default_manager.create_user(username="test", password="pwd") - UserSocialAuth.objects.create(user=user, provider="facebook") + UserSocialAuth.objects.create(user=user, provider="facebook", uid="some-mock-facebook-uid") self.client.login(username="test", password="pwd") url = reverse("social:disconnect", kwargs={"backend": "facebook"})