示例#1
0
    def test_request_without_authorization_header(
        self,
        client: TestClient,
        sqlalchemy_auth_session: Session,
    ) -> None:
        headers = {
            hdrs.CONTENT_TYPE: APPLICATION_JSON,
        }

        r = client.post(self.path, headers=headers)

        http_status = HTTPStatus.UNAUTHORIZED
        assert r.status_code == http_status

        assert r.json() == {
            "message": "Authorization header not recognized",
        }

        request_id = r.headers[hdrs.X_REQUEST_ID]
        assert utils.is_valid_uuid(request_id)

        response = sqlalchemy_auth_session.query(auth.Response).first()
        assert isinstance(response, auth.Response)

        assert response.id == request_id
        assert response.body == r.json()
        assert response.code == r.status_code

        assert response.request.id == request_id
        assert response.request.path == r.request.path_url
        assert response.request.method == r.request.method
        assert response.request.body == r.request.body
        assert response.request.remote == "testclient:50000"
def test_http_exception_handler(
        client: TestClient,
        sqlalchemy_auth_session: Session,
) -> None:
    headers = {
        hdrs.CONTENT_TYPE: APPLICATION_JSON,
    }

    r = client.get("undefined", headers=headers)

    http_status = HTTPStatus.NOT_FOUND
    assert r.status_code == http_status

    assert r.json() == {
        "message": "Not Found",
    }

    request_id = r.headers[hdrs.X_REQUEST_ID]
    assert utils.is_valid_uuid(request_id)

    response = sqlalchemy_auth_session.query(auth.Response).first()
    assert isinstance(response, auth.Response)

    assert response.id == request_id
    assert response.body == r.json()
    assert response.code == r.status_code

    assert response.request.id == request_id
    assert response.request.path == r.request.path_url
    assert response.request.method == r.request.method
    assert response.request.body is None
    assert response.request.remote == "testclient:50000"
示例#3
0
    def test_that_service_is_alive(
        self,
        client: TestClient,
        sqlalchemy_auth_session: Session,
        allowed_contract: auth.Contract,
    ) -> None:
        headers = {
            hdrs.CONTENT_TYPE: APPLICATION_JSON,
            hdrs.AUTHORIZATION: f"{hdrs.BEARER} {allowed_contract.token}"
        }

        r = client.get(self.path, headers=headers)

        http_status = HTTPStatus.OK
        assert r.status_code == http_status

        assert r.json() == {
            "message": "OK",
            "data": {},
        }

        request_id = r.headers[hdrs.X_REQUEST_ID]
        assert utils.is_valid_uuid(request_id)

        assert sqlalchemy_auth_session.query(auth.Request).first() is None
        assert sqlalchemy_auth_session.query(auth.Response).first() is None
def test_default_error_handler(
        client: TestClient,
        sqlalchemy_auth_session: Session,
) -> None:
    app: Starlette = client.app  # type: ignore

    path = "/error"
    app.add_route(path, error_endpoint)

    headers = {
        hdrs.CONTENT_TYPE: APPLICATION_JSON,
    }

    r = client.get(path, headers=headers)

    http_status = HTTPStatus.INTERNAL_SERVER_ERROR
    assert r.status_code == http_status

    assert r.json() == {
        "message": "Internal server error",
    }

    request_id = r.headers[hdrs.X_REQUEST_ID]
    assert utils.is_valid_uuid(request_id)

    assert sqlalchemy_auth_session.query(auth.Response).first() is None

    request = sqlalchemy_auth_session.query(auth.Request).first()
    assert isinstance(request, auth.Request)

    assert request.id == request_id
    assert request.path == r.request.path_url
    assert request.method == r.request.method
    assert request.body is None
    assert request.remote == "testclient:50000"
