public void ShouldAddSecurityRequirement_WhenAuthClaimDoesNotAllowAnonymous()
            {
                // Arrange
                var scheme = new OpenApiSecurityScheme
                {
                    Reference = new OpenApiReference {
                        Type = ReferenceType.SecurityScheme, Id = "oauth2"
                    }
                };
                var expectedSecurity = new OpenApiSecurityRequirement
                {
                    [scheme] = new List <string>()
                };

                var authClaims      = new DisallowAnonymoudAuthClaims();
                var operationFilter = new AuthenticationRequirementsOperationFilter(authClaims);

                var operationFilterContext = GetOperationFilterContext("GET");

                var openApiOperation = new OpenApiOperation();

                // Act
                operationFilter.Apply(openApiOperation, operationFilterContext);

                // Assert
                openApiOperation.Security.Should().NotBeNullOrEmpty();
                openApiOperation.Security.Should().ContainEquivalentOf(expectedSecurity);
            }
Exemplo n.º 2
0
        public void ShouldNullControllerActionDescriptor()
        {
            AuthenticationRequirementsOperationFilter filter = new AuthenticationRequirementsOperationFilter();
            OpenApiOperation       openApiOperation          = new OpenApiOperation();
            ApiDescription         apiDescription            = new ApiDescription();
            OperationFilterContext filterContext             = new OperationFilterContext(apiDescription, null, null, null);

            filter.Apply(openApiOperation, filterContext);
            Assert.Equal(0, openApiOperation.Security.Count);
        }
            public void ShouldDoNothing_WhenAuthClaimAllowsAnonymous(string httpMethod)
            {
                // Arrange
                var authClaims      = new AllowAnonymoudAuthClaims();
                var operationFilter = new AuthenticationRequirementsOperationFilter(authClaims);

                var operationFilterContext = GetOperationFilterContext(httpMethod);

                var openApiOperation = new OpenApiOperation();

                // Act
                operationFilter.Apply(openApiOperation, operationFilterContext);

                // Assert
                openApiOperation.Security.Should().BeNullOrEmpty();
            }
Exemplo n.º 4
0
        public void ShouldApply()
        {
            AuthenticationRequirementsOperationFilter filter = new AuthenticationRequirementsOperationFilter();
            OpenApiOperation openApiOperation = new OpenApiOperation();

            openApiOperation.Security = new List <OpenApiSecurityRequirement>();

            var actionDescriptor = new ControllerActionDescriptor()
            {
                ActionName         = "index",
                ControllerName     = "swagger",
                ControllerTypeInfo = typeof(MockMethodInfo).GetTypeInfo(),
                MethodInfo         = typeof(MockMethodInfo).GetMethod("MockMethod"),
            };
            ApiDescription apiDescription = new ApiDescription()
            {
                ActionDescriptor = actionDescriptor,
            };
            OperationFilterContext filterContext = new OperationFilterContext(apiDescription, null, null, null);

            filter.Apply(openApiOperation, filterContext);
            Assert.Equal(1, openApiOperation.Security.Count);
        }