public void DisplayNotAllowedTest() { var controller = new SharedFoldersController(); var context = CreateMockContext(controller, "Display"); controller.db = db; controller.RoleManager = CreateRoleManager(); controller.UserManager = MockApplicationUserManager((Mock <ApplicationUserManager> mockusermanager) => { mockusermanager.Setup(m => m.GetRolesAsync(It.IsAny <string>())).ReturnsAsync(new List <string>(new[] { Roles.Users })); }); var owner = CreateMockUser("jethro"); db.CurrentUser = owner; var adminsRole = controller.RoleManager.FindByNameAsync(Roles.Administrators); adminsRole.Wait(); var adminsRole_Id = adminsRole.Result.Id; var sharedFolder = db.SharedFolders.Add(new SharedFolder { Name = RandomString(), Description = RandomString(), Slug = RandomString(), Permissions = new Permission[] { new Permission { AppliesToRole_Id = adminsRole_Id } } }); db.SaveChanges(); var mockUser = CreateMockUser("tull"); db.CurrentUser = mockUser; context.HttpContext.Setup(r => r.User).Returns(mockUser); var exceptionThrown = false; try { var result = controller.Display(sharedFolder.Slug); result.Wait(); // should throw exception } catch (HttpException httpex) { Assert.AreEqual(404, httpex.GetHttpCode()); exceptionThrown = true; } catch (AggregateException agex) { Assert.IsInstanceOfType(agex.InnerException, typeof(HttpException)); var inner = (HttpException)agex.InnerException; Assert.AreEqual(404, inner.GetHttpCode()); exceptionThrown = true; } Assert.IsTrue(exceptionThrown); }
public void DisplayTest() { var controller = new SharedFoldersController(); var context = CreateMockContext(controller, "Display"); controller.db = db; controller.RoleManager = CreateRoleManager(); controller.UserManager = MockApplicationUserManager((Mock <ApplicationUserManager> mockusermanager) => { mockusermanager.Setup(m => m.GetRolesAsync(It.IsAny <string>())).ReturnsAsync(new List <string>(new[] { Roles.Users })); }); var owner = CreateMockUser("jethro"); db.CurrentUser = owner; var usersRole = controller.RoleManager.FindByNameAsync(Roles.Users); usersRole.Wait(); var usersRole_Id = usersRole.Result.Id; var webFile = db.WebFiles.Add(new WebFile { Name = RandomString(), Description = RandomString(), Location = RandomString(), Permissions = new Permission[] { new Permission { AppliesToRole_Id = usersRole_Id } } }); db.SaveChanges(); var sharedFolder = db.SharedFolders.Add(new SharedFolder { Name = RandomString(), Description = RandomString(), Slug = RandomString(), Permissions = new Permission[] { new Permission { AppliesToRole_Id = usersRole_Id } } }); db.SaveChanges(); sharedFolder.Files = new List <WebFile>(new[] { webFile }); db.SaveChanges(); var mockUser = CreateMockUser("tull"); db.CurrentUser = mockUser; context.HttpContext.Setup(r => r.User).Returns(mockUser); var result = controller.Display(sharedFolder.Slug); Assert.IsInstanceOfType(result.Result, typeof(ViewResult)); var viewResult = (ViewResult)result.Result; Assert.IsInstanceOfType(viewResult.Model, typeof(SharedFolder)); var model = (SharedFolder)viewResult.Model; Assert.IsNotNull(model); Assert.AreEqual(sharedFolder.Name, model.Name); Assert.IsNotNull(model.Files); Assert.IsTrue(model.Files.Contains(webFile)); }