Skip to content
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

Raise ValueError in phone validator #552

Open
wants to merge 3 commits into
base: main
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
5 changes: 0 additions & 5 deletions app/types/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,3 @@ def __init__(self, email: str):
super().__init__(
f"An account with the email {email} already exist",
)


class PhoneNumberValidationError(Exception):
def __init__(self):
super().__init__("The phone number is not valid")
13 changes: 8 additions & 5 deletions app/utils/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

import phonenumbers

from app.types.exceptions import PhoneNumberValidationError


def password_validator(password: str) -> str:
"""
Expand All @@ -26,11 +24,16 @@
This function is intended to be used as a Pydantic validator:
https://pydantic-docs.helpmanual.io/usage/validators/#reuse-validators
"""
parsed_phone = phonenumbers.parse(phone, None)

# We need to raise a ValueError for Pydantic to catch it and return an error response
try:
parsed_phone = phonenumbers.parse(phone, None)
except Exception as error:
raise ValueError(f"Invalid phone number: {error}") # noqa: B904, TRY003

Check warning on line 32 in app/utils/validators.py

View check run for this annotation

Codecov / codecov/patch

app/utils/validators.py#L31-L32

Added lines #L31 - L32 were not covered by tests
if not phonenumbers.is_possible_number(parsed_phone):
raise PhoneNumberValidationError
raise ValueError("Invalid phone number, number is not possible") # noqa: TRY003

Check warning on line 34 in app/utils/validators.py

View check run for this annotation

Codecov / codecov/patch

app/utils/validators.py#L34

Added line #L34 was not covered by tests
if not phonenumbers.is_valid_number(parsed_phone):
raise PhoneNumberValidationError
raise ValueError("Invalid phone number, number is not valid") # noqa: TRY003

Check warning on line 36 in app/utils/validators.py

View check run for this annotation

Codecov / codecov/patch

app/utils/validators.py#L36

Added line #L36 was not covered by tests
return phonenumbers.format_number(parsed_phone, phonenumbers.PhoneNumberFormat.E164)


Expand Down
30 changes: 30 additions & 0 deletions tests/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,36 @@ def test_create_and_activate_user(mocker: MockerFixture, client: TestClient) ->
assert response.status_code == 201


@pytest.mark.parametrize(
("email", "expected_error"),
[
(
"123456789",
"Value error, Invalid phone number: (0) Missing or invalid default region.",
),
("+3300000000000", "Value error, Invalid phone number, number is not possible"),
("+33000000000", "Value error, Invalid phone number, number is not valid"),
],
)
def activate_user_with_invalid_phone_number(
phone: str,
expected_error: str,
client: TestClient,
) -> None:
response = client.post(
"/users/activate",
json={
"activation_token": UNIQUE_TOKEN,
"password": "password",
"firstname": "firstname",
"name": "name",
"phone": phone,
},
)
assert response.status_code == 422
assert response.json()["detail"][0]["msg"] == expected_error


def test_update_batch_create_users(client: TestClient) -> None:
student = "39691052-2ae5-4e12-99d0-7a9f5f2b0136"
response = client.post(
Expand Down