def test_add_user_request(): role_id = 1 name = 'name' password = '******' request = AddUserRequest(name=name, password=password, roles=[role_id]) assert request.is_valid() is True assert request.name == name assert request.roles == [role_id]
def test_add_user_action_with_hooks(): name = 'name' password = '******' role_id = 1 roles_repo = mock.Mock() roles_repo.one.return_value = UserRole(name='role_name') users_repo = mock.Mock() users_repo.save.return_value = User(name=name, roles=[roles_repo.one.return_value]) request = AddUserRequest(name=name, password=password, roles=[role_id]) action = AddUserAction(users_repo, roles_repo) before = mock.MagicMock() after = mock.MagicMock() action.add_before_execution_hook(before) action.add_after_execution_hook(after) response = action.execute(request) assert before.called assert after.called assert users_repo.save.called assert bool(response) is True assert response.value == users_repo.save.return_value
def test_add_user_request_without_data(): request = AddUserRequest(name=None, password=None, roles=None) assert request.is_valid() is False assert len(request.errors) == 3 errors = request.errors assert any([ param == 'roles' and message == "is required" for param, message in errors ]) assert any([ param == 'name' and message == "is required" for param, message in errors ]) assert any([ param == 'password' and message == "is required" for param, message in errors ])
def add_user(): data = http_request.get_json() request = AddUserRequest(**data) action = AddUserAction(users_repository, user_roles_repository) action.add_before_execution_hook(authorization_manager.authorize) response = action.execute(request) status = get_status(response) return json_response(response.value, UserEncoder, status)
def test_add_user_bad_request(): role_id = 'abc' name = 1 password = 1 request = AddUserRequest(name=name, password=password, roles=role_id) assert request.is_valid() is False assert len(request.errors) == 3 errors = request.errors assert any([ param == 'roles' and message == "expected list, got str(abc)" for param, message in errors ]) assert any([ param == 'name' and message == "expected str, got int(1)" for param, message in errors ]) assert any([ param == 'password' and message == "expected str, got int(1)" for param, message in errors ])
def test_add_user_bad_request2(): role_id = 'abc' name = 1 password = 1 request = AddUserRequest(name=name, password=password, roles=[role_id]) assert request.is_valid() is False assert len(request.errors) == 3 errors = request.errors message_roles = "expected all elements to be int, got str(abc) at index 0" assert any([ param == 'roles' and message == message_roles for param, message in errors ]) assert any([ param == 'name' and message == "expected str, got int(1)" for param, message in errors ]) assert any([ param == 'password' and message == "expected str, got int(1)" for param, message in errors ])
def test_add_user_action(): name = 'name' password = '******' role_id = 1 roles_repo = mock.Mock() roles_repo.one.return_value = UserRole(name='role_name') users_repo = mock.Mock() users_repo.save.return_value = User(name=name, roles=[roles_repo.one.return_value]) request = AddUserRequest(name=name, password=password, roles=[role_id]) action = AddUserAction(users_repo, roles_repo) response = action.execute(request) assert users_repo.save.called assert bool(response) is True assert response.value == users_repo.save.return_value
def test_add_user_action_handles_bad_request(): name = None password = None role_id = None roles_repo = mock.Mock() roles_repo.one.return_value = UserRole(name='role_name') users_repo = mock.Mock() users_repo.save.return_value = User(name='name', roles=[roles_repo.one.return_value]) request = AddUserRequest(name=name, password=password, roles=role_id) action = AddUserAction(users_repo, roles_repo) response = action.execute(request) assert not users_repo.save.called assert bool(response) is False assert response.value == { 'message': 'name: is required\npassword: is required\nroles: is required', 'type': ResponseFailure.PARAMETER_ERROR }