Beispiel #1
0
        public static ControllerContext WithNotAuthenticatedUser(this ControllerContext context)
        {
            var user = new FakePrincipal(new FakeIdentity(String.Empty), null);

            context.HttpContext.Stub(x => x.User).Return(user);
            return(new ControllerContext(context.HttpContext, new RouteData(), context.Controller));
        }
		public void Current_user_should_be_set_to_guest_when_user_cannot_be_found_from_repository()
		{
			var user = new FakePrincipal() { Name = "foo@bar" };
			Thread.CurrentPrincipal = context.HttpContext.User = user;
			filter.OnAuthorization(context);

			AssertGuest();
		}
		public void Current_user_should_be_set_to_guest_when_user_is_not_authenticated()
		{
			var user = new FakePrincipal() { IsAuthenticated = false };
			context.HttpContext.User = user;
			filter.OnAuthorization(context);

			AssertGuest();
		}
        public void JackCannotAccessIndex()
        {
            // Arrange
            var controller = new JillController();
            var principal  = new FakePrincipal("Jack");

            // Act
            var result = controller.Index(principal);

            // Assert
            Assert.IsInstanceOfType(result, typeof(HttpUnauthorizedResult));
        }
        public void JillCanAccessIndex()
        {
            // Arrange
            var controller = new JillController();
            var principal  = new FakePrincipal("Jill");

            // Act
            var result = controller.Index(principal);

            // Assert
            Assert.IsInstanceOfType(result, typeof(ViewResult));
        }
Beispiel #6
0
        public void Current_user_should_be_set_to_guest_when_user_cannot_be_found_from_repository()
        {
            var user = new FakePrincipal()
            {
                Name = "foo@bar"
            };

            Thread.CurrentPrincipal = context.HttpContext.User = user;
            filter.OnAuthorization(context);

            AssertGuest();
        }
Beispiel #7
0
        public void Current_user_should_be_set_to_guest_when_user_is_not_authenticated()
        {
            var user = new FakePrincipal()
            {
                IsAuthenticated = false
            };

            context.HttpContext.User = user;
            filter.OnAuthorization(context);

            AssertGuest();
        }
        public void JillCanAccessIndex()
        {
            // Arrange
            var controller = new JillController();
            var principal = new FakePrincipal("Jill");

            // Act
            var result = controller.Index(principal);

            // Assert
            Assert.IsInstanceOfType(result, typeof(ViewResult));
        }
        public void JackCannotAccessIndex()
        {
            // Arrange
            var controller = new JillController();
            var principal = new FakePrincipal("Jack");

            // Act
            var result = controller.Index(principal);

            // Assert
            Assert.IsInstanceOfType(result, typeof(HttpUnauthorizedResult));
        }
        /// <summary>
        ///     Builds a controller of the required type using any data previously supplied (or defaults).
        /// </summary>
        /// <returns>An initialized controller of type <typeparamref name="TController" />.</returns>
        /// <exception cref="SpecificationException">Thrown if the controller cannot be built.</exception>
        public TController Build()
        {
            var dataLoader = new ObjectDataLoader(data);

            UnitOfWork = uowBuilder.WithData(dataLoader).Build();
            var mapperConfiguration = new MapperConfiguration(cfg => { cfg.AddProfile <ViewModelMappingProfile>(); });

            mapperConfiguration.AssertConfigurationIsValid();
            Mapper = mapperConfiguration.CreateMapper();
            var notifier = new FakeNotificationService();

            RulesService = new GameRulesService(UnitOfWork, Mapper, notifier);
            HttpContextBase httpContext;

            if (postedFile == null)
            {
                httpContext = new FakeHttpContext(requestPath, requestMethod.ToString("G"));
            }
            else
            {
                var filesCollection = new FakeHttpFileCollection(postedFile);
                httpContext = new FakeHttpContext(requestPath, filesCollection);
            }
            var fakeIdentity  = new FakeIdentity(requestUsername);
            var fakePrincipal = new FakePrincipal(fakeIdentity, requestUserRoles);

            httpContext.User = fakePrincipal;
            var context = new ControllerContext {
                HttpContext = httpContext
            };

            /*
             * Use Ninject to create the controller, as we don't know in advance what
             * type of controller or how many constructor parameters it has.
             */
            var kernel     = BuildNinjectKernel(fakeIdentity, requestUserId, RulesService);
            var controller = kernel.Get <TController>();

            if (controller == null)
            {
                throw new SpecificationException(
                          $"ControllerContextBuilder: Unable to create controller instance of type {nameof(TController)}");
            }

            controller.ControllerContext = context;
            controller.TempData          = tempdata;
            return(controller);
        }
Beispiel #11
0
        public void Construct_ShouldStoreParametersOnProperties()
        {
            //--------------- Arrange -------------------
            var identity     = Substitute.For <IIdentity>();
            var expectedName = GetRandomString();

            identity.Name.Returns(expectedName);
            var roles = GetRandomCollection <string>(2, 5);


            //--------------- Assume ----------------

            //--------------- Act ----------------------
            var sut = new FakePrincipal(identity, roles.ToArray());

            //--------------- Assert -----------------------
            roles.ForEach(r => Expect(sut.IsInRole(r)).To.Be.True());

            Expect(sut.Identity).To.Equal(identity);
        }
Beispiel #12
0
 public FakeHttpContext(FakePrincipal principal)
 {
     _principal = principal;
 }