Ejemplo n.º 1
0
        public async Task ConfirmDeletUserSendsDeleteUserCommandAsync()
        {
            var mediator = new Mock<IMediator>();
            var controller = new SiteController(null, null, null, null, mediator.Object);
            const string userId = "foo_id";

            await controller.ConfirmDeleteUser(userId);
            mediator.Verify(b => b.SendAsync(It.Is<DeleteUserCommand>(u => u.UserId == userId)));
        }
        public async Task DeleteUserSendsUserQueryWithCorrectUserId()
        {
            var mediator = new Mock<IMediator>();
            
            const string userId = "foo_id";
            mediator.Setup(x => x.SendAsync(It.Is<UserQuery>(q => q.UserId == userId))).ReturnsAsync(new EditUserModel());
            var controller = new SiteController(null, null, mediator.Object);

            await controller.DeleteUser(userId);
            mediator.Verify(m =>m.SendAsync(It.Is<UserQuery>(q =>q.UserId == userId)), Times.Once);
        }
        public async Task DeleteUserReturnsTheCorrectViewModel()
        {
            var mediator = new Mock<IMediator>();
            const string userId = "foo_id";
            mediator.Setup(x => x.SendAsync(It.IsAny<UserQuery>())).ReturnsAsync(new EditUserModel());
            var controller = new SiteController(null, null, mediator.Object);            

            var result = await controller.DeleteUser(userId);
            var model = ((ViewResult)result).ViewData.Model as DeleteUserModel;

            Assert.Equal(model.UserId, userId);
            Assert.IsType<DeleteUserModel>(model);
        }
        public async Task IndexReturnsCorrectViewModel()
        {
            var users = new List<ApplicationUser> { new ApplicationUser { Id = It.IsAny<string>() }, new ApplicationUser { Id = It.IsAny<string>() }};
            var viewModel = new IndexViewModel { Users = users };

            var mediator = new Mock<IMediator>();
            mediator.Setup(x => x.SendAsync(It.IsAny<IndexQuery>())).ReturnsAsync(viewModel);

            var controller = new SiteController(null, null, mediator.Object);
            var result = await controller.Index() as ViewResult;
            var model = result.ViewData.Model as IndexViewModel;
            
            Assert.Equal(model.Users.Count(), users.Count());
            Assert.IsType<IndexViewModel>(model);
        }
        public void IndexReturnsCorrectViewModel()
        {

            var mediator = new Mock<IMediator>();
            var users = new List<ApplicationUser>() {
                new ApplicationUser {
                    Id = It.IsAny<string>()
                },
                new ApplicationUser {
                    Id = It.IsAny<string>()
                }
            };
            mediator.Setup(x => x.Send(It.IsAny<AllUsersQuery>())).Returns(users);

            var controller = new SiteController(null, null, mediator.Object);
            var result = controller.Index();
            var model = ((ViewResult)result).ViewData.Model as SiteAdminModel;
            
            Assert.Equal(model.Users.Count(), users.Count());
            Assert.IsType<SiteAdminModel>(model);
        }
 public void EditUserPostHasValidateAntiForgeryTokenAttribute()
 {
     var controller = new SiteController(null, null, null);
     var attribute = controller.GetAttributesOn(x => x.EditUser(It.IsAny<EditUserModel>())).OfType<ValidateAntiForgeryTokenAttribute>().SingleOrDefault();
     Assert.NotNull(attribute);
 }
        public void EditUserGetReturnsCorrectViewModelWhenOrganizationIdIsNull()
        {
            {
                var mediator = new Mock<IMediator>();                

                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, null, mediator.Object);

                var result = controller.EditUser(userId);
                var model = ((ViewResult)result).ViewData.Model as EditUserModel;

                Assert.Equal(model.Organization, null);
                Assert.IsType<EditUserModel>(model);
            }
        }
        public async Task ConfirmDeletUserInvokesFindByIdAsync()
        {
            const string userId = "userId";
            var userManager = CreateApplicationUserMock();

            var controller = new SiteController(userManager.Object, null, null);
            
            await controller.ConfirmDeleteUser(userId);
            userManager.Verify(x => x.FindByIdAsync(userId), Times.Once);
        }
 public void RevokeOrganizationAdminHasHttpGetAttribute()
 {
     var controller = new SiteController(null, null,null);
     var attribute = controller.GetAttributesOn(x => x.RevokeOrganizationAdmin(It.IsAny<string>())).OfType<HttpGetAttribute>().SingleOrDefault();
     Assert.NotNull(attribute);
 }
        public async Task AssignSiteAdminRedirectsToCorrectAction()
        {
            var mediator = new Mock<IMediator>();
            var userManager = CreateApplicationUserMock();
            var user = new ApplicationUser()
            {
                Id = "1234"
            };

            mediator.Setup(x => x.Send(It.Is<UserByUserIdQuery>(q => q.UserId == user.Id)))
                .Returns(user);
            userManager.Setup(x => x.AddClaimAsync(It.IsAny<ApplicationUser>(), It.IsAny<Claim>()))
                .Returns(() => Task.FromResult(IdentityResult.Success));

            var controller = new SiteController(userManager.Object, null, mediator.Object);
            var result = (RedirectToActionResult) await controller.AssignSiteAdmin(user.Id);
            Assert.Equal("Index", result.ActionName);
        }
