mirror of
https://github.com/prowler-cloud/prowler.git
synced 2026-07-24 21:11:53 +00:00
fix(serializers): PRWLR-4869 hide email address information when it already exists (#60)
* fix(serializers): PRWLR-4869 hide email address information when it already exists * fix(serializers): PRWLR-4869 fix ruff format error
This commit is contained in:
committed by
GitHub
parent
674a38e80f
commit
98ec0532b2
@@ -69,7 +69,12 @@ class StateChoices(models.TextChoices):
|
||||
class User(AbstractBaseUser):
|
||||
id = models.UUIDField(primary_key=True, default=uuid4, editable=False)
|
||||
name = models.CharField(max_length=150, validators=[MinLengthValidator(3)])
|
||||
email = models.EmailField(max_length=254, unique=True, help_text="Case insensitive")
|
||||
email = models.EmailField(
|
||||
max_length=254,
|
||||
unique=True,
|
||||
help_text="Case insensitive",
|
||||
error_messages={"unique": "Please check the email address and try again."},
|
||||
)
|
||||
company_name = models.CharField(max_length=150, blank=True)
|
||||
is_active = models.BooleanField(default=True)
|
||||
date_joined = models.DateTimeField(auto_now_add=True, editable=False)
|
||||
|
||||
@@ -95,6 +95,45 @@ class TestUserViewSet:
|
||||
== "/data/attributes/password"
|
||||
)
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"email",
|
||||
[
|
||||
# Same email, validation error
|
||||
"nonexistentemail@prowler.com",
|
||||
# Same email with capital letters, validation error
|
||||
"NonExistentEmail@prowler.com",
|
||||
],
|
||||
)
|
||||
def test_users_create_used_email(self, authenticated_client, email):
|
||||
# First user created; no errors should occur
|
||||
user_payload = {
|
||||
"name": "test_email_validator",
|
||||
"password": "newpassword123",
|
||||
"email": "nonexistentemail@prowler.com",
|
||||
}
|
||||
response = authenticated_client.post(
|
||||
reverse("user-list"), data=user_payload, format="json"
|
||||
)
|
||||
assert response.status_code == status.HTTP_201_CREATED
|
||||
|
||||
user_payload = {
|
||||
"name": "test_email_validator",
|
||||
"password": "newpassword123",
|
||||
"email": email,
|
||||
}
|
||||
response = authenticated_client.post(
|
||||
reverse("user-list"), data=user_payload, format="json"
|
||||
)
|
||||
assert response.status_code == status.HTTP_400_BAD_REQUEST
|
||||
assert (
|
||||
response.json()["errors"][0]["source"]["pointer"]
|
||||
== "/data/attributes/email"
|
||||
)
|
||||
assert (
|
||||
response.json()["errors"][0]["detail"]
|
||||
== "Please check the email address and try again."
|
||||
)
|
||||
|
||||
def test_users_partial_update(self, authenticated_client, create_test_user):
|
||||
new_company_name = "new company test"
|
||||
payload = {
|
||||
|
||||
@@ -180,7 +180,9 @@ class UserCreateSerializer(BaseWriteSerializer):
|
||||
def validate_email(self, value):
|
||||
normalized_email = value.strip().lower()
|
||||
if User.objects.filter(email__iexact=normalized_email).exists():
|
||||
raise ValidationError("User with this email already exists.", code="unique")
|
||||
raise ValidationError(
|
||||
User._meta.get_field("email").error_messages["unique"], code="unique"
|
||||
)
|
||||
return value
|
||||
|
||||
def create(self, validated_data):
|
||||
|
||||
Reference in New Issue
Block a user