Skip to content

Update password reset functionality #31

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

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions sigma/authentication/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,16 @@ def to_representation(self, instance):


class ChangePasswordSerializer(serializers.Serializer):
old_password = serializers.CharField(write_only=True)
email = serializers.EmailField(write_only=True)
new_password = serializers.CharField(write_only=True)

def validate_old_password(self, value):
"""This is for validating old password"""
def validate_email(self, value):
"""This is for validating email"""

user = self.context["request"].user
user = User.objects.filter(email=value).first()

if not user.check_password(value):
raise serializers.ValidationError("Wrong Old Password", code="Invalid Password")
if user is None:
raise serializers.ValidationError("User doesn't exist", code="Not Found")

return value

Expand Down
2 changes: 1 addition & 1 deletion sigma/authentication/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
urlpatterns = [
path("register/admin/", RegisterAdminAPIView.as_view(), name="register_admin"),
path("login/", LoginInAPIView.as_view(), name="login"),
path("password/change/", ChangePasswordAPIView.as_view(), name="change_password"),
path("password/reset/", ChangePasswordAPIView.as_view(), name="change_password"),
]
6 changes: 2 additions & 4 deletions sigma/authentication/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,9 @@ def post(self, request, *args, **kwargs):

class ChangePasswordAPIView(generics.GenericAPIView):
serializer_class = ChangePasswordSerializer
permission_classes = [
permissions.IsAuthenticated,
]
permission_classes = (permissions.IsAuthenticated,)

def post(self, request, *args, **kwargs):
def put(self, request, *args, **kwargs):
user = self.request.user

serializer = self.get_serializer(data=request.data)
Expand Down
4 changes: 2 additions & 2 deletions tests/authentication/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,13 @@ def test_error_raised_for_short_length_of_password(self):
),
)

def test_error_not_raised_when_when_valid_data_are_provided(self):
def test_error_not_raised_when_valid_data_are_provided(self):
"""Test user password changed when valid data are provided"""

request = MagicMock()
request.user = self.user

data = {"old_password": "old_password", "new_password": "new_password"}
data = {"email": request.user.email, "new_password": "new_password"}

serializer = ChangePasswordSerializer(data=data, context={"request": request})
self.assertTrue(serializer.is_valid())
15 changes: 7 additions & 8 deletions tests/authentication/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,8 @@ def test_user_can_change_password_with_valid_data(self):

self.client.force_authenticate(self.user)

request_data = {"old_password": "old_password", "new_password": "new_password"}

response = self.client.post(self.url, request_data, format="json")
request_data = {"email": self.user.email, "new_password": "new_password"}
response = self.client.put(self.url, request_data, format="json")

self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), {"message": "Password Successfully updated"})
Expand All @@ -190,23 +189,23 @@ def test_user_cannot_change_password_with_invalid_data(self):

self.client.force_authenticate(self.user)

request_data = {"old_password": "old_password", "new_password": "new"}
request_data = {"email": "[email protected]", "new_password": "new"}

response = self.client.post(self.url, request_data, format="json")
response = self.client.put(self.url, request_data, format="json")

self.assertEqual(response.status_code, 400)

user = User.objects.filter(email="[email protected]").first()
self.assertTrue(user.check_password(request_data["old_password"]))
self.assertFalse(user.check_password(request_data["new_password"]))

def test_user_new_password_cannot_be_same_as_old_password(self):
"""Test user new password cannot be same as old password"""

self.client.force_authenticate(self.user)

request_data = {"old_password": "old_password", "new_password": "old_password"}
request_data = {"email": self.user.email, "new_password": "old_password"}

response = self.client.post(self.url, request_data)
response = self.client.put(self.url, request_data)

self.assertEqual(response.status_code, 400)
self.assertEqual(
Expand Down
Loading