public async Task <IActionResult> Profile(string username, int page = MinPage) { User user = await this.userManager.FindByNameAsync(username); if (user == null) { TempData.AddErrorMessage(string.Format(EntityNotFound, username)); return(this.RedirectToHomeIndex()); } if (page < MinPage) { return(RedirectToAction(nameof(Profile), new { username })); } PagingElementViewModel <UserProfileServiceModel> model = new PagingElementViewModel <UserProfileServiceModel> { Element = await this.userService.GetProfileByUsernameAsync(username, page), Pagination = new PaginationViewModel { TotalElements = await this.userService.TotalOrdersAsync(username), PageSize = OrderPageSize, CurrentPage = page } }; if (page > MinPage && page > model.Pagination.TotalPages) { return(RedirectToAction(nameof(Profile), new { username })); } return(View(model)); }
public async Task <IActionResult> Index(string searchToken, int page = MinPage) { if (page < MinPage) { return(RedirectToAction(nameof(Index), new { searchToken })); } PagingElementViewModel <HomeIndexViewModel> model = new PagingElementViewModel <HomeIndexViewModel> { Element = new HomeIndexViewModel { Categories = await this.categoryService.GetAllAdvancedListingAsync(), Supplements = await this.supplementService.GetAllAdvancedListingAsync(searchToken, page) }, SearchToken = searchToken, Pagination = new PaginationViewModel { TotalElements = await this.supplementService.TotalCountAsync(searchToken), PageSize = HomePageSize, CurrentPage = page } }; if (page > MinPage && page > model.Pagination.TotalPages) { return(RedirectToAction(nameof(Index), new { searchToken })); } ViewData["SearchToken"] = searchToken; return(View(model)); }
public async Task Details_WithCorrectCategoryIdAndCorrectPage_ShouldReturnValidPaginationModelAndValidViewModel() { const int page = 3; const int totalElements = 10; //Arrange Mock <ICategoryService> categoryService = new Mock <ICategoryService>(); categoryService .Setup(c => c.IsCategoryExistingById(categoryId, false)) .ReturnsAsync(true); categoryService .Setup(c => c.GetDetailsByIdAsync(categoryId, page)) .ReturnsAsync(new CategoryDetailsServiceModel { Subcategories = new List <SubcategoryAdvancedServiceModel> { new SubcategoryAdvancedServiceModel { } }, Supplements = new List <SupplementAdvancedServiceModel> { new SupplementAdvancedServiceModel { } } }); categoryService .Setup(c => c.TotalSupplementsCountAsync(categoryId)) .ReturnsAsync(totalElements); CategoriesController categoriesController = new CategoriesController(categoryService.Object); //Act var result = await categoriesController.Details(categoryId, categoryName, page); //Assert result.Should().BeOfType <ViewResult>(); result.As <ViewResult>().ViewData.Should().ContainKey("ReturnUrl"); result.As <ViewResult>().ViewData.Should().ContainValue($"/categories/details/{categoryId}?name={categoryName}"); result.As <ViewResult>().Model.Should().BeOfType <PagingElementViewModel <CategoryDetailsServiceModel> >(); PagingElementViewModel <CategoryDetailsServiceModel> model = result.As <ViewResult>().Model.As <PagingElementViewModel <CategoryDetailsServiceModel> >(); model.Element.Subcategories.Should().HaveCount(1); model.Element.Supplements.Should().HaveCount(1); model.Pagination.CurrentPage.Should().Be(page); model.Pagination.PreviousPage.Should().Be(2); model.Pagination.NextPage.Should().Be(4); model.Pagination.TotalPages.Should().Be(4); model.Pagination.TotalElements.Should().Be(totalElements); model.Pagination.PageSize.Should().Be(SupplementPageSize); }
public async Task Details_WithCorrectManufacturerIdAndCorrectPage_ShouldReturnValidPagionationModelAndValidViewModel() { const int page = 3; const int totalElements = 10; //Arrange Mock <IManufacturerService> manufacturerService = new Mock <IManufacturerService>(); manufacturerService .Setup(m => m.IsManufacturerExistingById(manufacturerId, false)) .ReturnsAsync(true); manufacturerService .Setup(s => s.GetDetailsByIdAsync(manufacturerId, page)) .ReturnsAsync(new ManufacturerDetailsServiceModel { Supplements = new List <SupplementAdvancedServiceModel> { new SupplementAdvancedServiceModel { } } }); manufacturerService .Setup(s => s.TotalSupplementsCountAsync(manufacturerId)) .ReturnsAsync(totalElements); ManufacturersController manufacturersController = new ManufacturersController(manufacturerService.Object); //Act var result = await manufacturersController.Details(manufacturerId, manufacturerName, page); //Assert result.Should().BeOfType <ViewResult>(); result.As <ViewResult>().ViewData.Should().ContainKey("ReturnUrl"); result.As <ViewResult>().ViewData.Should().ContainValue($"/manufacturers/details/{manufacturerId}?name={manufacturerName}"); result.As <ViewResult>().Model.Should().BeOfType <PagingElementViewModel <ManufacturerDetailsServiceModel> >(); PagingElementViewModel <ManufacturerDetailsServiceModel> model = result.As <ViewResult>().Model.As <PagingElementViewModel <ManufacturerDetailsServiceModel> >(); model.Element.Supplements.Should().HaveCount(1); model.Pagination.CurrentPage.Should().Be(page); model.Pagination.PreviousPage.Should().Be(2); model.Pagination.NextPage.Should().Be(4); model.Pagination.TotalPages.Should().Be(4); model.Pagination.TotalElements.Should().Be(totalElements); model.Pagination.PageSize.Should().Be(SupplementPageSize); }
public async Task Details_WithCorrectSubcategoryIdAndTotalPagesEqualToOne_ShouldReturnValidPaginationModelAndValidViewModel() { const int page = 1; const int totalElements = SupplementPageSize; //Arrange Mock <ISubcategoryService> subcategoryService = new Mock <ISubcategoryService>(); subcategoryService .Setup(s => s.IsSubcategoryExistingById(subcategoryId, false)) .ReturnsAsync(true); subcategoryService .Setup(s => s.GetDetailsByIdAsync(subcategoryId, page)) .ReturnsAsync(new SubcategoryDetailsServiceModel { Supplements = new List <SupplementAdvancedServiceModel> { new SupplementAdvancedServiceModel { } } }); subcategoryService .Setup(s => s.TotalSupplementsCountAsync(subcategoryId)) .ReturnsAsync(totalElements); SubcategoriesController subcategoriesController = new SubcategoriesController(subcategoryService.Object); //Act var result = await subcategoriesController.Details(subcategoryId, subcategoryName, page); //Assert result.Should().BeOfType <ViewResult>(); result.As <ViewResult>().ViewData.Should().ContainKey("ReturnUrl"); result.As <ViewResult>().ViewData.Should().ContainValue($"/subcategories/details/{subcategoryId}?name={subcategoryName}"); result.As <ViewResult>().Model.Should().BeOfType <PagingElementViewModel <SubcategoryDetailsServiceModel> >(); PagingElementViewModel <SubcategoryDetailsServiceModel> model = result.As <ViewResult>().Model.As <PagingElementViewModel <SubcategoryDetailsServiceModel> >(); model.Element.Supplements.Should().HaveCount(1); model.Pagination.CurrentPage.Should().Be(page); model.Pagination.PreviousPage.Should().Be(page); model.Pagination.NextPage.Should().Be(page); model.Pagination.TotalPages.Should().Be(page); model.Pagination.TotalElements.Should().Be(totalElements); model.Pagination.PageSize.Should().Be(SupplementPageSize); }
public async Task Orders_WithCorrectPage_ShouldReturnViewResultWithValidViewModel() { const int page = 3; const int totalElements = 10; //Arrange Mock <UserManager <User> > userManager = UserManagerMock.New(); userManager .Setup(u => u.FindByNameAsync(username)) .ReturnsAsync(user); Mock <IAdminUserService> adminUserService = new Mock <IAdminUserService>(); adminUserService .Setup(a => a.GetOrdersByUsernameAsync(username, page)) .ReturnsAsync(new AdminUserOrdersServiceModel()); Mock <IUserService> userService = new Mock <IUserService>(); userService .Setup(u => u.TotalOrdersAsync(username)) .ReturnsAsync(totalElements); UsersController usersController = new UsersController(null, userManager.Object, adminUserService.Object, userService.Object, null); //Assert var result = await usersController.Orders(username, page); //Assert result.Should().BeOfType <ViewResult>(); result.As <ViewResult>().Model.Should().BeOfType <PagingElementViewModel <AdminUserOrdersServiceModel> >(); PagingElementViewModel <AdminUserOrdersServiceModel> model = result.As <ViewResult>().Model.As <PagingElementViewModel <AdminUserOrdersServiceModel> >(); model.Pagination.CurrentPage.Should().Be(page); model.Pagination.PreviousPage.Should().Be(2); model.Pagination.NextPage.Should().Be(4); model.Pagination.TotalPages.Should().Be(4); model.Pagination.TotalElements.Should().Be(totalElements); model.Pagination.PageSize.Should().Be(OrderPageSize); }
public async Task Profile_WithTotalPagesEqualToOne_ShouldReturnValidPaginationModelAndValidViewModel() { const int page = 1; const int totalElements = OrderPageSize; //Arrange Mock <UserManager <User> > userManager = UserManagerMock.New(); userManager .Setup(u => u.FindByNameAsync(username)) .ReturnsAsync(user); Mock <IUserService> userService = new Mock <IUserService>(); userService .Setup(u => u.GetProfileByUsernameAsync(username, page)) .ReturnsAsync(new UserProfileServiceModel()); userService .Setup(u => u.TotalOrdersAsync(username)) .ReturnsAsync(totalElements); UsersController usersController = new UsersController(userManager.Object, userService.Object); //Act var result = await usersController.Profile(username, page); //Assert result.Should().BeOfType <ViewResult>(); result.As <ViewResult>().Model.Should().BeOfType <PagingElementViewModel <UserProfileServiceModel> >(); PagingElementViewModel <UserProfileServiceModel> model = result.As <ViewResult>().Model.As <PagingElementViewModel <UserProfileServiceModel> >(); model.Pagination.CurrentPage.Should().Be(page); model.Pagination.PreviousPage.Should().Be(page); model.Pagination.NextPage.Should().Be(page); model.Pagination.TotalPages.Should().Be(page); model.Pagination.TotalElements.Should().Be(totalElements); model.Pagination.PageSize.Should().Be(OrderPageSize); }
public async Task Index_WithTotalPagesEqualToOne_ShouldReturnValidPaginationModelAndValidViewModel() { const int page = 1; const int totalElements = HomePageSize; //Arrange Mock <ICategoryService> categoryService = new Mock <ICategoryService>(); categoryService .Setup(c => c.GetAllAdvancedListingAsync()) .ReturnsAsync(new List <CategoryAdvancedServiceModel>()); Mock <ISupplementService> supplementService = new Mock <ISupplementService>(); supplementService .Setup(s => s.GetAllAdvancedListingAsync(searchToken, page)) .ReturnsAsync(new List <SupplementAdvancedServiceModel>()); supplementService .Setup(s => s.TotalCountAsync(searchToken)) .ReturnsAsync(totalElements); HomeController HomeController = new HomeController(categoryService.Object, supplementService.Object); //Act var result = await HomeController.Index(searchToken, page); //Assert result.Should().BeOfType <ViewResult>(); result.As <ViewResult>().Model.Should().BeOfType <PagingElementViewModel <HomeIndexViewModel> >(); PagingElementViewModel <HomeIndexViewModel> model = result.As <ViewResult>().Model.As <PagingElementViewModel <HomeIndexViewModel> >(); model.Pagination.CurrentPage.Should().Be(page); model.Pagination.PreviousPage.Should().Be(page); model.Pagination.NextPage.Should().Be(page); model.Pagination.TotalPages.Should().Be(page); model.Pagination.TotalElements.Should().Be(totalElements); model.Pagination.PageSize.Should().Be(HomePageSize); }
public async Task <IActionResult> Details(int id, string name, int page = MinPage) { bool isManufacturerExisting = await this.manufacturerService.IsManufacturerExistingById(id, false); if (!isManufacturerExisting) { TempData.AddErrorMessage(string.Format(EntityNotFound, ManufacturerEntity)); return(this.RedirectToHomeIndex()); } if (page < MinPage) { return(RedirectToAction(nameof(Details), new { id, name })); } PagingElementViewModel <ManufacturerDetailsServiceModel> model = new PagingElementViewModel <ManufacturerDetailsServiceModel> { Element = await this.manufacturerService.GetDetailsByIdAsync(id, page), Pagination = new PaginationViewModel { TotalElements = await this.manufacturerService.TotalSupplementsCountAsync(id), PageSize = SupplementPageSize, CurrentPage = page } }; if (page > MinPage && page > model.Pagination.TotalPages) { return(RedirectToAction(nameof(Details), new { id, name })); } ViewData["ReturnUrl"] = this.ReturnToManufacturerDetails(id, name); return(View(model)); }
public async Task <IActionResult> Details(int id, string name, string returnUrl, int page = MinPage) { bool isSupplementExisting = await this.supplementService.IsSupplementExistingById(id, false); if (!isSupplementExisting) { TempData.AddErrorMessage(string.Format(EntityNotFound, SupplementEntity)); return(this.RedirectToHomeIndex()); } if (page < MinPage) { return(RedirectToAction(nameof(Details), new { id, name })); } PagingElementViewModel <SupplementDetailsServiceModel> model = new PagingElementViewModel <SupplementDetailsServiceModel> { Element = await this.supplementService.GetDetailsByIdAsync(id, page), Pagination = new PaginationViewModel { TotalElements = await this.supplementService.TotalCommentsAsync(id, false), PageSize = CommentPageSize, CurrentPage = page } }; if (page > MinPage && page > model.Pagination.TotalPages) { return(RedirectToAction(nameof(Details), new { id, name })); } returnUrl = SetOrUpdateReturnUrl(returnUrl); return(View(model)); }
public async Task Details_WithCorrectSupplementIdAndCorrectPage_ShouldReturnValidPaginationModelAndValidViewModel() { const string returnUrl = "returnUrl"; const int page = 3; const int totalElements = 20; //Arrange Mock <ISupplementService> supplementService = new Mock <ISupplementService>(); supplementService .Setup(s => s.IsSupplementExistingById(supplementId, false)) .ReturnsAsync(true); supplementService .Setup(s => s.GetDetailsByIdAsync(supplementId, page)) .ReturnsAsync(new SupplementDetailsServiceModel { Comments = new List <CommentAdvancedServiceModel> { new CommentAdvancedServiceModel { } } }); supplementService .Setup(s => s.TotalCommentsAsync(supplementId, false)) .ReturnsAsync(totalElements); Mock <IUrlHelper> urlHelper = new Mock <IUrlHelper>(); urlHelper .Setup(u => u.IsLocalUrl(It.IsAny <string>())) .Returns(true); Mock <ITempDataDictionary> tempData = new Mock <ITempDataDictionary>(); tempData .SetupGet(t => t["ReturnUrl"]) .Returns(returnUrl); SupplementsController supplementsController = new SupplementsController(supplementService.Object) { Url = urlHelper.Object, TempData = tempData.Object }; //Act var result = await supplementsController.Details(supplementId, supplementName, returnUrl, page); //Assert result.Should().BeOfType <ViewResult>(); result.As <ViewResult>().ViewData.Keys.Should().Contain("ReturnUrl"); result.As <ViewResult>().ViewData.Values.Should().Contain(returnUrl); result.As <ViewResult>().Model.Should().BeOfType <PagingElementViewModel <SupplementDetailsServiceModel> >(); PagingElementViewModel <SupplementDetailsServiceModel> model = result.As <ViewResult>().Model.As <PagingElementViewModel <SupplementDetailsServiceModel> >(); model.Element.Comments.Should().HaveCount(1); model.Pagination.CurrentPage.Should().Be(page); model.Pagination.PreviousPage.Should().Be(2); model.Pagination.NextPage.Should().Be(4); model.Pagination.TotalPages.Should().Be(4); model.Pagination.TotalElements.Should().Be(totalElements); model.Pagination.PageSize.Should().Be(CommentPageSize); }
public async Task Details_WithCorrectSupplementIdAndCorrectPageAndIncorrectReturnUrl_ShouldChangeReturnUrlAndReturnValidPaginationModelAndValidViewModel() { string expectedReturnUrl = "IncorrectUrl"; const int page = 1; const int totalElements = CommentPageSize; //Arrange Mock <IModeratorSupplementService> moderatorSupplementService = new Mock <IModeratorSupplementService>(); moderatorSupplementService .Setup(m => m.GetDetailsWithDeletedCommentsByIdAsync(supplementId, page)) .ReturnsAsync(new SupplementDetailsWithDeletedCommentsServiceModel() { Comments = new List <CommentAdvancedServiceModel>() { new CommentAdvancedServiceModel() } }); Mock <ISupplementService> supplementService = new Mock <ISupplementService>(); supplementService .Setup(s => s.IsSupplementExistingById(supplementId, false)) .ReturnsAsync(true); supplementService .Setup(s => s.TotalCommentsAsync(supplementId, true)) .ReturnsAsync(totalElements); Mock <IUrlHelper> urlHelper = new Mock <IUrlHelper>(); urlHelper .Setup(u => u.IsLocalUrl(It.IsAny <string>())) .Returns(false) .Callback((string url) => { expectedReturnUrl = url; }); Mock <ITempDataDictionary> tempData = new Mock <ITempDataDictionary>(); tempData .SetupGet(t => t["ReturnUrl"]) .Returns("/"); SupplementsController supplementsController = new SupplementsController(moderatorSupplementService.Object, supplementService.Object) { Url = urlHelper.Object, TempData = tempData.Object }; //Act var result = await supplementsController.Details(supplementId, supplementName, null, page); //Assert result.Should().BeOfType <ViewResult>(); result.As <ViewResult>().ViewData.Should().ContainKey("ReturnUrl"); result.As <ViewResult>().ViewData.Should().ContainValue("/"); result.As <ViewResult>().Model.Should().BeOfType <PagingElementViewModel <SupplementDetailsWithDeletedCommentsServiceModel> >(); PagingElementViewModel <SupplementDetailsWithDeletedCommentsServiceModel> model = result.As <ViewResult>().Model.As <PagingElementViewModel <SupplementDetailsWithDeletedCommentsServiceModel> >(); model.Element.Comments.Should().HaveCount(1); model.Pagination.CurrentPage.Should().Be(page); model.Pagination.PreviousPage.Should().Be(page); model.Pagination.NextPage.Should().Be(page); model.Pagination.TotalPages.Should().Be(page); model.Pagination.TotalElements.Should().Be(totalElements); model.Pagination.PageSize.Should().Be(CommentPageSize); }