Esempio n. 1
0
def auth_token(
    config: Config,
    logger: Logger,
) -> Generator[Dict[str, str], None, None]:
    retailer_repository = RetailerRepository(config, logger)
    auth_service = AuthService(config, logger, retailer_repository)

    reatailer_model = RetailerModel(
        full_name="Authenticated User",
        document="99899899899",
        email=EmailStr("*****@*****.**"),
        password="******",
    )

    retailer_repository.insert_retailer(reatailer_model)
    retailer = retailer_repository.get_retailer_by_email("*****@*****.**")

    auth_token = auth_service.generate_access_token(
        {"sub": "*****@*****.**"},
        expires_delta=timedelta(
            minutes=int(config.get_config("ACCESS_TOKEN_EXPIRE_MINUTES"))
        ),
    )

    yield {"Authorization": f"Bearer {auth_token}"}
    if retailer:
        retailer_repository.delete_retailer(retailer.id)
from fastapi import APIRouter, HTTPException
from starlette import status

from project.config import Config
from project.dal.retailer import RetailerRepository
from project.dtos.auth import Token, AuthData
from project.logger import Logger
from project.services.auth.auth_service import AuthService

config = Config()
logger = Logger()

router = APIRouter()
retailer_repository = RetailerRepository(config, logger)

auth_service = AuthService(config, logger, retailer_repository)


@router.post("/auth/token/", response_model=Token)
def login_for_access_token(auth_data: AuthData) -> Token:
    retailer = auth_service.authenticate_retailer(email=auth_data.email,
                                                  password=auth_data.password)

    if not retailer:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail="Incorrect username or password",
            headers={"WWW-Authenticate": "Bearer"},
        )
    access_token_expires = timedelta(
        minutes=int(config.get_config("ACCESS_TOKEN_EXPIRE_MINUTES")))
Esempio n. 3
0
 def test_generate_jwt_token(
     self, token: str, auth_service: AuthService
 ) -> None:
     assert (
         type(auth_service.get_current_retailer(token)) is RetailerOutputDTO
     )
Esempio n. 4
0
 def test_return_model(self, auth_service: AuthService) -> None:
     assert (
         type(auth_service.authenticate_retailer("*****@*****.**", "123456"))
         is RetailerOutputDTO
     )
Esempio n. 5
0
 def test_return_none(self, auth_service: AuthService) -> None:
     assert auth_service.authenticate_retailer("*****@*****.**", "12") is None
Esempio n. 6
0
 def auth_service(
     self, config: Config, logger: Logger, retailer_repository: RetailerRepository
 ) -> AuthService:
     return AuthService(config, logger, retailer_repository)
Esempio n. 7
0
 def token(
     self, expires_delta: timedelta, data: Dict[str, Any], auth_service: AuthService
 ) -> str:
     return auth_service.generate_access_token(data, expires_delta)