public void PdfGenerationLifecycle_WhereValidReportXmlAndCustomXslt_ExpectPdfFileDeletedAndPass() { // Create the custom XSLT file string customXsltFilePath = this.CreateCustomXsltFile(); Assert.True(File.Exists(customXsltFilePath)); // Create a PDF report from the custom report XML using a custom XSLT file string pdfFileName = testCandidate.CreatePdfReport(CustomReportXml, customXsltFilePath); Assert.True(!string.IsNullOrWhiteSpace(pdfFileName)); // Verify that the PDF report file has been created string pdfReportFolderPath = mockConfigurationHelper.Object.ReadAppSettingsConfigurationValues(PdfReportFolderPathConfigurationKey); string pdfFilePath = Path.Combine(pdfReportFolderPath, pdfFileName); Assert.True(File.Exists(pdfFilePath)); // Retrieve the PDF report file PdfFileDTO response = testCandidate.GetPdfReport(pdfFileName); Assert.True(response != null); Assert.True(response.Data != null); Assert.True(response.Data.Length > 0); // Delete the PDF report file testCandidate.DeletePdfReport(pdfFileName); Assert.True(!File.Exists(pdfFilePath)); // Delete the custom XSLT file this.DeleteCustomXsltFile(customXsltFilePath); Assert.True(!File.Exists(customXsltFilePath)); }
public void GetPdfReport_WherePdfFileNotExists_ExpectNull() { string pdfFileName = Guid.NewGuid().ToString() + ".pdf"; PdfFileDTO result = testCandidate.GetPdfReport(pdfFileName); Assert.True(result == null); }
public IActionResult GetPdfReport(string pdfFileName) { // Validate the arguments if (string.IsNullOrWhiteSpace(pdfFileName)) { throw new ArgumentException($"{nameof(pdfFileName)} must not be null or empty."); } using (this.loggingHelper.RMTraceManager.StartTrace($"WebService.{nameof(GetPdfReport)}")) { string methodName = typeof(PDFGeneratorController) + "." + nameof(GetPdfReport); this.loggingHelper.LogMethodEntry(methodName, LoggerTraceConstants.PDFGeneratorAPIPriority, LoggerTraceConstants.PDFGeneratorControllerMethodEntryEventId); // Initialize the PDF report PdfFileDTO pdfReport = null; try { // Get the PDF report for the specified PDF report file name pdfReport = this.pdfGeneratorBusinessService.GetPdfReport(pdfFileName); } catch (Exception ex) { this.loggingHelper.Log(ex, TraceEventType.Error); throw; } this.loggingHelper.LogMethodExit(methodName, LoggerTraceConstants.PDFGeneratorAPIPriority, LoggerTraceConstants.PDFGeneratorControllerMethodExitEventId); if (pdfReport != null) { return(this.Ok(pdfReport)); } else { // The PDF report could not be found return(this.NotFound(pdfReport)); } } }
/// <summary> /// Gets the PDF report for the specified PDF document file name /// </summary> /// <param name="pdfFileName">The PDF document file name</param> /// <returns>The PDF report encoded as a byte array in a DTO</returns> public PdfFileDTO GetPdfReport(string pdfFileName) { // Validate the arguments if (string.IsNullOrWhiteSpace(pdfFileName)) { throw new ArgumentException($"{nameof(pdfFileName)} must not be null or empty."); } if (pdfFileName.Contains("*")) { throw new ArgumentException($"{nameof(pdfFileName)} must not contain wildcards."); } if (pdfFileName.Contains("?")) { throw new ArgumentException($"{nameof(pdfFileName)} must not contain wildcards."); } if (pdfFileName.Contains("/")) { throw new ArgumentException($"{nameof(pdfFileName)} must not contain folder delimiters (/, \\ or ..)."); } if (pdfFileName.Contains("\\")) { throw new ArgumentException($"{nameof(pdfFileName)} must not contain folder delimiters (/, \\ or ..)."); } if (pdfFileName.Contains("..")) { throw new ArgumentException($"{nameof(pdfFileName)} must not contain folder delimiters (/, \\ or ..)."); } using (this.loggingHelper.RMTraceManager.StartTrace($"Business.{nameof(GetPdfReport)}")) { string methodName = typeof(PDFGeneratorBusinessService) + "." + nameof(GetPdfReport); this.loggingHelper.LogMethodEntry(methodName, LoggerTraceConstants.PDFGeneratorAPIPriority, LoggerTraceConstants.PDFGeneratorBusinessServiceMethodEntryEventId); // Initialize the PDF report PdfFileDTO pdfReport = null; // If the PDF document file exists string pdfFilePath = Path.Combine(this.pdfReportFolderPath, pdfFileName); FileInfo pdfFile = new FileInfo(pdfFilePath); if (pdfFile.Exists) { // If the PDF document file exists in the expected folder and has the expected name // This check helps to prevent attacks where the file name contains sequences of characters // that attempt to change folder if (pdfFile.Directory.FullName == this.pdfReportFolderPath && pdfFile.Name == pdfFileName) { try { // Retrieve the PDF report document byte[] pdfBytes = File.ReadAllBytes(pdfFile.FullName); pdfReport = new PdfFileDTO { Data = pdfBytes, FileName = pdfFileName }; } catch (IOException ex) { // Log the error but do not rethrow it because any undeleted PDF document files will be // deleted by an automated process this.loggingHelper.Log(ex, System.Diagnostics.TraceEventType.Error); } } } this.loggingHelper.LogMethodExit(methodName, LoggerTraceConstants.PDFGeneratorAPIPriority, LoggerTraceConstants.PDFGeneratorBusinessServiceMethodExitEventId); return(pdfReport); } }