示例#5
0
    def test_request_with_invalid_phone_number_format(
        self,
        client: TestClient,
        sqlalchemy_auth_session: Session,
        allowed_contract: auth.Contract,
        number: str,
    ) -> None:
        token = allowed_contract.token

        headers = {
            hdrs.CONTENT_TYPE: APPLICATION_JSON,
            hdrs.AUTHORIZATION: f"{hdrs.BEARER} {token}"
        }

        json = {
            "number": number,
        }

        r = client.post(self.path, json=json, headers=headers)

        http_status = HTTPStatus.UNPROCESSABLE_ENTITY
        assert r.status_code == http_status

        assert r.json() == {
            "message": "Input payload validation failed",
            "errors": {
                "number": [
                    "Phone number does't match expected pattern: 7\\d{10}.",
                ],
            },
        }

        request_id = r.headers[hdrs.X_REQUEST_ID]
        assert utils.is_valid_uuid(request_id)

        response = sqlalchemy_auth_session.query(auth.Response).first()
        assert isinstance(response, auth.Response)

        assert response.id == request_id
        assert response.body == r.json()
        assert response.code == r.status_code

        assert response.request.id == request_id
        assert response.request.path == r.request.path_url
        assert response.request.method == r.request.method
        assert response.request.body == json
        assert response.request.remote == "testclient:50000"
示例#6
0
    def test_request_with_undefined_phone_number(
        self,
        client: TestClient,
        sqlalchemy_auth_session: Session,
        allowed_contract: auth.Contract,
        phone_number_generator: Callable,
    ) -> None:
        token = allowed_contract.token

        headers = {
            hdrs.CONTENT_TYPE: APPLICATION_JSON,
            hdrs.AUTHORIZATION: f"{hdrs.BEARER} {token}"
        }

        json = {
            "number": phone_number_generator(),
        }

        r = client.post(self.path, json=json, headers=headers)

        http_status = HTTPStatus.OK
        assert r.status_code == http_status

        assert r.json() == {
            "message": "OK",
            "data": {
                "status": False,
                "period": None,
            },
        }

        request_id = r.headers[hdrs.X_REQUEST_ID]
        assert utils.is_valid_uuid(request_id)

        response = sqlalchemy_auth_session.query(auth.Response).first()
        assert isinstance(response, auth.Response)

        assert response.id == request_id
        assert response.body == r.json()
        assert response.code == r.status_code

        assert response.request.id == request_id
        assert response.request.path == r.request.path_url
        assert response.request.method == r.request.method
        assert response.request.body == json
        assert response.request.remote == "testclient:50000"
示例#7
0
    def test_request_without_content_type(
        self,
        client: TestClient,
        sqlalchemy_auth_session: Session,
    ) -> None:
        response = client.get(self.url)

        http_status = HTTPStatus.BAD_REQUEST
        assert response.status_code == http_status

        assert response.json() == {
            "message": "Content-Type header not recognized",
        }

        request_id = response.headers[hdrs.X_REQUEST_ID]
        assert utils.is_valid_uuid(request_id)

        assert sqlalchemy_auth_session.query(auth.Request).first() is None
        assert sqlalchemy_auth_session.query(auth.Response).first() is None
示例#8
0
    def test_request_with_revoked_contract(
        self,
        client: TestClient,
        sqlalchemy_auth_session: Session,
        revoked_contract: auth.Contract,
    ) -> None:
        token = revoked_contract.token

        headers = {
            hdrs.CONTENT_TYPE: APPLICATION_JSON,
            hdrs.AUTHORIZATION: f"{hdrs.BEARER} {token}"
        }

        r = client.post(self.path, headers=headers)

        http_status = HTTPStatus.UNAUTHORIZED
        assert r.status_code == http_status

        message = auth.ContractRevoked(revoked_contract).render()

        assert r.json() == {
            "message": message,
        }

        request_id = r.headers[hdrs.X_REQUEST_ID]
        assert utils.is_valid_uuid(request_id)

        response = sqlalchemy_auth_session.query(auth.Response).first()
        assert isinstance(response, auth.Response)

        assert response.id == request_id
        assert response.body == r.json()
        assert response.code == r.status_code

        assert response.request.id == request_id
        assert response.request.path == r.request.path_url
        assert response.request.method == r.request.method
        assert response.request.body == r.request.body
        assert response.request.remote == "testclient:50000"
