internal void OnActionExecuting(
            ActionExecutingContext filterContext, 
            ApplicationUserManager userManager,
            ClientIdCalculator clientIdCalculator)
        {
            if (filterContext.ActionParameters.ContainsKey(USER_CONTEXT_KEY))
            {
                ApplicationUser applicationUser;

                if (filterContext.HttpContext.User.Identity.IsAuthenticated)
                {
                    string userId = filterContext.HttpContext.User.Identity.GetUserId();
                    applicationUser = userManager.FindByIdAsync(userId).Result;
                }else
                {
                    applicationUser = new AnonymousApplicationUser();
                    if (this.RequiresGamingGroup)
                    {
                        filterContext.Result = new RedirectToRouteResult(MVC.Account.Login().GetRouteValueDictionary());
                    }
                }

                applicationUser.AnonymousClientId = clientIdCalculator.GetClientId(filterContext.HttpContext.Request, applicationUser);

                filterContext.ActionParameters[USER_CONTEXT_KEY] = applicationUser;
            }

            base.OnActionExecuting(filterContext);
        }
Example #2
0
        public void SetUp()
        {
            this.actionExecutingContext = new ActionExecutingContext
            {
                ActionParameters = new Dictionary <string, object>()
            };
            this.userStoreMock = MockRepository.GenerateMock <IUserStore <ApplicationUser> >();
            this.dataProtectionProviderMock = MockRepository.GenerateMock <IDataProtectionProvider>();
            var dataProtector = MockRepository.GenerateMock <IDataProtector>();

            this.dataProtectionProviderMock.Expect(mock => mock.Create(Arg <string> .Is.Anything)).Return(dataProtector);
            this.userManager       = new ApplicationUserManager(this.userStoreMock, this.dataProtectionProviderMock);
            clientIdCalculatorMock = MockRepository.GenerateMock <ClientIdCalculator>();
            //need to simulate like the parameter exists on the method
            this.actionExecutingContext.ActionParameters[UserContextAttribute.USER_CONTEXT_KEY] = null;

            HttpContextBase httpContextBase = MockRepository.GenerateMock <HttpContextBase>();

            this.actionExecutingContext.HttpContext = httpContextBase;

            IPrincipal principal = MockRepository.GenerateMock <IPrincipal>();

            httpContextBase.Expect(contextBase => contextBase.User)
            .Repeat.Any()
            .Return(principal);
            this.identity = MockRepository.GenerateMock <IIdentity>();
            principal.Expect(mock => mock.Identity)
            .Repeat.Any()
            .Return(this.identity);
            this.identity.Expect(mock => mock.IsAuthenticated)
            .Repeat.Once()
            .Return(true);

            HttpRequestBase requestBaseMock = MockRepository.GenerateMock <HttpRequestBase>();

            httpContextBase.Expect(mock => mock.Request)
            .Return(requestBaseMock);
            this.requestParameters = new NameValueCollection();
            requestBaseMock.Expect(mock => mock.Params)
            .Return(this.requestParameters);

            this.userContextActionFilter = new UserContextAttribute();
            this.applicationUser         = new ApplicationUser()
            {
                Id = "user id",
                CurrentGamingGroupId = 315
            };
            Task <ApplicationUser> task = Task.FromResult(this.applicationUser);

            //TODO can't figure out how to mock the GetUserId() extension method, so have to be less strict here
            this.userStoreMock.Expect(mock => mock.FindByIdAsync(Arg <string> .Is.Anything))
            .Repeat.Once()
            .Return(task);
        }
        private void SetupExpectations(bool isAuthenticated = true, bool userHasGamingGroup = true)
        {
            _actionExecutingContext = new ActionExecutingContext
            {
                ActionParameters = new Dictionary <string, object>()
            };
            _userStoreMock = MockRepository.GenerateMock <IUserStore <ApplicationUser> >();
            _dataProtectionProviderMock = MockRepository.GenerateMock <IDataProtectionProvider>();
            var dataProtector = MockRepository.GenerateMock <IDataProtector>();

            _dataProtectionProviderMock.Expect(mock => mock.Create(Arg <string> .Is.Anything)).Return(dataProtector);
            _userManager            = new ApplicationUserManager(_userStoreMock, _dataProtectionProviderMock);
            _clientIdCalculatorMock = MockRepository.GenerateMock <ClientIdCalculator>();
            //need to simulate like the parameter exists on the method
            _actionExecutingContext.ActionParameters[UserContextAttribute.UserContextKey] = null;

            var httpContextBase = MockRepository.GenerateMock <HttpContextBase>();

            _actionExecutingContext.HttpContext = httpContextBase;

            var principal = MockRepository.GenerateMock <IPrincipal>();

            httpContextBase.Expect(contextBase => contextBase.User)
            .Repeat.Any()
            .Return(principal);
            _identity = MockRepository.GenerateMock <IIdentity>();
            principal.Expect(mock => mock.Identity)
            .Repeat.Any()
            .Return(_identity);
            _identity.Expect(mock => mock.IsAuthenticated)
            .Repeat.Once()
            .Return(isAuthenticated);

            var requestBaseMock = MockRepository.GenerateMock <HttpRequestBase>();

            httpContextBase.Expect(mock => mock.Request)
            .Return(requestBaseMock);
            _requestParameters = new NameValueCollection();
            requestBaseMock.Expect(mock => mock.Params)
            .Return(_requestParameters);

            _userContextActionFilter = MockRepository.GeneratePartialMock <UserContextAttribute>();
            _applicationUser         = new ApplicationUser()
            {
                Id = "user id",
                CurrentGamingGroupId = userHasGamingGroup ? (int?)315 : null
            };
            var task = Task.FromResult(_applicationUser);

            //TODO can't figure out how to mock the GetUserId() extension method, so have to be less strict here
            _userStoreMock.Expect(mock => mock.FindByIdAsync(Arg <string> .Is.Anything))
            .Repeat.Once()
            .Return(task);
        }
