def test_update_endpoint_template(self):
        """
        Validate that an endpoint template can be updated provided that
        the id field matches.
        """
        eeapi_template_id = u"uuid-alternate-endpoint-template"

        new_url = "https://api.new_region.example.com:9090"
        new_region = "NEW_REGION"
        old_eeapi_template = exampleEndpointTemplate(
            name=self.eeapi_name,
            endpoint_uuid=eeapi_template_id,
            region=new_region,
        )
        new_eeapi_template = exampleEndpointTemplate(
            name=self.eeapi_name,
            endpoint_uuid=eeapi_template_id,
            region=new_region,
            url=new_url
        )
        eeapi = make_example_external_api(
            self,
            name=self.eeapi_name,
            set_enabled=True,
            endpoint_templates=[old_eeapi_template]
        )

        self.assertEqual(eeapi.endpoint_templates[eeapi_template_id],
                         old_eeapi_template)
        eeapi.update_template(new_eeapi_template)
        self.assertEqual(eeapi.endpoint_templates[eeapi_template_id],
                         new_eeapi_template)
    def test_update_endpoint_template(self):
        """
        Validate that an endpoint template can be updated provided that
        the id field matches.
        """
        eeapi_template_id = u"uuid-alternate-endpoint-template"

        new_url = "https://api.new_region.example.com:9090"
        new_region = "NEW_REGION"
        old_eeapi_template = exampleEndpointTemplate(
            name=self.eeapi_name,
            endpoint_uuid=eeapi_template_id,
            region=new_region,
        )
        new_eeapi_template = exampleEndpointTemplate(
            name=self.eeapi_name,
            endpoint_uuid=eeapi_template_id,
            region=new_region,
            url=new_url)
        eeapi = make_example_external_api(
            self,
            name=self.eeapi_name,
            set_enabled=True,
            endpoint_templates=[old_eeapi_template])

        self.assertEqual(eeapi.endpoint_templates[eeapi_template_id],
                         old_eeapi_template)
        eeapi.update_template(new_eeapi_template)
        self.assertEqual(eeapi.endpoint_templates[eeapi_template_id],
                         new_eeapi_template)
    def test_remove_endpoint_template_with_user_registration_alternate(self):
        """
        Validate that only the endpoint template that is suppose to be removed
        is removed.
        """
        eeapi_template_id = u"uuid-alternate-endpoint-template"
        alternate_eeapi_template_id = u"uuid-alternate-endpoint-template-alt"
        eeapi_template = exampleEndpointTemplate(
            name=self.eeapi_name, endpoint_uuid=eeapi_template_id)
        alternate_eeapi_template = exampleEndpointTemplate(
            name=self.eeapi_name, endpoint_uuid=alternate_eeapi_template_id)
        eeapi = make_example_external_api(
            self,
            name=self.eeapi_name,
            set_enabled=True,
            endpoint_templates=[eeapi_template, alternate_eeapi_template])
        eeapi.enable_endpoint_for_tenant('some-tenant', eeapi_template_id)
        eeapi.enable_endpoint_for_tenant('some-other-tenant',
                                         alternate_eeapi_template_id)

        self.assertIn(eeapi_template_id, eeapi.endpoint_templates)
        self.assertIn(alternate_eeapi_template_id, eeapi.endpoint_templates)
        eeapi.remove_template(eeapi_template_id)
        self.assertNotIn(eeapi_template_id, eeapi.endpoint_templates)
        self.assertIn(alternate_eeapi_template_id, eeapi.endpoint_templates)
    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 test_multiple_endpoints_enabled_for_tenant(self):
        """
        Validate when there are multiple endpoints enabled for a single
        tenant.
        """
        new_url = "https://api.new_region.example.com:9090"
        new_region = "NEW_REGION"
        new_eeapi_template_id = u"uuid-alternate-endpoint-template"
        new_eeapi_template = exampleEndpointTemplate(
            name=self.eeapi_name,
            endpoint_uuid=new_eeapi_template_id,
            region=new_region,
            url=new_url
        )
        self.eeapi.add_template(new_eeapi_template)
        self.eeapi.enable_endpoint_for_tenant(
            self.tenant_data.get_tenant_id(),
            new_eeapi_template_id
        )

        tenant_data = TenantAuthentication(
            self,
            self.root,
            self.tenant_enabled_for,
            self.tenant_enabled_for_password
        )
        externalService_endpoint = tenant_data.get_service_endpoint(
            "externalServiceName", new_region)
        self.assertTrue(
            externalService_endpoint.startswith(
                new_url))
    def test_remove_endpoint_template(self, template_is_valid):
        """
        Validate that an endpoint template can be removed from the
        :obj:`ExternalApiStore`.
        """
        eeapi_template_id = u"uuid-alternate-endpoint-template"
        eeapi_template = None
        if template_is_valid:
            eeapi_template = [
                exampleEndpointTemplate(
                    name=self.eeapi_name,
                    endpoint_uuid=eeapi_template_id
                )
            ]
        eeapi = make_example_external_api(
            self,
            name=self.eeapi_name,
            set_enabled=True,
            endpoint_templates=eeapi_template
        )

        if template_is_valid:
            self.assertIn(eeapi_template_id, eeapi.endpoint_templates)
            eeapi.remove_template(eeapi_template_id)
            self.assertNotIn(eeapi_template_id, eeapi.endpoint_templates)
        else:
            with self.assertRaises(InvalidEndpointTemplateId):
                eeapi.remove_template(eeapi_template_id)
    def test_update_endpoint_template_invalid_data(self, invalid_data, expected_exception):
        """
        :obj:`ExternalApiStore` will raise the appropriate exception when
        given fields are missing from the endpoint template during the update.
        """
        new_url = "https://api.new_region.example.com:9090"
        new_region = "NEW_REGION"
        eeapi = make_example_external_api(
            self,
            name=self.eeapi_name,
            set_enabled=True,
        )
        new_id = get_template_id(self, eeapi)
        new_eeapi_template = exampleEndpointTemplate(
            name=self.eeapi_name,
            endpoint_uuid=new_id,
            region=new_region,
            url=new_url
        )
        if invalid_data == 'type':
            new_eeapi_template.type_key = "some-other-type"
        elif invalid_data == 'id':
            eeapi.endpoint_templates[new_id].id_key = \
                u"uuid-alternate-endpoint-template"

        with self.assertRaises(expected_exception):
            eeapi.update_template(new_eeapi_template)
    def test_update_endpoint_template_invalid_data(self, invalid_data,
                                                   expected_exception):
        """
        :obj:`ExternalApiStore` will raise the appropriate exception when
        given fields are missing from the endpoint template during the update.
        """
        new_url = "https://api.new_region.example.com:9090"
        new_region = "NEW_REGION"
        eeapi = make_example_external_api(
            self,
            name=self.eeapi_name,
            set_enabled=True,
        )
        new_id = get_template_id(self, eeapi)
        new_eeapi_template = exampleEndpointTemplate(name=self.eeapi_name,
                                                     endpoint_uuid=new_id,
                                                     region=new_region,
                                                     url=new_url)
        if invalid_data == 'type':
            new_eeapi_template.type_key = "some-other-type"
        elif invalid_data == 'id':
            eeapi.endpoint_templates[new_id].id_key = \
                u"uuid-alternate-endpoint-template"

        with self.assertRaises(expected_exception):
            eeapi.update_template(new_eeapi_template)
    def test_duplicate_api_insertion_fails(self):
        """
        Validate only one template for by a given name (id) can be added at a
        time.
        """
        eeapi = make_example_external_api(
            self,
            name=self.eeapi_name,
            set_enabled=True
        )
        new_url = "https://api.new_region.example.com:9090"
        new_region = "NEW_REGION"
        new_eeapi_template_id = u"uuid-alternate-endpoint-template"
        new_eeapi_template = exampleEndpointTemplate(
            name=self.eeapi_name,
            endpoint_uuid=new_eeapi_template_id,
            region=new_region,
            url=new_url
        )

        # first time succeeds
        eeapi.add_template(new_eeapi_template)

        # second time fails
        with self.assertRaises(EndpointTemplateAlreadyExists):
            eeapi.add_template(new_eeapi_template)
    def test_multiple_endpoint_templates_only_one_enabled_for_tenant(self):
        """
        Code coverage for multiple templates when a disabled template
        is enabled for a specific tenant while another template remains
        in its default state.
        """
        new_url = "https://api.new_region.example.com:9090"
        new_region = "NEW_REGION"
        new_eeapi_template_id = u"uuid-alternate-endpoint-template"
        new_eeapi_template = exampleEndpointTemplate(
            name=self.eeapi_name,
            endpoint_uuid=new_eeapi_template_id,
            region=new_region,
            url=new_url
        )
        self.eeapi.add_template(new_eeapi_template)

        tenant_data = TenantAuthentication(
            self,
            self.root,
            self.tenant_enabled_for,
            self.tenant_enabled_for_password
        )
        with self.assertRaises(KeyError):
            tenant_data.get_service_endpoint("externalServiceName", new_region)
    def test_remove_endpoint_template_with_user_registration_alternate(self):
        """
        Validate that only the endpoint template that is suppose to be removed
        is removed.
        """
        eeapi_template_id = u"uuid-alternate-endpoint-template"
        alternate_eeapi_template_id = u"uuid-alternate-endpoint-template-alt"
        eeapi_template = exampleEndpointTemplate(
            name=self.eeapi_name,
            endpoint_uuid=eeapi_template_id
        )
        alternate_eeapi_template = exampleEndpointTemplate(
            name=self.eeapi_name,
            endpoint_uuid=alternate_eeapi_template_id
        )
        eeapi = make_example_external_api(
            self,
            name=self.eeapi_name,
            set_enabled=True,
            endpoint_templates=[
                eeapi_template,
                alternate_eeapi_template
            ]
        )
        eeapi.enable_endpoint_for_tenant(
            'some-tenant',
            eeapi_template_id
        )
        eeapi.enable_endpoint_for_tenant(
            'some-other-tenant',
            alternate_eeapi_template_id
        )

        self.assertIn(eeapi_template_id, eeapi.endpoint_templates)
        self.assertIn(alternate_eeapi_template_id, eeapi.endpoint_templates)
        eeapi.remove_template(eeapi_template_id)
        self.assertNotIn(eeapi_template_id, eeapi.endpoint_templates)
        self.assertIn(alternate_eeapi_template_id, eeapi.endpoint_templates)
    def test_remove_endpoint_template_with_user_registration(self):
        """
        Validate that an endpoint template can be removed even if it enabled
        for a specific tenant.
        """
        eeapi_template_id = u"uuid-alternate-endpoint-template"
        eeapi_template = exampleEndpointTemplate(
            name=self.eeapi_name, endpoint_uuid=eeapi_template_id)
        eeapi = make_example_external_api(self,
                                          name=self.eeapi_name,
                                          set_enabled=True,
                                          endpoint_templates=[eeapi_template])
        eeapi.enable_endpoint_for_tenant('some-tenant', eeapi_template_id)

        self.assertIn(eeapi_template_id, eeapi.endpoint_templates)
        eeapi.remove_template(eeapi_template_id)
        self.assertNotIn(eeapi_template_id, eeapi.endpoint_templates)
    def test_listing_templates(self):
        """
        Listing templates provides entries and changes as expected.
        Template status is not dependant on whether or not the template is enabled.
        """
        eeapi_template_id = 'some-template-id'
        eeapi_template = exampleEndpointTemplate(
            name=self.eeapi_name,
            endpoint_uuid=eeapi_template_id,
        )
        eeapi = make_example_external_api(self,
                                          name=self.eeapi_name,
                                          endpoint_templates=[eeapi_template])

        self.assertEqual(len(eeapi.list_templates()), 1)
        eeapi.remove_template(eeapi_template_id)
        self.assertEqual(len(eeapi.list_templates()), 0)
    def test_disable_endpoint_template_for_tenant(self):
        """
        Validate that an endpoint template can be enabled and disabled for a
        given tenant.
        """
        new_url = "https://api.new_region.example.com:9090"
        new_region = "NEW_REGION"
        new_eeapi_template_id = u"uuid-alternate-endpoint-template"
        new_eeapi_template = exampleEndpointTemplate(
            name=self.eeapi_name,
            endpoint_uuid=new_eeapi_template_id,
            region=new_region,
            url=new_url
        )
        eeapi = make_example_external_api(
            self,
            name=self.eeapi_name,
            set_enabled=False,
            endpoint_templates=[new_eeapi_template]
        )

        ept_for_tenant = eeapi.list_tenant_endpoints('some-tenant')
        self.assertEqual(len(ept_for_tenant), 0)

        eeapi.enable_endpoint_for_tenant(
            'some-tenant',
            new_eeapi_template_id
        )
        ept_for_tenant = eeapi.list_tenant_endpoints('some-tenant')
        self.assertEqual(len(ept_for_tenant), 1)
        self.assertEqual(ept_for_tenant[0].tenant_id, 'some-tenant')
        self.assertEqual(ept_for_tenant[0].region, new_region)
        self.assertEqual(ept_for_tenant[0].endpoint_id, new_eeapi_template_id)
        self.assertEqual(ept_for_tenant[0].prefix, "v1")
        self.assertTrue(ept_for_tenant[0].external)
        self.assertIsNotNone(ept_for_tenant[0].complete_url)
        self.assertEqual(ept_for_tenant[0].complete_url, new_url)

        eeapi.disable_endpoint_for_tenant(
            'some-tenant',
            new_eeapi_template_id
        )
        ept_for_tenant = eeapi.list_tenant_endpoints('some-tenant')
        self.assertEqual(len(ept_for_tenant), 0)
    def test_listing_templates(self):
        """
        Listing templates provides entries and changes as expected.
        Template status is not dependant on whether or not the template is enabled.
        """
        eeapi_template_id = 'some-template-id'
        eeapi_template = exampleEndpointTemplate(
            name=self.eeapi_name,
            endpoint_uuid=eeapi_template_id,
        )
        eeapi = make_example_external_api(
            self,
            name=self.eeapi_name,
            endpoint_templates=[eeapi_template]
        )

        self.assertEqual(len(eeapi.list_templates()), 1)
        eeapi.remove_template(eeapi_template_id)
        self.assertEqual(len(eeapi.list_templates()), 0)
    def test_add_template_with_mismatching_service_type(self):
        """
        Validate that adding a template the service type must match
        """
        eeapi = make_example_external_api(self,
                                          name=self.eeapi_name,
                                          set_enabled=True)
        new_url = "https://api.new_region.example.com:9090"
        new_region = "NEW_REGION"
        new_eeapi_template_id = u"uuid-alternate-endpoint-template"
        new_eeapi_template = exampleEndpointTemplate(
            name=self.eeapi_name,
            endpoint_uuid=new_eeapi_template_id,
            region=new_region,
            url=new_url)
        new_eeapi_template.type_key = "random-type"

        with self.assertRaises(InvalidEndpointTemplateServiceType):
            eeapi.add_template(new_eeapi_template)
    def test_multiple_endpoint_templates_only_one_enabled_for_tenant(self):
        """
        Code coverage for multiple templates when a disabled template
        is enabled for a specific tenant while another template remains
        in its default state.
        """
        new_url = "https://api.new_region.example.com:9090"
        new_region = "NEW_REGION"
        new_eeapi_template_id = u"uuid-alternate-endpoint-template"
        new_eeapi_template = exampleEndpointTemplate(
            name=self.eeapi_name,
            endpoint_uuid=new_eeapi_template_id,
            region=new_region,
            url=new_url)
        self.eeapi.add_template(new_eeapi_template)

        tenant_data = TenantAuthentication(self, self.root,
                                           self.tenant_enabled_for,
                                           self.tenant_enabled_for_password)
        with self.assertRaises(KeyError):
            tenant_data.get_service_endpoint("externalServiceName", new_region)
    def test_add_template_with_mismatching_service_type(self):
        """
        Validate that adding a template the service type must match
        """
        eeapi = make_example_external_api(
            self,
            name=self.eeapi_name,
            set_enabled=True
        )
        new_url = "https://api.new_region.example.com:9090"
        new_region = "NEW_REGION"
        new_eeapi_template_id = u"uuid-alternate-endpoint-template"
        new_eeapi_template = exampleEndpointTemplate(
            name=self.eeapi_name,
            endpoint_uuid=new_eeapi_template_id,
            region=new_region,
            url=new_url
        )
        new_eeapi_template.type_key = "random-type"

        with self.assertRaises(InvalidEndpointTemplateServiceType):
            eeapi.add_template(new_eeapi_template)
    def test_update_endpoint_template_invalid(self):
        """
        Validate that the :obj:`ExternalApiStore` will raise the `IndexError`
        exception if the template id is not found when doing an update; in
        otherwords, update != (update or add).
        """
        new_url = "https://api.new_region.example.com:9090"
        new_region = "NEW_REGION"
        new_eeapi_template_id = u"uuid-alternate-endpoint-template"
        new_eeapi_template = exampleEndpointTemplate(
            name=self.eeapi_name,
            endpoint_uuid=new_eeapi_template_id,
            region=new_region,
            url=new_url)
        eeapi = make_example_external_api(
            self,
            name=self.eeapi_name,
            set_enabled=True,
        )

        with self.assertRaises(EndpointTemplateDoesNotExist):
            eeapi.update_template(new_eeapi_template)
    def test_listing_templates_tenant_with_specific_enabled(self):
        """
        Listing templates for a tenant provides entries and changes as expected.
        Template status is dependant on whether or not the template is enabled;
        this disables the template instead of removing it.
        """
        tenant_id = 'some-tenant'
        eeapi_template_id = 'some-template-id'
        eeapi_template = exampleEndpointTemplate(
            name=self.eeapi_name,
            endpoint_uuid=eeapi_template_id,
        )
        eeapi = make_example_external_api(self,
                                          name=self.eeapi_name,
                                          endpoint_templates=[eeapi_template])
        eeapi.enable_endpoint_for_tenant(tenant_id, eeapi_template_id)

        listing = [ept for ept in eeapi.list_tenant_templates(tenant_id)]
        self.assertEqual(len(listing), 1)
        eeapi.disable_endpoint_for_tenant(tenant_id, eeapi_template_id)
        new_listing = [ept for ept in eeapi.list_tenant_templates(tenant_id)]
        self.assertEqual(len(new_listing), 0)
    def test_listing_templates_tenant(self):
        """
        Listing templates for a tenant provides entries and changes as expected.
        Template status is dependant on whether or not the template is enabled;
        this test works the same as the global test.
        """
        tenant_id = 'some-tenant'
        eeapi_template_id = 'some-template-id'
        eeapi_template = exampleEndpointTemplate(
            name=self.eeapi_name,
            endpoint_uuid=eeapi_template_id,
        )
        eeapi = make_example_external_api(self,
                                          name=self.eeapi_name,
                                          endpoint_templates=[eeapi_template],
                                          set_enabled=True)

        listing = [ept for ept in eeapi.list_tenant_templates(tenant_id)]
        self.assertEqual(len(listing), 1)
        eeapi.remove_template(eeapi_template_id)
        new_listing = [ept for ept in eeapi.list_tenant_templates(tenant_id)]
        self.assertEqual(len(new_listing), 0)
    def test_update_endpoint_template_invalid(self):
        """
        Validate that the :obj:`ExternalApiStore` will raise the `IndexError`
        exception if the template id is not found when doing an update; in
        otherwords, update != (update or add).
        """
        new_url = "https://api.new_region.example.com:9090"
        new_region = "NEW_REGION"
        new_eeapi_template_id = u"uuid-alternate-endpoint-template"
        new_eeapi_template = exampleEndpointTemplate(
            name=self.eeapi_name,
            endpoint_uuid=new_eeapi_template_id,
            region=new_region,
            url=new_url
        )
        eeapi = make_example_external_api(
            self,
            name=self.eeapi_name,
            set_enabled=True,
        )

        with self.assertRaises(EndpointTemplateDoesNotExist):
            eeapi.update_template(new_eeapi_template)
    def test_duplicate_api_insertion_fails(self):
        """
        Validate only one template for by a given name (id) can be added at a
        time.
        """
        eeapi = make_example_external_api(self,
                                          name=self.eeapi_name,
                                          set_enabled=True)
        new_url = "https://api.new_region.example.com:9090"
        new_region = "NEW_REGION"
        new_eeapi_template_id = u"uuid-alternate-endpoint-template"
        new_eeapi_template = exampleEndpointTemplate(
            name=self.eeapi_name,
            endpoint_uuid=new_eeapi_template_id,
            region=new_region,
            url=new_url)

        # first time succeeds
        eeapi.add_template(new_eeapi_template)

        # second time fails
        with self.assertRaises(EndpointTemplateAlreadyExists):
            eeapi.add_template(new_eeapi_template)
    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 test_multiple_endpoints_enabled_for_tenant(self):
        """
        Validate when there are multiple endpoints enabled for a single
        tenant.
        """
        new_url = "https://api.new_region.example.com:9090"
        new_region = "NEW_REGION"
        new_eeapi_template_id = u"uuid-alternate-endpoint-template"
        new_eeapi_template = exampleEndpointTemplate(
            name=self.eeapi_name,
            endpoint_uuid=new_eeapi_template_id,
            region=new_region,
            url=new_url)
        self.eeapi.add_template(new_eeapi_template)
        self.eeapi.enable_endpoint_for_tenant(self.tenant_data.get_tenant_id(),
                                              new_eeapi_template_id)

        tenant_data = TenantAuthentication(self, self.root,
                                           self.tenant_enabled_for,
                                           self.tenant_enabled_for_password)
        externalService_endpoint = tenant_data.get_service_endpoint(
            "externalServiceName", new_region)
        self.assertTrue(externalService_endpoint.startswith(new_url))
    def test_listing_templates_tenant_with_specific_enabled(self):
        """
        Listing templates for a tenant provides entries and changes as expected.
        Template status is dependant on whether or not the template is enabled;
        this disables the template instead of removing it.
        """
        tenant_id = 'some-tenant'
        eeapi_template_id = 'some-template-id'
        eeapi_template = exampleEndpointTemplate(
            name=self.eeapi_name,
            endpoint_uuid=eeapi_template_id,
        )
        eeapi = make_example_external_api(
            self,
            name=self.eeapi_name,
            endpoint_templates=[eeapi_template]
        )
        eeapi.enable_endpoint_for_tenant(tenant_id, eeapi_template_id)

        listing = [ept for ept in eeapi.list_tenant_templates(tenant_id)]
        self.assertEqual(len(listing), 1)
        eeapi.disable_endpoint_for_tenant(tenant_id, eeapi_template_id)
        new_listing = [ept for ept in eeapi.list_tenant_templates(tenant_id)]
        self.assertEqual(len(new_listing), 0)
    def test_remove_endpoint_template_with_user_registration(self):
        """
        Validate that an endpoint template can be removed even if it enabled
        for a specific tenant.
        """
        eeapi_template_id = u"uuid-alternate-endpoint-template"
        eeapi_template = exampleEndpointTemplate(
            name=self.eeapi_name,
            endpoint_uuid=eeapi_template_id
        )
        eeapi = make_example_external_api(
            self,
            name=self.eeapi_name,
            set_enabled=True,
            endpoint_templates=[eeapi_template]
        )
        eeapi.enable_endpoint_for_tenant(
            'some-tenant',
            eeapi_template_id
        )

        self.assertIn(eeapi_template_id, eeapi.endpoint_templates)
        eeapi.remove_template(eeapi_template_id)
        self.assertNotIn(eeapi_template_id, eeapi.endpoint_templates)
    def test_listing_templates_tenant(self):
        """
        Listing templates for a tenant provides entries and changes as expected.
        Template status is dependant on whether or not the template is enabled;
        this test works the same as the global test.
        """
        tenant_id = 'some-tenant'
        eeapi_template_id = 'some-template-id'
        eeapi_template = exampleEndpointTemplate(
            name=self.eeapi_name,
            endpoint_uuid=eeapi_template_id,
        )
        eeapi = make_example_external_api(
            self,
            name=self.eeapi_name,
            endpoint_templates=[eeapi_template],
            set_enabled=True
        )

        listing = [ept for ept in eeapi.list_tenant_templates(tenant_id)]
        self.assertEqual(len(listing), 1)
        eeapi.remove_template(eeapi_template_id)
        new_listing = [ept for ept in eeapi.list_tenant_templates(tenant_id)]
        self.assertEqual(len(new_listing), 0)
    def test_disable_endpoint_template_for_tenant(self):
        """
        Validate that an endpoint template can be enabled and disabled for a
        given tenant.
        """
        new_url = "https://api.new_region.example.com:9090"
        new_region = "NEW_REGION"
        new_eeapi_template_id = u"uuid-alternate-endpoint-template"
        new_eeapi_template = exampleEndpointTemplate(
            name=self.eeapi_name,
            endpoint_uuid=new_eeapi_template_id,
            region=new_region,
            url=new_url)
        eeapi = make_example_external_api(
            self,
            name=self.eeapi_name,
            set_enabled=False,
            endpoint_templates=[new_eeapi_template])

        ept_for_tenant = eeapi.list_tenant_endpoints('some-tenant')
        self.assertEqual(len(ept_for_tenant), 0)

        eeapi.enable_endpoint_for_tenant('some-tenant', new_eeapi_template_id)
        ept_for_tenant = eeapi.list_tenant_endpoints('some-tenant')
        self.assertEqual(len(ept_for_tenant), 1)
        self.assertEqual(ept_for_tenant[0].tenant_id, 'some-tenant')
        self.assertEqual(ept_for_tenant[0].region, new_region)
        self.assertEqual(ept_for_tenant[0].endpoint_id, new_eeapi_template_id)
        self.assertEqual(ept_for_tenant[0].prefix, "v1")
        self.assertTrue(ept_for_tenant[0].external)
        self.assertIsNotNone(ept_for_tenant[0].complete_url)
        self.assertEqual(ept_for_tenant[0].complete_url, new_url)

        eeapi.disable_endpoint_for_tenant('some-tenant', new_eeapi_template_id)
        ept_for_tenant = eeapi.list_tenant_endpoints('some-tenant')
        self.assertEqual(len(ept_for_tenant), 0)
    def test_remove_endpoint_template(self, template_is_valid):
        """
        Validate that an endpoint template can be removed from the
        :obj:`ExternalApiStore`.
        """
        eeapi_template_id = u"uuid-alternate-endpoint-template"
        eeapi_template = None
        if template_is_valid:
            eeapi_template = [
                exampleEndpointTemplate(name=self.eeapi_name,
                                        endpoint_uuid=eeapi_template_id)
            ]
        eeapi = make_example_external_api(self,
                                          name=self.eeapi_name,
                                          set_enabled=True,
                                          endpoint_templates=eeapi_template)

        if template_is_valid:
            self.assertIn(eeapi_template_id, eeapi.endpoint_templates)
            eeapi.remove_template(eeapi_template_id)
            self.assertNotIn(eeapi_template_id, eeapi.endpoint_templates)
        else:
            with self.assertRaises(InvalidEndpointTemplateId):
                eeapi.remove_template(eeapi_template_id)
Exemple #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)
Exemple #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)