def test_route():
    """ Testing route method in RegisterUserRouter """

    register_pet_use_case = RegisterPetSpy(PetRepositorySpy(),
                                           UserRepositorySpy())
    register_pet_router = RegisterPetRoute(register_pet_use_case)
    attributes = {
        "name": faker.word(),
        "specie": "Dog",
        "age": faker.random_number(),
        "user_information": {
            "user_id": faker.random_number(),
            "user_name": faker.word(),
        },
    }

    response = register_pet_router.route(HttpRequest(body=attributes))

    # Testing input
    assert register_pet_use_case.registry_param["name"] == attributes["name"]
    assert register_pet_use_case.registry_param["specie"] == attributes[
        "specie"]
    assert register_pet_use_case.registry_param["age"] == attributes["age"]
    assert (register_pet_use_case.registry_param["user_information"] ==
            attributes["user_information"])

    # Testing output
    assert response.status_code == 201
    assert "error" not in response.body
def test_should_return_400_if_no_password_confirmation_is_provide():
    sut = SignUpController()
    attributes = {
        "name": "any_name",
        "login": "******",
        "email": "*****@*****.**",
        "email_confirmation": "*****@*****.**",
        "password": "******",
    }
    request = HttpRequest(body=attributes)
    httpResponse = sut.route(request)
    assert httpResponse.status_code == 400
示例#3
0
def test_route_error_no_body():
    """ Testing route method in RegisterUserRouter """

    register_user_use_case = RegisterUserSpy(UserRepositorySpy())
    register_user_router = RegisterUserRouter(register_user_use_case)

    response = register_user_router.route(HttpRequest())

    # Testing input
    assert register_user_use_case.registry_param == {}

    # Testing output
    assert response.status_code == 400
    assert "error" in response.body
示例#4
0
def test_route_by_name():
    """ Testing route method in FindUserRouter """

    find_user_use_case = FindUserSpy(UserRepositorySpy())
    find_user_router = FindUserRouter(find_user_use_case)
    attributes = {"user_name": faker.word()}

    response = find_user_router.route(HttpRequest(query=attributes))

    # Testing input
    assert find_user_use_case.by_name_param["name"] == attributes["user_name"]

    # Testing Output
    assert response.status_code == 200
    assert response.body
示例#5
0
def test_route_by_id():
    """ Testing route method in FindUserRouter """

    find_user_use_case = FindUserSpy(UserRepositorySpy())
    find_user_router = FindUserRouter(find_user_use_case)
    attributes = {"user_id": faker.random_number(digits=2)}

    response = find_user_router.route(HttpRequest(query=attributes))

    # Testing input
    assert find_user_use_case.by_id_param["user_id"] == attributes["user_id"]

    # Testing output
    assert response.status_code == 200
    assert response.body
示例#6
0
def test_route_error_wrong_body():
    """ Testing route method in RegisterUserRouter """

    register_user_use_case = RegisterUserSpy(UserRepositorySpy())
    register_user_router = RegisterUserRouter(register_user_use_case)
    attributes = {"name": faker.word()}

    response = register_user_router.route(HttpRequest(body=attributes))

    # Testing input
    assert register_user_use_case.registry_param == {}

    # Testing output
    assert response.status_code == 422
    assert "error" in response.body
示例#7
0
def test_route_error_wrong_query():
    """ Testing route method in FindUserRouter """

    find_user_use_case = FindUserSpy(UserRepositorySpy())
    find_user_router = FindUserRouter(find_user_use_case)

    response = find_user_router.route(HttpRequest(query={"something": faker.word()}))

    # Testing input
    assert find_user_use_case.by_id_param == {}
    assert find_user_use_case.by_name_param == {}
    assert find_user_use_case.by_id_and_user_param == {}

    # Testing output
    assert response.status_code == 422
    assert "error" in response.body
