From ae9d0d42251c5aa128ed8ddb367d679d563198fb Mon Sep 17 00:00:00 2001 From: Gilles <43683714+corp-0@users.noreply.github.com> Date: Sat, 2 Mar 2024 21:05:35 -0300 Subject: [PATCH] fix: small fix to some serializers to improve responses (#86) * refactor: remove any "non_field_error" from responses. They will always explicitly mention the field that errored validation. * chore: updates bruno collections data --- .../Auth/CreateAccount/Invalid identifier.bru | 8 ++++---- .../Auth/Mail confirmation/ConfirmAccount.bru | 10 ++++++++-- src/accounts/api/serializers.py | 18 ++++++++++++------ 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/api-collection/Auth/CreateAccount/Invalid identifier.bru b/api-collection/Auth/CreateAccount/Invalid identifier.bru index 9f3a941..f43f97c 100644 --- a/api-collection/Auth/CreateAccount/Invalid identifier.bru +++ b/api-collection/Auth/CreateAccount/Invalid identifier.bru @@ -12,9 +12,9 @@ post { body:json { { - "unique_identifiers": "' 'DROP TABLE USERS;", - "username": "My Name", - "password": "qweasd123", - "email": "myname@email.com" + "unique_identifier": "' 'DROP TABLE USERS;", + "username": " ", + "password": "wasd123", + "email": "mynameemail.com" } } diff --git a/api-collection/Auth/Mail confirmation/ConfirmAccount.bru b/api-collection/Auth/Mail confirmation/ConfirmAccount.bru index d39a126..2be10dc 100644 --- a/api-collection/Auth/Mail confirmation/ConfirmAccount.bru +++ b/api-collection/Auth/Mail confirmation/ConfirmAccount.bru @@ -5,7 +5,13 @@ meta { } post { - url: {{baseUrl}}/accounts/confirm-account/ - body: none + url: {{baseUrl}}/accounts/confirm-account + body: json auth: none } + +body:json { + { + "token": "asdasdasdasdasd" + } +} diff --git a/src/accounts/api/serializers.py b/src/accounts/api/serializers.py index 67093ce..eba6557 100644 --- a/src/accounts/api/serializers.py +++ b/src/accounts/api/serializers.py @@ -22,9 +22,12 @@ class Meta: fields = ("unique_identifier", "username", "password", "email") extra_kwargs = {"password": {"write_only": True}} - def validate_password(self, value): + def validate_password(self, value: str): # Validate the password using Django's built-in validators - temp_user = Account(**self.initial_data) + temp_user = Account( + unique_identifier=self.initial_data.get("unique_identifier"), + username=self.initial_data.get("username"), + ) validate_password(value, temp_user) return value @@ -69,9 +72,12 @@ class VerifyAccountSerializer(serializers.Serializer): class ResetPasswordSerializer(serializers.Serializer): password = serializers.CharField(style={"input_type": "password"}) - def validate_password(self, value): + def validate_password(self, value: str): # Validate the password using Django's built-in validators - temp_user = Account(**self.initial_data) + temp_user = Account( + unique_identifier=self.initial_data.get("unique_identifier"), + username=self.initial_data.get("username"), + ) validate_password(value, temp_user) return value @@ -83,10 +89,10 @@ def validate(self, data): try: account_confirmation = AccountConfirmation.objects.get(token=data["token"]) except AccountConfirmation.DoesNotExist: - raise serializers.ValidationError("Token is invalid or expired.") + raise serializers.ValidationError({"token": "Token is invalid or expired."}) if not account_confirmation.is_token_valid(): - raise serializers.ValidationError("Token is invalid or expired.") + raise serializers.ValidationError({"token": "Token is invalid or expired."}) return {"token": data["token"]}