public async Task PostFile_NotFoundFileRecordId_ShouldReturnBadRequest() { // ARRANGE - set up test dependent data and state var fhFileToPost = new FhFile { Name = "mySampleFile.jpg", FileRecordId = Guid.NewGuid() }; var response = new HttpResponseMessage(); try { // ACT - perform test action response = await CreateFile(fhFileToPost); // ASSERT - verify test results response.StatusCode.Should().Be(HttpStatusCode.BadRequest, "because fileRecordId passed doesn't exist"); // clean up } catch (Exception) { Console.WriteLine("Response content:"); Console.WriteLine(await response.Content.ReadAsStringAsync()); throw; } }
private async Task <HttpResponseMessage> CreateFile(FhFile fhFile) { // ARRANGE - set up test dependent data and state var url = $"{_baseUrl}/api/files"; var imageInBytes = File.ReadAllBytes($"./InputTestFiles/{fhFile.Name}"); // ./ instead of ../ because tests are run from root folder in debug or release mode // set up content to upload HttpContent fileName = new StringContent(fhFile.Name); HttpContent fileRecordId = new StringContent(fhFile.FileRecordId.ToString()); HttpContent file = new StreamContent(new MemoryStream(imageInBytes)); var client = new HttpClient(); var content = new MultipartFormDataContent("Upload----" + DateTime.Now.ToString(CultureInfo.InvariantCulture)) { { fileName, "fileName" }, { fileRecordId, "fileRecordId" }, { file, "file", fhFile.Name } }; // fileName & fileRecord are the formData fields. fileName is required // ACT - perform test action var response = await client.PostAsync(url, content); return(response); }
public async Task GetFiles_ShouldReturnOk() { // ARRANGE - set up test dependent data and state var url = $"{_baseUrl}/api/files"; var httpClient = new HttpClient(); var response = new HttpResponseMessage(); try { // creating a file var fhFileToPost = new FhFile { Name = "mySampleFile.jpg", }; await CreateFile(fhFileToPost); // ACT - perform test action response = await httpClient.GetAsync(url); response.StatusCode.Should().Be(HttpStatusCode.OK); // ASSERT - verify test results var fhFileReturned = await response.Content.ReadAsAsync <List <FhFile> >(); fhFileReturned.Count.Should().BeGreaterThan(0, "because I added a file"); // Clean up } catch (Exception) { Console.WriteLine("Response content:"); Console.WriteLine(await response.Content.ReadAsStringAsync()); throw; } }
public async Task PostFile_WithNonExistentFileRecordId_ShouldReturnBadRequest() { // ARRANGE - set up test dependent data and state // create a fileRecord to get it's id // todo: var fhFileToPost = new FhFile { Name = "mySampleFile.jpg" }; var response = new HttpResponseMessage(); try { // ACT - perform test action response = await CreateFile(fhFileToPost); // ASSERT - verify test results var fhFileReturned = await response.Content.ReadAsAsync <FhFile>(); fhFileReturned.Name.Should().Be(fhFileToPost.Name); // clean up } catch (Exception) { Console.WriteLine("Response content:"); Console.WriteLine(await response.Content.ReadAsStringAsync()); throw; } }
private async Task SaveHtmlFileInAHtmlFolderIfHtmlFile(IFormFile formFile, FhFile file) { // check precondition var contentType = FileHelpers.GetContentType(file.Name); if (!contentType.Contains("text/html")) { return; } // if html file, also save it in the test-results folder so they can be access via bookmarks // save two copies: 1 called latest.html and the other with the normal file name var pathToWww = getPathToWwwFolder(); var htmlFolderName = _configuration.GetValue <string>("Data:HtmlUploadsFolderName"); var filePath = Path.Combine(pathToWww, htmlFolderName); var fullFilePathNormalName = Path.Combine(filePath, file.Name); var fullFilePathLatest = Path.Combine(filePath, "latest.html"); // save first copy with the same name using (var fileStream = new FileStream(fullFilePathNormalName, FileMode.Create)) { await formFile.CopyToAsync(fileStream); } // save second copy with name latest.html using (var fileStream = new FileStream(fullFilePathLatest, FileMode.Create)) { await formFile.CopyToAsync(fileStream); } }
private async Task SaveFileInFileSystem(IFormFile formFile, FhFile file) { var filePath = GetFileFullPathById(file.Id); using (var fileStream = new FileStream(filePath, FileMode.Create)) { await formFile.CopyToAsync(fileStream); } await SaveHtmlFileInAHtmlFolderIfHtmlFile(formFile, file); }
public IActionResult GetFile(Guid id) { // PRE-CONDITION if (string.IsNullOrEmpty(id.ToString())) { return(BadRequest("file id is required")); } // ACTIONS FhFile fhFileToReturn = _filesService.GetFile(id); if (fhFileToReturn == null) { return(NotFound($"file with id is not found: {id}")); } return(Ok(fhFileToReturn)); }
private void SaveFileDataInDb(FhFile file) { // ARRANGE // ACT // 1. Save a reference to DB using (var db = _dbConnectionFactory.Open()) { db.Insert(file); } // POST-CONDITION // The insert above should have given the POCO an guid automatically // Check that the GUID is unique, and it's not the default Guid. Default guid for new Guid() is "00000000-0000-0000-0000-000000000000" if (file.Id == new Guid()) { throw new Exception("filerecord guid cannot be 00000000-0000-0000-0000-000000000000. Most likely file didn't get inserted in the DB"); } }
public async Task <FhFile> CreateFile(IFormFile formFile, FhFile file) { // ARRANGE // PRE-CONDITION // file id should be null becuase it's autogenerated // Id is autogenerated and assigned to POCO by postgres on insert/save: https://github.com/ServiceStack/ServiceStack.OrmLite#auto-populated-guid-ids // ACT // 1. Save a reference to DB SaveFileDataInDb(file); // 2. Upload file to system await SaveFileInFileSystem(formFile, file); // update data to return with url return(file); }
public async Task GetFileDownload_ShouldReturnOk() { // ARRANGE - set up test dependent data and state var url = $"{_baseUrl}/api/files/downloadFile/"; var httpClient = new HttpClient(); var response = new HttpResponseMessage(); try { // creating a file var fhFileToPost = new FhFile { Name = "mySampleFile.jpg", }; var createdFileResponse = await CreateFile(fhFileToPost); createdFileResponse.StatusCode.Should().Be(HttpStatusCode.OK); var createdFile = await createdFileResponse.Content.ReadAsAsync <FhFile>(); // ACT - perform test action response = await httpClient.GetAsync(url + createdFile.Id); response.StatusCode.Should().Be(HttpStatusCode.OK); // ASSERT - verify test results var responseFilename = response.Content.Headers.ContentDisposition.FileName; responseFilename.Should().Be(fhFileToPost.Name, "because that's the filename I posted"); // Clean up } catch (Exception) { Console.WriteLine("Response content:"); Console.WriteLine(await response.Content.ReadAsStringAsync()); throw; } }
// aka upload file public async Task <IActionResult> CreateFile(IFormFile file, Guid fileRecordId) { // PRE-CONDITION if (file == null) { return(BadRequest("file is required")); } if (file.Length <= 0) { return(BadRequest("file is required")); } if (fileRecordId != Guid.Empty) { // verify provided fileRecordId exists var fileRecords = _fileRecordsService.GetFileRecords().ToList(); var foundFileRecordId = fileRecords.FirstOrDefault(fr => fr.Id == fileRecordId); if (foundFileRecordId == null) { return(BadRequest($"Invalid fileRecordId: {fileRecordId}. FileRecordId provided is not found")); } } // ACTIONS // initialize required data var fhFile = new FhFile { Name = file.FileName, FileRecordId = fileRecordId, CreatedUtc = DateTime.Now, // add required data UpdatedUtc = DateTime.Now, DeletedUtc = DateTime.MaxValue }; var fileDto = await _filesService.CreateFile(file, fhFile); return(Ok(fileDto)); }
public async Task <FhFile> CreateFile(IFormFile formFile, FhFile file) { return(await _filesRepository.CreateFile(formFile, file)); }