From 5f4a92e61016b581f69af34408fc0e0a2470ab92 Mon Sep 17 00:00:00 2001 From: Davide Vacca Date: Mon, 21 Oct 2024 10:59:22 +0200 Subject: [PATCH] Add schedule to CI, fix and improve tests after library refresh --- .github/workflows/node.yml | 3 +- test/resources/applicants.test.ts | 12 ++- test/resources/checks.test.ts | 12 ++- test/resources/documents.test.ts | 8 +- .../qualified-electronic-signature.test.ts | 2 +- test/resources/tasks.test.ts | 5 +- test/test-helpers.ts | 16 +++- test/webhook-event-verifier.test.ts | 92 +++++++++++++++---- 8 files changed, 118 insertions(+), 32 deletions(-) diff --git a/.github/workflows/node.yml b/.github/workflows/node.yml index d8c7267..63923bb 100644 --- a/.github/workflows/node.yml +++ b/.github/workflows/node.yml @@ -16,7 +16,8 @@ on: release: types: - published - + schedule: + - cron: "0 12 * * 0" # Every Sunday, at midday jobs: integration-tests: runs-on: ubuntu-latest diff --git a/test/resources/applicants.test.ts b/test/resources/applicants.test.ts index a9ccb41..1bae13a 100644 --- a/test/resources/applicants.test.ts +++ b/test/resources/applicants.test.ts @@ -1,4 +1,4 @@ -import { Applicant } from "onfido-node"; +import { Applicant, ApplicantConsentName } from "onfido-node"; import { onfido, @@ -66,7 +66,15 @@ it("restores an applicant", async () => { it("lists applicants", async () => { const anotherApplicant = { ...exampleApplicant, first_name: "Another" }; - await createApplicant({ first_name: "Another" }); + await createApplicant({ + first_name: "Another", + consents: [ + { + name: ApplicantConsentName.PrivacyNoticesRead, + granted: true + } + ] + }); const applicants = await onfido.listApplicants(1, 20, false); diff --git a/test/resources/checks.test.ts b/test/resources/checks.test.ts index 3b26cd7..2b366e2 100644 --- a/test/resources/checks.test.ts +++ b/test/resources/checks.test.ts @@ -1,4 +1,4 @@ -import { Applicant, Check, Document, Webhook } from "onfido-node"; +import { Applicant, Check, Document, Webhook, CheckStatus } from "onfido-node"; import { onfido, @@ -54,14 +54,16 @@ afterAll(() => { it("creates a check", async () => { const check = await createCheck(applicant, document, { - webhook_ids: [webhook1.id, webhook2.id] + webhook_ids: [webhook1.id, webhook2.id], + privacy_notices_read_consent_given: true }); expect(check.data).toEqual( getExpectedCheck(exampleCheck, { applicant_id: applicant.id, result: null, - status: "in_progress" + status: CheckStatus.InProgress, + privacy_notices_read_consent_given: true }) ); }); @@ -76,7 +78,7 @@ it("creates a check for generating a rejected sub-result for document report in getExpectedCheck(exampleCheck, { applicant_id: applicant.id, result: null, - status: "in_progress" + status: CheckStatus.InProgress }) ); }); @@ -91,7 +93,7 @@ it("creates a check for generating a consider result for a report in the sandbox getExpectedCheck(exampleCheck, { applicant_id: applicant.id, result: null, - status: "in_progress" + status: CheckStatus.InProgress }) ); }); diff --git a/test/resources/documents.test.ts b/test/resources/documents.test.ts index 5182f52..5d230ac 100644 --- a/test/resources/documents.test.ts +++ b/test/resources/documents.test.ts @@ -1,4 +1,4 @@ -import { Applicant, Document } from "onfido-node"; +import { Applicant, Document, DocumentTypes } from "onfido-node"; import { onfido, @@ -39,7 +39,7 @@ it("uploads a document", async () => { it("uploads a document with location", async () => { document = ( - await uploadDocument(applicant, "driving_licence", { + await uploadDocument(applicant, DocumentTypes.DrivingLicence, { country_of_residence: "FRA" }) ).data; @@ -61,7 +61,9 @@ it("finds a document", async () => { }); it("lists documents", async () => { - const anotherDocument = (await uploadDocument(applicant, "passport")).data; + const anotherDocument = ( + await uploadDocument(applicant, DocumentTypes.Passport) + ).data; const documents = ( await onfido.listDocuments(applicant.id) ).data.documents.sort(sortByDocumentType); diff --git a/test/resources/qualified-electronic-signature.test.ts b/test/resources/qualified-electronic-signature.test.ts index 4a52e1d..6c3f1c4 100644 --- a/test/resources/qualified-electronic-signature.test.ts +++ b/test/resources/qualified-electronic-signature.test.ts @@ -42,7 +42,7 @@ it("downloads a signed document file", async () => { const workflowRun = await createWorkflowRunWithCustomInputs( workflowRunBuilder ); - const taskId = (await onfido.listTasks(workflowRun.data.id)).data[0].id; + const taskId = (await onfido.listTasks(workflowRun.data.id)).data[1].id; const output = ( await repeatRequestUntilTaskOutputChanges( diff --git a/test/resources/tasks.test.ts b/test/resources/tasks.test.ts index e9d0b1f..ca46976 100644 --- a/test/resources/tasks.test.ts +++ b/test/resources/tasks.test.ts @@ -51,7 +51,10 @@ it("finds a task", async () => { const task = await onfido.findTask(workflowRunId, taskId); expect(task.data).toEqual( - getExpectedTask(exampleTask, { input: expect.anything(), output: null }) + getExpectedTask(exampleTask, { + input: expect.anything(), + output: expect.anything() + }) ); }); diff --git a/test/test-helpers.ts b/test/test-helpers.ts index b034d06..955c461 100644 --- a/test/test-helpers.ts +++ b/test/test-helpers.ts @@ -16,7 +16,8 @@ import { MotionCapture, Report, WatchlistMonitorReportNameEnum, - WorkflowRunBuilder + WorkflowRunBuilder, + DocumentTypes } from "onfido-node"; export const onfido = new DefaultApi( @@ -91,12 +92,21 @@ export async function cleanUpApplicants() { export async function uploadDocument( applicant: Applicant, - documentType = "driving_licence", + documentType: DocumentTypes = DocumentTypes.DrivingLicence, location?: LocationBuilder ) { let fileTransfer = new FileTransfer("test/media/sample_driving_licence.png"); - return onfido.uploadDocument(documentType, applicant.id, fileTransfer, undefined, undefined, undefined, undefined, location); + return onfido.uploadDocument( + documentType, + applicant.id, + fileTransfer, + undefined, + undefined, + undefined, + undefined, + location + ); } export async function uploadLivePhoto( diff --git a/test/webhook-event-verifier.test.ts b/test/webhook-event-verifier.test.ts index a620440..ddf2135 100644 --- a/test/webhook-event-verifier.test.ts +++ b/test/webhook-event-verifier.test.ts @@ -1,39 +1,81 @@ -import { OnfidoInvalidSignatureError, WebhookEventVerifier } from "onfido-node"; +import { + OnfidoInvalidSignatureError, + WebhookEventVerifier, + WebhookEventType, + WebhookEventResourceType, + WebhookEventObjectStatus +} from "onfido-node"; -const webhookToken = "_ABC123abc123ABC123abc123ABC123_"; -const verifier = new WebhookEventVerifier(webhookToken); +const classicVerifier = new WebhookEventVerifier( + "_ABC123abc123ABC123abc123ABC123_" +); +const studioVerifier = new WebhookEventVerifier( + "YKOC6mkBxi6yK2zlUIrLMvsJMFEZObP5" +); -const rawEvent = `{"payload":{"resource_type":"check","action":"check.completed","object":{"id":"check-123","status":"complete","completed_at_iso8601":"2020-01-01T00:00:00Z","href":"https://api.onfido.com/v3/checks/check-123"}}}`; +const classicRawEvent = `{"payload":{"resource_type":"check","action":"check.completed","object":{"id":"check-123","status":"complete","completed_at_iso8601":"2020-01-01T00:00:00Z","href":"https://api.onfido.com/v3/checks/check-123"}}}`; -const expectedEvent = { +const studioRawEvent = `{"payload":{"resource_type":"workflow_task","action":"workflow_task.started","object":{"id":"profile_1eb92","task_spec_id":"profile_1eb92","task_def_id":"profile_data","workflow_run_id":"bc77c6e5-753a-4580-96a6-aaed3e5a8d19","status":"started","started_at_iso8601":"2024-07-10T12:49:09Z","href":"https://api.eu.onfido.com/v3.6/workflow_runs/bc77c6e5-753a-4580-96a6-aaed3e5a8d19/tasks/profile_1eb92"},"resource":{"created_at":"2024-07-10T12:49:09Z","id":"profile_1eb92","workflow_run_id":"bc77c6e5-753a-4580-96a6-aaed3e5a8d19","updated_at":"2024-07-10T12:49:09Z","input":{},"task_def_version":null,"task_def_id":"profile_data","output":null}}}`; + +const classicExpectedEvent = { payload: { - action: "check.completed", - resource_type: "check", + action: WebhookEventType.CheckCompleted, + resource_type: WebhookEventResourceType.Check, object: { id: "check-123", href: "https://api.onfido.com/v3/checks/check-123", - status: "complete", + status: WebhookEventObjectStatus.Complete, completed_at_iso8601: "2020-01-01T00:00:00Z" } } }; -it("returns the event if the signature is valid", () => { +const studioExpectedEvent = { + payload: { + action: WebhookEventType.WorkflowTaskStarted, + resource_type: WebhookEventResourceType.WorkflowTask, + object: { + id: "profile_1eb92", + href: + "https://api.eu.onfido.com/v3.6/workflow_runs/bc77c6e5-753a-4580-96a6-aaed3e5a8d19/tasks/profile_1eb92", + status: WebhookEventObjectStatus.Started, + started_at_iso8601: "2024-07-10T12:49:09Z", + task_def_id: "profile_data", + task_spec_id: "profile_1eb92", + workflow_run_id: "bc77c6e5-753a-4580-96a6-aaed3e5a8d19" + }, + resource: { + created_at: "2024-07-10T12:49:09Z", + id: "profile_1eb92", + input: {}, + output: null, + task_def_id: "profile_data", + task_def_version: null, + updated_at: "2024-07-10T12:49:09Z", + workflow_run_id: "bc77c6e5-753a-4580-96a6-aaed3e5a8d19" + } + } +}; + +it("returns the event if classic webhook event signature is valid", () => { const signature = "a0082d7481f9f0a2907583dbe1f344d6d4c0d9989df2fd804f98479f60cd760e"; - const event = verifier.readPayload(rawEvent, signature); + const event = classicVerifier.readPayload(classicRawEvent, signature); - expect(event).toEqual(expectedEvent); + expect(event).toEqual(classicExpectedEvent); }); -it("allows passing the body as a buffer", () => { +it("allows passing the classic webhook event body body as a buffer", () => { const signature = "a0082d7481f9f0a2907583dbe1f344d6d4c0d9989df2fd804f98479f60cd760e"; - const event = verifier.readPayload(Buffer.from(rawEvent), signature); + const event = classicVerifier.readPayload( + Buffer.from(classicRawEvent), + signature + ); - expect(event).toEqual(expectedEvent); + expect(event).toEqual(classicExpectedEvent); expect(event.payload.object.href).toEqual( "https://api.onfido.com/v3/checks/check-123" ); @@ -42,11 +84,29 @@ it("allows passing the body as a buffer", () => { expect(event.payload.object["status"]).toEqual("complete"); }); -it("throws an error if the signature is invalid", () => { +it("throws an error if the classic webhook event signature is invalid", () => { const signature = "b0082d7481f9f0a2907583dbe1f344d6d4c0d9989df2fd804f98479f60cd760e"; - expect(() => verifier.readPayload(rawEvent, signature)).toThrow( + expect(() => studioVerifier.readPayload(classicRawEvent, signature)).toThrow( + OnfidoInvalidSignatureError + ); +}); + +it("returns the event if studio webhook event signature is valid", () => { + const signature = + "c95a5b785484f6fa1bc25f381b5595d66bf85cb442eefb06aa007802ee6a4dfa"; + + const event = studioVerifier.readPayload(studioRawEvent, signature); + + expect(event).toEqual(studioExpectedEvent); +}); + +it("throws an error if the studio webhook event signature is invalid", () => { + const signature = + "c95a5b785484f6fa1bc25f381b5595d66bf85cb442eefb06aa007802ee6a4dfb"; + + expect(() => studioVerifier.readPayload(studioRawEvent, signature)).toThrow( OnfidoInvalidSignatureError ); });