Example #1
0
    def test_entries_for_tenant_internal(self):
        """
        Validate that the internal API shows up in the service catalog for a
        given tenant.
        """
        iapi = make_example_internal_api(self)
        core = MimicCore(Clock(), [iapi])

        prefix_map = {}
        base_uri = "http://some/random/prefix"
        catalog_entries = [
            entry
            for entry in core.entries_for_tenant(
                'some-tenant',
                prefix_map,
                base_uri
            )
        ]

        self.assertEqual(len(core._uuid_to_api_internal), 1)
        self.assertEqual(len(core._uuid_to_api_external), 0)
        self.assertEqual(len(catalog_entries), 1)
        self.assertEqual(catalog_entries[0].type, "serviceType")
        self.assertEqual(catalog_entries[0].name, "serviceName")
        self.assertEqual(catalog_entries[0].tenant_id, "some-tenant")
        self.assertEqual(len(catalog_entries[0].endpoints), 1)
Example #2
0
    def setUp(self):
        self.eeapi_name = u"externalServiceName"
        self.eeapi_template_id = u"uuid-endpoint-template"
        self.eeapi_template = exampleEndpointTemplate(
            name=self.eeapi_name,
            endpoint_uuid=self.eeapi_template_id
        )
        self.eeapi = make_example_external_api(
            self,
            name=self.eeapi_name,
            endpoint_templates=[self.eeapi_template],
            set_enabled=False
        )
        self.helper = APIMockHelper(self, [make_example_internal_api(self)])
        self.core = self.helper.core
        self.root = self.helper.root
        self.uri = self.helper.uri

        self.tenant_enabled_for = u"tenantWithApi"
        self.tenant_enabled_for_password = "******"
        self.tenant_data = TenantAuthentication(
            self,
            self.root,
            self.tenant_enabled_for,
            self.tenant_enabled_for_password
        )
        self.eeapi.enable_endpoint_for_tenant(
            self.tenant_data.get_tenant_id(),
            self.eeapi_template_id
        )
        self.core.add_api(self.eeapi)
 def setUp(self):
     self.eeapi_name = u"externalServiceName"
     self.ieapi = make_example_internal_api(self)
     self.eeapi = make_example_external_api(self,
                                            name=self.eeapi_name,
                                            set_enabled=True)
     self.helper = APIMockHelper(self, [self.ieapi, self.eeapi])
     self.root = self.helper.root
     self.uri = self.helper.uri
Example #4
0
 def setUp(self):
     self.eeapi_name = u"externalServiceName"
     self.ieapi = make_example_internal_api(self)
     self.eeapi = make_example_external_api(
         self,
         name=self.eeapi_name,
         set_enabled=True
     )
     self.helper = APIMockHelper(self, [self.ieapi, self.eeapi])
     self.root = self.helper.root
     self.uri = self.helper.uri
    def test_list_only_internal_apis_available(self):
        """
        GET will not list Internal APIs.
        """
        self.core.add_api(make_example_internal_api(self))
        (response, json_body) = self.successResultOf(
            json_request(self, self.root, self.verb,
                         self.uri,
                         headers=self.headers))

        self.assertEqual(response.code, 200)
        self.assertEqual(len(json_body['endpoints']), 0)
        self.assertEqual(len(json_body['endpoints_links']), 0)
Example #6
0
    def make_request_to_site(self):
        """
        Make a request and return the response.
        """
        core = MimicCore(Clock(), [make_example_internal_api(self, b'response!')])
        root = MimicRoot(core).app.resource()

        # get the region and service id registered for the example API
        (region, service_id) = one_api(self, core)
        url = "/mimicking/{0}/{1}".format(service_id, region)
        response = self.successResultOf(request(
            self, root, b"GET", url, headers={b"one": [b"two"]}
        ))
        return (response, url)
