public async Task Delete_Category_Should_Return_Invalid_ModelState_With_Error_Message() { // Assign var category = new Category { Id = 1, Title = "title", Faqs = new List <Faq> { new Faq(), }, }; DbContextFake.SeedDb <IntranetApiContext>(c => c.Add(category)); using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { var categoryController = ControllerFake.GetController <CategoriesController>(context); // Act var response = await categoryController.DeleteConfirmed(1); var modelState = response.GetModelState(); var errorMessage = response.GetModelStateErrorMessages("Error").SingleOrDefault(); // Assert Assert.IsType <ViewResult>(response); Assert.False(modelState.IsValid); Assert.IsType <string>(errorMessage.ErrorMessage); } using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { Assert.Equal(1, context.Categories.Count()); } }
public async Task RedirectWhenUpdateAsAdmin() { // Assign var oldNewsItem = GetFakeNews().First(); var newNewsItem = GetFakeNews().First(); var dateTimeFactory = new DateTimeFactory(); var fileServiceMock = new Mock <IFileStorageService>(); var user = new ClaimsPrincipalFake(new Claim(ClaimTypes.Role, "Admin")); newNewsItem.UserId = "anne.the.admin"; DbContextFake.SeedDb <IntranetApiContext>(c => c.News.AddRange(oldNewsItem), ensureDeleted: true); using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { var newsController = ControllerFake.GetController <NewsController>(context, dateTimeFactory, fileServiceMock.Object); newsController.ControllerContext.HttpContext.User = user; // Act var result = await newsController.Edit(1, new NewsViewModel(newNewsItem)); // Assert Assert.True(result.ValidateActionRedirect("Details")); } }
public async Task Create_Should_Return_OkResult() { // Assign var faq = new FaqViewModel { Question = "q", Answer = "", Category = new Category { Title = "t", }, }; using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { var faqController = ControllerFake.GetController <FaqsController>(context); // Act var response = await faqController.Create(faq); // Assert Assert.True(response.ValidateActionRedirect("Details")); } using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { var savedFaq = context.Faqs.Include(f => f.Category).First(); Assert.Equal(savedFaq.Question, "q"); Assert.Equal(savedFaq.Url, "q"); Assert.Equal(savedFaq.Category.Title, "t"); Assert.Equal(savedFaq.Category.Url, "t"); } }
public async Task Get_Delete_Id_Should_Return_Category() { // Assign var faq = new Faq { Question = "q", Answer = "", Category = new Category { Id = 1, Title = "t", }, }; DbContextFake.SeedDb <IntranetApiContext>(c => c.Add(faq)); using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { var categoriesController = ControllerFake.GetController <CategoriesController>(context); // Act var result = await categoriesController.Delete(1); var returnedCategory = result.GetModelAs <Category>(); // Assert Assert.IsType <ViewResult>(result); Assert.Equal(returnedCategory.Title, "t"); Assert.Equal(returnedCategory.Faqs.Single().Question, "q"); } }
public async Task Delete_Category_Should_Return_OkResult() { // Assign var category = new Category { Id = 1, Title = "", }; DbContextFake.SeedDb <IntranetApiContext>(c => c.Add(category)); using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { var categoryController = ControllerFake.GetController <CategoriesController>(context); // Act var result = await categoryController.DeleteConfirmed(1); // Assert Assert.True(result.ValidateActionRedirect("Index")); } using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { Assert.Equal(0, context.Categories.Count()); } }
public async Task Create_Duplicate_Category_Should_Return_Invalid_ModelState_With_Error_Message() { // Assign var existingCategory = new Category { Title = "title", }; var category = new Category { Title = "title", }; DbContextFake.SeedDb <IntranetApiContext>(c => c.Add(existingCategory)); using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { var categoryController = ControllerFake.GetController <CategoriesController>(context); // Act var response = await categoryController.Create(category); var modelState = response.GetModelState(); var errorMessage = response.GetModelStateErrorMessages("Error").SingleOrDefault(); // Assert Assert.IsType <ViewResult>(response); Assert.False(modelState.IsValid); Assert.IsType <string>(errorMessage.ErrorMessage); } }
public async Task Get_Category_Id_Should_Return_Category() { // Assign var category = new Category { Id = 1, Title = "title" }; DbContextFake.SeedDb <IntranetApiContext>(c => c.Add(category)); using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { var categoryController = ControllerFake.GetController <CategoriesController>(context); // Act var response = await categoryController.Details(1); var returnedCategory = response.GetModelAs <Category>(); // Assert Assert.IsType <ViewResult>(response); Assert.Equal("title", returnedCategory.Title); } }
public void CheckNewsItemWasCorrectlyPosted() { // Assign var newsItem = GetFakeNews().First(); var dateTimeFactory = new DateTimeFactory(); var username = "******"; var displayName = "Test User"; var user = new ClaimsPrincipalFake(new[] { new Claim("username", username), new Claim("displayName", displayName), }); using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { var newsController = ControllerFake.GetController <NewsController>(context, dateTimeFactory); newsController.ControllerContext.HttpContext.User = user; // Act newsController.Post(new NewsViewModel(newsItem)); } using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { var news = context.News.Include(m => m.User).First(); // Assert Assert.NotNull(news); Assert.True(news.Text == newsItem.Text); Assert.True(news.Title == newsItem.Title); Assert.True(news.UserId == username); Assert.True(news.User.Username == username); Assert.True(news.User.DisplayName == displayName); } }
public async Task Get_Should_Return_All_Policies_Grouped_By_Category() { // Assign var policy = new Policy { Id = 1, Title = "t", Description = "d", Category = new Category { Title = "c", }, }; var fileServiceMock = new Mock <IFileStorageService>(); DbContextFake.SeedDb <IntranetApiContext>(c => c.Add(policy)); using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { var policyController = ControllerFake.GetController <PoliciesController>(context, fileServiceMock.Object); // Act var result = await policyController.Index(); var categories = result.GetModelAs <IEnumerable <Category> >(); var category = categories.First(); // Assert Assert.IsType <ViewResult>(result); Assert.Equal(1, categories.Count()); Assert.Equal("c", category.Title); Assert.Equal("t", category.Policies.First().Title); } }
public async Task Get_Should_Return_All_Faqs_Grouped_By_Category() { // Assign var faq = new Faq { Id = 1, Question = "q", Answer = "", Category = new Category { Title = "t", }, }; DbContextFake.SeedDb <IntranetApiContext>(c => c.Add(faq)); using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { var faqController = ControllerFake.GetController <FaqsController>(context); // Act var result = await faqController.Index(); var categories = result.GetModelAs <IEnumerable <Category> >(); var category = categories.First(); // Assert Assert.IsType <ViewResult>(result); Assert.Equal(1, categories.Count()); Assert.Equal("t", category.Title); Assert.Equal(category.Faqs.First().Question, "q"); } }
public async Task Get_Delete_Id_Should_Return_Policy() { // Assign var policy = new Policy { Id = 1, Title = "t", Description = "d", Category = new Category { Title = "c", }, }; var fileServiceMock = new Mock <IFileStorageService>(); DbContextFake.SeedDb <IntranetApiContext>(c => c.Add(policy)); using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { var policyController = ControllerFake.GetController <PoliciesController>(context, fileServiceMock.Object); // Act var result = await policyController.Delete(1); var returnedPolicy = result.GetModelAs <PolicyViewModel>(); // Assert Assert.IsType <ViewResult>(result); Assert.Equal("t", returnedPolicy.Title); Assert.Equal("c", returnedPolicy.Category.Title); } }
public async Task CheckNewsItemWasCorrectlyPosted() { // Assign var newsItem = GetFakeNews().First(); var dateTimeFactory = new DateTimeFactory(); var fileServiceMock = new Mock <IFileStorageService>(); var username = "******"; var displayName = "Test User"; var user = new ClaimsPrincipalFake(new[] { new Claim(ClaimTypes.NameIdentifier, username), new Claim(ClaimTypes.Name, displayName), }); using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { var newsController = ControllerFake.GetController <NewsController>(context, dateTimeFactory, fileServiceMock.Object); newsController.ControllerContext.HttpContext.User = user; // Act await newsController.Create(new NewsViewModel(newsItem)); } using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { var news = context.News.Include(m => m.User).First(); // Assert Assert.NotNull(news); Assert.True(news.Text == newsItem.Text); Assert.True(news.Title == newsItem.Title); Assert.True(news.UserId == username); Assert.True(news.User.Username == username); Assert.True(news.User.DisplayName == displayName); } }
public async Task Create_Policy_Should_Return_OkResult() { // Assign var policy = new PolicyViewModel { Title = "t", Description = "d", Category = new Category { Title = "c", }, }; var fileServiceMock = new Mock <IFileStorageService>(); using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { var policyController = ControllerFake.GetController <PoliciesController>(context, fileServiceMock.Object); // Act var response = await policyController.Create(policy); // Assert Assert.True(response.ValidateActionRedirect("Details")); } using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { var savedPolicy = context.Policies.Include(f => f.Category).First(); Assert.Equal("t", savedPolicy.Title); Assert.Equal("t", savedPolicy.Url); Assert.Equal("c", savedPolicy.Category.Title); Assert.Equal("c", savedPolicy.Category.Url); } }
public async Task SetPublishDateWhenCreatingNews() { // Assign var dateTimeOffsetCreated = new DateTimeOffset(2017, 7, 18, 0, 0, 0, TimeSpan.Zero); var newsItem = GetFakeNews(dateTimeOffsetCreated).First(); var dateTimeFactoryCreatedMock = new Mock <IDateTimeFactory>(); var fileServiceMock = new Mock <IFileStorageService>(); dateTimeFactoryCreatedMock.SetupGet(d => d.DateTimeOffsetUtc).Returns(dateTimeOffsetCreated); var user = new ClaimsPrincipalFake(new Claim(ClaimTypes.NameIdentifier, "anne.the.admin")); newsItem.UserId = "anne.the.admin"; newsItem.Published = true; DbContextFake.SeedDb <IntranetApiContext>(c => c.News.AddRange(newsItem), ensureDeleted: true); using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { var newsController = ControllerFake.GetController <NewsController>(context, dateTimeFactoryCreatedMock.Object, fileServiceMock.Object); newsController.ControllerContext.HttpContext.User = user; // Act var result = await newsController.Create(new NewsViewModel(newsItem)); } using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { // Assert Assert.Equal(context.News.First().Created, dateTimeOffsetCreated); Assert.Equal(context.News.First().Updated, dateTimeOffsetCreated); Assert.True(context.News.First().Published); Assert.True(context.News.First().HasEverBeenPublished); } }
public async Task Get_Delete_Id_Should_Return_Faq() { // Assign var faq = new Faq { Id = 1, Question = "q", Answer = "", Category = new Category { Title = "t", }, }; DbContextFake.SeedDb <IntranetApiContext>(c => c.Add(faq)); using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { var faqController = ControllerFake.GetController <FaqsController>(context); // Act var result = await faqController.Delete(1); var returnedFaq = result.GetModelAs <FaqViewModel>(); // Assert Assert.IsType <ViewResult>(result); Assert.Equal("q", returnedFaq.Question); Assert.Equal("t", returnedFaq.Category.Title); } }
public async Task Get_Categories_Should_Return_All_Categories() { // Assign var category = new Category { Id = 1, Title = "title", }; DbContextFake.SeedDb <IntranetApiContext>(c => c.Add(category)); using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { var categoryController = ControllerFake.GetController <CategoriesController>(context); // Act var result = await categoryController.Index(); var responses = result.GetModelAs <IEnumerable <Category> >(); var response = responses.First(); // Assert Assert.IsType <ViewResult>(result); Assert.Equal(ActionResultExtensions.CountModelItems(result), 1); Assert.Equal(response.Title, "title"); } }
public async Task Edit_Policy_Should_Return_Ok() { // Assign var policy = new Policy { Id = 1, Title = "old title", Description = "d", Url = "old-title", Category = new Category { Title = "old category", Url = "old-category", }, }; var newPolicy = new PolicyViewModel { Id = 1, Title = "t", Description = "d", Category = new Category { Title = "c", }, }; var fileServiceMock = new Mock <IFileStorageService>(); DbContextFake.SeedDb <IntranetApiContext>(c => c.Add(policy)); using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { var policyController = ControllerFake.GetController <PoliciesController>(context, fileServiceMock.Object); // Act var result = await policyController.Edit(1, newPolicy); // Assert Assert.True(result.ValidateActionRedirect("Details")); } using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { var updatedPolicy = context.Policies.Include(f => f.Category).First(); Assert.Equal("old title", policy.Title); Assert.Equal("t", updatedPolicy.Title); Assert.Equal("old category", policy.Category.Title); Assert.Equal("c", updatedPolicy.Category.Title); Assert.Equal("old-title", updatedPolicy.Url); Assert.Equal("c", updatedPolicy.Category.Url); } }
public async Task Edit_Faq_Should_Return_Ok() { // Assign var faq = new Faq { Id = 1, Question = "old question", Answer = "", Url = "old-question", Category = new Category { Title = "old category", Url = "old-category", }, }; var newFaq = new FaqViewModel { Id = 1, Question = "q", Answer = "", Category = new Category { Title = "t", }, }; DbContextFake.SeedDb <IntranetApiContext>(c => c.Add(faq)); using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { var faqController = ControllerFake.GetController <FaqsController>(context); // Act var result = await faqController.Edit(1, newFaq); // Assert Assert.True(result.ValidateActionRedirect("Details")); } using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { var updatedFaq = context.Faqs.Include(f => f.Category).First(); Assert.Equal(faq.Question, "old question"); Assert.Equal(updatedFaq.Question, "q"); Assert.Equal(faq.Category.Title, "old category"); Assert.Equal(updatedFaq.Category.Title, "t"); Assert.Equal(updatedFaq.Url, "old-question"); Assert.Equal(updatedFaq.Category.Url, "t"); } }
public async Task Delete_Should_Return_NotFound() { // Assign using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { var faqController = ControllerFake.GetController <FaqsController>(context); // Act var result = await faqController.DeleteConfirmed(1); // Assert Assert.IsType <NotFoundResult>(result); } }
public async Task Get_Category_Id_Should_Return_NotFound() { // Assign using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { var categoryController = ControllerFake.GetController <CategoriesController>(context); // Act var result = await categoryController.Details(1); // Assert Assert.IsType <NotFoundResult>(result); } }
public void Create_Should_Return_Empty_View() { // Assign using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { var faqController = ControllerFake.GetController <FaqsController>(context); // Act var response = faqController.Create(); var result = response.GetModel(); // Assert Assert.IsType <ViewResult>(response); Assert.IsType <FaqViewModel>(result); } }
public async Task Get_Id_Should_Return_NotFound() { // Assign var fileServiceMock = new Mock <IFileStorageService>(); using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { var policyController = ControllerFake.GetController <PoliciesController>(context, fileServiceMock.Object); // Act var result = await policyController.Details(1); // Assert Assert.IsType <NotFoundResult>(result); } }
public void Create_Should_Return_Empty_Category() { // Assign using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { var categoriesController = ControllerFake.GetController <CategoriesController>(context); // Act var response = categoriesController.Create(); var category = response.GetModel(); // Assert Assert.IsType <ViewResult>(response); Assert.IsType <Category>(category); } }
public async Task Get_All_Should_Return_Empty_List() { // Assign using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { var faqController = ControllerFake.GetController <FaqsController>(context); // Act var response = await faqController.Index(); var result = response.GetModelAs <IEnumerable <Category> >(); // Assert Assert.IsType <ViewResult>(response); Assert.Equal(0, result.Count()); } }
public async Task Get_All_Category_Should_Return_Empty_List() { // Assign using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { var categoryController = ControllerFake.GetController <CategoriesController>(context); // Act var result = await categoryController.Index(); var count = result.CountModelItems(); // Assert Assert.IsType <ViewResult>(result); Assert.Equal(0, count); } }
public void Create_Policy_Should_Return_Empty_View() { // Assign var fileServiceMock = new Mock <IFileStorageService>(); using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { var policyController = ControllerFake.GetController <PoliciesController>(context, fileServiceMock.Object); // Act var response = policyController.Create(); var result = response.GetModel(); // Assert Assert.IsType <ViewResult>(response); Assert.IsType <PolicyViewModel>(result); } }
public async Task Delete_Policy_Should_Return_OkResult() { // Assign var policy = new Policy { Id = 1, Title = "", Description = "d", Category = new Category { Title = "", }, PolicyTags = new List <PolicyTag> { new PolicyTag { Tag = new Tag { Id = 1, }, }, }, }; var fileServiceMock = new Mock <IFileStorageService>(); DbContextFake.SeedDb <IntranetApiContext>(c => c.Add(policy)); using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { var policyController = ControllerFake.GetController <PoliciesController>(context, fileServiceMock.Object); // Act var result = await policyController.DeleteConfirmed(1); // Assert Assert.True(result.ValidateActionRedirect("Index")); } using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { Assert.Equal(0, context.Policies.Count()); Assert.Equal(0, context.Categories.Count()); Assert.Equal(0, context.Tags.Count()); } }
public async Task Delete_Faq_Should_Return_OkResult() { // Assign var faq = new Faq { Id = 1, Question = "", Answer = "", Category = new Category { Title = "", }, FaqTags = new List <FaqTag> { new FaqTag { Tag = new Tag { Id = 1, }, }, }, }; DbContextFake.SeedDb <IntranetApiContext>(c => c.Add(faq)); using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { var faqController = ControllerFake.GetController <FaqsController>(context); // Act var result = await faqController.DeleteConfirmed(1); // Assert Assert.True(result.ValidateActionRedirect("Index")); } using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { Assert.Equal(0, context.Faqs.Count()); Assert.Equal(0, context.Categories.Count()); Assert.Equal(0, context.Tags.Count()); } }
public async Task Get_All_Should_Return_Empty_List() { // Assign var fileServiceMock = new Mock <IFileStorageService>(); using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { var policyController = ControllerFake.GetController <PoliciesController>(context, fileServiceMock.Object); // Act var response = await policyController.Index(); var result = response.GetModelAs <IEnumerable <Category> >(); // Assert Assert.IsType <ViewResult>(response); Assert.Equal(0, result.Count()); } }
public void ReturnForbidWhenDeleteOtherNews() { // Assign var news = GetFakeNews().First(); var dateTimeFactory = new DateTimeFactory(); DbContextFake.SeedDb <IntranetApiContext>(c => c.News.AddRange(news), ensureDeleted: true); using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { var newsController = ControllerFake.GetController <NewsController>(context, dateTimeFactory); // Act var result = newsController.Delete(1); // Assert Assert.IsType <ForbidResult>(result); } }