public async Task RevokeOrganizationAdminReturnsAViewWhenErrorIsThrown()
 {
     var mediator = new Mock<IMediator>();
     var userManager = CreateApplicationUserMock();
     var logger = new Mock<ILogger<SiteController>>();
     string userId = "1234";
     mediator.Setup(m => m.Send(It.Is<UserByUserIdQuery>(q => q.UserId == userId))).Throws<Exception>();
     var controller = new SiteController(userManager.Object, logger.Object, mediator.Object);
     var result = (ViewResult)await controller.RevokeOrganizationAdmin(userId);
     Assert.IsType<ViewResult>(result);
 }
        public async Task RevokeOrganizationAdminSendsUserByUserIdQueryWithCorrectUserId()
        {
            var mediator = new Mock<IMediator>();
            var logger = new Mock<ILogger<SiteController>>();

            string userId = It.IsAny<string>();
            mediator.Setup(x => x.Send(It.Is<UserByUserIdQuery>(q => q.UserId == userId))).Returns(new ApplicationUser());
            var controller = new SiteController(null, logger.Object, mediator.Object);

            await controller.RevokeOrganizationAdmin(userId);
            mediator.Verify(m => m.Send(It.Is<UserByUserIdQuery>(q => q.UserId == userId)), Times.Once);
        }
        public async Task RevokeOrganizationAdminAddsCorrectErrorMessageToViewBagWhenExceptionIsThrown()
        {
            var mediator = new Mock<IMediator>();
            var userManager = CreateApplicationUserMock();
            var logger = new Mock<ILogger<SiteController>>();
            string userId = "1234";
            mediator.Setup(m => m.Send(It.Is<UserByUserIdQuery>(q => q.UserId == userId))).Throws<Exception>();
            var controller = new SiteController(userManager.Object, logger.Object, mediator.Object);
            var result = await controller.RevokeOrganizationAdmin(userId);

            string expectedMessage = $"Failed to revoke organization admin for {userId}. Exception thrown.";

            Assert.Equal(controller.ViewBag.ErrorMessage, expectedMessage);
        }
        public async Task RevokeOrganizationAdminInvokesLogErrorWithCorrectParametersWhenExceptionIsThrown()
        {
            var mediator = new Mock<IMediator>();
            var userManager = CreateApplicationUserMock();
            var logger = new Mock<ILogger<SiteController>>();
            string userId = "1234";
            mediator.Setup(m => m.Send(It.Is<UserByUserIdQuery>(q => q.UserId == userId))).Throws<Exception>();
            var controller = new SiteController(userManager.Object, logger.Object, mediator.Object);
            var result = await controller.RevokeOrganizationAdmin(userId);

            string expectedMessage = $"Failed to revoke organization admin for {userId}";

            logger.Verify(l => l.Log(LogLevel.Error, 0,
                It.Is<Microsoft.Extensions.Logging.Internal.FormattedLogValues>(x => x.ToString().Equals(expectedMessage)),
                null, It.IsAny<Func<object, Exception, string>>()), Times.Once);
        }
        public async Task RevokeOrganizationAdminRedirectsToCorrectAction()
        {
            var mediator = new Mock<IMediator>();
            var userManager = CreateApplicationUserMock();
            var logger = new Mock<ILogger<SiteController>>();
            string userId = "1234";
            ApplicationUser user = new ApplicationUser() { UserName = "******", Id = userId };
            mediator.Setup(m => m.Send(It.Is<UserByUserIdQuery>(q => q.UserId == userId))).Returns(user);

            string orgId = "4567";
            IList<Claim> claims = new List<Claim>();
            claims.Add(new Claim(AllReady.Security.ClaimTypes.UserType, UserType.SiteAdmin.ToName()));
            claims.Add(new Claim(AllReady.Security.ClaimTypes.Organization, orgId));
            userManager.Setup(u => u.GetClaimsAsync(user)).ReturnsAsync(claims);

            var controller = new SiteController(userManager.Object, logger.Object, mediator.Object);
            var result = (RedirectToActionResult) await controller.RevokeOrganizationAdmin(userId);

            Assert.Equal("Index", result.ActionName);
        }
        public async Task RevokeOrganizationAdminInokesRemoveClaimAsyncTwiceWithCorrectParameters()
        {
            var mediator = new Mock<IMediator>();
            var userManager = CreateApplicationUserMock();
            var logger = new Mock<ILogger<SiteController>>();
            string userId = "1234";
            ApplicationUser user = new ApplicationUser() { UserName = "******", Id = userId };
            mediator.Setup(m => m.Send(It.Is<UserByUserIdQuery>(q => q.UserId == userId))).Returns(user);

            string orgId = "4567";
            IList<Claim> claims = new List<Claim>();
            claims.Add(new Claim(AllReady.Security.ClaimTypes.UserType, UserType.SiteAdmin.ToName()));
            claims.Add(new Claim(AllReady.Security.ClaimTypes.Organization, orgId));
            userManager.Setup(u => u.GetClaimsAsync(user)).ReturnsAsync(claims);

            var controller = new SiteController(userManager.Object, logger.Object, mediator.Object);
            var result = await controller.RevokeOrganizationAdmin(userId);

            userManager.Verify(u => u.RemoveClaimAsync(user, It.Is<Claim>(c =>
                                                c.Type == AllReady.Security.ClaimTypes.UserType
                                                && c.Value == UserType.SiteAdmin.ToName())), Times.Once);
            userManager.Verify(u => u.RemoveClaimAsync(user, It.Is<Claim>(c =>
                                                            c.Type == AllReady.Security.ClaimTypes.Organization
                                                            && c.Value == orgId)), Times.Once);
        }
        public async Task RevokeOrganizationAdminInvokesGetclaimsAsyncWithCorrectUser()
        {
            var mediator = new Mock<IMediator>();
            var userManager = CreateApplicationUserMock();
            var logger = new Mock<ILogger<SiteController>>();
            string userId = "1234";
            ApplicationUser user = new ApplicationUser() { UserName = "******", Id = userId };
            mediator.Setup(m => m.Send(It.Is<UserByUserIdQuery>(q => q.UserId == userId))).Returns(user);

            var controller = new SiteController(userManager.Object, logger.Object, mediator.Object);
            var result = await controller.RevokeOrganizationAdmin(userId);

            userManager.Verify(u => u.GetClaimsAsync(user), Times.Once);
        }