-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
babd29f
commit c1db25b
Showing
8 changed files
with
227 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from sqlalchemy import Column, Integer, String | ||
|
||
from src.settings.database import Base | ||
|
||
|
||
class User(Base): | ||
__tablename__ = "users" | ||
|
||
id = Column(Integer, primary_key=True, autoincrement=True) | ||
username = Column(String, nullable=False) | ||
email = Column(String, nullable=False) | ||
password = Column(String, nullable=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from fastapi import APIRouter, Depends, status | ||
from sqlalchemy.orm import Session | ||
|
||
from src.settings.database import SessionLocal | ||
from . import schemas, service | ||
|
||
|
||
def get_db(): | ||
db = SessionLocal() | ||
try: | ||
yield db | ||
finally: | ||
db.close() | ||
|
||
|
||
router = APIRouter() | ||
|
||
|
||
@router.post("/users/") | ||
async def valid_user(user: schemas.UserSchema, db: Session = Depends(get_db)): | ||
return service.validate_user(db, user.username) | ||
|
||
|
||
@router.post( | ||
"/login/", | ||
) | ||
async def login( | ||
user: schemas.UserSchema, | ||
db: Session = Depends(get_db), | ||
): | ||
return service.login(db, user) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from pydantic import BaseModel, ConfigDict | ||
|
||
|
||
class UserSchema(BaseModel): | ||
id: int | None = None | ||
username: str | None = None | ||
email: str | ||
password: str | ||
|
||
model_config = ConfigDict(from_attributes=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from fastapi.responses import JSONResponse | ||
from fastapi import status | ||
from sqlalchemy.orm import Session | ||
|
||
from . import models, schemas | ||
|
||
|
||
def validate_user(db: Session, username: str): | ||
user = db.query(models.User).first() | ||
if username == user.username: | ||
return JSONResponse(content={"is_user_valid": True}) | ||
return JSONResponse(content={"is_user_valid": False}) | ||
|
||
|
||
def login(db: Session, user: schemas.UserSchema): | ||
stored_user = db.query(models.User).first() | ||
if stored_user.email != user.email: | ||
return JSONResponse( | ||
status_code=status.HTTP_400_BAD_REQUEST, | ||
content={"erorr": "Invalid credentials"}, | ||
) | ||
if stored_user.password != user.password: | ||
return JSONResponse( | ||
status_code=status.HTTP_400_BAD_REQUEST, | ||
content={"erorr": "Invalid credentials"}, | ||
) | ||
return JSONResponse(content={"success": True}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import os | ||
import json | ||
|
||
import pytest | ||
from sqlalchemy.orm import Session | ||
from fastapi.responses import JSONResponse | ||
from fastapi import FastAPI | ||
from fastapi.testclient import TestClient | ||
from unittest.mock import patch | ||
from src.settings.database import create_database, get_db | ||
from src.users.router import router | ||
from src.users.schemas import UserSchema | ||
from src.users.models import User | ||
|
||
app = FastAPI() | ||
app.include_router(router) | ||
|
||
|
||
@pytest.fixture | ||
def client(): | ||
with TestClient(router) as client: | ||
yield client | ||
|
||
|
||
@pytest.fixture | ||
def mock_db_session(): | ||
session = Session() | ||
try: | ||
yield session | ||
session.rollback() | ||
finally: | ||
session.close() | ||
|
||
|
||
app.dependency_overrides[get_db] = mock_db_session | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def run_around_tests(): | ||
# Setup: can be used to initialize the database | ||
create_database() | ||
|
||
yield # this is where the testing happens | ||
|
||
# Teardown | ||
try: | ||
os.remove("test.db") | ||
except: | ||
""" | ||
Do nothing | ||
""" | ||
|
||
|
||
def test_db(): | ||
assert os.getenv("DATABASE_URL") == "sqlite:///./test.db" | ||
|
||
|
||
def test_validate_user(client): | ||
with patch( | ||
"src.users.service.validate_user", | ||
return_value=JSONResponse(content={"is_valid_user": True}), | ||
): | ||
response = client.post( | ||
"/users/", | ||
json={"email": "[email protected]", "password": "test_password"}, | ||
) | ||
expected_status_code = 200 | ||
assert response.content == b'{"is_valid_user":true}' | ||
assert response.status_code == expected_status_code | ||
|
||
|
||
def test_login(client): | ||
with patch( | ||
"src.users.service.login", return_value=JSONResponse(content={"success": True}) | ||
): | ||
response = client.post( | ||
"/login/", json={"email": "[email protected]", "password": "test_password"} | ||
) | ||
expected_status_code = 200 | ||
assert response.status_code == expected_status_code | ||
assert response.content == b'{"success":true}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import os | ||
import json | ||
import pytest | ||
from fastapi import FastAPI | ||
from fastapi.testclient import TestClient | ||
from unittest.mock import Mock | ||
from sqlalchemy.orm import Session | ||
from src.settings.database import create_database, get_db | ||
from src.users import service, schemas | ||
from src.users.router import router | ||
|
||
|
||
app = FastAPI() | ||
app.include_router(router) | ||
|
||
|
||
@pytest.fixture | ||
def client(): | ||
with TestClient(router) as client: | ||
yield client | ||
|
||
|
||
@pytest.fixture | ||
def mock_db_session(): | ||
return Mock(spec=Session) | ||
|
||
|
||
app.dependency_overrides[get_db] = mock_db_session | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def run_around_tests(): | ||
# Setup: can be used to initialize the database | ||
create_database() | ||
|
||
yield # this is where the testing happens | ||
|
||
# Teardown | ||
try: | ||
os.remove("test.db") | ||
except: | ||
""" | ||
Do nothing | ||
""" | ||
|
||
|
||
def test_validate_user(mock_db_session): | ||
username = "test_username" | ||
tasks = service.validate_user(mock_db_session, username) | ||
assert tasks is not None | ||
|
||
|
||
def test_login(mock_db_session): | ||
user = schemas.UserSchema( | ||
username="test_username", email="[email protected]", password="test_password" | ||
) | ||
tasks = service.login(mock_db_session, user) | ||
assert tasks is not None |