public void PermissionEvaluator_ReturnsTrue_IfScopesMatchRequiredPermissions(string method, string endpoint, string userScopes)
        {
            var path       = _parser.Parse(_serviceRoot, endpoint, _serviceProvider);
            var scopesList = userScopes.Split(',');

            var permissionHandler = _model.ExtractPermissionsForRequest(method, path);

            Assert.True(permissionHandler.AllowsScopes(scopesList));
        }
コード例 #2
0
        public void SelectAction_ReturnsFunctionName_DollarCount()
        {
            // Arrange
            var       model             = new CustomersModelWithInheritance();
            var       handler           = new DefaultODataPathHandler();
            ODataPath odataPath         = handler.Parse(model.Model, _serviceRoot, "Customers(1)/NS.GetOrders(parameter=5)/$count");
            var       requestContext    = new HttpRequestContext();
            var       controllerContext = new HttpControllerContext
            {
                Request        = new HttpRequestMessage(HttpMethod.Get, "http://localhost/"),
                RequestContext = requestContext,
                RouteData      = new HttpRouteData(new HttpRoute())
            };

            controllerContext.Request.SetRequestContext(requestContext);
            ILookup <string, HttpActionDescriptor> actionMap = new HttpActionDescriptor[1].ToLookup(desc => "GetOrders");

            // Act
            string selectedAction = new FunctionRoutingConvention().SelectAction(odataPath, controllerContext, actionMap);

            // Assert
            Assert.Equal("GetOrders", selectedAction);
            Assert.Equal(2, controllerContext.Request.GetRouteData().Values.Count);
            Assert.Equal(1, controllerContext.Request.GetRouteData().Values["key"]);
            Assert.Equal(5, controllerContext.Request.GetRouteData().Values["parameter"]);
        }
コード例 #3
0
        public void SelectAction_ReturnsTheActionName_ForSingletonActionBoundToEntity()
        {
            // Arrange
            ActionRoutingConvention actionConvention = new ActionRoutingConvention();
            HttpContext             context          = CreateHttpContext("Post");

            IEdmModel model = new CustomersModelWithInheritance().Model;
            //IODataPathHandler pathHandler = context.RequestServices.GetRequiredService<IODataPathHandler>();
            IODataPathHandler pathHandler = new DefaultODataPathHandler(context.RequestServices);
            ODataPath         odataPath   = pathHandler.Parse(model, _serviceRoot, "VipCustomer/NS.upgrade");

            context.ODataFeature().Path = odataPath;

            ControllerActionDescriptor descriptor = new ControllerActionDescriptor();

            descriptor.ControllerName = "VipCustomer";
            descriptor.ActionName     = "upgrade";

            RouteContext routeContext = new RouteContext(context);
            IEnumerable <ControllerActionDescriptor> actionDescriptors = new[] { descriptor };

            // Act
            ActionDescriptor actionDescriptor = actionConvention.SelectAction(routeContext, actionDescriptors);

            // Assert
            Assert.Same(descriptor, actionDescriptor);
            Assert.Empty(routeContext.RouteData.Values);
        }
コード例 #4
0
        public void BuildQuery_ReturnsNull_IfPathHasUnsupportedSegmen(string path)
        {
            var                  model        = ODataPathQueryModel.GetModel();
            var                  pathHandler  = new DefaultODataPathHandler();
            var                  odataPath    = pathHandler.Parse(model, "http://any/", path);
            IQueryable           source       = Array.CreateInstance(typeof(ODataPathQuery_Customer), 0).AsQueryable();
            var                  queryBuilder = new ODataPathQueryBuilder(source, odataPath);
            ODataPathQueryResult result       = queryBuilder.BuildQuery();

            Assert.Null(result);
        }
コード例 #5
0
            internal static IDictionary <string, string> GetKeys(DefaultODataPathHandler pathHandler, IEdmModel edmModel, string serviceRoot, Uri uri)
            {
                ODataPath           odataPath = pathHandler.Parse(edmModel, serviceRoot, uri.ToString());
                KeyValuePathSegment segment   = odataPath.Segments.OfType <KeyValuePathSegment>().Last();

                if (segment == null)
                {
                    throw Error.InvalidOperation(SRResources.EntityReferenceMustHasKeySegment, uri);
                }

                return(segment.Values);
            }
