public void EditPost_ShouldReturnForbidden_IfAuthenticatedUserIsNotPermitted() { var userId = 3; var fileSource = new FileSource { PostedFile = new List <HttpPostedFileBase> { new Mock <HttpPostedFileBase>().Object } }; var model = new PersonWithIUserData { Id = ValidId, UserId = userId, IsSystemStandard = false, Photos = fileSource }; _limitedRepository.Setup(_ => _.Find(ValidId)) .Returns(model); _authentication.SetupGet(_ => _.IsAuthenticated) .Returns(true); _authentication.SetupGet(_ => _.CurrentUserId) .Returns(87); var viewResult = Assert.IsType <ViewResult>(_limitedController.Edit(model)); Assert.Equal("Unauthorized", viewResult.ViewName); Assert.Equal((int)HttpStatusCode.Forbidden, _statusCode); }
public void Create_ShouldUpateUserId_ToAuthenticatedUser_IfImplementsIUserData() { MockControllerContext.SetupGet(_ => _.HttpContext.Request.QueryString) .Returns(new NameValueCollection()); _authentication.SetupGet(_ => _.CurrentUserId) .Returns(CurrentUserId); PersonWithIUserData modelSentToEvent = null; _limitedController.RecordBeforeCreate += (sender, e) => { modelSentToEvent = (PersonWithIUserData)e.Item; }; var viewResult = Assert.IsType <ViewResult>(_limitedController.Create()); var model = viewResult.Model as PersonWithIUserData; Assert.Equal("PersonWithIUserDatas", _limitedController.ViewBag.Title); Assert.Equal("Create New PersonWithIUserData for Gizzie", _limitedController.ViewBag.SubTitle); Assert.Equal(CurrentUserId, model.UserId); var crumb = (_limitedController.ViewBag.Crumbs as List <Crumb>).First(); Assert.Equal("PersonWithIUserDatas", crumb.Label); Assert.Equal("Index", crumb.ActionName); Assert.Equal("MockLimitedByUser", crumb.ControllerName); Assert.Equal(0, (model).Id); Assert.Equal(modelSentToEvent, model); Assert.Equal("", viewResult.ViewName); }
public void Edit_HappyPath() { var userId = 34; var fileSource = new FileSource { PostedFile = new List <HttpPostedFileBase> { new Mock <HttpPostedFileBase>().Object } }; var model = new PersonWithIUserData { Id = ValidId, UserId = userId, Photos = fileSource }; _limitedRepository.Setup(_ => _.Find(ValidId)) .Returns(model); _authentication.SetupGet(_ => _.IsAuthenticated) .Returns(true); _authentication.SetupGet(_ => _.CurrentUserId) .Returns(userId); PersonWithIUserData modelSentToEvent = null; _limitedController.RecordBeforeUpdate += (sender, e) => { modelSentToEvent = (PersonWithIUserData)e.Item; }; var viewResult = Assert.IsType <ViewResult>(_limitedController.Edit(ValidId)); Assert.Equal(modelSentToEvent, model); Assert.Equal("", viewResult.ViewName); Assert.Equal(model, viewResult.Model); Assert.Equal("PersonWithIUserDatas", _limitedController.ViewBag.Title); Assert.Equal("Edit PersonWithIUserData", _limitedController.ViewBag.SubTitle); var crumb = (_limitedController.ViewBag.Crumbs as List <Crumb>).First(); Assert.Equal("PersonWithIUserDatas", crumb.Label); Assert.Equal("Index", crumb.ActionName); Assert.Equal("MockLimitedByUser", crumb.ControllerName); _fileSourceHelper.Verify(_ => _.LoadFiles(fileSource, false), Times.Once); }
public void Delete_HappyPath() { var userId = 34; var fileSource = new FileSource { PostedFile = new List <HttpPostedFileBase> { new Mock <HttpPostedFileBase>().Object } }; var model = new PersonWithIUserData { Id = ValidId, UserId = userId, Photos = fileSource }; _limitedRepository.Setup(_ => _.Find(ValidId)) .Returns(model); _authentication.SetupGet(_ => _.IsAuthenticated) .Returns(true); _authentication.SetupGet(_ => _.CurrentUserId) .Returns(userId); PersonWithIUserData modelSentToEvent = null; PersonWithIUserData modelSentToEvent2 = null; _limitedController.RecordBeforeDeleted += (sender, e) => { modelSentToEvent = (PersonWithIUserData)e.Item; }; _limitedController.RecordDeleted += (sender, e) => { modelSentToEvent2 = (PersonWithIUserData)e.Item; }; var redirectResult = Assert.IsType <RedirectToRouteResult>(_limitedController.DeleteConfirmed(ValidId)); Assert.Equal(modelSentToEvent, model); Assert.Equal(modelSentToEvent2, model); Assert.Equal("Index", redirectResult.RouteValues["action"]); _limitedRepository.Verify(_ => _.Delete(ValidId), Times.Once); _fileSourceHelper.Verify(_ => _.DeleteFilesFromDisk(It.IsAny <FileSource>()), Times.Once); }
public void EditPost_IfModelStateIsInvalid() { var userId = 3; var fileSource = new FileSource { PostedFile = new List <HttpPostedFileBase> { new Mock <HttpPostedFileBase>().Object } }; var person = new PersonWithIUserData { Id = ValidId, UserId = userId, Photos = fileSource }; _personController.ModelState.AddModelError("", "Something is wrong"); var viewResult = Assert.IsType <ViewResult>(_personController.Edit(person)); var model = viewResult.Model as Person; Assert.Equal("", viewResult.ViewName); Assert.Equal(person, viewResult.Model); Assert.Equal("Persons", _personController.ViewBag.Title); Assert.Equal("Edit Person", _personController.ViewBag.SubTitle); var crumb = (_personController.ViewBag.Crumbs as List <Crumb>).First(); Assert.Equal("Persons", crumb.Label); Assert.Equal("Index", crumb.ActionName); Assert.Equal("Persons", crumb.ControllerName); Assert.Equal(3, model.Id); Assert.Equal("", viewResult.ViewName); _fileSourceHelper.Verify(_ => _.LoadFiles(fileSource, false), Times.Once); }
public void EditPost_ThrowsError() { var userId = 3; var fileSource = new FileSource { PostedFile = new List <HttpPostedFileBase> { new Mock <HttpPostedFileBase>().Object } }; var model = new PersonWithIUserData { Id = ValidId, UserId = userId, IsSystemStandard = false, Photos = fileSource }; _limitedRepository.Setup(_ => _.Find(ValidId)) .Returns(model); _limitedRepository.Setup(_ => _.Update(model)) .Throws(new Exception("Oops")); _authentication.SetupGet(_ => _.IsAuthenticated) .Returns(true); _authentication.SetupGet(_ => _.CurrentUserId) .Returns(userId); PersonWithIUserData modelSentToEvent = null; PersonWithIUserData modelSentToEvent2 = null; PersonWithIUserData modelSentToEvent3 = null; _limitedController.RecordBeforeUpdated += (sender, e) => { modelSentToEvent = (PersonWithIUserData)e.Item; }; _limitedController.RecordUpdated += (sender, e) => { modelSentToEvent2 = (PersonWithIUserData)e.Item; }; _limitedController.RecordUpdateError += (sender, e) => { modelSentToEvent3 = (PersonWithIUserData)e.Item; }; var viewResult = Assert.IsType <ViewResult>(_limitedController.Edit(model)); _fileSourceHelper.Verify(_ => _.SaveFilesToDisk(fileSource, It.IsAny <bool>()), Times.Once); Assert.Equal(modelSentToEvent, model); _limitedRepository.Verify(_ => _.Update(model), Times.Once); Assert.Null(modelSentToEvent2); Assert.Equal(modelSentToEvent3, model); Assert.Equal("", viewResult.ViewName); Assert.Equal(model, viewResult.Model); Assert.Equal("PersonWithIUserDatas", _limitedController.ViewBag.Title); Assert.Equal("Edit PersonWithIUserData", _limitedController.ViewBag.SubTitle); var crumb = (_limitedController.ViewBag.Crumbs as List <Crumb>).First(); Assert.Equal("PersonWithIUserDatas", crumb.Label); Assert.Equal("Index", crumb.ActionName); Assert.Equal("MockLimitedByUser", crumb.ControllerName); Assert.Equal(3, model.Id); Assert.Equal("", viewResult.ViewName); _fileSourceHelper.Verify(_ => _.LoadFiles(fileSource, false), Times.Once); }