Example #7
0
    def make_request_to_site(self):
        """
        Make a request and return the response.
        """
        core = MimicCore(Clock(),
                         [make_example_internal_api(self, b'response!')])
        root = MimicRoot(core).app.resource()

        # get the region and service id registered for the example API
        (region, service_id) = one_api(self, core)
        url = "/mimicking/{0}/{1}".format(service_id, region)
        response = self.successResultOf(
            request(self, root, b"GET", url, headers={b"one": [b"two"]}))
        return (response, url)
    def test_list_single_template_external_and_internal_apis(self):
        """
        GET will only list external API endpoint templates.
        """
        self.core.add_api(self.eeapi)
        self.core.add_api(make_example_internal_api(self))

        (response, json_body) = self.successResultOf(
            json_request(self, self.root, self.verb,
                         self.uri,
                         headers=self.headers))

        self.assertEqual(response.code, 200)
        self.assertEqual(len(json_body['endpoints']), 1)
        self.assertEqual(len(json_body['endpoints_links']), 0)
Example #9
0
    def test_list_only_internal_apis_available(self):
        """
        GET will not list Internal APIs.
        """
        self.core.add_api(make_example_internal_api(self))
        (response, json_body) = self.successResultOf(
            json_request(self,
                         self.root,
                         self.verb,
                         self.uri,
                         headers=self.headers))

        self.assertEqual(response.code, 200)
        self.assertEqual(len(json_body['endpoints']), 0)
        self.assertEqual(len(json_body['endpoints_links']), 0)
Example #10
0
    def test_entries_for_tenant_combined(self):
        """
        Validate that both internal and external APIs show up in the service
        catalog for a given tenant.
        """
        eeapi = make_example_external_api(
            self,
            name=self.eeapi_name,
            set_enabled=True
        )

        core = MimicCore(Clock(), [eeapi, make_example_internal_api(self)])

        prefix_map = {}
        base_uri = "http://some/random/prefix"
        catalog_entries = [
            entry
            for entry in core.entries_for_tenant(
                'some-tenant',
                prefix_map,
                base_uri
            )
        ]

        self.assertEqual(len(core._uuid_to_api_internal), 1)
        self.assertEqual(len(core._uuid_to_api_external), 1)
        self.assertEqual(len(catalog_entries), 2)

        found_internal = False
        found_external = False

        for catalog_entry in catalog_entries:
            if catalog_entry.name == eeapi.name_key:
                found_external = True
                self.assertEqual(catalog_entry.type, eeapi.type_key)
                self.assertEqual(catalog_entry.name, eeapi.name_key)
                self.assertEqual(catalog_entry.tenant_id, "some-tenant")
                self.assertEqual(len(catalog_entry.endpoints), 1)

            elif catalog_entry.name == "serviceName":
                found_internal = True
                self.assertEqual(catalog_entry.type, "serviceType")
                self.assertEqual(catalog_entry.name, "serviceName")
                self.assertEqual(catalog_entry.tenant_id, "some-tenant")
                self.assertEqual(len(catalog_entry.endpoints), 1)

        self.assertTrue(found_internal)
        self.assertTrue(found_external)
    def test_list_only_internal_apis_available(self):
        """
        GET will return an empty listing when there are no External API
        endpoint templates.
        """
        self.core.add_api(make_example_internal_api(self))
        (response, json_body) = self.successResultOf(
            json_request(self, self.root, self.verb,
                         self.uri,
                         headers=self.headers))

        self.assertEqual(response.code, 200)
        self.assertEqual(len(json_body['OS-KSCATALOG']), 0)
        self.assertEqual(
            len(json_body['OS-KSCATALOG:endpointsTemplates_links']),
            0)
Example #12
0
    def test_list_single_template_external_and_internal_apis(self):
        """
        GET will only list external API endpoint templates.
        """
        self.core.add_api(self.eeapi)
        self.core.add_api(make_example_internal_api(self))

        (response, json_body) = self.successResultOf(
            json_request(self,
                         self.root,
                         self.verb,
                         self.uri,
                         headers=self.headers))

        self.assertEqual(response.code, 200)
        self.assertEqual(len(json_body['endpoints']), 1)
        self.assertEqual(len(json_body['endpoints_links']), 0)
