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

🐛[maykinmedia/open-api-framework#39] make user emails unique #120

Merged
merged 2 commits into from
Jul 5, 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
11 changes: 11 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@
Change history
==============

2.x (TBD)
------------------

**New features**

* made user emails unique to prevent two users logging in with the same email, causing an error

.. warning::
User email addresses will now be unique on a database level. The database migration will fail if there are already
two or more users with the same email address. You must ensure this is not the case before upgrading.

2.2.0 (2024-06-27)
------------------

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 4.2.11 on 2024-07-02 14:48

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("accounts", "0001_initial"),
]

operations = [
migrations.AddConstraint(
model_name="user",
constraint=models.UniqueConstraint(
condition=models.Q(("email", ""), _negated=True),
fields=("email",),
name="filled_email_unique",
),
),
]
6 changes: 6 additions & 0 deletions src/objecttypes/accounts/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin
from django.db import models
from django.db.models import Q
from django.utils import timezone
from django.utils.translation import gettext_lazy as _

Expand Down Expand Up @@ -46,6 +47,11 @@ class User(AbstractBaseUser, PermissionsMixin):
class Meta:
verbose_name = _("user")
verbose_name_plural = _("users")
constraints = [
models.UniqueConstraint(
fields=["email"], condition=~Q(email=""), name="filled_email_unique"
)
]

def get_full_name(self):
"""
Expand Down
13 changes: 13 additions & 0 deletions src/objecttypes/accounts/tests/test_user_manager.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.db import IntegrityError
from django.test import TestCase

from ..models import User
Expand All @@ -20,3 +21,15 @@ def test_create_user(self):
self.assertFalse(user.is_superuser)
self.assertFalse(user.is_staff)
self.assertFalse(user.has_usable_password())

def test_create_users_with_same_email(self):
User.objects.create(username="AAA", email="[email protected]", password="aaa!")

with self.assertRaises(IntegrityError):
User.objects.create(username="BBB", email="[email protected]", password="bbb!")

def test_create_user_with_blank_emails(self):
User.objects.create(username="AAA", email="", password="aaa!")
User.objects.create(username="BBB", email="", password="bbb!")

self.assertEqual(User.objects.count(), 2)
Loading