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 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 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 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 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_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 void ReturnCorrectNewsInfoWhenGetAllNews(int id, string newsDate, string title, string text, string username) { // Assign var utcDate = new DateTimeOffset(Convert.ToDateTime(newsDate)); var news = GetFakeNews(id, utcDate, title, text, username); var dateTimeFactory = new DateTimeFactory(); DbContextFake.SeedDb <IntranetApiContext>(c => c.News.AddRange(news), ensureDeleted: true); using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { var newsController = new NewsController(context, dateTimeFactory); // Act var fetchNews = newsController.Get(); var okObjectResult = fetchNews as OkObjectResult; var iEnumerable = okObjectResult.Value as IEnumerable <News>; var newsFromController = iEnumerable.First(); // Assert Assert.Equal(newsFromController.Id, id); Assert.Equal(newsFromController.Created, utcDate); Assert.Equal(newsFromController.Title, title); Assert.Equal(newsFromController.Text, text); Assert.Equal(newsFromController.UserId, username); } }
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_All_News_Should_Return_All_News(int id, string newsDate, string title, string text, string username, string url) { // Assign var utcDate = new DateTimeOffset(Convert.ToDateTime(newsDate)); var news = GetFakeNews(id, utcDate, title, text, username, url); var dateTimeFactory = new DateTimeFactory(); var fileServiceMock = new Mock <IFileStorageService>(); DbContextFake.SeedDb <IntranetApiContext>(c => c.News.AddRange(news)); using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { var newsController = new NewsController(context, dateTimeFactory, fileServiceMock.Object); // Act var response = await newsController.Index(); var models = response.GetModelAs <IEnumerable <NewsViewModel> >(); var newsFromController = models.First(); // Assert Assert.Equal(newsFromController.Id, id); Assert.Equal(newsFromController.Created, utcDate); Assert.Equal(newsFromController.Title, title); Assert.Equal(newsFromController.Text, text); Assert.Equal(newsFromController.UserId, username); } }
public async Task ReturnNewsByUrl() { // Assign var url = "news-title-1"; var news = GetFakeNews(); var dateTimeFactory = new DateTimeFactory(); var fileServiceMock = new Mock <IFileStorageService>(); var newsToFind = news.SingleOrDefault(n => n.Url == url); DbContextFake.SeedDb <IntranetApiContext>(c => c.News.AddRange(news), ensureDeleted: true); using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { var newsController = new NewsController(context, dateTimeFactory, fileServiceMock.Object); // Act var response = await newsController.Details(newsToFind.Created.Year, newsToFind.Created.Month, newsToFind.Created.Day, url); var newsContent = response.GetModelAs <NewsViewModel>(); // Assert Assert.Equal(url, newsContent.Url); Assert.IsType <ViewResult>(response); } }
public async Task Get_All_News_Should_Return_Paginated_News() { // Assign var news = GetFakeNews(); var dateTimeFactory = new DateTimeFactory(); var fileServiceMock = new Mock <IFileStorageService>(); DbContextFake.SeedDb <IntranetApiContext>(c => c.News.AddRange(news)); using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { var newsController = new NewsController(context, dateTimeFactory, fileServiceMock.Object); var newsController2 = new NewsController(context, dateTimeFactory, fileServiceMock.Object); // Act var firstPageResponse = await newsController.Index(1); var secondPageResponse = await newsController2.Index(2); var firstPageModels = firstPageResponse.GetModelAs <IPagedList <NewsViewModel> >(); var secondPageModels = secondPageResponse.GetModelAs <IPagedList <NewsViewModel> >(); // Assert Assert.Equal(1, firstPageModels.Count); Assert.Equal(0, secondPageModels.Count); } }
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"); } }
async public void Ignore_Category_Relation_Has_Other() { // Assign var result = true; var category = new Category { Faqs = new List <Faq> { new Faq(), new Faq(), }, }; DbContextFake.SeedDb <IntranetApiContext>(c => c.Add(category)); using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { // Act result = await CategoryHelpers.HasNoRelatedEntities(context.Categories.SingleOrDefault(), context, category.Faqs.First()); } using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { // Assert Assert.False(result); } }
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 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 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")); } }
async public void Has_Relations() { // Assign var result = true; var tag = new Tag { FaqTags = new List <FaqTag> { new FaqTag(), }, }; DbContextFake.SeedDb <IntranetApiContext>(c => c.Tags.Add(tag)); using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { // Act result = await TagHelpers.HasNoRelatedEntities(context.Tags.SingleOrDefault(), context); } using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { // Assert Assert.False(result); } }
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_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()); } }
async public void No_Relations() { // Assign var result = false; DbContextFake.SeedDb <IntranetApiContext>(c => c.Add(new Tag())); using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { // Act result = await TagHelpers.HasNoRelatedEntities(context.Tags.SingleOrDefault(), context); } using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { // Assert Assert.True(result); } }
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 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); } }
public void ReturnNotFoundWhenUpdate() { // Assign var news = GetFakeNews(); int id = 5; 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.Put(id, new NewsViewModel(news.First())); // Assert Assert.IsType <NotFoundObjectResult>(result); } }
public void ReturnNotFoundResultWhenSearchById() { // Assign int id = 2; var news = GetFakeNews(); var dateTimeFactory = new DateTimeFactory(); DbContextFake.SeedDb <IntranetApiContext>(c => c.News.AddRange(news), ensureDeleted: true); using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { var newsController = new NewsController(context, dateTimeFactory); // Act var result = newsController.Get(id); // Assert Assert.IsType <NotFoundResult>(result); } }
public async Task ReturnInvalidModelStateWhenDeleteOtherNews() { // Assign var news = GetFakeNews().First(); var dateTimeFactory = new DateTimeFactory(); var fileServiceMock = new Mock <IFileStorageService>(); DbContextFake.SeedDb <IntranetApiContext>(c => c.News.AddRange(news), ensureDeleted: true); using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { var newsController = ControllerFake.GetController <NewsController>(context, dateTimeFactory, fileServiceMock.Object); // Act var result = await newsController.DeleteConfirmed(1); // Assert Assert.False(result.ModelStateIsValid()); } }
public void ValidModelStateOnGet() { // Assign var news = GetFakeNews(); var dateTimeFactory = new DateTimeFactory(); var fileServiceMock = new Mock <IFileStorageService>(); DbContextFake.SeedDb <IntranetApiContext>(c => c.News.AddRange(news), ensureDeleted: true); using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { var newsController = new NewsController(context, dateTimeFactory, fileServiceMock.Object); // Act var response = newsController.Create(); // Assert Assert.IsType <ViewResult>(response); } }
async public void Ignore_Category_Relation_Has_No_Other() { // Assign var result = false; DbContextFake.SeedDb <IntranetApiContext>(c => c.Add(new Category())); DbContextFake.SeedDb <IntranetApiContext>(c => c.Add(new Faq()), ensureDeleted: false); DbContextFake.SeedDb <IntranetApiContext>(c => c.Faqs.SingleOrDefault().Category = c.Categories.SingleOrDefault(), ensureDeleted: false); using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { // Act result = await CategoryHelpers.HasNoRelatedEntities(context.Categories.SingleOrDefault(), context, context.Faqs.SingleOrDefault()); } using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { // Assert Assert.True(result); } }
public async Task ReturnNotFoundResultWhenSearchByUrl() { // Assign var url = "news-title-2"; var news = GetFakeNews(); var dateTimeFactory = new DateTimeFactory(); var fileServiceMock = new Mock <IFileStorageService>(); DbContextFake.SeedDb <IntranetApiContext>(c => c.News.AddRange(news), ensureDeleted: true); using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { var newsController = new NewsController(context, dateTimeFactory, fileServiceMock.Object); // Act var result = await newsController.Details(2017, 7, 21, url); // Assert Assert.IsType <NotFoundResult>(result); } }