コード例 #6
0
        private static IEnumerable <KeyValuePair <string, object> > GetKeys(DefaultODataPathHandler pathHandler,
                                                                            string serviceRoot, Uri uri, IServiceProvider requestContainer)
        {
            ODataPath  odataPath = pathHandler.Parse(serviceRoot, uri.ToString(), requestContainer);
            KeySegment segment   = odataPath.Segments.OfType <KeySegment>().Last();

            if (segment == null)
            {
                throw Error.InvalidOperation(SRResources.EntityReferenceMustHasKeySegment, uri);
            }

            return(segment.Keys);
        }
コード例 #7
0
        public void GetResponseStatusCode_ReturnsNoContentForProperties_AndNotFoundForEntities(string odataPath,
                                                                                               HttpStatusCode?expected)
        {
            // Arrange
            IEdmModel         model       = BuildModel();
            IODataPathHandler pathHandler = new DefaultODataPathHandler();
            ODataPath         path        = pathHandler.Parse(model, "http://localhost/any", odataPath);

            // Guard
            Assert.NotNull(path);

            // Act
            HttpStatusCode?statusCode = ODataNullValueMessageHandler.GetUpdatedResponseStatusCodeOrNull(path);

            // Assert
            Assert.Equal(expected, statusCode);
        }
コード例 #8
0
        public void GetSingleEntityEntityType_ReturnsEntityTypeForSingleEntityResources(string odataPath, string typeName)
        {
            // Arrange
            IEdmModel         model       = SetupModel();
            IODataPathHandler pathHandler = new DefaultODataPathHandler();
            ODataPath         path        = pathHandler.Parse(model, "http://localhost/any", odataPath);

            // Guard
            Assert.NotNull(path);

            // Act
            IEdmEntityType entityType = ETagMessageHandler.GetSingleEntityEntityType(path);

            // Assert
            Assert.NotNull(entityType);
            Assert.Equal(typeName, entityType.FullName());
        }
コード例 #9
0
        public void Validate_Throws_DollarCountAppliedOnNotCountableCollection(string uri, string message)
        {
            // Arrange
            IEdmModel model       = GetEdmModel();
            var       pathHandler = new DefaultODataPathHandler();
            string    serviceRoot = "http://localhost/";
            ODataPath path        = pathHandler.Parse(model, serviceRoot, uri);
            var       context     = new ODataQueryContext(
                model,
                EdmCoreModel.Instance.GetInt32(false).Definition,
                path);
            var option   = new CountQueryOption("true", context);
            var settings = new ODataValidationSettings();

            // Act & Assert
            ExceptionAssert.Throws <InvalidOperationException>(() => _validator.Validate(option, settings), message);
        }
コード例 #10
0
        public void BuildQuery_SetValueFlagToTrue_IfPathHasValueSegment(string path, string expectedQuery)
        {
            var                  model        = ODataPathQueryModel.GetModel();
            var                  pathHandler  = new DefaultODataPathHandler();
            var                  odataPath    = pathHandler.Parse(model, "http://any/", path);
            IQueryable           source       = Array.CreateInstance(typeof(ODataPathQuery_Customer), 0).AsQueryable();
            var                  queryBuilder = new ODataPathQueryBuilder(source, odataPath);
            ODataPathQueryResult result       = queryBuilder.BuildQuery();

            string queryExpression = ExpressionStringBuilder.ToString(result.Result.Expression);

            queryExpression = RemoveNameSpace(queryExpression);

            Assert.Equal(expectedQuery, queryExpression);
            Assert.True(result.HasValueSegment);
            Assert.False(result.HasCountSegment);
        }
コード例 #11
0
        public void SelectAction_ReturnsFunctionName_DollarCount()
        {
            // Arrange
            var       model     = new CustomersModelWithInheritance();
            var       handler   = new DefaultODataPathHandler();
            ODataPath odataPath = handler.Parse(model.Model, _serviceRoot, "Customers(1)/NS.GetOrders(parameter=5)/$count");
            var       request   = RequestFactory.Create(HttpMethod.Get, "http://localhost/");
            var       actionMap = SelectActionHelper.CreateActionMap("GetOrders");

            // Act
            string selectedAction = SelectActionHelper.SelectAction(new FunctionRoutingConvention(), odataPath, request, actionMap);

            // Assert
            Assert.Equal("GetOrders", selectedAction);
            Assert.Equal(2, SelectActionHelper.GetRouteData(request).Values.Count);
            Assert.Equal(1, SelectActionHelper.GetRouteData(request).Values["key"]);
            Assert.Equal(5, SelectActionHelper.GetRouteData(request).Values["parameter"]);
        }