Ejemplo n.º 11
0
        public async Task SearchForCorrectUserWhenGeneratingApiKeys()
        {
            var mediator = new Mock<IMediator>();
            var logger = new Mock<ILogger<SiteController>>();

            string userId = Guid.NewGuid().ToString();
            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.GenerateToken(userId);
            mediator.Verify(m => m.Send(It.Is<UserByUserIdQuery>(q => q.UserId == userId)), 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 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 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 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 async Task AssignSiteAdminReturnsAViewWhenExceptionIsThrown()
        {
            var mediator = new Mock<IMediator>();
            var logger = new Mock<ILogger<SiteController>>();
            var userManager = CreateApplicationUserMock();
            var user = new ApplicationUser()
            {
                Id = "1234"
            };

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

            var controller = new SiteController(userManager.Object, logger.Object, mediator.Object);
            var result = await controller.AssignSiteAdmin(user.Id);
            Assert.IsType<ViewResult>(result);
        }
        public async Task AssignSiteAdminAddsCorrectErrorMessageToViewBagWhenExceptionIsThrown()
        {
            var mediator = new Mock<IMediator>();
            var logger = new Mock<ILogger<SiteController>>();
            var userManager = CreateApplicationUserMock();
            var user = new ApplicationUser()
            {
                Id = "1234"
            };

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

            var controller = new SiteController(userManager.Object, logger.Object, mediator.Object);
            var result = await controller.AssignSiteAdmin(user.Id);

            Assert.Equal($"Failed to assign site admin for {user.Id}. Exception thrown.", controller.ViewBag.ErrorMessage);
        }
        public async Task AssignSiteAdminInvokesLogErrorWhenExceptionIsThrown()
        {
            var mediator = new Mock<IMediator>();
            var logger = new Mock<ILogger<SiteController>>();
            var user = new ApplicationUser()
            {
                Id = "1234"
            };

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

            var controller = new SiteController(null, logger.Object, mediator.Object);
            var result = await controller.AssignSiteAdmin(user.Id);

            logger.Verify(l => l.Log<object>(LogLevel.Error, 0,
                It.IsAny<Microsoft.Extensions.Logging.Internal.FormattedLogValues>(), null,
                It.IsAny<Func<object, Exception, string>>()));
        }
Ejemplo n.º 19
0
        public async Task AssignApiRoleQueriesForCorrectId()
        {
            var mediator = new Mock<IMediator>();
            var logger = new Mock<ILogger<SiteController>>();

            string userId = Guid.NewGuid().ToString();
            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.AssignApiAccessRole(userId);
            mediator.Verify(m => m.Send(It.Is<UserByUserIdQuery>(q => q.UserId == userId)), Times.Once);
        }
Ejemplo n.º 20
0
        public void AssignOrganizationAdminPostHasHttpPostAttribute()
        {
            var mediator = new Mock<IMediator>();
            var controller = new SiteController(null, null, mediator.Object);

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

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

            Assert.NotNull(attribute);
        }
        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 AssignSiteAdminInvokesAddClaimAsyncWithCorrrectParameters()
        {
            var mediator = new Mock<IMediator>();
            var userManager = CreateApplicationUserMock();
            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(userManager.Object, null, mediator.Object);
            await controller.AssignSiteAdmin(user.Id);

            userManager.Verify(x => x.AddClaimAsync(user, It.Is<Claim>(c => 
                                                        c.Value == UserType.SiteAdmin.ToName() && 
                                                        c.Type == AllReady.Security.ClaimTypes.UserType)), Times.Once);
        }
Ejemplo n.º 23
0
 public void AssignOrganizationAdminPostHasValidateAntiForgeryTokenAttribute()
 {
     var controller = new SiteController(null, null, null);
     var attribute = controller.GetAttributesOn(x => x.AssignOrganizationAdmin(It.IsAny<AssignOrganizationAdminModel>())).OfType<ValidateAntiForgeryTokenAttribute>().SingleOrDefault();
     Assert.NotNull(attribute);
 }
        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);
        }
Ejemplo n.º 25
0
        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 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);
        }
Ejemplo n.º 27
0
 public void DeleteUserHasHttpGetAttribute()
 {
     var controller = new SiteController(null, null, null);
     var attribute = controller.GetAttributesOn(x => x.DeleteUser(It.IsAny<string>())).OfType<HttpGetAttribute>().SingleOrDefault();
     Assert.NotNull(attribute);
 }
Ejemplo n.º 28
0
        public void ConfirmDeletUserHasValidateAntiForgeryTokenAttribute()
        {            
            var controller = new SiteController(null, null, null);
            var attribute = controller.GetAttributesOn(x => x.ConfirmDeleteUser(It.IsAny<string>())).OfType<ValidateAntiForgeryTokenAttribute>().SingleOrDefault();

            Assert.NotNull(attribute);
        }
Ejemplo n.º 29
0
        public async Task ConfirmDeletUserRedirectsToCorrectAction()
        {
            var applicationUser = CreateApplicationUserMock();

            var controller = new SiteController(applicationUser.Object, null, null);

            var result = await controller.ConfirmDeleteUser(It.IsAny<string>()) as RedirectToActionResult;

            Assert.Equal(result.ActionName, nameof(SiteController.Index));
        }
Ejemplo n.º 30
0
        public void EditUserGetSendsUserByUserIdQueryWithCorrectUserId()
        {
            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);

            controller.EditUser(userId);
            mediator.Verify(m => m.Send(It.Is<UserByUserIdQuery>(q => q.UserId == userId)), Times.Once);
        }