Skip to content

Commit

Permalink
[VAS-812] feat: Updated puppeteer version, helper syntax, and introdu…
Browse files Browse the repository at this point in the history
…ced title parameter for accessibility reasons
  • Loading branch information
alessio-cialini committed Mar 7, 2024
1 parent 528c9ce commit 9c09d31
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 12 deletions.
8 changes: 3 additions & 5 deletions node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
"axios": "^1.5.0",
"express": "^4.18.2",
"fs-extra": "^11.1.1",
"handlebars": "^4.7.7",
"handlebars-helpers": "^0.10.0",
"hbs-cli": "^1.4.1",
"handlebars": "^4.7.8",
"puppeteer": "^22.4.0",
"multer": "^1.4.5-lts.1",
"nodemon": "^3.0.1",
"puppeteer": "^18.0.2"
"nodemon": "^3.0.1"
},
"devDependencies": {
"@types/jest": "^24.0.15",
Expand Down
1 change: 1 addition & 0 deletions node/pdf-generate/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe("generatePdf", () => {

it("should return a pdf when passing valid data", async () => {
const formData = new FormData();
formData.append('title', 'Test Title');
formData.append('data', '{\n' +
'\t\t"transaction": {\n' +
'\t\t\t"id": "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890123456789",\n' +
Expand Down
2 changes: 2 additions & 0 deletions node/pdf-generate/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ const generatePdf = async function (req, res, next) {
page = await browser.newPage();

let data = req.body.data;
let title = req.body.title || "Documento PDF PagoPA";

if (data == undefined) {
res.status(400);
Expand Down Expand Up @@ -99,6 +100,7 @@ const generatePdf = async function (req, res, next) {
await waitForRender(page);
await page.pdf({
path: path.join(workingDir, "pagopa-receipt.pdf"),
title: title,
format: 'A4',
landscape: false,
printBackground: true,
Expand Down
2 changes: 1 addition & 1 deletion node/pdf-generate/helpers/eq.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var eq = function (a, b, options) {
return a === b ? options.fn(this) : null;
return a === b ? options.fn(this) : options.inverse(this);
};

module.exports = eq;
1 change: 0 additions & 1 deletion node/pdf-generate/utils/browserManager.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const puppeteer = require('puppeteer');
const registerHelpers = require("handlebars-helpers");
let handlebars = require("handlebars");
let splitAndSpace = require('../helpers/splitAndSpace');
let not = require('../helpers/not')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public enum AppErrorCodeEnum {

PDFE_714("PDFE_708", "I/O error when reading generate type from request, and writing it to the output stream"),

PDFE_715("PDFE_715", "I/O error when reading title from request and writing it to the output stream"),

PDFE_896("PDFE_896", "Unexpected field in request body"),
PDFE_897("PDFE_897", "Invalid HTML template, template not provided"),
PDFE_898("PDFE_898", "Invalid document data"),
Expand All @@ -41,8 +43,6 @@ public enum AppErrorCodeEnum {
PDFE_907("PDFE_907", "I/O error on handling the PDF generation result"),
PDFE_908("PDFE_908", "I/O error on creating the working directory");



private final String errorCode;
private final String errorMessage;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class GeneratePDFInput {
private boolean applySignature;
private boolean generateZipped;
private ZipFile templateZip;
private String title;

private GeneratorType generatorType = GeneratorType.ITEXT;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ public class PdfEngineRequest {

String data;

String workingDirPath;
ZipFile template;
String workingDirPath;

String title;

ZipFile template;


}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public BufferedInputStream generatePDF(GeneratePDFInput generatePDFInput, Path w

PdfEngineClientImpl pdfEngineClient = PdfEngineClientImpl.getInstance();
PdfEngineRequest pdfEngineRequest = new PdfEngineRequest();
pdfEngineRequest.setTitle(generatePDFInput.getTitle() != null ?
generatePDFInput.getTitle() : "Documento PDF PagoPA");
pdfEngineRequest.setWorkingDirPath(workingDirPath.toFile().getAbsolutePath());
pdfEngineRequest.setData(ObjectMapperUtils.writeValueAsString(generatePDFInput.getData()));
pdfEngineRequest.setTemplate(generatePDFInput.getTemplateZip());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public GeneratePDFInput retrieveInputData(byte[] requestBody, Map<String, String
case "data":
generatePDFInput.setData(getDocumentInputData(multipartStream));
break;
case "title":
generatePDFInput.setTitle(getStringField(multipartStream, PDFE_715));
break;
case "applySignature":
generatePDFInput.setApplySignature(getBooleanField(multipartStream, PDFE_708));
break;
Expand Down Expand Up @@ -87,6 +90,17 @@ private boolean getBooleanField(MultipartStream multipartStream, AppErrorCodeEnu
return Boolean.parseBoolean(outputStream.toString());
}

private String getStringField(MultipartStream multipartStream, AppErrorCodeEnum errorCode) throws RequestBodyParseException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
multipartStream.readBodyData(outputStream);
} catch (IOException e) {
throw new RequestBodyParseException(errorCode, errorCode.getErrorMessage(), e);
}
return outputStream.toString();
}



private Map<String,Object> getDocumentInputData(MultipartStream multipartStream) throws RequestBodyParseException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ void generatePDFZippedWithSuccess() {
pdfInput.setData(Collections.singletonMap("a", "b"));
pdfInput.setApplySignature(false);
pdfInput.setGenerateZipped(true);
pdfInput.setTitle("title");

PdfEngineResponse pdfEngineResponse = new PdfEngineResponse();
pdfEngineResponse.setStatusCode(200);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ void retrieveInputDataSuccess() {
doReturn(
String.format(HEADER_TEMPLATE, "applySignature"),
String.format(HEADER_TEMPLATE, "generateZipped"),
String.format(HEADER_TEMPLATE, "title"),
String.format(HEADER_TEMPLATE, "data")
).when(multipartStreamMock).readHeaders();
doReturn(Collections.singletonMap("ke1", "value1")).when(objectMapperMock).readValue(anyString(), any(TypeReference.class));
doReturn(true, true, false).when(multipartStreamMock).readBoundary();
doReturn(true, true, true, false).when(multipartStreamMock).readBoundary();

GeneratePDFInput result = sut.retrieveInputData(new byte[2], Collections.singletonMap(CONTENT_TYPE_HEADER, CONTENT_TYPE_HEADER_VALUE), workingPath);

Expand Down Expand Up @@ -306,4 +307,23 @@ void retrieveInputDataFailReadBoundaryThrowsMalformedStreamException() {

Assertions.assertEquals(AppErrorCodeEnum.PDFE_710, e.getErrorCode());
}

@Test
@SneakyThrows
void retrieveInputDataFailReadTitleThrowsIOException() {
MultipartStream multipartStreamMock = mock(MultipartStream.class);

doReturn(multipartStreamMock).when(sut).getMultipartStream(any(), anyString());
doReturn(true).when(multipartStreamMock).skipPreamble();
doReturn(String.format(HEADER_TEMPLATE, "title")).when(multipartStreamMock).readHeaders();
doThrow(IOException.class).when(multipartStreamMock).readBodyData(any());

RequestBodyParseException e = assertThrows(
RequestBodyParseException.class,
() -> sut.retrieveInputData(new byte[2], Collections.singletonMap(CONTENT_TYPE_HEADER, CONTENT_TYPE_HEADER_VALUE), workingPath)
);

Assertions.assertEquals(AppErrorCodeEnum.PDFE_715, e.getErrorCode());
}

}

0 comments on commit 9c09d31

Please sign in to comment.