Example #13
0
    def test_service_with_region_internal(self):
        """
        Validate that an external service does not provide an internal service
        resource.
        """
        iapi = make_example_internal_api(self)
        helper = APIMockHelper(self, [iapi])
        core = helper.core

        service_id = None
        for a_service_id in core._uuid_to_api_internal:
            if core._uuid_to_api_internal[a_service_id] == iapi:
                service_id = a_service_id

        resource = core.service_with_region(u"ORD", service_id,
                                            u"http://some/random/prefix")
        self.assertTrue(IResource.providedBy(resource))
    def test_list_only_internal_apis_available(self):
        """
        GET will return an empty listing when there are no External API
        endpoint templates.
        """
        self.core.add_api(make_example_internal_api(self))
        (response, json_body) = self.successResultOf(
            json_request(self,
                         self.root,
                         self.verb,
                         self.uri,
                         headers=self.headers))

        self.assertEqual(response.code, 200)
        self.assertEqual(len(json_body['OS-KSCATALOG']), 0)
        self.assertEqual(
            len(json_body['OS-KSCATALOG:endpointsTemplates_links']), 0)
Example #15
0
    def test_service_endpoint_returns_service_resource(self):
        """
        When the URI used to access a real service has the right service ID
        and right region, the service's resource is used to respond to the
        request.
        """
        core = MimicCore(Clock(), [make_example_internal_api(self, b'response!')])
        root = MimicRoot(core).app.resource()

        # get the region and service id registered for the example API
        (region, service_id) = one_api(self, core)

        (response, content) = self.successResultOf(request_with_content(
            self, root, b"GET",
            "http://mybase/mimicking/{0}/{1}".format(service_id, region)
        ))
        self.assertEqual(200, response.code)
        self.assertEqual(b'response!', content)
Example #16
0
    def test_uri_for_service(self):
        """
        Validate that the URI returned by uri_for_service returns the
        appropriate base URI.
        """
        iapi = make_example_internal_api(self)
        helper = APIMockHelper(self, [iapi])
        core = helper.core

        service_id = None
        for a_service_id in core._uuid_to_api_internal:
            if core._uuid_to_api_internal[a_service_id] == iapi:
                service_id = a_service_id

        base_uri = "http://some/random/prefix"

        uri = core.uri_for_service(u"ORD", service_id, base_uri)
        self.assertTrue(uri.startswith(base_uri + '/'))
    def test_list_single_template_external_and_internal_apis(self):
        """
        GET will only return the External API endpoint templates when they
        are available,
        even if there are also Internal APIs.
        """
        self.core.add_api(self.eeapi)
        self.core.add_api(make_example_internal_api(self))

        (response, json_body) = self.successResultOf(
            json_request(self, self.root, self.verb,
                         self.uri,
                         headers=self.headers))

        self.assertEqual(response.code, 200)
        self.assertEqual(len(json_body['OS-KSCATALOG']), 1)
        self.assertEqual(
            len(json_body['OS-KSCATALOG:endpointsTemplates_links']),
            0)
Example #18
0
    def test_service_endpoint_returns_service_resource(self):
        """
        When the URI used to access a real service has the right service ID
        and right region, the service's resource is used to respond to the
        request.
        """
        core = MimicCore(Clock(),
                         [make_example_internal_api(self, b'response!')])
        root = MimicRoot(core).app.resource()

        # get the region and service id registered for the example API
        (region, service_id) = one_api(self, core)

        (response, content) = self.successResultOf(
            request_with_content(
                self, root, b"GET",
                "http://mybase/mimicking/{0}/{1}".format(service_id, region)))
        self.assertEqual(200, response.code)
        self.assertEqual(b'response!', content)
Example #19
0
    def test_entries_for_tenant_combined(self):
        """
        Validate that both internal and external APIs show up in the service
        catalog for a given tenant.
        """
        eeapi = make_example_external_api(self,
                                          name=self.eeapi_name,
                                          set_enabled=True)

        core = MimicCore(Clock(), [eeapi, make_example_internal_api(self)])

        prefix_map = {}
        base_uri = "http://some/random/prefix"
        catalog_entries = [
            entry for entry in core.entries_for_tenant('some-tenant',
                                                       prefix_map, base_uri)
        ]

        self.assertEqual(len(core._uuid_to_api_internal), 1)
        self.assertEqual(len(core._uuid_to_api_external), 1)
        self.assertEqual(len(catalog_entries), 2)

        found_internal = False
        found_external = False

        for catalog_entry in catalog_entries:
            if catalog_entry.name == eeapi.name_key:
                found_external = True
                self.assertEqual(catalog_entry.type, eeapi.type_key)
                self.assertEqual(catalog_entry.name, eeapi.name_key)
                self.assertEqual(catalog_entry.tenant_id, "some-tenant")
                self.assertEqual(len(catalog_entry.endpoints), 1)

            elif catalog_entry.name == "serviceName":
                found_internal = True
                self.assertEqual(catalog_entry.type, "serviceType")
                self.assertEqual(catalog_entry.name, "serviceName")
                self.assertEqual(catalog_entry.tenant_id, "some-tenant")
                self.assertEqual(len(catalog_entry.endpoints), 1)

        self.assertTrue(found_internal)
        self.assertTrue(found_external)
