public void Image_CreateImage() { //Arrange var controller = new ImageController(new ImageBLL(new ImageRepoStub()), new ProductBLL(new ProductRepoStub())); var expectedImage = new Image { ImageId = 1, ProductId = 1, ImageUrl = "test" }; var allProducts = new List<Product> { new Product { ProductId = 1, Name = "tank", Price = 150, Stock = 5, Description = "blows things up", CategoryId = 1}, new Product { ProductId = 1, Name = "tank", Price = 150, Stock = 5, Description = "blows things up", CategoryId = 1}, new Product { ProductId = 1, Name = "tank", Price = 150, Stock = 5, Description = "blows things up", CategoryId = 1} }; List<SelectListItem> expectedProductIDs = new List<SelectListItem>(); foreach (Product p in allProducts) { string productId = Convert.ToString(p.ProductId); expectedProductIDs.Add(new SelectListItem { Text = productId, Value = productId }); } //Act var viewResult = controller.CreateImage() as ViewResult; var actualProductIDs = controller.ViewBag.ProductIDs; //Assert Assert.AreEqual(expectedProductIDs.Count, actualProductIDs.Count); for (int i = 0; i < actualProductIDs.Count; i++) { Assert.AreEqual(expectedProductIDs[i].Text, actualProductIDs[i].Text); Assert.AreEqual(expectedProductIDs[i].Value, actualProductIDs[i].Value); } Assert.AreEqual("", viewResult.ViewName); }
public void Image_Create_BadInput() { //Arrange var controller = new ImageController(new ImageBLL(new ImageRepoStub()) , null, new AccountBLL(new AccountRepoStub())); var sessionMock = new TestControllerBuilder(); sessionMock.InitializeController(controller); controller.Session["Admin"] = true; controller.Session["Email"] = "admin"; string productId = "bad input"; string imageUrl = "url"; //Act var viewResult = controller.Create(productId, imageUrl) as ViewResult; //Assert Assert.AreEqual("Error", controller.ViewBag.Title); Assert.AreEqual("Invalid product id", controller.ViewBag.Message); Assert.AreEqual("~/Views/Shared/Result.cshtml", viewResult.ViewName); }
public void Image_Index() { //Arrange var controller = new ImageController(new ImageBLL(new ImageRepoStub())); var expectedResults = new List<Image> { new Image { ImageId = 1, ProductId = 1, ImageUrl = "test1"}, new Image { ImageId = 2, ProductId = 2, ImageUrl = "test2"}, new Image { ImageId = 3, ProductId = 3, ImageUrl = "test3"}, new Image { ImageId = 4, ProductId = 4, ImageUrl = "test4"} }; //Act var viewResult = controller.Index() as ViewResult; var actualResults = controller.ViewBag.Images; //Assert Assert.AreEqual(expectedResults.Count, actualResults.Count); for (int i = 0; i < actualResults.Count; i++) { Assert.AreEqual(expectedResults[i].ImageId, actualResults[i].ImageId); Assert.AreEqual(expectedResults[i].ProductId, actualResults[i].ProductId); Assert.AreEqual(expectedResults[i].ImageUrl, actualResults[i].ImageUrl); } Assert.AreEqual("ListImage", viewResult.ViewName); }
public void Image_Edit_NotAdmin() { //Arrange var controller = new ImageController(new ImageBLL(new ImageRepoStub()) , null, new AccountBLL(new AccountRepoStub())); var sessionMock = new TestControllerBuilder(); sessionMock.InitializeController(controller); controller.Session["Admin"] = false; controller.Session["Email"] = "ole"; string imageId = "1"; string productId = "1"; string imageUrl = "url"; //Act var viewResult = controller.Edit(imageId, productId, imageUrl) as ViewResult; //Assert Assert.AreEqual("Error", controller.ViewBag.Title); Assert.AreEqual("Only administrators can edit images", controller.ViewBag.Message); Assert.AreEqual("~/Views/Shared/Result.cshtml", viewResult.ViewName); }
public void Image_Edit_NoIdentifierFound() { //Arrange var controller = new ImageController(new ImageBLL(new ImageRepoStub()) , null, new AccountBLL(new AccountRepoStub())); var sessionMock = new TestControllerBuilder(); sessionMock.InitializeController(controller); controller.Session["Admin"] = true; string imageId = "1"; string productId = "1"; string imageUrl = "url"; //Act var viewResult = controller.Edit(imageId, productId, imageUrl) as ViewResult; //Assert Assert.AreEqual("Error", controller.ViewBag.Title); Assert.AreEqual("Cannot perform admin tasks without a valid email", controller.ViewBag.Message); Assert.AreEqual("~/Views/Shared/Result.cshtml", viewResult.ViewName); }
public void Image_Edit_InvalidProductId() { //Arrange var controller = new ImageController(new ImageBLL(new ImageRepoStub()) , null, new AccountBLL(new AccountRepoStub())); var sessionMock = new TestControllerBuilder(); sessionMock.InitializeController(controller); controller.Session["Admin"] = true; controller.Session["Email"] = "admin"; string imageId = "1"; string productId = "-1"; string imageUrl = "url"; //Act var viewResult = controller.Edit(imageId, productId, imageUrl) as ViewResult; //Assert Assert.AreEqual("Error", controller.ViewBag.Title); Assert.AreEqual("Could not update the image", controller.ViewBag.Message); Assert.AreEqual("~/Views/Shared/Result.cshtml", viewResult.ViewName); }
public void Image_EditImage_NoImageFound() { //Arrange var controller = new ImageController(new ImageBLL(new ImageRepoStub())); string badImageId = "-1"; //Act var viewResult = controller.EditImage(badImageId) as ViewResult; //Assert Assert.AreEqual("Error", controller.ViewBag.Title); Assert.AreEqual("Couldnt find an image with id: " + badImageId, controller.ViewBag.Message); Assert.AreEqual("~/Views/Shared/Result.cshtml", viewResult.ViewName); }
public void Image_EditImage_BadInput() { //Arrange var controller = new ImageController(new ImageBLL(new ImageRepoStub())); string badInput = "bad input"; //Act var viewResult = controller.EditImage(badInput) as ViewResult; //Assert Assert.AreEqual("Error", controller.ViewBag.Title); Assert.AreEqual("Invalid image id: " + badInput, controller.ViewBag.Message); Assert.AreEqual("~/Views/Shared/Result.cshtml", viewResult.ViewName); }
public void Image_Delete_GoodInput() { //Arrange var controller = new ImageController(new ImageBLL(new ImageRepoStub()) , null, new AccountBLL(new AccountRepoStub())); var sessionMock = new TestControllerBuilder(); sessionMock.InitializeController(controller); controller.Session["Admin"] = true; controller.Session["Email"] = "admin"; string imageId = "1"; Image expectedResult = new Image { ImageId = 1, ProductId = 1, ImageUrl = "test" }; //Act var viewResult = controller.Delete(imageId) as ViewResult; //Assert Assert.AreEqual("Success", controller.ViewBag.Title); Assert.AreEqual("Image was deleted", controller.ViewBag.Message); Assert.AreEqual("~/Views/Shared/Result.cshtml", viewResult.ViewName); }
public void Image_DeleteImage_GoodInput() { //Arrange var controller = new ImageController(new ImageBLL(new ImageRepoStub())); string imageId = "2"; Image expectedResult = new Image { ImageId = 2, ProductId = 1, ImageUrl = "test" }; //Act var viewResult = controller.DeleteImage(imageId) as ViewResult; var actualResult = controller.ViewBag.Image; //Assert Assert.AreEqual(expectedResult.ImageId, actualResult.ImageId); Assert.AreEqual(expectedResult.ProductId, actualResult.ProductId); Assert.AreEqual(expectedResult.ImageUrl, actualResult.ImageUrl); Assert.AreEqual("", viewResult.ViewName); }