Mock auth

This commit is contained in:
Jacob Windsor 2025-02-19 13:55:42 +01:00
parent 851906a029
commit e0f8112d13
7 changed files with 66 additions and 18 deletions

View File

@ -1,10 +0,0 @@
from pydantic_settings import BaseSettings
class Settings(BaseSettings):
API_V1_STR: str = "/api/v1"
PROJECT_NAME: str = "House Price Predictor"
class Config:
case_sensitive = True
settings = Settings()

View File

@ -0,0 +1,9 @@
from fastapi import HTTPException, status
class BaseError(HTTPException):
status_code: int = status.HTTP_500_INTERNAL_SERVER_ERROR
def __init__(self, detail: str):
self.detail = detail
super().__init__(status_code=self.status_code, detail=self.detail)

View File

@ -0,0 +1,10 @@
from fastapi import status
from .base_error import BaseError
class NotAuthenticatedError(BaseError):
status_code: int = status.HTTP_401_UNAUTHORIZED
def __init__(self):
super().__init__("Not authenticated")

View File

@ -0,0 +1,38 @@
from ..errors.not_authenticated import NotAuthenticatedError
from ..models.user import User
from ..settings import get_settings
class AuthProvider:
"""
Provides authentication methods for the API.
"""
def __init__(
self,
) -> None:
if not get_settings().environment == "development":
raise NotImplementedError("AuthProvider is only implemented for development environment.")
self._authenticated_user = self._get_mocked_user()
def _get_mocked_user(self):
return User(
email="test@test.com",
username="test",
password_hash="test",
)
@property
def is_authenticated(self) -> bool:
return self._authenticated_user is not None
@property
def user(self) -> User:
"""
Returns the authenticated user.
"""
if not self._authenticated_user:
raise NotAuthenticatedError()
return self._authenticated_user

View File

@ -1,11 +1,13 @@
from fastapi import APIRouter
from fastapi import APIRouter, Depends
from typing import Annotated
from ..providers.auth_provider import AuthProvider
router = APIRouter()
@router.post("")
async def create_house():
raise NotImplementedError("This endpoint is not implemented yet.")
async def create_house(auth_provider: Annotated[AuthProvider, Depends()]):
return auth_provider.user
@router.get("/all")
@router.get("")
async def get_all_houses():
raise NotImplementedError("This endpoint is not implemented yet.")

View File

@ -10,11 +10,10 @@ import os
load_dotenv()
class _BaseConfig(BaseSettings):
pass
class _AppSettings(_BaseConfig):
environment: str = Field(default=os.getenv("ENVIRONMENT", "development"))
class _AppSettings(_BaseConfig):
pass
class _DbSettings(_BaseConfig):
username: str = Field(default=os.getenv("PG_USER"))