def test_register(self):
     user_mock = User(username='******', email='*****@*****.**')
     self.repository.save = MagicMock(return_value=user_mock)
     user_usecases = UserUseCases(self.repository)
     user_register = UserCreateCommand(username='******',
                                       email='*****@*****.**')
     self.assertIsNotNone(user_usecases.register(user_register))
 def test_list_with_filtered_results(self):
     user_id = UserId(id=str(uuid.uuid4()))
     user_mock = User(
         id=user_id, username='******', email='*****@*****.**', password='******'
     )
     self.uow.repository.all = MagicMock(return_value=[user_mock])
     user_usecases = UserUseCases(self.uow, self.pwd_encoder)
     user_query = UserQuery(q='mock')
     self.assertEqual(user_usecases.list(user_query), [user_mock])
 def test_register(self):
     user_id = UserId(id=str(uuid.uuid4()))
     user_mock = User(
         id=user_id, username='******', email='*****@*****.**', password='******'
     )
     self.uow.repository.find_by_email_or_username = MagicMock(return_value=None)
     self.uow.repository.save = MagicMock(return_value=user_mock)
     self.pwd_encoder.encode = MagicMock(return_value='aaaa')
     user_usecases = UserUseCases(self.uow, self.pwd_encoder)
     user_register = UserCreateCommand(
         username='******', email='*****@*****.**', password='******'
     )
     self.assertIsNotNone(user_usecases.register(user_register))
async def get_users(
        user_from_token=Depends(user_token_validation),
        user_query: Optional[UserQuery] = Depends(user_query),
        user_usecases: UserUseCases = Depends(user_usecases_dependency),
):
    logger.info("Get all users called")
    return user_usecases.list(user_query)
 def test_update_user_status_to_active(self):
     user_id = UserId(id=str(uuid.uuid4()))
     user_mock = User(
         id=user_id,
         username='******',
         email='*****@*****.**',
         password='******',
         status=UserStatus.BLOCKED,
     )
     self.uow.repository.find_by_id = MagicMock(return_value=user_mock)
     self.uow.repository.save = MagicMock(return_value=user_mock)
     user_usecases = UserUseCases(self.uow, self.pwd_encoder)
     command = UpdateUserStatusCommand(
         user_id=user_id.id, status=UserStatus.ACTIVE.value
     )
     updated_user = user_usecases.update_status(command)
     assert not updated_user.is_blocked()
async def get_user(
        user_id: str,
        user_usecases: UserUseCases = Depends(user_usecases_dependency),
        user_from_token=Depends(user_token_validation),
):
    logger.info("Get user by id called")
    user = user_usecases.find_by_id(user_id)
    return user
async def update_users_status(
        user_id: str,
        user_status_request: UserStatusRequest,
        user_from_token=Depends(user_token_validation),
        user_usecases: UserUseCases = Depends(user_usecases_dependency),
):
    logger.info("Update user status called")
    return user_usecases.update_status(
        UpdateUserStatusCommand(user_id=user_id,
                                status=user_status_request.status))
 def test_register_duplicate_username(self):
     user_id = UserId(id=str(uuid.uuid4()))
     user_mock = User(
         id=user_id, username='******', email='*****@*****.**', password='******'
     )
     previous_user_mock = User(
         id=user_id, username='******', email='*****@*****.**', password='******'
     )
     self.uow.repository.find_by_email_or_username = MagicMock(
         return_value=previous_user_mock
     )
     self.uow.repository.save = MagicMock(return_value=user_mock)
     self.pwd_encoder.encode = MagicMock(return_value='aaaa')
     user_usecases = UserUseCases(self.uow, self.pwd_encoder)
     user_register = UserCreateCommand(
         username='******', email='*****@*****.**', password='******'
     )
     self.assertRaises(
         UserAlreadyExistException, user_usecases.register, user_register
     )
Esempio n. 9
0
def user_usecases_dependency(
    user_uow: AbstractUserUnitOfWork = Depends(user_uow_dependency),
    pwd_encoder: PasswordEncoder = Depends(password_encoder_dependency),
) -> UserUseCases:
    return UserUseCases(user_uow, pwd_encoder)
 def test_list_with_results(self):
     user_mock = User(username='******', email='*****@*****.**')
     self.repository.all = MagicMock(return_value=[user_mock])
     user_usecases = UserUseCases(self.repository)
     self.assertEqual(user_usecases.list(), [user_mock])
 def test_list_empty(self):
     user_usecases = UserUseCases(self.repository)
     self.repository.all = MagicMock(return_value=[])
     self.assertEqual(user_usecases.list(), [])
import logging

from fastapi import APIRouter, Depends
from typing import List

from app.adapters.database.users.in_memory_user_repository import InMemoryUserRepository
from app.adapters.http.users.input.user import UserRequest
from app.adapters.http.users.output.user import UserResponse
from app.domain.users.usecases.user_usecases import UserUseCases

router = APIRouter()
logger = logging.getLogger(__name__)

user_repository = InMemoryUserRepository()
user_usecases = UserUseCases(user_repository)


@router.get('/users', response_model=List[UserResponse])
async def get_users():
    logger.info("Get all users called")
    return user_usecases.list()


@router.post('/users', response_model=UserResponse)
async def create_users(user_request: UserRequest):
    logger.info("Get all users called")
    user = user_usecases.register(user_request.to_create_user_command())
    return user
 def test_list_empty(self):
     user_usecases = UserUseCases(self.uow, self.pwd_encoder)
     self.uow.repository.all = MagicMock(return_value=[])
     user_query = UserQuery()
     self.assertEqual([], user_usecases.list(user_query))
async def create_users(
        user_request: UserRequest,
        user_usecases: UserUseCases = Depends(user_usecases_dependency),
):
    logger.info("Create user called")
    return user_usecases.register(user_request.to_create_user_command())