public async Task AssignOrganizationAdminPostRedirectsToCorrectActionIsUserIsNull()
        {
            var mediator = new Mock<IMediator>();
            AssignOrganizationAdminViewModel model = new AssignOrganizationAdminViewModel()
            {
                UserId = "1234"
            };
            ApplicationUser nullUser = null;
            mediator.Setup(m => m.Send(It.Is<UserByUserIdQuery>(q => q.UserId == model.UserId))).Returns(nullUser);
            var controller = new SiteController(null, null, mediator.Object);
            var result = (RedirectToActionResult)await controller.AssignOrganizationAdmin(model);

            Assert.Equal("Index", result.ActionName);
        }
        public void AssignOrganizationAdminGetReturnsCorrectViewModel()
        {
            var mediator = new Mock<IMediator>();
            var user = new ApplicationUser()
            {
                Id = "1234"
            };
            mediator.Setup(x => x.Send(It.Is<UserByUserIdQuery>(q => q.UserId == user.Id)))
                .Returns(user);
            List<Organization> orgs = new List<Organization>();
            orgs.Add(new Organization() { Id = 2, Name = "Borg" });
            orgs.Add(new Organization() { Id = 1, Name = "Aorg" });
            mediator.Setup(x => x.Send(It.IsAny<AllOrganizationsQuery>())).Returns(orgs);
            var controller = new SiteController(null, null, mediator.Object);
            var result = (ViewResult)controller.AssignOrganizationAdmin(user.Id);

            Assert.IsType<AssignOrganizationAdminViewModel>(result.Model);
        }
        public async Task AssignOrganizationAdminPostSendsUserByUserIdQueryWithCorrectUserId()
        {
            var mediator = new Mock<IMediator>();
            AssignOrganizationAdminViewModel model = new AssignOrganizationAdminViewModel()
            {
                UserId = "1234"
            };

            var controller = new SiteController(null, null, mediator.Object);
            await controller.AssignOrganizationAdmin(model);

            mediator.Verify(m => m.Send(It.Is<UserByUserIdQuery>(q => q.UserId == model.UserId)), Times.Once);
        }
        public void AssignOrganizationAdminGetSendsAllOrganizationsQuery()
        {
            var mediator = new Mock<IMediator>();
            var user = new ApplicationUser()
            {
                Id = "1234"
            };
            mediator.Setup(x => x.Send(It.Is<UserByUserIdQuery>(q => q.UserId == user.Id)))
                .Returns(user);

            var controller = new SiteController(null, null, mediator.Object);
            var result = controller.AssignOrganizationAdmin(user.Id);
            mediator.Verify(m => m.Send(It.IsAny<AllOrganizationsQuery>()), Times.Once);
        }
        public void AssignOrganizationAdminGetAddsCorrectSelectListItemToOrganizationsOnViewBag()
        {
            var mediator = new Mock<IMediator>();
            var user = new ApplicationUser()
            {
                Id = "1234"
            };
            mediator.Setup(x => x.Send(It.Is<UserByUserIdQuery>(q => q.UserId == user.Id)))
                .Returns(user);
            List<Organization> orgs = new List<Organization>();
            orgs.Add(new Organization() { Id = 2, Name = "Borg" });
            orgs.Add(new Organization() { Id = 1, Name = "Aorg" });
            mediator.Setup(x => x.Send(It.IsAny<AllOrganizationsQuery>())).Returns(orgs);
            var controller = new SiteController(null, null, mediator.Object);
            var result = controller.AssignOrganizationAdmin(user.Id);

            List<Microsoft.AspNetCore.Mvc.Rendering.SelectListItem> viewBagOrgs = 
                ((IEnumerable<Microsoft.AspNetCore.Mvc.Rendering.SelectListItem>)controller.ViewBag.Organizations).ToList();

            Assert.Equal(3, viewBagOrgs.Count);
            Assert.Equal("0", viewBagOrgs[0].Value); // Select One item added in controller
            Assert.Equal("1", viewBagOrgs[1].Value); // sorted items
            Assert.Equal("2", viewBagOrgs[2].Value);
        }
 public void AssignOrganizationAdminGetSendsUserByUserIdQueryWithCorrectUserId()
 {
     var mediator = new Mock<IMediator>();
     string userId = "1234";
     var controller = new SiteController(null, null, mediator.Object);
     controller.AssignOrganizationAdmin(userId);
     mediator.Verify(m => m.Send(It.Is<UserByUserIdQuery>(q => q.UserId == userId)), Times.Once);
 }
        public void AssignOrganizationAdminGetRedirectsToCorrectActionWhenUserIsASiteAdmin()
        {
            var mediator = new Mock<IMediator>();
            var user = new ApplicationUser()
            {
                Id = "1234"
            };
            user.Claims.Add(new Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityUserClaim<string>()
            {
                ClaimType = AllReady.Security.ClaimTypes.UserType,
                ClaimValue = Enum.GetName(typeof(UserType), UserType.SiteAdmin)
            });

            mediator.Setup(x => x.Send(It.Is<UserByUserIdQuery>(q => q.UserId == user.Id)))
                .Returns(user);

            var controller = new SiteController(null, null, mediator.Object);
            var result = (RedirectToActionResult)controller.AssignOrganizationAdmin(user.Id);
            Assert.Equal("Index", result.ActionName);
        }
        public async Task AssignOrganizationAdminPostAddsCorrectKeyAndErrorMessageToModelStateWhenOrganzationNotFound()
        {
            var mediator = new Mock<IMediator>();
            var userManager = CreateApplicationUserMock();
            AssignOrganizationAdminViewModel model = new AssignOrganizationAdminViewModel()
            {
                UserId = "1234",
                OrganizationId = 5678
            };
            ApplicationUser user = new ApplicationUser() { UserName = "******" };
            mediator.Setup(m => m.Send(It.Is<UserByUserIdQuery>(q => q.UserId == model.UserId))).Returns(user);
            List<Organization> orgs = new List<Organization>();
            orgs.Add(new Organization() { Id = 9123, Name = "Borg" });
            mediator.Setup(x => x.Send(It.IsAny<AllOrganizationsQuery>())).Returns(orgs);

            var controller = new SiteController(userManager.Object, null, mediator.Object);
            var result = await controller.AssignOrganizationAdmin(model);

            Assert.False(controller.ModelState.IsValid);
            Assert.True(controller.ModelState.ContainsKey("OrganizationId"));
            Assert.True(controller.ModelState.GetErrorMessagesByKey("OrganizationId").FirstOrDefault(x => x.ErrorMessage.Equals("Invalid Organization. Please contact support.")) != null);
        }
        public async Task AssignOrganizationAdminPostReturnsAView()
        {
            var mediator = new Mock<IMediator>();
            AssignOrganizationAdminViewModel model = new AssignOrganizationAdminViewModel()
            {
                UserId = "1234",
                OrganizationId = 0
            };
            ApplicationUser user = new ApplicationUser() { UserName = "******" };
            mediator.Setup(m => m.Send(It.Is<UserByUserIdQuery>(q => q.UserId == model.UserId))).Returns(user);

            var controller = new SiteController(null, null, mediator.Object);
            var result = (IActionResult) await controller.AssignOrganizationAdmin(model);

            Assert.IsType<ViewResult>(result);
        }
        public async Task AssignOrganizationAdminPostRedirectsToCorrectActionWhenOrganizationIdOnModelMatchesAnyExistingOrganization()
        {
            var mediator = new Mock<IMediator>();
            var userManager = CreateApplicationUserMock();
            AssignOrganizationAdminViewModel model = new AssignOrganizationAdminViewModel()
            {
                UserId = "1234",
                OrganizationId = 5678
            };
            ApplicationUser user = new ApplicationUser() { UserName = "******" };
            mediator.Setup(m => m.Send(It.Is<UserByUserIdQuery>(q => q.UserId == model.UserId))).Returns(user);
            List<Organization> orgs = new List<Organization>();
            orgs.Add(new Organization() { Id = 5678, Name = "Borg" });
            mediator.Setup(x => x.Send(It.IsAny<AllOrganizationsQuery>())).Returns(orgs);

            var controller = new SiteController(userManager.Object, null, mediator.Object);
            var result = (RedirectToActionResult) await controller.AssignOrganizationAdmin(model);

            Assert.Equal("Index", result.ActionName);
        }
        public async Task AssignOrganizationAdminPostInvokesAddClaimAsyncTwiceAddingTheCorrectClaimsWhenOrganizationIdOnModelMatchesAnyExistingOrganization()
        {
            var mediator = new Mock<IMediator>();
            var userManager = CreateApplicationUserMock();
            AssignOrganizationAdminViewModel model = new AssignOrganizationAdminViewModel()
            {
                UserId = "1234",
                OrganizationId = 5678
            };
            ApplicationUser user = new ApplicationUser() { UserName = "******" };
            mediator.Setup(m => m.Send(It.Is<UserByUserIdQuery>(q => q.UserId == model.UserId))).Returns(user);
            List<Organization> orgs = new List<Organization>();
            orgs.Add(new Organization() { Id = 5678, Name = "Borg" });
            mediator.Setup(x => x.Send(It.IsAny<AllOrganizationsQuery>())).Returns(orgs);

            var controller = new SiteController(userManager.Object, null, mediator.Object);
            var result = await controller.AssignOrganizationAdmin(model);

            userManager.Verify(x => x.AddClaimAsync(user, It.Is<Claim>(c =>
                                            c.Value == UserType.OrgAdmin.ToName() &&
                                            c.Type == AllReady.Security.ClaimTypes.UserType)), Times.Once);
            userManager.Verify(x => x.AddClaimAsync(user, It.Is<Claim>(c =>
                                            c.Value == model.OrganizationId.ToString() &&
                                            c.Type == AllReady.Security.ClaimTypes.Organization)), Times.Once);
        }
        public async Task AssignOrganizationAdminPostSendsAllOrganizationsQueryWhenModelStateIsValid()
        {
            var mediator = new Mock<IMediator>();
            AssignOrganizationAdminViewModel model = new AssignOrganizationAdminViewModel()
            {
                UserId = "1234",
                OrganizationId = 5678
            };
            ApplicationUser user = new ApplicationUser() { UserName = "******" };
            mediator.Setup(m => m.Send(It.Is<UserByUserIdQuery>(q => q.UserId == model.UserId))).Returns(user);
            var controller = new SiteController(null, null, mediator.Object);
            var result = await controller.AssignOrganizationAdmin(model);

            mediator.Verify(x => x.Send(It.IsAny<AllOrganizationsQuery>()), Times.Once);
        }
        public async Task AssignOrganizationAdminPostAddsCorrectKeyAndErrorMessageToModelStateWhenOrganizationIdIsZero()
        {
            var mediator = new Mock<IMediator>();
            AssignOrganizationAdminViewModel model = new AssignOrganizationAdminViewModel()
            {
                UserId = "1234"
            };
            ApplicationUser user = new ApplicationUser() { UserName = "******" };
            mediator.Setup(m => m.Send(It.Is<UserByUserIdQuery>(q => q.UserId == model.UserId))).Returns(user);
            var controller = new SiteController(null, null, mediator.Object);
            var result = await controller.AssignOrganizationAdmin(model);

            Assert.False(controller.ModelState.IsValid);
            Assert.True(controller.ModelState.ContainsKey("OrganizationId"));
            Assert.True(controller.ModelState.GetErrorMessagesByKey("OrganizationId").FirstOrDefault(x => x.ErrorMessage.Equals("You must pick a valid organization.")) != null);
        }
        public async Task AssignOrganizationAdminGetRedirectsToCorrectActionWhenUserIsAnOrganizationAdmin()
        {
            var user = new ApplicationUser { Id = "1234" };
            user.MakeOrgAdmin();

            var mediator = new Mock<IMediator>();
            mediator.Setup(x => x.SendAsync(It.Is<UserByUserIdQuery>(q => q.UserId == user.Id)))
                .ReturnsAsync(user);

            var controller = new SiteController(null, null, mediator.Object);
            var result = (RedirectToActionResult) await controller.AssignOrganizationAdmin(user.Id);
            Assert.Equal("Index", result.ActionName);
        }