Skip to content

Commit

Permalink
fix(user-create): duplicate email error message (#2642)
Browse files Browse the repository at this point in the history
  • Loading branch information
gagantrivedi authored Aug 16, 2023
1 parent db98743 commit 7b65a8d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
20 changes: 15 additions & 5 deletions api/custom_auth/serializers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from django.conf import settings
from djoser.conf import settings as djoser_settings
from djoser.serializers import UserCreateSerializer, UserDeleteSerializer
from rest_framework import serializers
from rest_framework.authtoken.models import Token
from rest_framework.exceptions import PermissionDenied
from rest_framework.validators import UniqueValidator

from organisations.invites.models import Invite
from users.constants import DEFAULT_DELETE_ORPHAN_ORGANISATIONS_VALUE
Expand All @@ -18,17 +20,27 @@ class Meta:


class CustomUserCreateSerializer(UserCreateSerializer):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["key"] = serializers.SerializerMethodField()
key = serializers.SerializerMethodField()

class Meta(UserCreateSerializer.Meta):
fields = UserCreateSerializer.Meta.fields + (
"is_active",
"marketing_consent_given",
"key",
)
read_only_fields = ("is_active",)
write_only_fields = ("sign_up_type",)
extra_kwargs = {
"email": {
"validators": [
UniqueValidator(
queryset=FFAdminUser.objects.all(),
lookup="iexact",
message=djoser_settings.CONSTANTS.messages.CANNOT_CREATE_USER_ERROR,
)
]
}
}

def validate(self, attrs):
attrs = super().validate(attrs)
Expand All @@ -42,8 +54,6 @@ def validate(self, attrs):
self.context.get("request"), email=email, raise_exception=True
)

if FFAdminUser.objects.filter(email__iexact=email).count() != 0:
raise serializers.ValidationError({"detail": "Unable to create account"})
attrs["email"] = email.lower()
return attrs

Expand Down
2 changes: 1 addition & 1 deletion api/custom_auth/tests/test_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def test_CustomUserCreateSerializer_does_case_insensitive_lookup_with_email(db):

# When
assert serializer.is_valid() is False
assert serializer.errors["detail"][0].title() == "Unable To Create Account"
assert serializer.errors["email"][0].title() == "Unable To Create Account."


def test_CustomUserCreateSerializer_calls_is_authentication_method_valid_correctly_if_auth_controller_is_installed(
Expand Down

3 comments on commit 7b65a8d

@vercel
Copy link

@vercel vercel bot commented on 7b65a8d Aug 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

docs – ./docs

docs-flagsmith.vercel.app
docs-git-main-flagsmith.vercel.app
docs.bullet-train.io
docs.flagsmith.com

@vercel
Copy link

@vercel vercel bot commented on 7b65a8d Aug 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 7b65a8d Aug 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.