forked from City-of-Helsinki/respa
-
Notifications
You must be signed in to change notification settings - Fork 3
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
TTVA-199 | Allow duplicate email addresses #181
Merged
jorilindell
merged 1 commit into
tre-varaamo-qa
from
TTVA-199-allow-duplicate-email-addresses
Sep 23, 2024
+55
−1
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should check if the constraint exists before trying to remove it. Would something like this work:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking something like this: