Example #1
0
def initialise_routing():
    spine_route_lookup_url = config.get_config('SPINE_ROUTE_LOOKUP_URL')
    spine_org_code = config.get_config('SPINE_ORG_CODE')

    route_data_dir = pathlib.Path(definitions.ROOT_DIR) / "route"
    certificates = certs.Certs.create_certs_files(
        route_data_dir,
        private_key=secrets.get_secret_config('SPINE_ROUTE_LOOKUP_CLIENT_KEY',
                                              default=None),
        local_cert=secrets.get_secret_config('SPINE_ROUTE_LOOKUP_CLIENT_CERT',
                                             default=None),
        ca_certs=secrets.get_secret_config('SPINE_ROUTE_LOOKUP_CA_CERTS',
                                           default=None))

    route_proxy_host = config.get_config('SPINE_ROUTE_LOOKUP_HTTP_PROXY',
                                         default=None)
    route_proxy_port = None
    if route_proxy_host is not None:
        route_proxy_port = int(
            config.get_config('SPINE_ROUTE_LOOKUP_HTTP_PROXY_PORT',
                              default="3128"))

    return routing_reliability.RoutingAndReliability(
        spine_route_lookup_url,
        spine_org_code,
        client_cert=certificates.local_cert_path,
        client_key=certificates.private_key_path,
        ca_certs=certificates.ca_certs_path,
        http_proxy_host=route_proxy_host,
        http_proxy_port=route_proxy_port)
    async def test_should_pass_through_exception_if_raised_when_retrieving_endpoint_details(
            self):
        self.routing = routing_reliability.RoutingAndReliability(
            BASE_URL, SPINE_ORG_CODE)
        self.mock_http_client.fetch.side_effect = IOError(
            "Something went wrong!")

        with self.assertRaises(IOError):
            await self.routing.get_end_point(SERVICE_ID, ORG_CODE)
    async def test_should_retrieve_endpoint_details_if_given_org_code(self):
        self.routing = routing_reliability.RoutingAndReliability(
            BASE_URL, SPINE_ORG_CODE)
        self._given_http_client_returns_a_json_response()

        endpoint_details = await self.routing.get_end_point(
            SERVICE_ID, ORG_CODE)

        self.assertEqual(EXPECTED_RESPONSE, endpoint_details)
        expected_url = self._build_url(path=ROUTING_PATH)
        self._assert_http_client_called_with_expected_args(expected_url)
    async def test_should_use_proxy_if_provided_when_retrieving_endpoint_details(
            self):
        self.routing = routing_reliability.RoutingAndReliability(
            BASE_URL,
            SPINE_ORG_CODE,
            http_proxy_host=HTTP_PROXY_HOST,
            http_proxy_port=HTTP_PROXY_PORT)
        self._given_http_client_returns_a_json_response()

        await self.routing.get_end_point(SERVICE_ID, ORG_CODE)

        expected_url = self._build_url(path=ROUTING_PATH)
        self._assert_http_client_called_with_expected_args(
            expected_url,
            proxy_host=HTTP_PROXY_HOST,
            proxy_port=HTTP_PROXY_PORT)
    async def test_should_use_certificate_details_if_provided_when_retrieving_endpoint_details(
            self):
        self.routing = routing_reliability.RoutingAndReliability(
            BASE_URL,
            SPINE_ORG_CODE,
            client_cert=CLIENT_CERT_PATH,
            client_key=CLIENT_KEY_PATH,
            ca_certs=CA_CERTS_PATH)
        self._given_http_client_returns_a_json_response()

        await self.routing.get_end_point(SERVICE_ID, ORG_CODE)

        expected_url = self._build_url(path=ROUTING_PATH)
        self._assert_http_client_called_with_expected_args(
            expected_url,
            client_cert=CLIENT_CERT_PATH,
            client_key=CLIENT_KEY_PATH,
            ca_certs=CA_CERTS_PATH)