コード例 #12
0
        public void GetODataPayloadSerializer_ReturnsRawValueSerializer_ForDollarCountRequests(string uri, Type elementType)
        {
            // Arrange
            IEdmModel model       = ODataCountTest.GetEdmModel();
            Type      type        = typeof(ICollection <>).MakeGenericType(elementType);
            var       pathHandler = new DefaultODataPathHandler();
            var       path        = pathHandler.Parse(model, "http://localhost/", uri);
            var       request     = RequestFactory.CreateFromModel(model);

            request.ODataContext().Path = path;

            // Act
            var serializer = _serializerProvider.GetODataPayloadSerializer(type, request);

            // Assert
            Assert.NotNull(serializer);
            var rawValueSerializer = Assert.IsType <ODataRawValueSerializer>(serializer);

            Assert.Equal(ODataPayloadKind.Value, rawValueSerializer.ODataPayloadKind);
        }
コード例 #13
0
        public void GetODataPayloadSerializer_ReturnsRawValueSerializer_ForDollarCountRequests(string uri, Type elementType)
        {
            // Arrange
            IEdmModel model       = ODataCountTest.GetEdmModel();
            Type      type        = typeof(ICollection <>).MakeGenericType(elementType);
            var       request     = new HttpRequestMessage();
            var       pathHandler = new DefaultODataPathHandler();
            var       path        = pathHandler.Parse(model, "http://localhost/", uri);

            request.ODataProperties().Path = path;
            request.EnableHttpDependencyInjectionSupport(model);

            // Act
            var serializer = _serializerProvider.GetODataPayloadSerializer(type, request);

            // Assert
            Assert.NotNull(serializer);
            var rawValueSerializer = Assert.IsType <ODataRawValueSerializer>(serializer);

            Assert.Equal(ODataPayloadKind.Value, rawValueSerializer.ODataPayloadKind);
        }
        public void GetODataPayloadSerializer_ReturnsRawValueSerializer_ForDollarCountRequests(string uri, Type elementType)
        {
            // Arrange
            IEdmModel model = ODataCountTest.GetEdmModel();
            Type      type  = typeof(ICollection <>).MakeGenericType(elementType);
            DefaultODataSerializerProvider serialierProvider = new DefaultODataSerializerProvider();
            HttpContext context = CreateHttpContext(serialierProvider);

            var pathHandler = new DefaultODataPathHandler(context.RequestServices);
            var path        = pathHandler.Parse(model, "http://localhost/", uri);

            context.ODataFeature().Path  = path;
            context.ODataFeature().Model = model;

            // Act
            var serializer = serialierProvider.GetODataPayloadSerializer(context, type);

            // Assert
            Assert.NotNull(serializer);
            var rawValueSerializer = Assert.IsType <ODataRawValueSerializer>(serializer);

            Assert.Equal(ODataPayloadKind.Value, rawValueSerializer.ODataPayloadKind);
        }
コード例 #15
0
        public void SelectAction_ReturnsFunctionName_DollarCount()
        {
            // Arrange
            var model = new CustomersModelWithInheritance();
            var handler = new DefaultODataPathHandler();
            ODataPath odataPath = handler.Parse(model.Model, _serviceRoot, "Customers(1)/NS.GetOrders(parameter=5)/$count");
            var requestContext = new HttpRequestContext();
            var controllerContext = new HttpControllerContext
            {
                Request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/"),
                RequestContext = requestContext,
                RouteData = new HttpRouteData(new HttpRoute())
            };
            controllerContext.Request.SetRequestContext(requestContext);
            ILookup<string, HttpActionDescriptor> actionMap = new HttpActionDescriptor[1].ToLookup(desc => "GetOrders");

            // Act
            string selectedAction = new FunctionRoutingConvention().SelectAction(odataPath, controllerContext, actionMap);

            // Assert
            Assert.Equal("GetOrders", selectedAction);
            Assert.Equal(2, controllerContext.Request.GetRouteData().Values.Count);
            Assert.Equal("1", controllerContext.Request.GetRouteData().Values["key"]);
            Assert.Equal(5, controllerContext.Request.GetRouteData().Values["parameter"]);
        }