Skip to content

Commit

Permalink
test(qes): cover document download
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscocastanho-onfido committed Jul 17, 2024
1 parent 5d6dbaa commit a06d937
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 7 deletions.
7 changes: 3 additions & 4 deletions test/file-transfer.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
import { FileTransfer } from "onfido-node";
import { createReadStream, readFileSync } from "fs";


it("create a file transfer from a string and filename", () => {
const fileTransfer = new FileTransfer("PAYLOAD", "test-file.jpg")
const fileTransfer = new FileTransfer("PAYLOAD", "test-file.jpg");

expect(fileTransfer.filename).toEqual("test-file.jpg");
expect(fileTransfer.buffer.toString()).toEqual("PAYLOAD");
});

it("create a file transfer from a buffer and filename", () => {
let buffer = readFileSync("test/media/sample_photo.png");
const fileTransfer = new FileTransfer(buffer, "filename.png")
const fileTransfer = new FileTransfer(buffer, "filename.png");

expect(fileTransfer.filename).toEqual("filename.png");
expect(fileTransfer.buffer.slice(1, 4).toString()).toEqual("PNG");
});

it("create a file transfer from a file path", () => {
const fileTransfer = new FileTransfer("test/media/sample_photo.png")
const fileTransfer = new FileTransfer("test/media/sample_photo.png");

expect(fileTransfer.filename).toEqual("test/media/sample_photo.png");
expect(fileTransfer.buffer.slice(1, 4).toString()).toEqual("PNG");
Expand Down
63 changes: 63 additions & 0 deletions test/resources/qualified-electronic-signature.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import {
cleanUpApplicants,
cleanUpWebhooks,
createApplicant,
repeatRequestUntilTaskOutputChanges,
createWorkflowRunWithCustomInputs,
onfido,
sleep
} from "../test-helpers";

import { WorkflowRunBuilder } from "onfido-node";

afterAll(() => {
return Promise.all([cleanUpApplicants(), cleanUpWebhooks()]);
});

it("downloads a signed document file", async () => {
function sleep(milliseconds) {
const date = Date.now();
let currentDate = null;
do {
currentDate = Date.now();
} while (currentDate - date < milliseconds);
}
const applicant = (await createApplicant()).data;

const workflowRunBuilder: WorkflowRunBuilder = {
applicant_id: applicant.id,
workflow_id: "8b74614f-9e7f-42fd-852a-5f2bcc852587",
custom_data: {
country_of_operation: "GBR",
document_date_of_expiry: "2022-01-01",
document_issuing_country: "FRA",
document_issuing_date: "2022-01-01",
document_number: "Example string",
document_to_sign_url:
"https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf",
document_type: "driving_licence"
}
};

const workflowRun = await createWorkflowRunWithCustomInputs(
workflowRunBuilder
);
const taskId = (await onfido.listTasks(workflowRun.data.id)).data[0].id;

const output = (
await repeatRequestUntilTaskOutputChanges(
"findTask",
[workflowRun.data.id, taskId],
10,
3000
)
)["output"];

const fileId = output["properties"]["signed_documents"][0]["id"];

const file = await onfido.downloadQesDocument(workflowRun.data.id, fileId);

expect(file.status).toEqual(200);
expect(file.headers["content-type"]).toEqual("application/pdf");
expect(file.data.buffer.slice(0, 5)).toEqual("%PDF-");
});
4 changes: 2 additions & 2 deletions test/test-examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const exampleApplicant: Applicant = {
href: "/v3.6/applicants/123-abc",
first_name: "Test",
last_name: "Applicant",
email: null,
email: "[email protected]",
dob: null,
id_numbers: [],
address: {
Expand All @@ -34,7 +34,7 @@ export const exampleApplicant: Applicant = {
line2: null,
line3: null
},
phone_number: null,
phone_number: "351911111111",
location: {
ip_address: "127.0.0.1",
country_of_residence: "GBR"
Expand Down
29 changes: 28 additions & 1 deletion test/test-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ export async function createApplicant(overrideProperties = {}) {
ip_address: "127.0.0.1",
country_of_residence: "GBR"
},
email: "[email protected]",
phone_number: "351911111111",
...overrideProperties
});
}
Expand Down Expand Up @@ -101,7 +103,11 @@ export async function uploadLivePhoto(
) {
let buffer = readFileSync("test/media/sample_photo.png");

return onfido.uploadLivePhoto(applicant.id, new FileTransfer(buffer, "sample_photo.png"), advancedValidation);
return onfido.uploadLivePhoto(
applicant.id,
new FileTransfer(buffer, "sample_photo.png"),
advancedValidation
);
}

export async function uploadIdPhoto(applicant: Applicant) {
Expand Down Expand Up @@ -273,6 +279,27 @@ export async function repeatRequestUntilStatusChanges(
return instance;
}

export async function repeatRequestUntilTaskOutputChanges(
fn: string,
params: any[],
maxRetries = 10,
sleepTime = 1000
): Promise<any> {
let instance = (await onfido[fn](...params)).data;
let iteration = 0;

while (instance["output"] === null) {
if (iteration >= maxRetries) {
throw new Error("Task output did not change in time");
}
iteration += 1;
await sleep(sleepTime);

instance = (await onfido[fn](...params)).data;
}
return instance;
}

export async function repeatRequestUntilHttpCodeChanges(
fn: string,
params: any[],
Expand Down

0 comments on commit a06d937

Please sign in to comment.