Example #1
0
        private async Task AssertSecureControllerAccess(ClaimsPrincipal user, string method, int expectedStatusCode, IAuthorizationPolicyStore policyStore = null)
        {
            var ctrl = new Fakes.FakeLimitedControllerDiscoverer(typeof(Controllers.SecureController)).GetControllers(null).Single();

            if (policyStore != null)
            {
                var options = LiteApiOptions.Default;
                foreach (var policy in policyStore.GetPolicyNames())
                {
                    options.AuthorizationPolicyStore.SetPolicy(policy, policyStore.GetPolicy(policy));
                }
                ctrl.Filters = null; // force refresh init with new policy store
                foreach (var action in ctrl.Actions)
                {
                    action.Filters = null;
                }
                ctrl.Init(new LiteApiOptionsAccessor(options));
            }

            var actionCtx = ctrl.Actions.Single(x => string.Compare(method, x.Name, StringComparison.OrdinalIgnoreCase) == 0);
            var invoker   = new ActionInvoker(new ControllerBuilder((new Moq.Mock <IServiceProvider>()).Object), new ModelBinderCollection(
                                                  new JsonSerializer(), Fakes.FakeServiceProvider.GetServiceProvider(), new Fakes.FakeDefaultLiteApiOptionsRetriever()), new JsonSerializer());
            var httpCtx = new Fakes.FakeHttpContext();

            httpCtx.User         = user;
            httpCtx.Request.Path = "/api/secure/" + method;
            await invoker.Invoke(httpCtx, actionCtx);

            Assert.Equal(expectedStatusCode, httpCtx.Response.StatusCode);
        }
Example #2
0
 private IEnumerable <string> GetMissingAuthorizationPolicies(ActionContext actionCtx)
 {
     return(actionCtx
            .Method
            .GetAttributesAs <IPolicyApiFilter>()
            .Select(x => x.PolicyName)
            .Where(x => _policyStore.GetPolicy(x) == null));
 }
Example #3
0
 private IEnumerable <string> GetMissingAuthorizationPolicies(ControllerContext ctrlCtx)
 {
     return(ctrlCtx
            .ControllerType
            .GetTypeInfo()
            .GetAttributesAs <IPolicyApiFilter>()
            .Select(x => x.PolicyName)
            .Where(x => _policyStore.GetPolicy(x) == null));
 }