public async Task Should_Handle_Empty_List_Of_Sources()
        {
            var requestDelegate = new RequestDelegate((innerContext) => Task.FromResult(0));
            var middleware      = new AddPermissionMiddleware(requestDelegate, new List <IPermissionSource>(), null);

            var context  = new DefaultHttpContext();
            var identity = new Mock <ClaimsIdentity>();

            identity.Setup(c => c.IsAuthenticated).Returns(true);
            var user = new ClaimsPrincipal(identity.Object);

            context.User = user;

            Func <Task> action = async() => await middleware.InvokeAsync(context);

            await action.Should().NotThrowAsync();
        }
        public async Task Should_Handle_Null_User()
        {
            var requestDelegate = new RequestDelegate((innerContext) => Task.FromResult(0));
            var middleware      = new AddPermissionMiddleware(requestDelegate, new List <IPermissionSource>(), null);

            var context = new Mock <HttpContext>();

            context.Setup(c => c.User).Returns((ClaimsPrincipal)null);
            using (new AssertionScope())
            {
                context.Object.User.Should().BeNull();

                Func <Task> action = async() => await middleware.InvokeAsync(context.Object);

                await action.Should().NotThrowAsync();
            }
        }
        public async Task Should_Add_One_Or_Multiple_Permissions_From_One_PertmissionSource_If_User_Is_Authenticated(int numberOfPermissions)
        {
            var permissionSource = new Mock <IPermissionSource>();
            var permissions      = new List <string>();

            for (var i = 0; i < numberOfPermissions; i++)
            {
                permissions.Add($"test-permission{i}");
            }

            permissionSource.Setup(c => c.GetPermissions(It.IsAny <HttpContext>())).Returns(permissions);

            var requestDelegate = new RequestDelegate((innerContext) => Task.FromResult(0));

            var middleware = new AddPermissionMiddleware(requestDelegate, new List <IPermissionSource>()
            {
                permissionSource.Object
            }, null);

            var context  = new DefaultHttpContext();
            var identity = new Mock <ClaimsIdentity>();

            identity.Setup(c => c.IsAuthenticated).Returns(true);
            var user = new ClaimsPrincipal(identity.Object);

            context.User = user;

            await middleware.InvokeAsync(context);

            using (new AssertionScope())
            {
                for (var i = 0; i < numberOfPermissions; i++)
                {
                    context.User.Claims.Should().Contain(c => c.Type.Equals("permissions") && c.Value.Equals($"test-permission{i}"));
                }
            }
        }