diff --git a/posthog/api/test/test_user.py b/posthog/api/test/test_user.py index 472ddb5aad468..9eaeefee08624 100644 --- a/posthog/api/test/test_user.py +++ b/posthog/api/test/test_user.py @@ -1403,7 +1403,7 @@ def test_user_can_reset_password(self, mock_capture, mock_identify): "organization": str(self.team.organization_id), "project": str(self.team.uuid), }, - ) + ) # user logged in after password successfully reset mock_capture.assert_any_call( self.user.distinct_id, "user password reset", @@ -1413,7 +1413,9 @@ def test_user_can_reset_password(self, mock_capture, mock_identify): "project": str(self.team.uuid), }, ) - self.assertEqual(mock_capture.call_count, 2) + self.assertEqual( + mock_capture.call_count, 3 + ) # extra login event from user being logged out and back in during the test def test_cant_set_short_password(self): token = password_reset_token_generator.make_token(self.user) diff --git a/posthog/api/user.py b/posthog/api/user.py index f7ca2b56114a7..3709e57c20960 100644 --- a/posthog/api/user.py +++ b/posthog/api/user.py @@ -453,17 +453,18 @@ def validate_password_reset_token(self, user_uuid: str, token: str) -> bool: {"token": ["This reset token is invalid or has expired."]}, code="invalid_token", ) - token_valid = PasswordResetter.check_token(user, token) - if not token_valid: - capture_exception( - Exception("Invalid password reset token in serializer"), - {"user_uuid": user.uuid, "token": token}, - ) - raise serializers.ValidationError( - {"token": ["This reset token is invalid or has expired."]}, - code="invalid_token", - ) - return True + else: + token_valid = PasswordResetter.check_token(user, token) + if not token_valid: + capture_exception( + Exception("Invalid password reset token in serializer"), + {"user_uuid": user.uuid, "token": token}, + ) + raise serializers.ValidationError( + {"token": ["This reset token is invalid or has expired."]}, + code="invalid_token", + ) + return True @action( methods=["POST"], @@ -476,11 +477,11 @@ def validate_password_reset(self, request, **kwargs): user_uuid = request.data["uuid"] try: self.validate_password_reset_token(user_uuid, token) - except ValidationError as e: + except ValidationError: error_response = { "type": "validation_error", - "code": e.detail["token"][0].code, # Assuming the error detail structure from your ValidationError - "detail": e.detail["token"][0], + "code": "invalid_token", + "detail": "This reset token is invalid or has expired.", "attr": "token", } return Response(error_response, status=status.HTTP_400_BAD_REQUEST) @@ -501,17 +502,16 @@ def reset_password(self, request, **kwargs): return Response(status=status.HTTP_204_NO_CONTENT) try: self.validate_password_reset_token(user_uuid, token) - except ValidationError as e: + user: Optional[User] = User.objects.filter(is_active=True).get(uuid=user_uuid) + except ValidationError: error_response = { "type": "validation_error", - "code": e.detail["token"][0].code, # Assuming the error detail structure from your ValidationError - "detail": e.detail["token"][0], - "attr": "tokenyy", + "code": "invalid_token", + "detail": "This reset token is invalid or has expired.", + "attr": "token", } return Response(error_response, status=status.HTTP_400_BAD_REQUEST) - user: Optional[User] = User.objects.filter(is_active=True).get(uuid=user_uuid) - password = request.data["password"] if "password" in request.data else None try: diff --git a/posthog/tasks/email.py b/posthog/tasks/email.py index cecce77a797ec..b53d2bb796098 100644 --- a/posthog/tasks/email.py +++ b/posthog/tasks/email.py @@ -72,7 +72,7 @@ def send_member_join(invitee_uuid: str, organization_id: str) -> None: @shared_task(**EMAIL_TASK_KWARGS) def send_password_reset(user_id: int, token: str) -> None: - user: User = User.objects.get(pk=user_id) + user = User.objects.get(pk=user_id) message = EmailMessage( campaign_key=f"password-reset-{user.uuid}-{timezone.now().timestamp()}", subject=f"Reset your PostHog password", diff --git a/posthog/tasks/test/test_email.py b/posthog/tasks/test/test_email.py index 571132fd1ca84..e96b6d780ab48 100644 --- a/posthog/tasks/test/test_email.py +++ b/posthog/tasks/test/test_email.py @@ -3,8 +3,8 @@ from freezegun import freeze_time -from posthog.api.authentication import password_reset_token_generator from posthog.api.email_verification import email_verification_token_generator +from posthog.api.password_reset import password_reset_token_generator from posthog.models import Organization, Team, User from posthog.models.instance_setting import set_instance_setting from posthog.models.organization import OrganizationInvite, OrganizationMembership