Exemple #1
0
 public async Task ShouldThrowErrorForInvalidInput(string name, string location)
 {
     var command = new AddNewDocumentCommand
     {
         Location = location,
         Name     = name
     };
     await Assert.ThrowsAsync <ValidationException>(() => SendAsync(command));
 }
        private async Task <Guid> CreateTestData()
        {
            var name    = "ITestDelete-" + DateTime.Now.ToString();
            var command = new AddNewDocumentCommand
            {
                Name     = name,
                Location = "test",
                Size     = 1234
            };

            return(await SendAsync(command));
        }
Exemple #3
0
        public async Task <ActionResult> UploadDoument()
        {
            if (Request.Form.Files.Count == 0)
            {
                throw new CustomException(System.Net.HttpStatusCode.NotAcceptable, "Please choose a file");
            }
            // take first file as it is supported for single file upload for now, we can extend this for next release
            var formFile = Request.Form.Files[0];

            // validate if the file is pdf
            if (formFile.ContentType != FileTypeAllowed)
            {
                throw new CustomException(System.Net.HttpStatusCode.NotAcceptable, "Invalid file type. Please choose only pdf");
            }

            // validate if the file size is not more than max file size in mb given in appSettings
            var sizeInMb = formFile.Length / 1024 / 1024;
            var maxSize  = appSettings.UploadMaxFileSizeInMb > 0 ? appSettings.UploadMaxFileSizeInMb : 5; // default to size 5MB

            if (sizeInMb > maxSize)
            {
                throw new CustomException(System.Net.HttpStatusCode.RequestEntityTooLarge, $"File size must not exceed {maxSize}MB");
            }

            // upload file
            var newFileRequest = new UploadFileCommand();

            using (var memoryStream = new MemoryStream())
            {
                formFile.CopyTo(memoryStream);
                newFileRequest.File = memoryStream.ToArray();
            }
            newFileRequest.Name     = formFile.FileName;
            newFileRequest.MimeType = FileTypeAllowed;
            var documentUri = await Mediator.Send(newFileRequest);

            // add file to document
            var newDocumentRequest = new AddNewDocumentCommand
            {
                Name     = string.IsNullOrEmpty(formFile.Name) ? formFile.FileName : formFile.Name,
                Location = documentUri,
                Size     = formFile.Length
            };

            await Mediator.Send(newDocumentRequest);

            return(Ok());
        }
Exemple #4
0
        public async Task ShouldCreateNewDocument()
        {
            var name    = "ITest-" + DateTime.Now.ToString();
            var command = new AddNewDocumentCommand
            {
                Name     = name,
                Location = "test",
                Size     = 1234
            };
            var rowKey = await SendAsync(command);

            var result = await SendAsync(new FetchDocumentLocationQuery { RowKey = rowKey });

            Assert.NotNull(result);
            Assert.Equal("test", result.DocumentLocation);
        }
Exemple #5
0
        public async Task ShouldFetchDocumentLocation()
        {
            var name    = "ITestFetch-" + DateTime.Now.ToString();
            var command = new AddNewDocumentCommand
            {
                Name     = name,
                Location = Guid.NewGuid().ToString(),
                Size     = 1234
            };
            var rowKey = await SendAsync(command);

            var query = new FetchDocumentLocationQuery {
                RowKey = rowKey
            };
            var result = await SendAsync(query);

            Assert.NotNull(result);
            var documentLocation = result.DocumentLocation;

            Assert.Equal(command.Location, documentLocation);
        }
Exemple #6
0
        public async Task ShouldFetchAllDocuments()
        {
            var name    = "ITestFetchAll-" + DateTime.Now.ToString();
            var command = new AddNewDocumentCommand
            {
                Name     = name,
                Location = Guid.NewGuid().ToString(),
                Size     = 1234
            };

            await SendAsync(command);

            var query  = new FetchAllDocumentsQuery();
            var result = await SendAsync(query);

            var document = result.Documents.FirstOrDefault(x => x.Name == name);

            Assert.NotNull(document);
            Assert.Equal(command.Name, document.Name);
            Assert.Equal(command.Location, document.Location);
            Assert.Equal(command.Size, document.Size);
        }