示例#1
0
        public void Test_RoleRequirementFilter_NoFeatureFlagsSupplied()
        {
            // Arrange
            var mockFeatureFlags = new Mock <IFeatureFlag>();
            var filter           = new RoleRequirementFilter(new string[] { "" });

            var authContext = new AuthorizationFilterContext(new ActionContext
            {
                HttpContext      = FakeHttpContext.GetRequestHttpContext(new byte[] { }),
                RouteData        = new RouteData(),
                ActionDescriptor = new ActionDescriptor()
                {
                    AttributeRouteInfo = new AttributeRouteInfo()
                }
            },
                                                             new List <IFilterMetadata>());

            authContext.HttpContext.User = new System.Security.Claims.ClaimsPrincipal(
                new ClaimsIdentity(new List <Claim>
            {
                new Claim("oid", "token")
            })
                );

            // Act
            filter.OnAuthorization(authContext);

            // Assert
            mockFeatureFlags.Verify(m => m.GetFeatureFlag(It.IsAny <string>(), It.IsAny <bool>()), Times.Never);
        }
示例#2
0
        public void Test_RoleRequirementFilter_NoFeatureFlagsSupplied_ValidRoles()
        {
            // Arrange
            var filter = new RoleRequirementFilter(new string[] { "TestRole" });

            var authContext = new AuthorizationFilterContext(new ActionContext
            {
                HttpContext      = FakeHttpContext.GetRequestHttpContext(new byte[] { }),
                RouteData        = new RouteData(),
                ActionDescriptor = new ActionDescriptor()
                {
                    AttributeRouteInfo = new AttributeRouteInfo()
                }
            }, new List <IFilterMetadata>());

            authContext.HttpContext.User = new System.Security.Claims.ClaimsPrincipal(
                new ClaimsIdentity(new List <Claim>
            {
                new Claim("oid", "token"),
                new Claim(ClaimTypes.Role, "TestRole")
            })
                );

            // Act
            filter.OnAuthorization(authContext);

            // Assert
            Assert.Null(authContext.Result);
        }
        public void FeatureFlagAttribute_NoFeatureFlagServiceRegistered()
        {
            // Arrange
            var featureFlag         = new FeatureFlagAttribute("featureFlagKey");
            var mockServiceProvider = new Mock <IServiceProvider>();

            var context = new ActionExecutingContext(new ActionContext
            {
                HttpContext      = FakeHttpContext.GetRequestHttpContext(new byte[] { }),
                RouteData        = new RouteData(),
                ActionDescriptor = new ActionDescriptor()
                {
                    AttributeRouteInfo = new AttributeRouteInfo()
                    {
                        Name = "TestName"
                    }
                }
            },
                                                     new List <IFilterMetadata>(),
                                                     new Dictionary <string, object>()
            {
                { "id", 1 }
            }, new Mock <ControllerBase>().Object);

            context.HttpContext.RequestServices = mockServiceProvider.Object;

            // Act/Assert
            var ex = Assert.Throws <InvalidOperationException>(() => featureFlag.OnActionExecuting(context));

            Assert.Equal("No feature flag service registered", ex.Message);
        }
示例#4
0
        public void Test_RoleRequirementFilter_FeatureFlagsSupplied_RolesAuthOff()
        {
            // Arrange
            var mockFeatureFlags = new Mock <IFeatureFlag>();

            mockFeatureFlags.Setup(m => m.GetFeatureFlag(
                                       It.IsAny <string>(),
                                       It.IsAny <bool>()
                                       )).Returns(false);
            var filter = new RoleRequirementFilter(new string[] { "TestRole" }, mockFeatureFlags.Object);

            var authContext = new AuthorizationFilterContext(new ActionContext
            {
                HttpContext      = FakeHttpContext.GetRequestHttpContext(new byte[] { }),
                RouteData        = new RouteData(),
                ActionDescriptor = new ActionDescriptor()
                {
                    AttributeRouteInfo = new AttributeRouteInfo()
                }
            },
                                                             new List <IFilterMetadata>());

            authContext.HttpContext.User = new ClaimsPrincipal(
                new ClaimsIdentity(new List <Claim>
            {
                new Claim("oid", "token"),
                new Claim(ClaimTypes.Role, "InvalidTestRole")
            }));

            // Act
            filter.OnAuthorization(authContext);

            // Assert
            Assert.Null(authContext.Result);
        }
        public void Test_AuditingFilter_NoIdParameter()
        {
            // Arrange
            var mockAuditLogger = new Mock <IAuditLogger>();
            var filter          = new AuditingFilter("EventName", "EventMessage", mockAuditLogger.Object);
            var context         = new ActionExecutingContext(new ActionContext
            {
                HttpContext      = FakeHttpContext.GetRequestHttpContext(new byte[] { }),
                RouteData        = new RouteData(),
                ActionDescriptor = new ActionDescriptor()
                {
                    AttributeRouteInfo = new AttributeRouteInfo()
                    {
                    },
                    DisplayName = "TestDisplayName"
                }
            }, new List <IFilterMetadata>(), new Dictionary <string, object>(), new Mock <ControllerBase>().Object);

            // Act
            filter.OnActionExecuting(context);

            // Assert
            mockAuditLogger.Verify(m => m.WriteLog(
                                       It.Is <string>(x => x.Equals("EventName")),
                                       It.Is <string>(x => x.Equals("EventMessage")),
                                       It.Is <string>(x => x.Equals("TestUser")),
                                       It.Is <string>(x => x.Equals("TestDisplayName")),
                                       It.Is <Dictionary <string, string> >(x =>
                                                                            x["EventType"].Equals("No Route Name Supplied") &&
                                                                            x["EventTargetId"].Equals("No id Parameter Supplied")
                                                                            )
                                       ));
        }
