Esempio n. 1
0
        public void WhenUpdateIsCalled_AndTheModelStateIsInvalid_ThenTheCorrectViewIsReturned()
        {
            var controller = new MediaController(_mediaService.Object, null);
            var model      = new UpdateMediaViewModel();

            controller.ViewData.ModelState.AddModelError("Key", "ErrorMessage");
            var result = (JsonResult)controller.Update(model);

            Assert.That(result, Is.Not.Null);
        }
Esempio n. 2
0
        public virtual ActionResult Update(UpdateMediaViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(Json(new MediaCreateJsonResponse {
                    success = false, message = "Invalid values"
                }));
            }
            var user = (UserViewModel)HttpContext.User;

            var media = _mediaService.UpdateMediaDetails(model.Id, model.Title, model.Caption, model.Description,
                                                         model.Alternate, user.Id);
            string anchor = string.Format("<a href='{0}'><img src='{0}' class='{1}'/></a>",
                                          Url.Action("show", "media", new { year = media.Year, month = media.Month, day = media.Day, linkKey = media.LinkKey }),
                                          model.ClassString);

            return(Json(new MediaCreateJsonResponse {
                success = true, imageAnchor = anchor
            }));
        }
Esempio n. 3
0
        public void WhenUpdateIsCalled_AndTheModelStateIsValid_ThenTheFileIsWrittenToTheDatabase()
        {
            var fileBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };

            const int userId   = 1001;
            var       fileBase = new Mock <HttpPostedFileBase>();

            fileBase.Setup(f => f.ContentLength).Returns(fileBytes.Length);
            fileBase.Setup(s => s.InputStream).Returns(new MemoryStream(fileBytes));
            fileBase.Setup(s => s.ContentType).Returns("contentType");
            const string fileName = "fileName";

            fileBase.Setup(s => s.FileName).Returns(fileName);
            _mediaService.Setup(m => m.UpdateMediaDetails(1, "title", "caption", "description", "alternate", 1001)).
            Returns(new Media());

            var controller = new MediaController(_mediaService.Object, null);

            controller.Url = new UrlHelper(new RequestContext(MockHttpContext.Object, new RouteData()), Routes);
            SetControllerContext(controller);
            MockHttpContext.SetupProperty(h => h.User);
            controller.HttpContext.User = new UserViewModel {
                IsLoggedIn = true, Id = userId
            };

            var model = new UpdateMediaViewModel
            {
                Id          = 1,
                Title       = "title",
                Caption     = "caption",
                Description = "description",
                Alternate   = "alternate",
            };

            controller.Update(model);

            _mediaService.Verify(i => i.UpdateMediaDetails(1, "title", "caption", "description", "alternate", 1001), Times.Once());
        }
Esempio n. 4
0
        public virtual ActionResult Update(UpdateMediaViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return Json(new MediaCreateJsonResponse { success = false, message = "Invalid values" });
            }
            var user = (UserViewModel)HttpContext.User;

            var media = _mediaService.UpdateMediaDetails(model.Id, model.Title, model.Caption, model.Description,
                                                           model.Alternate, user.Id);
            string anchor = string.Format("<a href='{0}'><img src='{0}' class='{1}'/></a>",
                Url.Action("show", "media", new { year = media.Year, month = media.Month, day = media.Day, linkKey = media.LinkKey }),
                model.ClassString);
            return Json(new MediaCreateJsonResponse { success = true, imageAnchor = anchor });
        }
Esempio n. 5
0
        public void WhenUpdateIsCalled_AndTheModelStateIsValid_ThenTheFileIsWrittenToTheDatabase()
        {
            var fileBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };

            const int userId = 1001;
            var fileBase = new Mock<HttpPostedFileBase>();
            fileBase.Setup(f => f.ContentLength).Returns(fileBytes.Length);
            fileBase.Setup(s => s.InputStream).Returns(new MemoryStream(fileBytes));
            fileBase.Setup(s => s.ContentType).Returns("contentType");
            const string fileName = "fileName";
            fileBase.Setup(s => s.FileName).Returns(fileName);
            _mediaService.Setup(m => m.UpdateMediaDetails(1, "title", "caption", "description", "alternate", 1001)).
                Returns(new Media());

            var controller = new MediaController(_mediaService.Object, null);
            controller.Url = new UrlHelper(new RequestContext(MockHttpContext.Object, new RouteData()), Routes);
            SetControllerContext(controller);
            MockHttpContext.SetupProperty(h => h.User);
            controller.HttpContext.User = new UserViewModel { IsLoggedIn = true, Id = userId };

            var model = new UpdateMediaViewModel
            {
                Id = 1,
                Title = "title",
                Caption = "caption",
                Description = "description",
                Alternate = "alternate",
            };

            controller.Update(model);
            
            _mediaService.Verify(i => i.UpdateMediaDetails(1, "title", "caption", "description", "alternate", 1001), Times.Once());
        }
Esempio n. 6
0
 public void WhenUpdateIsCalled_AndTheModelStateIsInvalid_ThenTheCorrectViewIsReturned()
 {
     var controller = new MediaController(_mediaService.Object, null);
     var model = new UpdateMediaViewModel();
     controller.ViewData.ModelState.AddModelError("Key", "ErrorMessage");
     var result = (JsonResult)controller.Update(model);
     Assert.That(result, Is.Not.Null);
 }