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')")); }
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); }
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); }
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); }
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); }
public void Initialize() { _size = 10 * 1024 * 1024; _validate = new MaxFileSizeAttribute(_size); }