Skip to content

Commit e5740aa

Browse files
Zaimwa9emyllerpre-commit-ci[bot]
authored
fix: check-split-length-before-accessing-with-index (#5848)
Co-authored-by: Evandro Myller <22429+emyller@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 3a46748 commit e5740aa

5 files changed

Lines changed: 49 additions & 4 deletions

File tree

api/custom_auth/serializers.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
import re
12
from typing import Any
23

34
from common.core.utils import is_saas
45
from django.conf import settings
5-
from djoser.serializers import UserCreateSerializer # type: ignore[import-untyped]
6+
from djoser.serializers import ( # type: ignore[import-untyped]
7+
TokenCreateSerializer,
8+
UserCreateSerializer,
9+
)
610
from rest_framework import serializers
711
from rest_framework.authtoken.models import Token
812
from rest_framework.exceptions import PermissionDenied
@@ -19,6 +23,15 @@
1923
USER_REGISTRATION_WITHOUT_INVITE_ERROR_MESSAGE,
2024
)
2125

26+
EMAIL_REGEX = re.compile(r"^[^@]+@(?:\w+\.)+\w{2,}$")
27+
28+
29+
class CustomTokenCreateSerializer(TokenCreateSerializer): # type: ignore[misc]
30+
def validate_email(self, value: str) -> str:
31+
if not EMAIL_REGEX.match(value):
32+
raise serializers.ValidationError("Invalid email format.")
33+
return value
34+
2235

2336
class CustomTokenSerializer(serializers.ModelSerializer): # type: ignore[type-arg]
2437
class Meta:

api/custom_auth/views.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from custom_auth.mfa.trench.responses import ErrorResponse
3131
from custom_auth.mfa.trench.serializers import CodeLoginSerializer
3232
from custom_auth.mfa.trench.utils import user_token_generator
33-
from custom_auth.serializers import CustomUserDelete
33+
from custom_auth.serializers import CustomTokenCreateSerializer, CustomUserDelete
3434
from integrations.lead_tracking.hubspot.services import (
3535
register_hubspot_tracker_and_track_user,
3636
)
@@ -46,6 +46,7 @@ class CustomAuthTokenLoginOrRequestMFACode(TokenCreateView): # type: ignore[mis
4646
Class to handle throttling for login requests
4747
"""
4848

49+
serializer_class = CustomTokenCreateSerializer
4950
authentication_classes = [] # type: ignore[var-annotated]
5051
throttle_classes = [ScopedRateThrottle]
5152
throttle_scope = "login"

api/integrations/lead_tracking/hubspot/services.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ def register_hubspot_tracker(
6969
def create_self_hosted_onboarding_lead(
7070
email: str, first_name: str, last_name: str, organisation_name: str
7171
) -> None:
72-
email_domain = email.split("@")[1]
72+
email_parts = email.split("@")
73+
email_domain = email_parts[1] if len(email_parts) > 1 else organisation_name
7374
hubspot_client = HubspotClient()
7475
company = hubspot_client.get_company_by_domain(email_domain)
7576
if not company:

api/tests/integration/custom_auth/end_to_end/test_custom_auth_integration.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,3 +746,33 @@ def test_marketing_consent_given_defaults_to_true(
746746
# Then
747747
assert response.status_code == status.HTTP_201_CREATED
748748
assert response.json()["marketing_consent_given"] is True
749+
750+
751+
@pytest.mark.parametrize(
752+
"invalid_email",
753+
[
754+
"invalid_email",
755+
"12345",
756+
"invalid@email@com.com",
757+
"invalid_email.com",
758+
"invalid_email@com",
759+
"foo@..!.bar.",
760+
],
761+
)
762+
def test_create_user_returns_error_if_email_is_invalid(
763+
staff_client: APIClient,
764+
invalid_email: str,
765+
) -> None:
766+
# Given
767+
url = reverse("api-v1:custom_auth:custom-mfa-authtoken-login")
768+
register_data = {
769+
"email": invalid_email,
770+
"password": "password",
771+
}
772+
773+
# When
774+
response = staff_client.post(url, data=register_data)
775+
776+
# Then
777+
assert response.status_code == status.HTTP_400_BAD_REQUEST
778+
assert response.json()["email"][0] == "Invalid email format."

api/tests/unit/users/test_unit_users_models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,5 +241,5 @@ def test_delete_user(): # type: ignore[no-untyped-def]
241241
assert Organisation.objects.filter(name="org1").count() == 1
242242

243243

244-
def test_user_email_domain_property(): # type: ignore[no-untyped-def]
244+
def test_user_email_domain_property() -> None:
245245
assert FFAdminUser(email="test@example.com").email_domain == "example.com"

0 commit comments

Comments
 (0)