public async Task<IActionResult> AssignOrganizationAdmin(AssignOrganizationAdminViewModel model)
        {
            var user = GetUser(model.UserId);
            if (user == null)
            {
                return RedirectToAction(nameof(Index));
            }
            
            if (model.OrganizationId == 0)
            {
                ModelState.AddModelError(nameof(AssignOrganizationAdminViewModel.OrganizationId), "You must pick a valid organization.");
            }

            if (ModelState.IsValid)
            {
                if (_mediator.Send(new AllOrganizationsQuery()).Any(t => t.Id == model.OrganizationId))
                {
                    await _userManager.AddClaimAsync(user, new Claim(Security.ClaimTypes.UserType, UserType.OrgAdmin.ToName()));
                    await _userManager.AddClaimAsync(user, new Claim(Security.ClaimTypes.Organization, model.OrganizationId.ToString()));
                    return RedirectToAction(nameof(Index));
                }

                ModelState.AddModelError(nameof(AssignOrganizationAdminViewModel.OrganizationId), "Invalid Organization. Please contact support.");
            }

            return View();
        }
        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 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 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 void AssignOrganizationAdminPostHasHttpPostAttribute()
        {
            var mediator = new Mock<IMediator>();
            var controller = new SiteController(null, null, mediator.Object);

            var model = new AssignOrganizationAdminViewModel { UserId = It.IsAny<string>(), OrganizationId = It.IsAny<int>() };

            var attribute = controller.GetAttributesOn(x => x.AssignOrganizationAdmin(model)).OfType<HttpPostAttribute>().SingleOrDefault();

            Assert.NotNull(attribute);
        }
        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 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);
        }