Пример #1
0
        public void PreventFloodAttribute_Role_Test()
        {
            //set up context
            var controller = TestHelper.Resolve<TopicsController>();
            var controllerContext = new FakeControllerContext(controller, "http://localhost");
            var executingFilterContext = new ActionExecutingContext(controllerContext, new FakeActionDescriptor(), new Dictionary<string, object>());
            var executedfilterContext = new ActionExecutedContext(controllerContext, new FakeActionDescriptor(), false, null);
            var httpContext = (FakeHttpContext) controllerContext.HttpContext;
            httpContext.CleanCache();

            //set up attr
            var attr = new PreventFloodAttribute(typeof(EmptyResult));
            attr.Config.SpamPrevention.FloodControl.TimeBetweenPosts = 5;
            attr.Config.SpamPrevention.FloodControl.IgnoreForRole = UserRole.Moderator; //ignore for moderator or admin

            var session = new SessionWrapper(httpContext);
            session.SetUser(new User() { Role = UserRole.Moderator }, AuthenticationProvider.CustomDb);

            //first execution
            attr.OnActionExecuting(executingFilterContext);
            Assert.AreNotEqual<bool?>(true, (bool?)controller.ViewBag.ShowCaptcha);
            attr.OnActionExecuted(executedfilterContext);

            //second execution: must NOT be considered as flooding
            attr.OnActionExecuting(executingFilterContext);
            Assert.AreNotEqual<bool?>(true, (bool?)controller.ViewBag.ShowCaptcha);
            attr.OnActionExecuted(executedfilterContext);
        }
Пример #2
0
        public void PreventFloodAttribute_Time_Test()
        {
            //set up context
            var controller = TestHelper.Resolve<MessagesController>();
            var controllerContext = new FakeControllerContext(controller, "http://localhost");
            var executingFilterContext = new ActionExecutingContext(controllerContext, new FakeActionDescriptor(), new Dictionary<string, object>());
            var executedfilterContext = new ActionExecutedContext(controllerContext, new FakeActionDescriptor(), false, null);
            var httpContext = (FakeHttpContext)controllerContext.HttpContext;
            httpContext.CleanCache();

            //set up attr
            var attr = new PreventFloodAttribute(typeof(EmptyResult));
            attr.Config.SpamPrevention.FloodControl.TimeBetweenPosts = 5;
            attr.Config.SpamPrevention.FloodControl.IgnoreForRole = (UserRole) Int16.MaxValue; //do not ignore

            //first execution
            attr.OnActionExecuting(executingFilterContext);
            Assert.AreNotEqual<bool?>(true, (bool?)controller.ViewBag.ShowCaptcha);
            attr.OnActionExecuted(executedfilterContext);

            //second execution: must be considered as flooding
            attr.OnActionExecuting(executingFilterContext);
            Assert.AreEqual<bool?>(true, (bool?)controller.ViewBag.ShowCaptcha);
            attr.OnActionExecuted(executedfilterContext);
        }