public async Task UpdateFolderAsync_InvalidFolderNamePostRequest_Handle() { var model = new FolderWriteViewModel() { Slug = GroupSlugValid, FolderId = FolderIdValid, FolderName = null, ParentGroup = GroupIdInvalid, ParentFolder = null, Description = null }; SetUserInContext.SetContext("admin"); SetRouting <FolderController> .SetupController(_folderController, "GroupUrls", "routeUrl", "routeController", "routeAction"); _folderServiceMocks.Setup(x => x.UserHasFolderWriteAccessAsync(It.IsAny <Guid>(), It.IsAny <Guid>(), It.IsAny <CancellationToken>())).Returns(Task.FromResult(true)); _folderServiceMocks.Setup(x => x.GetFolderAsync(It.IsAny <string>(), It.IsAny <Guid>(), It.IsAny <CancellationToken>())) .Returns(Task.FromResult(new FolderViewModel() { Folder = new FolderReadViewModel() { FolderId = FolderIdValid } })); _folderServiceMocks.Setup(x => x.IsFolderNameValidAsync(It.IsAny <Guid?>(), It.IsAny <string>(), It.IsAny <Guid?>(), It.IsAny <Guid>(), It.IsAny <CancellationToken>())) .Returns(Task.FromResult(false)); var result = (await _folderController.UpdateFolderAsync(model, CancellationToken.None)) as ViewResult; var returnedErrorMessage = result.ViewData.ModelState.Values.FirstOrDefault().Errors.FirstOrDefault().ErrorMessage; Assert.IsInstanceOf <ActionResult>(result); Assert.IsInstanceOf <FolderWriteViewModel>(result.Model); Assert.AreEqual(DuplicateFolderError, returnedErrorMessage); }
public void UpdateFolder(FolderWriteViewModel model) { if (model.FolderId != null) { _folderCommand.UpdateFolder(model); } }
public async Task CreateFolderAsync_ValidPostRequest_Handle() { var model = new FolderWriteViewModel() { Slug = GroupSlugValid, ParentGroup = GroupIdValid, ParentFolder = null, FolderName = FolderNameValid, Description = FolderDescription }; SetUserInContext.SetContext("admin"); _folderServiceMocks.Setup(x => x.IsUserAdminAsync(It.IsAny <string>(), It.IsAny <Guid>(), It.IsAny <CancellationToken>())) .Returns(Task.FromResult(true)); _folderServiceMocks.Setup(x => x.IsFolderNameValidAsync(It.IsAny <Guid?>(), It.IsAny <string>(), It.IsAny <Guid?>(), It.IsAny <Guid>(), It.IsAny <CancellationToken>())) .Returns(Task.FromResult(true)); _folderServiceMocks.Setup(x => x.CreateFolder(It.IsAny <FolderWriteViewModel>())) .Returns(Guid.NewGuid()); var result = await _folderController.CreateFolderAsync(model, CancellationToken.None); var redirectedRouteName = (result as RedirectToRouteResult).RouteName; var redirectedFolderId = (result as RedirectToRouteResult).RouteValues["folder"]; Assert.IsInstanceOf <RedirectToRouteResult>(result); Assert.AreEqual("GroupUrls", redirectedRouteName); Assert.AreNotEqual(Guid.Empty, redirectedFolderId); }
public async Task UpdateFolderAsync_InvalidModelPostRequest_Handle() { var model = new FolderWriteViewModel() { Slug = GroupSlugInvalid, FolderName = FolderNameValid, Description = FolderDescription, ParentGroup = GroupIdValid, ParentFolder = null, }; _folderController.ModelState.AddModelError("error", "invalid state"); SetUserInContext.SetContext("admin"); SetRouting <FolderController> .SetupController(_folderController, "GroupUrls", "routeUrl", "routeController", "routeAction"); _folderServiceMocks.Setup(x => x.IsUserAdminAsync(It.IsAny <string>(), It.IsAny <Guid>(), It.IsAny <CancellationToken>())) .Returns(Task.FromResult(true)); var result = (await _folderController.UpdateFolderAsync(model, CancellationToken.None)) as ViewResult; var returnedViewModelSlug = (result.Model as FolderWriteViewModel).Slug; var returnedViewModelParentGroupId = (result.Model as FolderWriteViewModel).ParentGroup; Assert.IsInstanceOf <ActionResult>(result); Assert.IsInstanceOf <FolderWriteViewModel>(result.Model); Assert.AreEqual(GroupSlugInvalid, returnedViewModelSlug); Assert.AreEqual(GroupIdValid, returnedViewModelParentGroupId); }
public async Task <ActionResult> CreateFolderAsync(FolderWriteViewModel folder, CancellationToken cancellationToken) { if (!_featureManager.IsEnabled(Features.FilesAndFolders)) { return(RedirectToAction("Index", "Home")); } if (!await _folderService.IsUserAdminAsync(folder.Slug, _membershipService.GetUser(System.Web.HttpContext.Current.User.Identity.Name, true).Id, cancellationToken)) { return(RedirectToRoute("GroupUrls", new { slug = folder.Slug, tab = Constants.GroupFilesTab, folder = folder.ParentFolder })); } if (ModelState.IsValid) { if (await _folderService.IsFolderNameValidAsync(folder.FolderId, folder.FolderName, folder.ParentFolder, folder.ParentGroup, cancellationToken)) { folder.AddedBy = _membershipService.GetUser(System.Web.HttpContext.Current.User.Identity.Name, true).Id; var newId = _folderService.CreateFolder(folder); return(RedirectToRoute("GroupUrls", new { slug = folder.Slug, tab = Constants.GroupFilesTab, folder = newId })); } ModelState.AddModelError(string.Empty, _localizationService.GetResourceString("Folder.Error.DuplicateTitle")); } ViewBag.HideSideBar = true; folder.Breadcrumbs = await GetBreadcrumbsAsync(folder.FolderId, folder.Slug, "Create folder", cancellationToken); return(View("_CreateFolder", folder)); }
public async Task <ActionResult> UpdateFolderAsync(string slug, Guid groupId, Guid folderId, Guid?parentId, CancellationToken cancellationToken = default(CancellationToken)) { if (!_featureManager.IsEnabled(Features.FilesAndFolders)) { return(RedirectToAction("Index", "Home")); } var result = await _folderService.GetFolderAsync(slug, folderId, cancellationToken); if (!await _folderService.UserHasFolderWriteAccessAsync(result.Folder.FolderId, _membershipService.GetUser(System.Web.HttpContext.Current.User.Identity.Name, true).Id, cancellationToken)) { return(RedirectToRoute("GroupUrls", new { slug = slug, tab = Constants.GroupFilesTab, folder = folderId })); } var WriteFolder = new FolderWriteViewModel { FolderId = result.Folder.FolderId, Slug = result.Slug, FolderName = result.Folder.FolderName, OriginalFolderName = result.Folder.FolderName, Description = result.Folder.Description, Breadcrumbs = await GetBreadcrumbsAsync(result.Folder.FolderId, result.Slug, cancellationToken : cancellationToken), ParentGroup = groupId, ParentFolder = result.Folder.ParentId }; ViewBag.HideSideBar = true; ViewData.Add("FolderName", WriteFolder.FolderName); return(View("_UpdateFolder", WriteFolder)); }
public Guid CreateFolder(FolderWriteViewModel folder) { var newFolder = new Folder { Name = folder.FolderName.Trim(), Description = folder.Description, AddedBy = folder.AddedBy, ParentFolder = folder.ParentFolder, CreatedAtUtc = DateTime.UtcNow, ParentGroup = folder.ParentGroup }; _context.Folder.Add(newFolder); _context.SaveChanges(); return(newFolder.Id); }
public void UpdateFolder(FolderWriteViewModel folder) { var result = _context.Folder.FirstOrDefault(x => x.Id == folder.FolderId); if (result != null) { if (result.Name != folder.FolderName || result.IsDeleted != folder.IsDeleted || result.Description != folder.Description) { result.Name = folder.FolderName.Trim(); result.Description = folder.Description; result.IsDeleted = folder.IsDeleted; _context.SaveChanges(); } } }
public void SetUp() { _folderController = new FolderController(_folderServiceMocks.Object, _featureManagerMocks.Object, _membershipServiceMocks.Object, _localizationServiceMocks.Object, _groupServiceMocks.Object); _model = new FolderWriteViewModel() { FolderName = FolderNameValid, FolderId = Guid.NewGuid(), ParentFolder = Guid.Empty, Slug = GroupSlugValid, Description = FolderDescription }; _featureManagerMocks.Setup(x => x.IsEnabled(It.IsAny <string>())) .Returns(true); _membershipServiceMocks.Setup(x => x.GetUser(It.IsAny <string>(), It.IsAny <bool>())) .Returns(new MembershipUser() { Id = Guid.NewGuid() }); _localizationServiceMocks.Setup(x => x.GetResourceString(DuplicateFolderError)) .Returns(DuplicateFolderError); _folderServiceMocks.Setup(service => service.GenerateBreadcrumbTrailAsync(It.IsAny <Guid>(), It.IsAny <CancellationToken>())).ReturnsAsync( (Guid folderId, CancellationToken cancellationToken) => { return(new List <BreadCrumbItem>() { new BreadCrumbItem() { Id = FolderIdValid, Name = "Breadcrumb Name" } }); }); }
public async Task <ActionResult> CreateFolderAsync(string slug, Guid?parentId, Guid groupId, CancellationToken cancellationToken) { if (!_featureManager.IsEnabled(Features.FilesAndFolders)) { return(RedirectToAction("Index", "Home")); } if (!await _folderService.IsUserAdminAsync(slug, _membershipService.GetUser(System.Web.HttpContext.Current.User.Identity.Name, true).Id, cancellationToken)) { return(RedirectToRoute("GroupUrls", new { slug = slug, tab = Constants.GroupFilesTab, folder = parentId })); } var WriteFolder = new FolderWriteViewModel { ParentFolder = parentId, Slug = slug, ParentGroup = groupId, Breadcrumbs = await GetBreadcrumbsAsync(parentId, slug, "Create folder", cancellationToken) }; ViewBag.HideSideBar = true; return(View("_CreateFolder", WriteFolder)); }
public async Task <ActionResult> DeleteFolderAsync(FolderWriteViewModel folder, CancellationToken cancellationToken = default(CancellationToken)) { if (!_featureManager.IsEnabled(Features.FilesAndFolders)) { return(RedirectToAction("Index", "Home")); } var result = await _folderService.GetFolderAsync(folder.Slug, folder.FolderId, cancellationToken); if (folder.FolderId == result.Folder.FolderId) { if (!await _folderService.UserHasFolderWriteAccessAsync(result.Folder.FolderId, _membershipService.GetUser(System.Web.HttpContext.Current.User.Identity.Name, true).Id, cancellationToken)) { return(RedirectToRoute("GroupUrls", new { slug = folder.Slug, tab = Constants.GroupFilesTab, folder = folder.FolderId, hasError = true })); } if (await _folderService.DeleteFolderAsync(result.Folder.FolderId, cancellationToken)) { return(RedirectToRoute("GroupUrls", new { slug = folder.Slug, tab = Constants.GroupFilesTab, folder = result.Folder.ParentId })); } } return(RedirectToRoute("GroupUrls", new { slug = folder.Slug, tab = Constants.GroupFilesTab, folder = folder.FolderId, hasError = true })); }
public async Task <ActionResult> UpdateFolderAsync(FolderWriteViewModel folder, CancellationToken cancellationToken = default(CancellationToken)) { if (!_featureManager.IsEnabled(Features.FilesAndFolders)) { return(RedirectToAction("Index", "Home")); } if (ModelState.IsValid) { var result = await _folderService.GetFolderAsync(folder.Slug, folder.FolderId, cancellationToken); if (!await _folderService.UserHasFolderWriteAccessAsync(result.Folder.FolderId, _membershipService.GetUser(System.Web.HttpContext.Current.User.Identity.Name, true).Id, cancellationToken)) { return(RedirectToRoute("GroupUrls", new { slug = folder.Slug, tab = Constants.GroupFilesTab, folder = folder.ParentFolder })); } // Check folder exists for folder Id passed in if (folder.FolderId == result.Folder.FolderId) { // Ensure folder name is valid if (await _folderService.IsFolderNameValidAsync(folder.FolderId, folder.FolderName, folder.ParentFolder, folder.ParentGroup, cancellationToken)) { folder.IsDeleted = folder.IsDeleted; _folderService.UpdateFolder(folder); return(RedirectToRoute("GroupUrls", new { slug = folder.Slug, tab = Constants.GroupFilesTab, folder = folder.FolderId })); } ModelState.AddModelError(string.Empty, _localizationService.GetResourceString("Folder.Error.DuplicateTitle")); } } ViewBag.HideSideBar = true; folder.Breadcrumbs = await GetBreadcrumbsAsync(folder.FolderId, folder.Slug, cancellationToken : cancellationToken); return(View("_UpdateFolder", folder)); }
public Guid CreateFolder(FolderWriteViewModel model) { return(_folderCommand.CreateFolder(model)); }