-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #527 from MTES-MCT/fix-user-casing
Retire la possibilité de créer des utilisateurs avec la même adresse email mais avec un casing différent
- Loading branch information
Showing
4 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Generated by Django 4.2.13 on 2024-11-26 11:07 | ||
|
||
from django.db import migrations, models | ||
import django.db.models.functions.text | ||
from django.db.utils import IntegrityError | ||
|
||
|
||
def remove_duplicate_emails(apps, schema_editor): | ||
User = apps.get_model("users", "User") | ||
users = User.objects.all() | ||
|
||
for user in users: | ||
user.email = user.email.lower() | ||
try: | ||
user.save() | ||
except IntegrityError: | ||
user.delete() | ||
|
||
|
||
class Migration(migrations.Migration): | ||
atomic = False | ||
dependencies = [ | ||
("users", "0011_alter_user_organism"), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(remove_duplicate_emails), | ||
migrations.AddConstraint( | ||
model_name="user", | ||
constraint=models.UniqueConstraint( | ||
django.db.models.functions.text.Lower("email"), name="user_email_ci_uniqueness" | ||
), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Generated by Django 4.2.13 on 2024-11-26 11:19 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("users", "0012_user_user_email_ci_uniqueness"), | ||
("users", "0015_auto_20240930_1148"), | ||
] | ||
|
||
operations = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from django.db.utils import IntegrityError | ||
from django.test import TestCase | ||
|
||
from users.models import User | ||
|
||
|
||
class TestUsers(TestCase): | ||
def test_user_with_the_same_email_cannot_be_created(self): | ||
def create_two_users_with_same_email(): | ||
User.objects.create(email="[email protected]") | ||
User.objects.create(email="[email protected]") | ||
|
||
with self.assertRaises(IntegrityError): | ||
create_two_users_with_same_email() | ||
|
||
def test_user_with_the_same_email_but_different_casing_cannot_be_created(self): | ||
def create_two_users_with_same_email_but_different_casing(): | ||
User.objects.create(email="[email protected]") | ||
User.objects.create(email="[email protected]") | ||
|
||
with self.assertRaises(IntegrityError): | ||
create_two_users_with_same_email_but_different_casing() |