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

fix: server crashes on login if password is not provided in the request payload #1215

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from
18 changes: 12 additions & 6 deletions api/v1/schemas/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
List, Annotated, Dict,
Literal)

from pydantic import (BaseModel, EmailStr,
field_validator, ConfigDict,
StringConstraints,
model_validator)

from pydantic import Field # Added this import
from pydantic import (
BaseModel,
EmailStr,
field_validator,
ConfigDict,
StringConstraints,
model_validator,
Field
)

def validate_mx_record(domain: str):
"""
Expand Down Expand Up @@ -252,6 +255,9 @@ def validate_password(cls, values: dict):
email = values.get("email")
totp_code = values.get("totp_code")

if not password:
return values

# constraints for password
if not any(c.islower() for c in password):
raise ValueError("password must include at least one lowercase character")
Expand Down
17 changes: 17 additions & 0 deletions tests/v1/auth/test_signin.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,23 @@ def test_swagger_ui_auth_form_handling(self):
assert response_json.get("status_code") == 422
assert response_json.get("message") == "Invalid input" or "Invalid" in response_json.get("message", "")

def test_user_login_failure_without_password(self, monkeypatch):
"""Test login failure when password is not provided"""

monkeypatch.setattr(
user_service,
"authenticate_user",
lambda db, email, password: self.mock_user
)

response = self.client.post(
"/api/v1/auth/login",
json={"email": "[email protected]"},
)
response_json = response.json()

assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
assert response_json.get("message") == "Invalid input"

# Mock the database dependency
@pytest.fixture
Expand Down