Example #20
0
    def test_service_endpoint_returns_404_if_wrong_service_id(self):
        """
        When the URI used to access a real service has the right region but
        wrong service ID, a 404 is returned and the resource for the service
        is accessed.
        """
        example = make_example_internal_api(self)

        core = MimicCore(Clock(), [example])
        root = MimicRoot(core).app.resource()

        # get the region and service id registered for the example API
        (region, service_id) = one_api(self, core)

        response = self.successResultOf(request(
            self, root, b"GET",
            "http://mybase/mimicking/not_{0}/{1}".format(service_id, region)
        ))
        self.assertEqual(404, response.code)
        self.assertEqual([], list(example.store.keys()))
    def test_list_single_template_external_and_internal_apis(self):
        """
        GET will only return the External API endpoint templates when they
        are available,
        even if there are also Internal APIs.
        """
        self.core.add_api(self.eeapi)
        self.core.add_api(make_example_internal_api(self))

        (response, json_body) = self.successResultOf(
            json_request(self,
                         self.root,
                         self.verb,
                         self.uri,
                         headers=self.headers))

        self.assertEqual(response.code, 200)
        self.assertEqual(len(json_body['OS-KSCATALOG']), 1)
        self.assertEqual(
            len(json_body['OS-KSCATALOG:endpointsTemplates_links']), 0)
Example #22
0
    def test_service_endpoint_returns_404_if_wrong_service_id(self):
        """
        When the URI used to access a real service has the right region but
        wrong service ID, a 404 is returned and the resource for the service
        is accessed.
        """
        example = make_example_internal_api(self)

        core = MimicCore(Clock(), [example])
        root = MimicRoot(core).app.resource()

        # get the region and service id registered for the example API
        (region, service_id) = one_api(self, core)

        response = self.successResultOf(
            request(
                self, root, b"GET",
                "http://mybase/mimicking/not_{0}/{1}".format(
                    service_id, region)))
        self.assertEqual(404, response.code)
        self.assertEqual([], list(example.store.keys()))
Example #23
0
    def test_entries_for_tenant_internal(self):
        """
        Validate that the internal API shows up in the service catalog for a
        given tenant.
        """
        iapi = make_example_internal_api(self)
        core = MimicCore(Clock(), [iapi])

        prefix_map = {}
        base_uri = "http://some/random/prefix"
        catalog_entries = [
            entry for entry in core.entries_for_tenant('some-tenant',
                                                       prefix_map, base_uri)
        ]

        self.assertEqual(len(core._uuid_to_api_internal), 1)
        self.assertEqual(len(core._uuid_to_api_external), 0)
        self.assertEqual(len(catalog_entries), 1)
        self.assertEqual(catalog_entries[0].type, "serviceType")
        self.assertEqual(catalog_entries[0].name, "serviceName")
        self.assertEqual(catalog_entries[0].tenant_id, "some-tenant")
        self.assertEqual(len(catalog_entries[0].endpoints), 1)
Example #24
0
    def test_child_resource_gets_base_uri_from_request(self):
        """
        Whatever the URI is used to access mimic is the one that is passed
        back to the plugin when
        :func:`mimic.imimic.IAPMock.resource_for_region` is called.
        """
        example = make_example_internal_api(self)

        core = MimicCore(Clock(), [example])
        root = MimicRoot(core).app.resource()

        # get the region and service id registered for the example API
        (region, service_id) = one_api(self, core)

        request(
            self, root, b"GET",
            "http://mybase/mimicking/{0}/{1}/more/stuff".format(service_id, region)
        )

        self.assertEqual(
            "http://mybase/mimicking/{0}/{1}/".format(service_id, region),
            example.store['uri_prefix'])
