From 3ee406429819e9b33057d205923f323270d168b3 Mon Sep 17 00:00:00 2001 From: Rony Agus Vian <ravnsy@gmail.com> Date: Sun, 16 May 2021 10:08:54 +0700 Subject: [PATCH] [REFACTOR] Remove unuse statement --- apps/accounts/serializers.py | 8 -------- .../tests/test_units/test_serializers.py | 17 ++++++++++++++++- apps/accounts/views.py | 14 ++++++-------- 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/apps/accounts/serializers.py b/apps/accounts/serializers.py index 5e57c82..ec71f10 100644 --- a/apps/accounts/serializers.py +++ b/apps/accounts/serializers.py @@ -88,11 +88,3 @@ class AccountChangePassSerializer(serializers.ModelSerializer): "new_password", "confirm_new_password", ] - - def validate_password(self, value): - password_validation.validate_password(value) - return value - - def to_representation(self, instance): - serializer = AccountSerializer(instance) - return serializer.data diff --git a/apps/accounts/tests/test_units/test_serializers.py b/apps/accounts/tests/test_units/test_serializers.py index 903b51a..d898d6e 100644 --- a/apps/accounts/tests/test_units/test_serializers.py +++ b/apps/accounts/tests/test_units/test_serializers.py @@ -1,7 +1,7 @@ from django.test.testcases import TestCase from rest_framework.exceptions import ValidationError -from ...serializers import AccountSerializer, AccountRegisterSerializer +from ...serializers import AccountSerializer, AccountRegisterSerializer, AccountChangePassSerializer from ..factories.accounts import AccountFactory @@ -52,3 +52,18 @@ class AccountRegisterSerializerTest(TestCase): AccountRegisterSerializer( data=data).is_valid, raise_exception=True) + +class AccountChangePassSerializerTest(TestCase): + def test_account_change_pass_inconsistent_should_raise_error( + self): + account = AccountFactory() + data = { + 'new_password': 'justpass', + 'confirm_new_passwored': 'exampleinconsistent', + } + + self.assertRaises( + ValidationError, + AccountChangePassSerializer( + data=data).is_valid, + raise_exception=True) diff --git a/apps/accounts/views.py b/apps/accounts/views.py index 99acfce..5da6d56 100644 --- a/apps/accounts/views.py +++ b/apps/accounts/views.py @@ -141,7 +141,7 @@ class AccountViewSet(viewsets.ModelViewSet): new_password = serializer.validated_data.pop("new_password") confirm_new_password = serializer.validated_data.pop("confirm_new_password") - if User.objects.filter(username=username).exists() and new_password == confirm_new_password: + if new_password == confirm_new_password: user = User.objects.get(username=username) user.set_password(new_password) user.save() @@ -158,10 +158,8 @@ class AccountViewSet(viewsets.ModelViewSet): new_password = ''.join(SystemRandom().choice(ascii_lowercase + ascii_uppercase + digits) for _ in range(10)) - if User.objects.filter(username=username).exists(): - user = User.objects.get(username=username) - user.set_password(new_password) - user.save() - return Response({"Success, the random password is: ", - new_password}, status=status.HTTP_200_OK) - return Response("Sorry, user didn't existst", status=status.HTTP_400_BAD_REQUEST) + user = User.objects.get(username=username) + user.set_password(new_password) + user.save() + return Response({"Success, the random password is: ", + new_password}, status=status.HTTP_200_OK) -- GitLab