/// <summary>
        /// Locates the ApiKey resource. Or returns null if it is invalid or not found.
        /// </summary>
        /// <param name="apiKey">The API key</param>
        /// <param name="request">The request</param>
        /// <returns>True if the API key is associated with the API.</returns>
        private bool CanApiKeyAccessApi(ApiKey apiKey, ConnectorRequest request)
        {
            EndpointAddressResult apiDetails;

            try
            {
                apiDetails = _endpointResolver.ResolveEndpoint(request.ApiPath, true);
            }
            catch (EndpointNotFoundException)
            {
                return(false);
            }
            if (apiDetails == null)
            {
                return(false);
            }

            long apiId = apiDetails.ApiId;

            if (apiId <= 0)
            {
                return(false);
            }

            bool result = apiKey.KeyForApis.Any(api => api.Id == apiId);

            return(result);
        }
        public void Test_ApiOnly_Disabled( )
        {
            Api api = MakeApi("address1", false);
            IEndpointResolver resolver = GetResolver( );

            Assert.Throws <EndpointNotFoundException>(() => resolver.ResolveEndpoint("address1", false));
        }
        private ApiEndpoint GetEndpoint(ConnectorRequest request)
        {
            // Resolve the endpoint entity
            EndpointAddressResult address;

            try
            {
                address = _endpointResolver.ResolveEndpoint(request.ApiPath, false);
            }
            catch (EndpointNotFoundException)
            {
                return(null);    // can't find it, so return null
            }

            // Check the API
            Api apiEntity = _entityRepository.Get <Api>(address.ApiId);

            if (apiEntity == null)
            {
                return(null);
            }

            // Get the endpoint
            ApiEndpoint endpointEntity = _entityRepository.Get <ApiEndpoint>(address.EndpointId);

            return(endpointEntity);
        }
        public void Test_Endpoint_NotFound( )
        {
            Api api = MakeApi("my-api");

            IEndpointResolver resolver = GetResolver( );

            Assert.Throws <EndpointNotFoundException>(() => resolver.ResolveEndpoint("my-api/my-end-point", false));
        }
        public void Test_Empty( )
        {
            IEndpointResolver     resolver = GetResolver( );
            EndpointAddressResult result   = resolver.ResolveEndpoint(new string[] {}, false);

            Assert.That(result, Is.Not.Null);
            Assert.That(result.ApiId, Is.EqualTo(0));
            Assert.That(result.EndpointId, Is.EqualTo(0));
        }
        public void Test_Endpoint_CaseMismatch( )
        {
            Api api = MakeApi("my-api");
            ApiResourceEndpoint endpoint = MakeEndpoint(api, "my-eNd-point");

            IEndpointResolver resolver = GetResolver( );

            Assert.Throws <EndpointNotFoundException>(() => resolver.ResolveEndpoint("my-api/my-end-point", false));
        }
        public void Test_ApiOnly_DuplicateName( )
        {
            Api api  = MakeApi("address1", true);
            Api api2 = MakeApi("address1", true);

            IEndpointResolver resolver = GetResolver( );

            Assert.Throws <ConnectorConfigException>(() => resolver.ResolveEndpoint("address1", false));
        }
        public void Test_Endpoint_DuplicateName( )
        {
            Api api = MakeApi("my-api");
            ApiResourceEndpoint endpoint  = MakeEndpoint(api, "my-end-point");
            ApiResourceEndpoint endpoint2 = MakeEndpoint(api, "my-end-point");

            IEndpointResolver resolver = GetResolver( );

            Assert.Throws <ConnectorConfigException>(() => resolver.ResolveEndpoint("my-api/my-end-point", false));
        }
        public void Test_ApiOnly_Exists( )
        {
            Api api = MakeApi("address1");

            IEndpointResolver     resolver = GetResolver( );
            EndpointAddressResult result   = resolver.ResolveEndpoint("address1", false);

            Assert.That(result, Is.Not.Null);
            Assert.That(result.ApiId, Is.EqualTo(api.Id));
            Assert.That(result.EndpointId, Is.EqualTo(0));
        }
        public void Test_Endpoint_WithSuffix( )
        {
            Api api = MakeApi("my-api");
            ApiResourceEndpoint endpoint = MakeEndpoint(api, "my-end-point");

            IEndpointResolver     resolver = GetResolver( );
            EndpointAddressResult result   = resolver.ResolveEndpoint("my-api/my-end-point/0", false);

            Assert.That(result, Is.Not.Null);
            Assert.That(result.ApiId, Is.EqualTo(api.Id));
            Assert.That(result.EndpointId, Is.EqualTo(endpoint.Id));
        }
 public static EndpointAddressResult ResolveEndpoint(this IEndpointResolver resolver, string apiPath, bool apiOnly)
 {
     string[] parts = apiPath.Split(new[] { '/' });
     return(resolver.ResolveEndpoint(parts, false));
 }
        public void Test_ApiOnly_NameNotFound( )
        {
            IEndpointResolver resolver = GetResolver( );

            Assert.Throws <EndpointNotFoundException>(() => resolver.ResolveEndpoint("address1", false));
        }