Example #25
0
    def test_child_resource_gets_base_uri_from_request(self):
        """
        Whatever the URI is used to access mimic is the one that is passed
        back to the plugin when
        :func:`mimic.imimic.IAPMock.resource_for_region` is called.
        """
        example = make_example_internal_api(self)

        core = MimicCore(Clock(), [example])
        root = MimicRoot(core).app.resource()

        # get the region and service id registered for the example API
        (region, service_id) = one_api(self, core)

        request(
            self, root, b"GET",
            "http://mybase/mimicking/{0}/{1}/more/stuff".format(
                service_id, region))

        self.assertEqual(
            "http://mybase/mimicking/{0}/{1}/".format(service_id, region),
            example.store['uri_prefix'])
Example #26
0
    def test_service_with_region_internal(self):
        """
        Validate that an external service does not provide an internal service
        resource.
        """
        iapi = make_example_internal_api(self)
        helper = APIMockHelper(self, [iapi])
        core = helper.core

        service_id = None
        for a_service_id in core._uuid_to_api_internal:
            if core._uuid_to_api_internal[a_service_id] == iapi:
                service_id = a_service_id

        resource = core.service_with_region(
            u"ORD",
            service_id,
            u"http://some/random/prefix"
        )
        self.assertTrue(
            IResource.providedBy(resource)
        )
    def setUp(self):
        self.eeapi_name = u"externalServiceName"
        self.eeapi_template_id = u"uuid-endpoint-template"
        self.eeapi_template = exampleEndpointTemplate(
            name=self.eeapi_name, endpoint_uuid=self.eeapi_template_id)
        self.eeapi = make_example_external_api(
            self,
            name=self.eeapi_name,
            endpoint_templates=[self.eeapi_template],
            set_enabled=False)
        self.helper = APIMockHelper(self, [make_example_internal_api(self)])
        self.core = self.helper.core
        self.root = self.helper.root
        self.uri = self.helper.uri

        self.tenant_enabled_for = u"tenantWithApi"
        self.tenant_enabled_for_password = "******"
        self.tenant_data = TenantAuthentication(
            self, self.root, self.tenant_enabled_for,
            self.tenant_enabled_for_password)
        self.eeapi.enable_endpoint_for_tenant(self.tenant_data.get_tenant_id(),
                                              self.eeapi_template_id)
        self.core.add_api(self.eeapi)
Example #28
0
    def test_uri_for_service(self):
        """
        Validate that the URI returned by uri_for_service returns the
        appropriate base URI.
        """
        iapi = make_example_internal_api(self)
        helper = APIMockHelper(self, [iapi])
        core = helper.core

        service_id = None
        for a_service_id in core._uuid_to_api_internal:
            if core._uuid_to_api_internal[a_service_id] == iapi:
                service_id = a_service_id

        base_uri = "http://some/random/prefix"

        uri = core.uri_for_service(
            u"ORD",
            service_id,
            base_uri
        )
        self.assertTrue(
            uri.startswith(base_uri + '/')
        )
Example #29
0
 def test_load_internal_api(self):
     """
     Validate loading a valid internal API object.
     """
     APIMockHelper(self, [make_example_internal_api(self)])
Example #30
0
 def test_load_internal_api(self):
     """
     Validate loading a valid internal API object.
     """
     APIMockHelper(self, [make_example_internal_api(self)])
