forked from City-of-Helsinki/respa
-
Notifications
You must be signed in to change notification settings - Fork 3
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 #181 from andersinno/TTVA-199-allow-duplicate-emai…
…l-addresses TTVA-199 | Allow duplicate email addresses
- Loading branch information
Showing
4 changed files
with
55 additions
and
1 deletion.
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
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
37 changes: 37 additions & 0 deletions
37
users/migrations/0012_allow_duplicate_account_emailaddresses.py
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,37 @@ | ||
from django.db import migrations | ||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('account', '0002_email_max_length'), | ||
('users', '0011_change_last_name_length'), | ||
] | ||
|
||
operations = [ | ||
migrations.RunSQL( | ||
sql=[ | ||
""" | ||
DO $$ | ||
DECLARE | ||
r RECORD; | ||
BEGIN | ||
FOR r IN | ||
SELECT conname | ||
FROM pg_constraint | ||
JOIN pg_class ON conrelid = pg_class.oid | ||
WHERE pg_class.relname = 'account_emailaddress' | ||
AND conname LIKE '%email%' | ||
AND contype = 'u' | ||
LOOP | ||
EXECUTE 'ALTER TABLE account_emailaddress DROP CONSTRAINT ' || r.conname; | ||
END LOOP; | ||
END $$; | ||
""", | ||
"ALTER TABLE account_emailaddress ADD CONSTRAINT account_emailaddress_email_key UNIQUE (user_id, email);", | ||
], | ||
reverse_sql=[ | ||
"ALTER TABLE account_emailaddress DROP CONSTRAINT account_emailaddress_email_key;", | ||
"ALTER TABLE account_emailaddress ADD CONSTRAINT account_emailaddress_email_key UNIQUE (email);", | ||
], | ||
), | ||
] |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from allauth.account.models import EmailAddress | ||
from django.conf import settings | ||
from django.contrib.auth import get_user_model | ||
from django.urls import reverse | ||
from rest_framework import status | ||
|
@@ -64,6 +65,7 @@ def test_set_email_not_provided(self): | |
self.assertEqual(self.user.email, "") | ||
|
||
def test_set_email_already_in_use(self): | ||
settings.ACCOUNT_UNIQUE_EMAIL = True | ||
url = reverse("user-set-email", kwargs={"pk": self.user.pk}) | ||
data = {"email": "[email protected]"} # Email already used by other_user | ||
|
||
|
@@ -74,3 +76,16 @@ def test_set_email_already_in_use(self): | |
response.data, {"detail": "The email address is already in use."} | ||
) | ||
self.assertEqual(self.user.email, "") | ||
|
||
def test_allow_to_set_duplicate_email(self): | ||
settings.ACCOUNT_UNIQUE_EMAIL = False | ||
url = reverse("user-set-email", kwargs={"pk": self.user.pk}) | ||
data = {"email": "[email protected]"} # Email already used by other_user | ||
|
||
response = self.client.post(url, data, format="json") | ||
|
||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual( | ||
response.data, {"detail": "Email set successfully."} | ||
) | ||
self.assertEqual(self.user.email, "[email protected]") |