private static ApplicationModelProviderContext CreateProviderContext(Type controllerType)
        {
            var defaultProvider = new DefaultApplicationModelProvider(
                Options.Create(new MvcOptions()),
                new EmptyModelMetadataProvider());

            var context = new ApplicationModelProviderContext(new[] { controllerType.GetTypeInfo() });

            defaultProvider.OnProvidersExecuting(context);
            return(context);
        }
예제 #2
0
        private static ApplicationModelProviderContext GetContext(
            Type type,
            IModelMetadataProvider modelMetadataProvider = null)
        {
            var context    = new ApplicationModelProviderContext(new[] { type.GetTypeInfo() });
            var mvcOptions = Options.Create(new MvcOptions {
                AllowValidatingTopLevelNodes = true
            });

            modelMetadataProvider = modelMetadataProvider ?? new EmptyModelMetadataProvider();
            var provider = new DefaultApplicationModelProvider(mvcOptions, modelMetadataProvider);

            provider.OnProvidersExecuting(context);

            return(context);
        }
        public void CreateControllerModel_AuthorizeAttributeAddsAuthorizeFilter()
        {
            // Arrange
            var provider        = new AuthorizationApplicationModelProvider(new DefaultAuthorizationPolicyProvider(Options.Create(new AuthorizationOptions())));
            var defaultProvider = new DefaultApplicationModelProvider(Options.Create(new MvcOptions()));

            var context = new ApplicationModelProviderContext(new[] { typeof(AccountController).GetTypeInfo() });

            defaultProvider.OnProvidersExecuting(context);

            // Act
            provider.OnProvidersExecuting(context);

            // Assert
            var controller = Assert.Single(context.Result.Controllers);

            Assert.Single(controller.Filters, f => f is AuthorizeFilter);
        }
        public void CreateControllerModelAndActionModel_AllowAnonymousAttributeAddsAllowAnonymousFilter()
        {
            // Arrange
            var provider        = new AuthorizationApplicationModelProvider(new DefaultAuthorizationPolicyProvider(new TestOptionsManager <AuthorizationOptions>()));
            var defaultProvider = new DefaultApplicationModelProvider(new TestOptionsManager <MvcOptions>());

            var context = new ApplicationModelProviderContext(new[] { typeof(AnonymousController).GetTypeInfo() });

            defaultProvider.OnProvidersExecuting(context);

            // Act
            provider.OnProvidersExecuting(context);

            // Assert
            var controller = Assert.Single(context.Result.Controllers);

            Assert.Single(controller.Filters, f => f is AllowAnonymousFilter);
            var action = Assert.Single(controller.Actions);

            Assert.Single(action.Filters, f => f is AllowAnonymousFilter);
        }
        public void CreateControllerModelAndActionModel_NoAuthNoFilter()
        {
            // Arrange
            var provider = new AuthorizationApplicationModelProvider(
                new DefaultAuthorizationPolicyProvider(
                    Options.Create(new AuthorizationOptions())
                    ));
            var defaultProvider = new DefaultApplicationModelProvider(Options.Create(new MvcOptions()));

            var context = new ApplicationModelProviderContext(new[] { typeof(NoAuthController).GetTypeInfo() });

            defaultProvider.OnProvidersExecuting(context);

            // Act
            provider.OnProvidersExecuting(context);

            // Assert
            var controller = Assert.Single(context.Result.Controllers);

            Assert.Empty(controller.Filters);
            var action = Assert.Single(controller.Actions);

            Assert.Empty(action.Filters);
        }