public async Task RevokeSiteAdminSendsUserByUserIdQueryWithCorrectUserId() { 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.RevokeSiteAdmin(userId); mediator.Verify(m => m.Send(It.Is<UserByUserIdQuery>(q => q.UserId == userId)), Times.Once); }
public async Task RevokeSiteAdminReturnsAViewWhenExceptionIsThrown() { var mediator = new Mock<IMediator>(); var logger = new Mock<ILogger<SiteController>>(); string userId = "1234"; Exception thrown = new Exception("Test"); mediator.Setup(x => x.Send(It.IsAny<UserByUserIdQuery>())) .Throws(thrown); var controller = new SiteController(null, logger.Object, mediator.Object); var result = (IActionResult)await controller.RevokeSiteAdmin(userId); Assert.IsType<ViewResult>(result); }
public async Task RevokeSiteAdminAddsCorrectErrorMessageToViewBagWhenExceptionIsThrown() { var mediator = new Mock<IMediator>(); var logger = new Mock<ILogger<SiteController>>(); string userId = "1234"; Exception thrown = new Exception("Test"); mediator.Setup(x => x.Send(It.IsAny<UserByUserIdQuery>())) .Throws(thrown); var controller = new SiteController(null, logger.Object, mediator.Object); var result = await controller.RevokeSiteAdmin(userId); string expectedMessage = $"Failed to revoke site admin for {userId}. Exception thrown."; Assert.Equal(expectedMessage, controller.ViewBag.ErrorMessage); }
public async Task RevokeSiteAdminInvokesLogErrorWithCorrectParametersWhenExceptionIsThrown() { var mediator = new Mock<IMediator>(); var logger = new Mock<ILogger<SiteController>>(); string userId = "1234"; Exception thrown = new Exception("Test"); mediator.Setup(x => x.Send(It.IsAny<UserByUserIdQuery>())) .Throws(thrown); var controller = new SiteController(null, logger.Object, mediator.Object); var result = await controller.RevokeSiteAdmin (userId); string expectedMessage = $"Failed to revoke site 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 RevokeSiteAdminRedirectsToCorrectAction() { var mediator = new Mock<IMediator>(); var userManager = CreateApplicationUserMock(); mediator.Setup(m => m.Send(It.IsAny<UserByUserIdQuery>())).Returns(new ApplicationUser()); var controller = new SiteController(userManager.Object, null, mediator.Object); var result = (RedirectToActionResult) await controller.RevokeSiteAdmin(It.IsAny<string>()); Assert.Equal("Index", result.ActionName); }
public async Task RevokeSiteAdminInvokesRemoveClaimAsyncWithCorrectParameters() { var mediator = new Mock<IMediator>(); var userManager = CreateApplicationUserMock(); 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, null, mediator.Object); var result = await controller.RevokeSiteAdmin(userId); userManager.Verify(u => u.RemoveClaimAsync(user, It.Is<Claim>(c => c.Type == AllReady.Security.ClaimTypes.UserType && c.Value == UserType.SiteAdmin.ToName())), Times.Once); }