public void UpdateGet_ShouldReturnRedirect_IfNotAuthorAndNotAdmin() { user.Stories = new List <Story>(); var service = new Mock <IStoryService>(); StoriesController controller = new StoriesController( service.Object, userManager.Object, logger) { ControllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext() { User = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Role, "User") })), }, }, TempData = new TempDataDictionary(httpContext.Object, tempDataProvider.Object) }; var result = controller.Update("testId"); var viewResult = Assert.IsAssignableFrom <RedirectResult>(result); viewResult.Url.ShouldBe("/Stories/Details/testId"); }
public void UpdateGet_ShouldReturnRedirect_IfNotAuthorButAdmin() { user.Stories = new List <Story>(); var service = new Mock <IStoryService>(); service.Setup(s => s.GetStoryByIdAsViewModel("testId")) .Returns(() => new StoryViewModel { Id = "testId" }); StoriesController controller = new StoriesController( service.Object, userManager.Object, logger) { ControllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext() { User = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Role, "Administrator") })), }, }, TempData = new TempDataDictionary(httpContext.Object, tempDataProvider.Object) }; var result = controller.Update("testId"); var viewResult = Assert.IsAssignableFrom <ViewResult>(result); viewResult.Model.ShouldBeOfType <StoryViewModel>(); viewResult.Model.ShouldNotBeNull(); }
public async Task UpdatePost_ShouldReturnView_OnInValidModel() { user.Stories = new List <Story> { new Story { Id = "testId", AuthorId = "testId" } }; var model = new StoryViewModel { Id = "testId", Author = user }; var service = new Mock <IStoryService>(); service.Setup(s => s.UpdateAsync(model)) .ReturnsAsync(() => true); StoriesController controller = new StoriesController( service.Object, userManager.Object, logger) { ControllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext() { User = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Role, "Administrator") })), }, }, TempData = new TempDataDictionary(httpContext.Object, tempDataProvider.Object) }; controller.ModelState.AddModelError("K", "E"); var result = await controller.Update(model); result.ShouldNotBeNull(); var viewResult = Assert.IsAssignableFrom <ViewResult>(result); viewResult.Model.ShouldBeOfType <StoryViewModel>(); controller.TempData.ContainsKey("notification").ShouldBeTrue(); controller.TempData["notification"].ShouldNotBeNull(); controller.TempData["notification"].ShouldBeOfType <string[]>(); string[] arr = controller.TempData["notification"] as string[]; arr[0].ShouldBe("danger"); }
public async Task UpdatePost_ShouldReturnView_IfAllIsOkAndServiceTrue() { user.Stories = new List <Story> { new Story { Id = "testId", AuthorId = "testId" } }; var model = new StoryViewModel { Id = "testId", Author = user, Content = "Lorem ipsum dolor sit amet,consectetur adipiscing eli" }; var service = new Mock <IStoryService>(); service.Setup(s => s.UpdateAsync(model)) .ReturnsAsync(() => true); StoriesController controller = new StoriesController( service.Object, userManager.Object, logger) { ControllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext() { User = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Role, "Administrator") })), }, }, TempData = new TempDataDictionary(httpContext.Object, tempDataProvider.Object) }; var result = await controller.Update(model); result.ShouldNotBeNull(); var viewResult = Assert.IsAssignableFrom <RedirectResult>(result); viewResult.Url.ShouldBe("/Stories/Details/testId"); controller.TempData.ContainsKey("notification").ShouldBeTrue(); controller.TempData["notification"].ShouldNotBeNull(); controller.TempData["notification"].ShouldBeOfType <string[]>(); string[] arr = controller.TempData["notification"] as string[]; arr[0].ShouldBe("success"); }
public void UpdateGet_ShouldReturnRedirect_OnException() { user.Stories = new List <Story> { new Story { Id = "testId", AuthorId = "testId" } }; var service = new Mock <IStoryService>(); service.Setup(s => s.GetStoryByIdAsViewModel("testId")) .Throws(new Exception()); StoriesController controller = new StoriesController( service.Object, userManager.Object, logger) { ControllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext() { User = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Role, "Administrator") })), }, }, TempData = new TempDataDictionary(httpContext.Object, tempDataProvider.Object) }; var result = controller.Update("testId"); var viewResult = Assert.IsAssignableFrom <RedirectResult>(result); viewResult.Url.ShouldBe("/Stories"); controller.TempData.ContainsKey("notification").ShouldBeTrue(); controller.TempData["notification"].ShouldNotBeNull(); controller.TempData["notification"].ShouldBeOfType <string[]>(); string[] arr = controller.TempData["notification"] as string[]; arr[0].ShouldBe("danger"); }