Skip to content

Commit

Permalink
Merge pull request activist-org#907 from thesarfo/test/password-reset…
Browse files Browse the repository at this point in the history
…-endpoints

Tests for the password reset endpoints
  • Loading branch information
andrewtavis authored Jun 29, 2024
2 parents 99b25e9 + e353594 commit b0b62a7
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
2 changes: 1 addition & 1 deletion backend/authentication/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Meta:
description = factory.Faker("text", max_nb_chars=500)
verified = factory.Faker("boolean")
verification_method = factory.Faker("word")
verification_code = factory.Faker("uuid4")
verifictaion_code = factory.Faker("uuid4")
email = factory.Faker("email")
social_links = factory.List([factory.Faker("user_name") for _ in range(3)])
is_private = factory.Faker("boolean")
Expand Down
8 changes: 6 additions & 2 deletions backend/authentication/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,15 @@ def validate(self, data: Dict[str, Union[str, Any]]) -> Dict[str, Union[str, Any


class PasswordResetSerializer(serializers.Serializer[UserModel]):
email = serializers.EmailField()
email = serializers.EmailField(required=False)
password = serializers.CharField(write_only=True)
code = serializers.UUIDField(required=False)

def validate(self, data: Dict[str, Union[str, Any]]) -> UserModel:
user = UserModel.objects.filter(email=data.get("email")).first()
if data.get("code") is not None:
user = UserModel.objects.filter(verifictaion_code=data.get("code")).first()
else:
user = UserModel.objects.filter(email=data.get("email")).first()

if user is None:
raise serializers.ValidationError(
Expand Down
33 changes: 31 additions & 2 deletions backend/authentication/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from .models import UserModel
from django.test import Client
from uuid import UUID
import uuid


@pytest.mark.django_db
Expand Down Expand Up @@ -210,15 +211,43 @@ def test_pwreset(client: Client) -> None:
Scenarios:
1. Password reset email is sent successfully
2. Password reset with invalid email
3. Password reset is performed successfully
4. Password reset with invalid verification code
"""
# Setup
plaintext_password = "Activist@123!?"
user = UserFactory(plaintext_password=plaintext_password)
old_password = "password123!?"
new_password = "Activist@123!?"

# 1. User exists and password reset is successful
user = UserFactory(plaintext_password=old_password)
response = client.get(
path="/v1/auth/pwreset/",
data={"email": user.email},
)
assert response.status_code == 200
assert len(mail.outbox) == 1

# 2. Password reset with invalid email
response = client.get(
path="/v1/auth/pwreset/", data={"email": "[email protected]"}
)
assert response.status_code == 404

# 3. Password reset is performed successfully
user.verifictaion_code = uuid.uuid4()
user.save()
response = client.post(
path=f"/v1/auth/pwreset/?code={user.verifictaion_code}",
data={"password": new_password},
)
assert response.status_code == 200
user.refresh_from_db()
assert user.check_password(new_password)

# 4. Password reset with invalid verification code
response = client.post(
path="/v1/auth/pwreset/invalid_code/",
data={"password": new_password},
)
assert response.status_code == 404
8 changes: 6 additions & 2 deletions backend/authentication/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,14 @@ def get(self, request: Request) -> Response:
)

def post(self, request: Request) -> Response:
serializer = PasswordResetSerializer(data=request.data)
data = {
"password": request.data.get("password"),
"code": request.query_params.get("code"),
}
serializer = PasswordResetSerializer(data=data)
serializer.is_valid(raise_exception=True)

user = serializer.validated_data
user: UserModel = serializer.validated_data

user.set_password(request.data.get("password"))
user.save()
Expand Down

0 comments on commit b0b62a7

Please sign in to comment.