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 16, 2024
1 parent 5d6dbaa commit 088fb55
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
62 changes: 62 additions & 0 deletions test/resources/qualified-electronic-signature.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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-");
});
23 changes: 23 additions & 0 deletions 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 @@ -273,6 +275,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 088fb55

Please sign in to comment.