def test_update_user_request_without_data(): request = UpdateUserRequest(id=None) assert not request.is_valid() assert len(request.errors) == 1 error = request.errors[0] assert error.parameter == 'id' assert error.message == 'is required'
def test_update_user_bad_request(): request = UpdateUserRequest(id='ac', name=[], role_id='sdfs') assert not request.is_valid() assert len(request.errors) == 3 assert any([e.parameter == 'id' and e.message == 'expected int, got str(ac)' for e in request.errors]) assert any([e.parameter == 'name' and e.message == 'expected str, got list([])' for e in request.errors]) assert any([ e.parameter == 'role_id' and e.message == 'expected int, got str(sdfs)' for e in request.errors])
def update_user(): data = http_request.get_json() request = UpdateUserRequest(**data) action = UpdateUserAction(users_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_update_user_action(): id, name = 1, 'name' users_repo = mock.Mock() users_repo.update.return_value = User(id=id, name=name, roles=mock.Mock()) request = UpdateUserRequest(id=id, name=name) action = UpdateUserAction(repo=users_repo) response = action.execute(request) assert bool(response) is True assert users_repo.update.called assert response.value == users_repo.update.return_value
def test_update_action_handles_exception(): error_message = 'Error!!!' users_repo = mock.Mock() users_repo.update.side_effect = Exception(error_message) request = UpdateUserRequest(id=1) action = UpdateUserAction(repo=users_repo) response = action.execute(request) assert bool(response) is False assert users_repo.update.called assert response.value == { 'type': ResponseFailure.SYSTEM_ERROR, 'message': 'Exception: {}'.format(error_message) }
def test_update_user_action_handles_bad_requst(): name = 'name' users_repo = mock.Mock() users_repo.update.return_value = User(id=id, name=name, roles=mock.Mock()) request = UpdateUserRequest(id=None) action = UpdateUserAction(repo=users_repo) response = action.execute(request) assert bool(response) is False assert not users_repo.update.called assert response.value == { 'type': ResponseFailure.PARAMETER_ERROR, 'message': 'id: is required' }
def test_update_user_action_with_hooks(): id, name = 1, 'name' users_repo = mock.Mock() users_repo.update.return_value = User(id=id, name=name, roles=mock.Mock()) request = UpdateUserRequest(id=id, name=name) action = UpdateUserAction(repo=users_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 bool(response) is True assert users_repo.update.called assert response.value == users_repo.update.return_value
def test_update_user_request(): request = UpdateUserRequest(id=1, name='abc') assert request.is_valid() assert request.id == 1 assert request.name == 'abc'
def test_update_user_request_without_id(): with pytest.raises(Exception): UpdateUserRequest()