Example #1
0
        public ActionResult AddProduct(ProductModel model, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                ValidateFileAttribute fileValidator = new ValidateFileAttribute();
                if (fileValidator.IsValid(file))
                {
                    file.SaveAs(HttpContext.Server.MapPath("~/Images/") + file.FileName);
                    var      filePath = file.FileName;
                    DateTime date     = DateTime.Now;
                    date.ToString("dd/MM/yyyy");

                    int CategoryId = Int32.Parse(model.Category);
                    int productId  = CreateProduct(model.ProductName, CategoryId, model.Description, model.ProductPrice, date, model.UrlLink);

                    SaveImagePath(filePath, productId);
                    ModelState.Clear();
                }

                return(RedirectToAction("ViewProducts"));
            }
            else
            {
                return(View());
            }
        }
Example #2
0
        public void ValidateFileAttribute_IsInValid_WhenInputIsNotFile()
        {
            var attribute = new ValidateFileAttribute();
            var result    = attribute.IsValid(1);

            Assert.False(result);
        }
Example #3
0
        public void ValidateFileAttribute_IsValid_WhenNoInput()
        {
            var attribute = new ValidateFileAttribute();
            var result    = attribute.IsValid(null);

            Assert.True(result);
        }
Example #4
0
        public void ValidateFileAttribute_IsInValid_WheInputIsNotSupportedFormat()
        {
            using (var fileStream = new MemoryStream())
            {
                Image image = new Bitmap(1, 1);
                image.Save(fileStream, ImageFormat.Gif);
                IFormFile file = new FormFile(fileStream, 0, fileStream.Length, "Data", "dummy.png");

                var attribute = new ValidateFileAttribute();
                var result    = attribute.IsValid(file);
                Assert.False(result);
            }
        }
Example #5
0
        public void ValidateFileAttribute_IsInValid_WhenInputIsTooBig()
        {
            using (var fileStream = new MemoryStream())
            {
                Image image = new Bitmap(1, 1);
                image.Save(fileStream, ImageFormat.Png);
                IFormFile file = new FormFile(fileStream, 0, 2 * 1024 * 1024, "Data", "dummy.png");

                var attribute = new ValidateFileAttribute();
                var result    = attribute.IsValid(file);
                Assert.False(result);
            }
        }
Example #6
0
        public void ValidateFileAttribute_IsValid_WhenInputIsCorrectFormat()
        {
            using (var fileStream = new MemoryStream())
            {
                Image image = new Bitmap(1, 1);
                image.Save(fileStream, ImageFormat.Png);
                IFormFile file = new FormFile(fileStream, 0, fileStream.Length, "Data", "dummy.png");

                var attribute = new ValidateFileAttribute();
                var result    = attribute.IsValid(file);
                Assert.True(result);
            }
        }