From 6d9c0c787ac60ca63dcba62ba20b632d6c3915be Mon Sep 17 00:00:00 2001 From: Gabriel de Godoy Braz <61150821+GabrielGodoy01@users.noreply.github.com> Date: Tue, 7 May 2024 14:21:29 -0300 Subject: [PATCH] Create User increase TU --- .../create_user/app/create_user_controller.py | 8 +-- .../create_user/app/create_user_usecase.py | 2 +- .../repositories/user_repository_mock.py | 5 +- .../app/test_create_user_controller.py | 49 ++++++++++++++++++- .../app/test_create_user_presenter.py | 2 +- .../repositories/test_user_repository_mock.py | 30 +++++++++--- 6 files changed, 78 insertions(+), 18 deletions(-) diff --git a/src/modules/create_user/app/create_user_controller.py b/src/modules/create_user/app/create_user_controller.py index 72ab859..5f98de4 100644 --- a/src/modules/create_user/app/create_user_controller.py +++ b/src/modules/create_user/app/create_user_controller.py @@ -8,7 +8,7 @@ from .create_user_usecase import CreateUserUsecase from src.shared.helpers.errors.controller_errors import MissingParameters from src.shared.helpers.errors.domain_errors import EntityError -from src.shared.helpers.errors.usecase_errors import DuplicatedItem, InvalidCredentials, InvalidTokenError, NoItemsFound +from src.shared.helpers.errors.usecase_errors import DuplicatedItem, InvalidCredentials from src.shared.helpers.external_interfaces.external_interface import IRequest, IResponse from src.shared.helpers.external_interfaces.http_codes import BadRequest, InternalServerError, Conflict, \ Created, Forbidden, Unauthorized @@ -69,11 +69,5 @@ def __call__(self, request: IRequest) -> IResponse: except EntityError as err: return BadRequest(body=f"Parâmetro inválido: {err.message}") - except NoItemsFound as err: - return BadRequest(body=err.message) - - except InvalidTokenError as err: - return Unauthorized(body="Token inválido ou expirado") - except Exception as err: return InternalServerError(body=err.args[0]) diff --git a/src/modules/create_user/app/create_user_usecase.py b/src/modules/create_user/app/create_user_usecase.py index f7a9287..7d1d303 100644 --- a/src/modules/create_user/app/create_user_usecase.py +++ b/src/modules/create_user/app/create_user_usecase.py @@ -1,7 +1,7 @@ from src.shared.domain.entities.user import User from src.shared.domain.enums.role_enum import ROLE from src.shared.domain.repositories.user_repository_interface import IUserRepository -from src.shared.helpers.errors.usecase_errors import ForbiddenAction, NoItemsFound +from src.shared.helpers.errors.usecase_errors import ForbiddenAction class CreateUserUsecase: diff --git a/src/shared/infra/repositories/user_repository_mock.py b/src/shared/infra/repositories/user_repository_mock.py index 85e3e3c..249035d 100644 --- a/src/shared/infra/repositories/user_repository_mock.py +++ b/src/shared/infra/repositories/user_repository_mock.py @@ -6,7 +6,7 @@ from src.shared.domain.enums.user_status_enum import USER_STATUS from src.shared.domain.repositories.user_repository_interface import IUserRepository from src.shared.helpers.errors.domain_errors import EntityError -from src.shared.helpers.errors.usecase_errors import NoItemsFound +from src.shared.helpers.errors.usecase_errors import DuplicatedItem, NoItemsFound class UserRepositoryMock(IUserRepository): @@ -23,6 +23,9 @@ def get_all_users(self) -> List[User]: return self.users def create_user(self, email: str, name: str, role: ROLE, groups: List[GROUPS]) -> User: + for user in self.users: + if user.email == email: + raise DuplicatedItem("Usuário já cadastrado") new_user = User(email=email, name=name, role=role, groups=groups, enabled=True, user_status=USER_STATUS.CONFIRMED) new_user.user_id = str(len(self.users) + 1) self.users.append(new_user) diff --git a/tests/modules/create_user/app/test_create_user_controller.py b/tests/modules/create_user/app/test_create_user_controller.py index 053de7f..34da1a7 100644 --- a/tests/modules/create_user/app/test_create_user_controller.py +++ b/tests/modules/create_user/app/test_create_user_controller.py @@ -10,7 +10,6 @@ def test_create_user_controller(self): repo = UserRepositoryMock() usecase = CreateUserUsecase(repo) controller = CreateUserController(usecase) - header = {"Authorization": "Bearer valid_access_token-teste@gmail.com"} request = HttpRequest(body={"requester_user": { "sub": repo.users[0].user_id, @@ -23,7 +22,7 @@ def test_create_user_controller(self): 'email': 'teste123@gmail.com', 'role': 'COLLABORATOR', 'groups': ['GAIA'] - }, headers=header) + }) response = controller(request) @@ -169,3 +168,49 @@ def test_create_user_controller_no_groups(self): assert response.status_code == 400 assert response.body == "Parâmetro ausente: groups" + + def test_create_user_controller_group_not_valid(self): + repo = UserRepositoryMock() + usecase = CreateUserUsecase(repo) + controller = CreateUserController(usecase) + + request = HttpRequest(body={"requester_user": { + "sub": repo.users[0].user_id, + "name": repo.users[0].name, + "email": repo.users[0].email, + "custom:general_role": repo.users[0].role.value, + "cognito:groups": ','.join([group.value for group in repo.users[0].groups]) + }, + 'name': 'Gabriel Godoy', + 'email': 'teste@gmail.com', + 'role': 'COLLABORATOR', + 'groups': ['123'] + }) + + response = controller(request) + + assert response.status_code == 400 + assert response.body == "Parâmetro inválido: groups" + + def test_create_user_controller_duplicated(self): + repo = UserRepositoryMock() + usecase = CreateUserUsecase(repo) + controller = CreateUserController(usecase) + + request = HttpRequest(body={"requester_user": { + "sub": repo.users[0].user_id, + "name": repo.users[0].name, + "email": repo.users[0].email, + "custom:general_role": repo.users[0].role.value, + "cognito:groups": ','.join([group.value for group in repo.users[0].groups]) + }, + 'name': 'Gabriel Godoy', + 'email': 'teste@gmail.com', + 'role': 'COLLABORATOR', + 'groups': ['GAIA'] + }) + + response = controller(request) + + assert response.status_code == 409 + assert response.body == "Usuário ja cadastrado com esses dados" diff --git a/tests/modules/create_user/app/test_create_user_presenter.py b/tests/modules/create_user/app/test_create_user_presenter.py index 12e36e5..87f2838 100644 --- a/tests/modules/create_user/app/test_create_user_presenter.py +++ b/tests/modules/create_user/app/test_create_user_presenter.py @@ -55,7 +55,7 @@ def test_create_user_presenter(self): "stageVariables": None, "body": { 'name': 'Gabriel Godoy', - 'email': 'teste@gmail.com', + 'email': 'teste4@gmail.com', 'role': 'USER', 'groups': ['GAIA'], } diff --git a/tests/shared/infra/repositories/test_user_repository_mock.py b/tests/shared/infra/repositories/test_user_repository_mock.py index 1dff4e0..bbf6e3c 100644 --- a/tests/shared/infra/repositories/test_user_repository_mock.py +++ b/tests/shared/infra/repositories/test_user_repository_mock.py @@ -3,6 +3,7 @@ from src.shared.domain.enums.groups_enum import GROUPS from src.shared.domain.enums.role_enum import ROLE from src.shared.domain.enums.user_status_enum import USER_STATUS +from src.shared.helpers.errors.usecase_errors import DuplicatedItem from src.shared.infra.repositories.user_repository_mock import UserRepositoryMock import pytest @@ -29,15 +30,20 @@ def test_get_user_by_email(self): def test_create_user(self): repo = UserRepositoryMock() - user = repo.create_user(email='teste3@gmail.com', name='Gabriel Godoy', role=ROLE.COLLABORATOR, groups=[]) + user = repo.create_user(email='teste4@gmail.com', name='Gabriel Godoy', role=ROLE.COLLABORATOR, groups=[]) assert len(repo.users) == 4 assert type(user) == User assert repo.users[-1].user_id == '4' - assert repo.users[-1].email == 'teste3@gmail.com' + assert repo.users[-1].email == 'teste4@gmail.com' assert repo.users[-1].name == 'Gabriel Godoy' assert repo.users[-1].role == ROLE.COLLABORATOR assert repo.users[-1].groups == [] + + def test_create_user_duplicated(self): + repo = UserRepositoryMock() + with pytest.raises(DuplicatedItem): + repo.create_user(email='teste@gmail.com', name='Gabriel Godoy', role=ROLE.COLLABORATOR, groups=[]) def test_get_users_in_group(self): repo = UserRepositoryMock() @@ -70,10 +76,22 @@ def test_update_user_enabled_null(self): assert user.groups == [GROUPS.JUNDIAI] assert user.enabled == True + def test_update_user_return_none(self): + repo = UserRepositoryMock() + response = repo.update_user(user_email='teste@gmail.', kvp_to_update={'role': ROLE.USER}, addGroups=[GROUPS.JUNDIAI], removeGroups=[GROUPS.GAIA], enabled=False) + + assert response == None + def test_refresh_token(self): repo = UserRepositoryMock() - resp = repo.refresh_token( + response = repo.refresh_token( refresh_token="valid_refresh_token-teste@gmail.com") - assert resp[0] == 'valid_access_token-teste@gmail.com' - assert resp[1] == 'valid_refresh_token-teste@gmail.com' - assert resp[2] == 'valid_id_token-teste@gmail.com' \ No newline at end of file + assert response[0] == 'valid_access_token-teste@gmail.com' + assert response[1] == 'valid_refresh_token-teste@gmail.com' + assert response[2] == 'valid_id_token-teste@gmail.com' + + def test_refresh_token_return_none(self): + repo = UserRepositoryMock() + response = repo.refresh_token( + refresh_token="valid_refresh_tokenteste@gmail.com") + assert response == (None, None) \ No newline at end of file