示例#9
0
    def test_request_without_invalid_content_type(
        self,
        client: TestClient,
        sqlalchemy_auth_session: Session,
    ) -> None:
        headers = {
            hdrs.CONTENT_TYPE: "application/html",
        }

        response = client.get(self.url, headers=headers)

        http_status = HTTPStatus.UNSUPPORTED_MEDIA_TYPE
        assert response.status_code == http_status

        assert response.json() == {
            "message": "Unsupported media type",
        }

        request_id = response.headers[hdrs.X_REQUEST_ID]
        assert utils.is_valid_uuid(request_id)

        assert sqlalchemy_auth_session.query(auth.Request).first() is None
        assert sqlalchemy_auth_session.query(auth.Response).first() is None
示例#10
0
    def test_that_response_is_pong(
        self,
        client: TestClient,
        sqlalchemy_auth_session: Session,
    ) -> None:
        headers = {
            hdrs.CONTENT_TYPE: APPLICATION_JSON,
        }

        r = client.get(self.path, headers=headers)

        http_status = HTTPStatus.OK
        assert r.status_code == http_status

        assert r.json() == {
            "message": "pong",
            "data": {},
        }

        request_id = r.headers[hdrs.X_REQUEST_ID]
        assert utils.is_valid_uuid(request_id)

        assert sqlalchemy_auth_session.query(auth.Request).first() is None
        assert sqlalchemy_auth_session.query(auth.Response).first() is None
示例#11
0
    def test_request_with_invalid_json_body(
        self,
        client: TestClient,
        sqlalchemy_auth_session: Session,
    ) -> None:
        headers = {
            hdrs.CONTENT_TYPE: APPLICATION_JSON,
        }

        data = "{key:value}"
        response = client.post(self.url, headers=headers, data=data)

        http_status = HTTPStatus.BAD_REQUEST
        assert response.status_code == http_status

        assert response.json() == {
            "message": "Could not parse request body",
        }

        request_id = response.headers[hdrs.X_REQUEST_ID]
        assert utils.is_valid_uuid(request_id)

        assert sqlalchemy_auth_session.query(auth.Request).first() is None
        assert sqlalchemy_auth_session.query(auth.Response).first() is None
示例#12
0
    def test_request_with_known_phone_number_hit(
        self,
        client: TestClient,
        sqlalchemy_auth_session: Session,
        allowed_contract: auth.Contract,
        phone_number_generator: Callable,
        create_submission: Callable,
    ) -> None:
        registered_at = date(2000, 1, 1)
        updated_at = date(2020, 1, 1)

        person_name = "Jake"
        person_birthday = "1970.01.01"
        person_phone_number = phone_number_generator()

        create_submission(
            submission_number=1,
            submission_created_at=registered_at,
            person_name=person_name,
            person_birthday=person_birthday,
            person_phone_number=person_phone_number,
        )

        create_submission(
            submission_number=2,
            submission_created_at=updated_at,
            person_name=person_name,
            person_birthday=person_birthday,
            person_phone_number=person_phone_number,
        )

        token = allowed_contract.token

        headers = {
            hdrs.CONTENT_TYPE: APPLICATION_JSON,
            hdrs.AUTHORIZATION: f"{hdrs.BEARER} {token}"
        }

        json = {
            "number": person_phone_number,
        }

        r = client.post(self.path, json=json, headers=headers)

        http_status = HTTPStatus.OK
        assert r.status_code == http_status

        assert r.json() == {
            "message": "OK",
            "data": {
                "status": True,
                "period": {
                    "registered_at": "2000.01.01",
                    "updated_at": "2020.01.01",
                },
            },
        }

        request_id = r.headers[hdrs.X_REQUEST_ID]
        assert utils.is_valid_uuid(request_id)

        response = sqlalchemy_auth_session.query(auth.Response).first()
        assert isinstance(response, auth.Response)

        assert response.id == request_id
        assert response.body == r.json()
        assert response.code == r.status_code

        assert response.request.id == request_id
        assert response.request.path == r.request.path_url
        assert response.request.method == r.request.method
        assert response.request.body == json
        assert response.request.remote == "testclient:50000"