def test_list_users_handles_bad_request():
    repo = mock.Mock()

    list_users_use_case = ListUsersUseCase(repo)
    request_object = ListUsersRequestObject.from_dict({'filters': 5})

    response_object = list_users_use_case.execute(request_object)

    assert bool(response_object) is False
    assert response_object.value == {
        'type': ResponseFailure.PARAMETERS_ERROR,
        'message': 'filters: Is not iterable'
    }
def test_list_users_without_params(domain_users):
    repo = mock.Mock()
    repo.list.return_value = domain_users

    list_users_use_case = ListUsersUseCase(repo)
    request_object = ListUsersRequestObject.from_dict({})

    response_object = list_users_use_case.execute(request_object)

    assert bool(response_object) is True
    repo.list.assert_called_with(filters=None)

    assert response_object.value == domain_users
def test_list_users_handles_generic_error():
    repo = mock.Mock()
    repo.list.side_effect = Exception('Just an error message')

    list_users_use_case = ListUsersUseCase(repo)
    request_object = ListUsersRequestObject.from_dict({})

    response_object = list_users_use_case.execute(request_object)

    assert bool(response_object) is False
    assert response_object.value == {
        'type': ResponseFailure.SYSTEM_ERROR,
        'message': 'Exception: Just an error message'
    }
def test_list_users_with_filters(domain_users):
    repo = mock.Mock()
    repo.list.return_value = domain_users

    list_users_use_case = ListUsersUseCase(repo)
    query_filters = {'a': 5}
    request_object = ListUsersRequestObject.from_dict(
        {'filters': query_filters})

    response_object = list_users_use_case.execute(request_object)

    assert bool(response_object) is True
    repo.list.assert_called_with(filters=query_filters)
    assert response_object.value == domain_users
def test_build_list_users_request_object_from_dict_with_invalid_filters():
    request = ListUsersRequestObject.from_dict({'filters': 5})

    assert request.has_errors()
    assert request.errors[0]['parameter'] == 'filters'
    assert bool(request) is False
def test_build_list_users_request_object_from_dict_with_filters():
    filters = {'a': 1, 'b': 2}
    request = ListUsersRequestObject.from_dict({'filters': filters})

    assert request.filters == filters
    assert bool(request) is True
def test_build_list_users_request_object_without_params():
    request = ListUsersRequestObject()

    assert request.filters is None
    assert bool(request) is True
def test_build_list_users_request_object_from_dict_with_empty_filters():
    request = ListUsersRequestObject.from_dict({'filters': {}})

    assert request.filters == {}
    assert bool(request) is True
def test_build_list_users_request_object_from_empty_dict():
    request = ListUsersRequestObject.from_dict({})

    assert request.filters is None
    assert bool(request) is True