Example #4
0
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //TODO this doesn't appear to honor the HttpContextScoped structureMap directive... so it is getting a new
            // instance each time
            this.userManager = DependencyResolver.Current.GetService <ApplicationUserManager>();
            //TODO if there is only one implementation is this OK?
            this.clientIdCalculator = new ClientIdCalculator();

            this.OnActionExecuting(filterContext, this.userManager, clientIdCalculator);

            base.OnActionExecuting(filterContext);
        }
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //TODO this doesn't appear to honor the HttpContextScoped structureMap directive... so it is getting a new
            // instance each time
            this.userManager = DependencyResolver.Current.GetService<ApplicationUserManager>();
            //TODO if there is only one implementation is this OK?
            this.clientIdCalculator = new ClientIdCalculator();

            this.OnActionExecuting(filterContext, this.userManager, clientIdCalculator);

            base.OnActionExecuting(filterContext);
        }
Example #6
0
        public void SetUp()
        {
            request = new HttpRequestMessage();
            request.SetConfiguration(new HttpConfiguration());
            authTokenValidatorMock = MockRepository.GenerateMock <IAuthTokenValidator>();
            clientIdCalculatorMock = MockRepository.GenerateMock <ClientIdCalculator>();
            attribute = new ApiAuthenticationAttribute(authTokenValidatorMock, clientIdCalculatorMock);
            var controllerMock = MockRepository.GeneratePartialMock <ApiControllerBase>();

            controllerContext = new HttpControllerContext {
                Request = request, Controller = controllerMock
            };
            actionContext = new HttpActionContext(controllerContext, new ReflectedHttpActionDescriptor());
        }
        internal void OnActionExecuting(
            ActionExecutingContext filterContext,
            ApplicationUserManager userManager,
            ClientIdCalculator clientIdCalculator)
        {
            if (filterContext.ActionParameters.ContainsKey(UserContextKey))
            {
                ApplicationUser applicationUser;

                if (filterContext.HttpContext.User.Identity.IsAuthenticated)
                {
                    var userId = filterContext.HttpContext.User.Identity.GetUserId();
                    applicationUser = userManager.FindByIdAsync(userId).Result;
                    if (RequiresGamingGroup &&
                        !applicationUser.CurrentGamingGroupId.HasValue &&
                        !filterContext.IsChildAction)
                    {
                        var url = CreateManageAccountUrl(filterContext.RequestContext);

                        filterContext.Result = new RedirectResult(url);
                    }
                }
                else
                {
                    applicationUser = new AnonymousApplicationUser();
                    if (RequiresGamingGroup)
                    {
                        filterContext.Result = new RedirectToRouteResult(MVC.Account.Login().GetRouteValueDictionary());
                    }
                }

                applicationUser.AnonymousClientId = clientIdCalculator.GetClientId(filterContext.HttpContext.Request, applicationUser);

                filterContext.ActionParameters[UserContextKey] = applicationUser;
            }

            base.OnActionExecuting(filterContext);
        }