Example #31
0
    def test_entries_for_tenant_external_multiple_regions(self):
        """
        Test with multiple regions for the External APIs.
        """
        iapi = make_example_internal_api(self)
        eeapi = make_example_external_api(self,
                                          name=self.eeapi_name,
                                          set_enabled=True)
        eeapi2_name = "alternate-external-api"
        eeapi2_template_id = u"uuid-alternate-endpoint-template"
        eeapi2_template = exampleEndpointTemplate(
            name=eeapi2_name,
            endpoint_uuid=eeapi2_template_id,
            region=u"NEW_REGION",
            url=u"https://api.new_region.example.com:9090")
        eeapi2 = make_example_external_api(
            self,
            name=eeapi2_name,
            endpoint_templates=[eeapi2_template],
            set_enabled=True)

        core = MimicCore(Clock(), [eeapi, eeapi2, iapi])

        prefix_map = {}
        base_uri = "http://some/random/prefix"
        catalog_entries = [
            entry for entry in core.entries_for_tenant('some-tenant',
                                                       prefix_map, base_uri)
        ]

        self.assertEqual(len(core._uuid_to_api_internal), 1)
        self.assertEqual(len(core._uuid_to_api_external), 2)
        self.assertEqual(len(catalog_entries), 3)

        found_internal = False
        found_first_external = False
        found_second_external = False

        for catalog_entry in catalog_entries:
            if catalog_entry.name == eeapi.name_key:
                found_first_external = True
                self.assertEqual(catalog_entry.type, eeapi.type_key)
                self.assertEqual(catalog_entry.name, eeapi.name_key)
                self.assertEqual(catalog_entry.tenant_id, "some-tenant")
                self.assertEqual(len(catalog_entry.endpoints), 1)

            elif catalog_entry.name == eeapi2.name_key:
                found_second_external = True
                self.assertEqual(catalog_entry.type, eeapi2.type_key)
                self.assertEqual(catalog_entry.name, eeapi2.name_key)
                self.assertEqual(catalog_entry.tenant_id, "some-tenant")
                self.assertEqual(len(catalog_entry.endpoints), 1)

            elif catalog_entry.name == "serviceName":
                found_internal = True
                self.assertEqual(catalog_entry.type, "serviceType")
                self.assertEqual(catalog_entry.name, "serviceName")
                self.assertEqual(catalog_entry.tenant_id, "some-tenant")
                self.assertEqual(len(catalog_entry.endpoints), 1)

        self.assertTrue(found_internal)
        self.assertTrue(found_first_external)
        self.assertTrue(found_second_external)
Example #32
0
    def test_entries_for_tenant_external_multiple_regions(self):
        """
        Test with multiple regions for the External APIs.
        """
        iapi = make_example_internal_api(self)
        eeapi = make_example_external_api(
            self,
            name=self.eeapi_name,
            set_enabled=True
        )
        eeapi2_name = "alternate-external-api"
        eeapi2_template_id = u"uuid-alternate-endpoint-template"
        eeapi2_template = exampleEndpointTemplate(
            name=eeapi2_name,
            endpoint_uuid=eeapi2_template_id,
            region=u"NEW_REGION",
            url=u"https://api.new_region.example.com:9090"
        )
        eeapi2 = make_example_external_api(
            self,
            name=eeapi2_name,
            endpoint_templates=[eeapi2_template],
            set_enabled=True
        )

        core = MimicCore(Clock(), [eeapi, eeapi2, iapi])

        prefix_map = {}
        base_uri = "http://some/random/prefix"
        catalog_entries = [
            entry
            for entry in core.entries_for_tenant(
                'some-tenant',
                prefix_map,
                base_uri
            )
        ]

        self.assertEqual(len(core._uuid_to_api_internal), 1)
        self.assertEqual(len(core._uuid_to_api_external), 2)
        self.assertEqual(len(catalog_entries), 3)

        found_internal = False
        found_first_external = False
        found_second_external = False

        for catalog_entry in catalog_entries:
            if catalog_entry.name == eeapi.name_key:
                found_first_external = True
                self.assertEqual(catalog_entry.type, eeapi.type_key)
                self.assertEqual(catalog_entry.name, eeapi.name_key)
                self.assertEqual(catalog_entry.tenant_id, "some-tenant")
                self.assertEqual(len(catalog_entry.endpoints), 1)

            elif catalog_entry.name == eeapi2.name_key:
                found_second_external = True
                self.assertEqual(catalog_entry.type, eeapi2.type_key)
                self.assertEqual(catalog_entry.name, eeapi2.name_key)
                self.assertEqual(catalog_entry.tenant_id, "some-tenant")
                self.assertEqual(len(catalog_entry.endpoints), 1)

            elif catalog_entry.name == "serviceName":
                found_internal = True
                self.assertEqual(catalog_entry.type, "serviceType")
                self.assertEqual(catalog_entry.name, "serviceName")
                self.assertEqual(catalog_entry.tenant_id, "some-tenant")
                self.assertEqual(len(catalog_entry.endpoints), 1)

        self.assertTrue(found_internal)
        self.assertTrue(found_first_external)
        self.assertTrue(found_second_external)