Skip to content

Commit

Permalink
Use FileTransfer object in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dvacca-onfido committed Jun 20, 2024
1 parent 13f8cd9 commit a0918fb
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 25 deletions.
25 changes: 25 additions & 0 deletions test/file-transfer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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")

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")

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")

expect(fileTransfer.filename).toEqual("test/media/sample_photo.png");
expect(fileTransfer.buffer.slice(1, 4).toString()).toEqual("PNG");
});
3 changes: 2 additions & 1 deletion test/resources/checks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,5 +153,6 @@ it("downloads a check", async () => {

expect(file.status).toEqual(200);
expect(file.headers["content-type"]).toEqual("application/pdf");
expect(file.data.slice(0, 5)).toEqual("%PDF-");
expect(file.data.buffer.slice(0, 5)).toEqual("%PDF-");
expect(file.data.filename).toBeTruthy();
});
3 changes: 2 additions & 1 deletion test/resources/documents.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ it("downloads a document", async () => {

expect(file.status).toEqual(200);
expect(file.headers["content-type"]).toEqual("image/png");
expect(file.data.slice(1, 4)).toEqual("PNG");
expect(file.data.buffer.slice(1, 4)).toEqual("PNG");
expect(file.data.filename).toBeTruthy();
});

it("finds a document", async () => {
Expand Down
3 changes: 2 additions & 1 deletion test/resources/id-photos.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ it("downloads a id photo", async () => {
const file = await onfido.downloadIdPhoto(photo.data.id);

expect(file.status).toEqual(200);
expect(file.data.slice(1, 4)).toEqual("PNG");
expect(file.data.buffer.slice(1, 4)).toEqual("PNG");
expect(file.data.filename).toBeTruthy();
});

it("finds a id photo", async () => {
Expand Down
3 changes: 2 additions & 1 deletion test/resources/live-photos.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ it("downloads a live photo", async () => {
const file = await onfido.downloadLivePhoto(photo.data.id);

expect(file.status).toEqual(200);
expect(file.data.slice(1, 4)).toEqual("PNG");
expect(file.data.buffer.slice(1, 4)).toEqual("PNG");
expect(file.data.filename).toEqual("sample_photo.png");
});

it("finds a live photo", async () => {
Expand Down
4 changes: 3 additions & 1 deletion test/resources/live-videos.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,16 @@ it("downloads a live video", async () => {

expect(file.status).toEqual(200);
expect(file.headers["content-type"]).toEqual("video/quicktime");
expect(file.data.filename).toEqual("video.mov");
});

it("downloads a live video frame", async () => {
const file = await onfido.downloadLiveVideoFrame(sampleLiveVideoId2);

expect(file.status).toEqual(200);
expect(file.headers["content-type"]).toEqual("image/jpeg");
expect(file.data.slice(0, 10)).toContain("JFIF");
expect(file.data.buffer.slice(0, 10)).toContain("JFIF");
expect(file.data.filename).toBeTruthy();
});

it("finds a live video", async () => {
Expand Down
4 changes: 3 additions & 1 deletion test/resources/motion-captures.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,16 @@ it("downloads a motion capture", async () => {

expect(file.status).toEqual(200);
expect(file.headers["content-type"]).toContain("video/mp4");
expect(file.data.filename).toBeTruthy();
});

it("downloads a motion capture frame", async () => {
const file = await onfido.downloadMotionCaptureFrame(sampleId1);

expect(file.status).toEqual(200);
expect(file.headers["content-type"]).toContain("image/jpeg");
expect(file.data.slice(0, 10)).toContain("JFIF");
expect(file.data.buffer.slice(0, 10)).toContain("JFIF");
expect(file.data.filename).toBeTruthy();
});

it("finds a motion capture", async () => {
Expand Down
4 changes: 2 additions & 2 deletions test/resources/workflow-runs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ it("downloads a signed evidence file", async () => {

expect(file.status).toEqual(200);
expect(file.headers["content-type"]).toEqual("application/pdf");
expect(file.data.slice(0, 5)).toEqual("%PDF-");
expect(file.data.buffer.slice(0, 5)).toEqual("%PDF-");
});

it("generates a timeline file", async () => {
Expand Down Expand Up @@ -147,5 +147,5 @@ it("downloads a timeline file", async () => {

expect(file.status).toEqual(200);
expect(file.headers["content-type"]).toEqual("binary/octet-stream");
expect(file.data.slice(0, 5)).toEqual("%PDF-");
expect(file.data.buffer.slice(0, 5)).toEqual("%PDF-");
}, 30000);
28 changes: 11 additions & 17 deletions test/test-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isAxiosError } from "axios";
import { createReadStream } from "fs";
import { createReadStream, readFileSync } from "fs";
import "dotenv/config";

import {
Expand All @@ -10,6 +10,7 @@ import {
Document,
DocumentReport,
FacialSimilarityPhotoReport,
FileTransfer,
LiveVideo,
MotionCapture,
Report,
Expand All @@ -24,6 +25,8 @@ export const onfido = new DefaultApi(
})
);

jest.setTimeout(60_000);

export const sampleapplicant_id =
process.env.ONFIDO_SAMPLE_APPLICANT_ID || "sample_applicant_id";

Expand Down Expand Up @@ -83,37 +86,28 @@ export async function cleanUpApplicants() {
});
}

export async function uploadDocumentFromStream(
applicant: Applicant,
readStream: File,
documentType = "driving_licence"
) {
return onfido.uploadDocument(documentType, applicant.id, readStream);
}

export async function uploadDocument(
applicant: Applicant,
documentType = "driving_licence"
) {
let readStream: any = createReadStream(
"test/media/sample_driving_licence.png"
);
return uploadDocumentFromStream(applicant, readStream, documentType);
let fileTransfer = new FileTransfer("test/media/sample_driving_licence.png");

return onfido.uploadDocument(documentType, applicant.id, fileTransfer);
}

export async function uploadLivePhoto(
applicant: Applicant,
advancedValidation?: boolean
) {
let readStream: any = createReadStream("test/media/sample_photo.png");
let buffer = readFileSync("test/media/sample_photo.png");

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

export async function uploadIdPhoto(applicant: Applicant) {
let readStream: any = createReadStream("test/media/sample_photo.png");
let fileTransfer = new FileTransfer("test/media/sample_photo.png");

return onfido.uploadIdPhoto(applicant.id, readStream);
return onfido.uploadIdPhoto(applicant.id, fileTransfer);
}

export async function createWebhook() {
Expand Down

0 comments on commit a0918fb

Please sign in to comment.