public async Task GivenMemberLoggedIn_WhenMemberHasNoRolesAndWrongUsername_ThenAccessDeniedResult(
            IMemberManager memberManager,
            IPublicAccessService publicAccessService,
            IContentService contentService,
            IContent protectedNode,
            IContent loginNode,
            IContent noAccessNode,
            string username)
        {
            PublicAccessChecker sut = CreateSut(memberManager, publicAccessService, contentService, out HttpContext httpContext);

            Mock.Get(publicAccessService).Setup(x => x.GetEntryForContent(It.IsAny <IContent>()))
            .Returns(new PublicAccessEntry(protectedNode, loginNode, noAccessNode, new []
            {
                new PublicAccessRule(Guid.Empty, Guid.Empty)
                {
                    RuleType  = Constants.Conventions.PublicAccess.MemberUsernameRuleType,
                    RuleValue = "AnotherUsername"
                }
            }));
            httpContext.User = GetLoggedInUser();
            MockGetUserAsync(memberManager, new MemberIdentityUser()
            {
                IsApproved = true, UserName = username
            });
            MockGetRolesAsync(memberManager, Enumerable.Empty <string>());

            var result = await sut.HasMemberAccessToContentAsync(123);

            Assert.AreEqual(PublicAccessStatus.AccessDenied, result);
        }
        private PublicAccessChecker CreateSut(IMemberManager memberManager, IPublicAccessService publicAccessService, IContentService contentService, out HttpContext httpContext)
        {
            var publicAccessChecker = new PublicAccessChecker(
                GetHttpContextAccessor(memberManager, out httpContext),
                publicAccessService,
                contentService);

            return(publicAccessChecker);
        }
        public async Task GivenMemberNotLoggedIn_WhenIdentityIsChecked_ThenNotLoggedInResult(
            IMemberManager memberManager,
            IPublicAccessService publicAccessService,
            IContentService contentService)
        {
            PublicAccessChecker sut = CreateSut(memberManager, publicAccessService, contentService, out HttpContext httpContext);

            httpContext.User = new ClaimsPrincipal();
            MockGetUserAsync(memberManager, new MemberIdentityUser());

            var result = await sut.HasMemberAccessToContentAsync(123);

            Assert.AreEqual(PublicAccessStatus.NotLoggedIn, result);
        }
        public async Task GivenMemberNotLoggedIn_WhenMemberIsRequested_AndIsNull_ThenNotLoggedInResult(
            IMemberManager memberManager,
            IPublicAccessService publicAccessService,
            IContentService contentService)
        {
            PublicAccessChecker sut = CreateSut(memberManager, publicAccessService, contentService, out HttpContext httpContext);

            httpContext.User = GetLoggedInUser();
            MockGetUserAsync(memberManager, null);

            var result = await sut.HasMemberAccessToContentAsync(123);

            Assert.AreEqual(PublicAccessStatus.NotLoggedIn, result);
        }
        public async Task GivenMemberLoggedIn_WhenMemberIsNotApproved_ThenNotApprovedResult(
            IMemberManager memberManager,
            IPublicAccessService publicAccessService,
            IContentService contentService)
        {
            PublicAccessChecker sut = CreateSut(memberManager, publicAccessService, contentService, out HttpContext httpContext);

            httpContext.User = GetLoggedInUser();
            MockGetUserAsync(memberManager, new MemberIdentityUser {
                IsApproved = false
            });
            MockGetRolesAsync(memberManager);

            var result = await sut.HasMemberAccessToContentAsync(123);

            Assert.AreEqual(PublicAccessStatus.NotApproved, result);
        }
        public async Task GivenMemberLoggedIn_WhenMemberIsLockedOut_ThenLockedOutResult(
            IMemberManager memberManager,
            IPublicAccessService publicAccessService,
            IContentService contentService)
        {
            PublicAccessChecker sut = CreateSut(memberManager, publicAccessService, contentService, out HttpContext httpContext);

            httpContext.User = GetLoggedInUser();
            MockGetUserAsync(memberManager, new MemberIdentityUser {
                IsApproved = true, LockoutEnd = DateTime.UtcNow.AddDays(10)
            });
            MockGetRolesAsync(memberManager);

            var result = await sut.HasMemberAccessToContentAsync(123);

            Assert.AreEqual(PublicAccessStatus.LockedOut, result);
        }
        public async Task GivenMemberLoggedIn_WhenMemberHasRoles_AndContentDoesNotExist_ThenAccessAcceptedResult(
            IMemberManager memberManager,
            IPublicAccessService publicAccessService,
            IContentService contentService)
        {
            PublicAccessChecker sut = CreateSut(memberManager, publicAccessService, contentService, out HttpContext httpContext);

            httpContext.User = GetLoggedInUser();
            MockGetUserAsync(memberManager, new MemberIdentityUser {
                IsApproved = true
            });
            MockGetRolesAsync(memberManager);
            Mock.Get(contentService).Setup(x => x.GetById(123)).Returns((IContent)null);

            var result = await sut.HasMemberAccessToContentAsync(123);

            Assert.AreEqual(PublicAccessStatus.AccessAccepted, result);
        }
        public async Task GivenMemberLoggedIn_WhenMemberHasRoles_AndRoleRuleMatches_ThenAccessAcceptedResult(
            IMemberManager memberManager,
            IPublicAccessService publicAccessService,
            IContentService contentService,
            IContent content)
        {
            PublicAccessChecker sut = CreateSut(memberManager, publicAccessService, contentService, out HttpContext httpContext);

            httpContext.User = GetLoggedInUser();
            MockGetUserAsync(memberManager, new MemberIdentityUser {
                UserName = "******", IsApproved = true
            });
            MockGetRolesAsync(memberManager);
            Mock.Get(contentService).Setup(x => x.GetById(123)).Returns(content);
            Mock.Get(publicAccessService).Setup(x => x.GetEntryForContent(content)).Returns(GetPublicAccessEntry(string.Empty, "role1"));

            var result = await sut.HasMemberAccessToContentAsync(123);

            Assert.AreEqual(PublicAccessStatus.AccessAccepted, result);
        }