Skip to content

Commit

Permalink
minor fixes on the pwreset test and fixed pwreset view and serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
to-sta committed Jun 23, 2024
1 parent cb07e3b commit 7169d1a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion backend/authentication/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,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 @@ -177,11 +177,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
14 changes: 7 additions & 7 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 @@ -215,11 +216,11 @@ def test_pwreset(client: Client) -> None:
4. Password reset with invalid verification code
"""
# Setup
faker = Faker()
old_password = "password123!?"
new_password = "Activist@123!?"

# 1. User exists and password reset is successful
user = UserFactory()
user = UserFactory(plaintext_password=old_password)
response = client.get(
path="/v1/auth/pwreset/",
data={"email": user.email},
Expand All @@ -229,17 +230,16 @@ def test_pwreset(client: Client) -> None:

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

# 3. Password reset is performed successfully
user.verification_code = uuid.uuid4()
user.verifictaion_code = uuid.uuid4()
user.save()
response = client.post(
path=f"/v1/auth/pwreset/{user.verification_code}/",
data={"password": new_password}
path=f"/v1/auth/pwreset/?code={user.verifictaion_code}",
data={"password": new_password},
)
assert response.status_code == 200
user.refresh_from_db()
Expand Down
8 changes: 6 additions & 2 deletions backend/authentication/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,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
2 changes: 1 addition & 1 deletion backend/entities/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Organization(models.Model):
status = models.ForeignKey(
"StatusType",
on_delete=models.CASCADE,
default=StatusTypes.PENDING,
default=StatusTypes.PENDING.value,
blank=True,
null=True,
)
Expand Down

0 comments on commit 7169d1a

Please sign in to comment.