示例#6
0
        public void Test_HttpExtensions_RequestToFormattedString()
        {
            // Arrange
            var fake = FakeHttpContext.GetRequestHttpContext(Encoding.UTF8.GetBytes("test"));

            // Act
            var str = fake.Request.ToFormattedString();

            // Assert
            str.IndexOf("Headers: testKey:testVal", StringComparison.InvariantCulture).Should().BeGreaterThan(0);
            str.IndexOf("Hostname: localhost", StringComparison.InvariantCulture).Should().BeGreaterThan(0);
        }
示例#7
0
        public void Test_HttpContextExtensions_GetRequestHeader()
        {
            // Arrange
            var fakeRequest = FakeHttpContext.GetRequestHttpContext(Encoding.UTF8.GetBytes("test"));

            // Act
            var header      = fakeRequest.GetRequestHeader("testKey");
            var emptyHeader = fakeRequest.GetRequestHeader("doesnotexist");

            // Assert
            header.Should().Be("testVal");
            emptyHeader.Should().BeNullOrEmpty();
        }
        public void FeatureFlagAttribute_FlagOff()
        {
            // Arrange
            var mockFeatureFlags = new Mock <IFeatureFlag>();

            mockFeatureFlags.Setup(
                m => m.GetFeatureFlag(
                    It.IsAny <string>(),
                    It.IsAny <bool>()
                    )).Returns(false);

            var mockServiceProvider = new Mock <IServiceProvider>();

            mockServiceProvider.Setup(m => m.GetService(It.IsAny <Type>())).Returns(mockFeatureFlags.Object);

            var featureFlag = new FeatureFlagAttribute("featureFlagKey");
            var context     = new ActionExecutingContext(new ActionContext
            {
                HttpContext      = FakeHttpContext.GetRequestHttpContext(new byte[] { }),
                RouteData        = new RouteData(),
                ActionDescriptor = new ActionDescriptor()
                {
                    AttributeRouteInfo = new AttributeRouteInfo()
                    {
                        Name = "TestName"
                    }
                }
            },
                                                         new List <IFilterMetadata>(),
                                                         new Dictionary <string, object>()
            {
                { "id", 1 }
            }, new Mock <ControllerBase>().Object);

            context.HttpContext.RequestServices = mockServiceProvider.Object;

            // Act
            featureFlag.OnActionExecuting(context);
            var result   = context.Result as NotFoundObjectResult;
            var apiError = result.Value as ApiErrorResult;

            // Assert
            Assert.NotNull(result);
            Assert.Equal("This route has been disabled.", apiError.Errors.First().Message);

            mockFeatureFlags.Verify(m =>
                                    m.GetFeatureFlag(It.IsAny <string>(), It.IsAny <bool>()), Times.Once);
        }
        public void FeatureFlagAttribute_FlagOn()
        {
            // Arrange
            var mockFeatureFlags = new Mock <IFeatureFlag>();

            mockFeatureFlags.Setup(m => m.GetFeatureFlag(It.IsAny <string>(), It.IsAny <bool>())).Returns(true);

            var mockServiceProvider = new Mock <IServiceProvider>();

            mockServiceProvider.Setup(m => m.GetService(It.IsAny <Type>())).Returns(mockFeatureFlags.Object);

            var featureFlag = new FeatureFlagAttribute("featureFlagKey");
            var context     = new ActionExecutingContext(new ActionContext
            {
                HttpContext      = FakeHttpContext.GetRequestHttpContext(new byte[] { }),
                RouteData        = new RouteData(),
                ActionDescriptor = new ActionDescriptor()
                {
                    AttributeRouteInfo = new AttributeRouteInfo()
                    {
                        Name = "TestName"
                    }
                }
            },
                                                         new List <IFilterMetadata>(),
                                                         new Dictionary <string, object>()
            {
                { "id", 1 }
            },
                                                         new Mock <ControllerBase>().Object);

            context.HttpContext.RequestServices = mockServiceProvider.Object;

            // Act
            featureFlag.OnActionExecuting(context);

            // Assert
            Assert.Null(context.Result);

            mockFeatureFlags.Verify(m =>
                                    m.GetFeatureFlag(
                                        It.IsAny <string>(),
                                        It.IsAny <bool>()
                                        ),
                                    Times.Once);
        }