Ejemplo n.º 1
0
        public void IsValid_InvalidObjectarameter_ThrowsArgumentNullExceptionException()
        {
            var subject = new MaxFileSizeAttribute(5 * 1024 * 1024);
            var ex      = Assert.Throws <ArgumentException>(() => subject.IsValid(new object()));

            Assert.That(ex.Message, Is.EqualTo("Invalid argument type, expected type : IFormFile (Parameter 'value')"));
        }
Ejemplo n.º 2
0
        public void TestMaxFileSizeAttribute()
        {
            var attribute = new MaxFileSizeAttribute();

            var configuration = new ConfigurationRoot(new List <IConfigurationProvider>
            {
                new MemoryConfigurationProvider(new MemoryConfigurationSource())
            })
            {
                ["MaxFileSize"] = "100"
            };

            var context = new ValidationContext(
                string.Empty, Mock.Of <IServiceProvider>(x =>
                                                         x.GetService(typeof(IConfiguration)) == configuration
                                                         ), null);

            Assert.True(attribute.GetValidationResult(
                            Mock.Of <IFormFile>(x => x.Length == 10), context) == ValidationResult.Success);

            Assert.True(attribute.GetValidationResult(
                            Mock.Of <IFormFile>(x => x.Length == 100), context) == ValidationResult.Success);

            Assert.False(attribute.GetValidationResult(
                             Mock.Of <IFormFile>(x => x.Length == 101), context) == ValidationResult.Success);
        }
Ejemplo n.º 3
0
        public void MaxFileSizeAttributeShouldReturnFalseIfTheFileIsBiggerThanMaxFileSize()
        {
            var       attribute = new MaxFileSizeAttribute(500);
            IFormFile fileJpg   = new FormFile(new MemoryStream(Encoding.UTF8.GetBytes("dummy image")), 0, 1024, "Data", "fake.jpg");

            var actualResult = attribute.IsValid(fileJpg);

            Assert.False(actualResult);
        }
Ejemplo n.º 4
0
        public void MaxFileSizeAttributeShouldReturnTrueIfTheFileIsSamllerThanMaxFileSize()
        {
            var       attribute = new MaxFileSizeAttribute(5 * 1024 * 1024);
            IFormFile file      = new FormFile(new MemoryStream(Encoding.UTF8.GetBytes("dummy image")), 0, 10, "Data", "fake.jpg");

            var actualResult = attribute.IsValid(file);

            Assert.True(actualResult);
        }
        public void IsValid_ValidatesCorrectly_WhenSizeIsBiggerThenMax(int maxSize)
        {
            var       allowedExtensionsAttribute = new MaxFileSizeAttribute(maxSize);
            IFormFile file = new FormFile(new MemoryStream(Encoding.UTF8.GetBytes("Test file")), 0, 10, "Data", "testfile.xls");

            var result = allowedExtensionsAttribute.IsValid(file);

            Assert.False(result);
        }
Ejemplo n.º 6
0
        public void IsValid_IncorrectSmallFile_ReturnTure()
        {
            // arrange
            Mock <IFormFile> fileMock = new Mock <IFormFile>();

            fileMock.Setup(f => f.Length).Returns(4 * 1024 * 1024);
            var subject = new MaxFileSizeAttribute(5 * 1024 * 1024);

            // act
            var result = subject.IsValid(fileMock.Object);

            // assert
            Assert.That(result, Is.True);
        }
Ejemplo n.º 7
0
 public void Initialize()
 {
     _size     = 10 * 1024 * 1024;
     _validate = new MaxFileSizeAttribute(_size);
 }