public void Fail_When_FileNotGiven()
        {
            var uploadAttachmentDto = new UploadAttachmentForceOverwriteDto();

            var result = _dut.Validate(uploadAttachmentDto);

            Assert.IsFalse(result.IsValid);
            Assert.AreEqual(1, result.Errors.Count);
            Assert.AreEqual(result.Errors[0].ErrorMessage, "'File' must not be empty.");
        }
        public void Validate_OK()
        {
            var uploadAttachmentDto = new UploadAttachmentForceOverwriteDto
            {
                File = new TestFile("picture.gif", 1000)
            };

            var result = _dut.Validate(uploadAttachmentDto);

            Assert.IsTrue(result.IsValid);
        }
        public void Fail_When_FileToBig()
        {
            var uploadAttachmentDto = new UploadAttachmentForceOverwriteDto
            {
                File = new TestFile("picture.gif", (_options.MaxSizeMb * 1024 * 1024) + 1)
            };

            var result = _dut.Validate(uploadAttachmentDto);

            Assert.IsFalse(result.IsValid);
            Assert.AreEqual(1, result.Errors.Count);
            Assert.AreEqual(result.Errors[0].ErrorMessage, $"Maximum file size is {_options.MaxSizeMb}MB!");
        }
        public void Fail_WhenFileNameIsTooLong()
        {
            var uploadAttachmentDto = new UploadAttachmentForceOverwriteDto
            {
                File = new TestFile(new string('x', Attachment.FileNameLengthMax + 1), 1000)
            };

            var result = _dut.Validate(uploadAttachmentDto);

            Assert.IsFalse(result.IsValid);
            Assert.AreEqual(1, result.Errors.Count);
            Assert.IsTrue(result.Errors[0].ErrorMessage.StartsWith("Filename to long! Max"));
        }
        public void Fail_WhenFileNameNotExists()
        {
            var uploadAttachmentDto = new UploadAttachmentForceOverwriteDto
            {
                File = new TestFile(null, 1000)
            };

            var result = _dut.Validate(uploadAttachmentDto);

            Assert.IsFalse(result.IsValid);
            Assert.AreEqual(1, result.Errors.Count);
            Assert.AreEqual(result.Errors[0].ErrorMessage, "Filename not given!");
        }
        public void Fail_When_IllegalFileType()
        {
            var uploadAttachmentDto = new UploadAttachmentForceOverwriteDto
            {
                File = new TestFile("picture.exe", 500)
            };

            var result = _dut.Validate(uploadAttachmentDto);

            Assert.IsFalse(result.IsValid);
            Assert.AreEqual(1, result.Errors.Count);
            Assert.AreEqual(result.Errors[0].ErrorMessage, $"File {uploadAttachmentDto.File.FileName} is not a valid file for upload!");
        }
        public async Task <IActionResult> UploadFieldValueAttachment(
            [FromHeader(Name = CurrentPlantMiddleware.PlantHeader)]
            [Required]
            [StringLength(PlantEntityBase.PlantLengthMax, MinimumLength = PlantEntityBase.PlantLengthMin)]
            string plant,
            [FromRoute] int id,
            [FromRoute] int requirementId,
            [FromRoute] int fieldId,
            [FromForm] UploadAttachmentForceOverwriteDto dto)
        {
            await using var stream = dto.File.OpenReadStream();

            var command = new UploadFieldValueAttachmentCommand(
                id,
                requirementId,
                fieldId,
                dto.File.FileName,
                stream);

            var result = await _mediator.Send(command);

            return(this.FromResult(result));
        }