Skip to content

Commit

Permalink
Create User increase TU
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielGodoy01 committed May 7, 2024
1 parent dc0d34c commit 6d9c0c7
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 18 deletions.
8 changes: 1 addition & 7 deletions src/modules/create_user/app/create_user_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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])
2 changes: 1 addition & 1 deletion src/modules/create_user/app/create_user_usecase.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
5 changes: 4 additions & 1 deletion src/shared/infra/repositories/user_repository_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)
Expand Down
49 changes: 47 additions & 2 deletions tests/modules/create_user/app/test_create_user_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -23,7 +22,7 @@ def test_create_user_controller(self):
'email': 'teste123@gmail.com',
'role': 'COLLABORATOR',
'groups': ['GAIA']
}, headers=header)
})

response = controller(request)

Expand Down Expand Up @@ -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"
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
}
Expand Down
30 changes: 24 additions & 6 deletions tests/shared/infra/repositories/test_user_repository_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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()
Expand Down Expand Up @@ -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'
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)

0 comments on commit 6d9c0c7

Please sign in to comment.