示例#8
0
def test_route():
    """ Testing route method in RegisterUserRouter """

    register_user_use_case = RegisterUserSpy(UserRepositorySpy())
    register_user_router = RegisterUserRouter(register_user_use_case)
    attributes = {"name": faker.word(), "password": faker.word()}

    response = register_user_router.route(HttpRequest(body=attributes))

    # Testing input
    assert register_user_use_case.registry_param["name"] == attributes["name"]
    assert register_user_use_case.registry_param["password"] == attributes["password"]

    # Testing output
    assert response.status_code == 201
    assert "error" not in response.body
示例#9
0
def test_route_by_pet_id():
    """ Testing route method in FindPetRouter """

    find_pet_use_case = FindPetSpy(PetRepositorySpy())
    find_pet_router = FindPetRouter(find_pet_use_case)
    attributes = {"pet_id": faker.random_number(digits=2)}

    http_request = HttpRequest(query=attributes)

    http_response = find_pet_router.route(http_request)

    # Testing input
    assert find_pet_use_case.by_pet_id_param["pet_id"] == attributes["pet_id"]

    # Testing output
    assert http_response.status_code == 200
    assert "error" not in http_response.body
示例#10
0
def test_route_error_no_query():
    """ Testing route method in FindPetRouter """

    find_pet_use_case = FindPetSpy(PetRepositorySpy())
    find_pet_router = FindPetRouter(find_pet_use_case)

    http_request = HttpRequest()

    http_response = find_pet_router.route(http_request)

    # Testing input
    assert find_pet_use_case.by_pet_id_param == {}
    assert find_pet_use_case.by_user_id_param == {}
    assert find_pet_use_case.by_pet_id_and_user_id_param == {}

    # Testing output
    assert http_response.status_code == 400
    assert "error" in http_response.body
示例#11
0
def test_route_error_wrong_query():
    """ Testing route method in FindPetRouter """

    find_pet_use_case = FindPetSpy(PetRepositorySpy())
    find_pet_router = FindPetRouter(find_pet_use_case)

    http_request = HttpRequest(
        query={"something": faker.random_number(digits=2)})

    http_response = find_pet_router.route(http_request)

    # Testing input
    assert find_pet_use_case.by_pet_id_param == {}
    assert find_pet_use_case.by_user_id_param == {}
    assert find_pet_use_case.by_pet_id_and_user_id_param == {}

    # Testing output
    assert http_response.status_code == 422
    assert "error" in http_response.body
def test_route_error_wrong_user_information():
    """ Testing route method in RegisterUserRouter """

    register_pet_use_case = RegisterPetSpy(PetRepositorySpy(),
                                           UserRepositorySpy())
    register_pet_router = RegisterPetRoute(register_pet_use_case)

    attributes = {
        "name": faker.word(),
        "specie": "Dog",
        "user_information": {}
    }

    response = register_pet_router.route(HttpRequest(body=attributes))

    # Testing input
    assert register_pet_use_case.registry_param == {}

    # Testing output
    assert response.status_code == 422
    assert "error" in response.body
def flask_adapter(request: any, api_route: Type[Route]) -> any:
    """Adapter pattern to Flask
    :param - Flask Request
    :api_route: Composite Routes
    """

    try:
        # Query string params
        query_string_params = request.args.to_dict()

        # Formating information
        if "user_id" in query_string_params.keys():
            query_string_params["user_id"] = int(
                query_string_params["user_id"])

        if "pet_id" in query_string_params.keys():
            query_string_params["pet_id"] = int(query_string_params["pet_id"])
    except:
        https_error = HttpErrors.error_400()
        return HttpResponse(status_code=https_error["status_code"],
                            body=https_error["body"])

    http_request = HttpRequest(header=request.headers,
                               body=request.json,
                               query=query_string_params)

    try:
        response = api_route.route(http_request)

    except IntegrityError:
        https_error = HttpErrors.error_409()
        return HttpResponse(status_code=https_error["status_code"],
                            body=https_error["body"])
    except:
        https_error = HttpErrors.error_500()
        return HttpResponse(status_code=https_error["status_code"],
                            body=https_error["body"])
    return response