Ejemplo n.º 1
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);
        }