Skip to content

Commit

Permalink
Add schedule to CI, fix and improve tests after library refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
dvacca-onfido committed Oct 21, 2024
1 parent 8f3d64a commit 9a991c5
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 31 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ on:
release:
types:
- published
schedule:
- cron: "0 15 * * 0" # Every Sunday, 3 hours after midday

jobs:
integration-tests:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,19 @@

use Onfido as Onfido;

class WebhookEventVerifierTest extends TestCase
class ClassicWebhookEventVerifierTest extends TestCase
{
private static $rawEvent = "{\"payload\":{\"resource_type\":\"check\",\"action\":\"check.completed\",\"object\":{\"id\":\"f2302f45-227d-413d-ad61-09ec077a086a\",\"status\":\"complete\",\"completed_at_iso8601\":\"2024-04-04T09:21:21Z\",\"href\":\"https://api.onfido.com/v3.6/checks/f2302f45-227d-413d-ad61-09ec077a086a\"}}}";
private static $webhookToken = "wU99mE6jJ7nXOLFwZ0tJymM1lpI15pZh";

private static $webhookEventVerifier;

private static $expectedEvent;

/**
* Setup before running any test case
*/
public static function setUpBeforeClass(): void
{
self::$webhookEventVerifier = new Onfido\WebhookEventVerifier(self::$webhookToken);

self::$expectedEvent = new Onfido\Model\WebhookEvent(
[ 'payload' => new Onfido\Model\WebhookEventPayload(
[ 'action' => Onfido\Model\WebhookEventType::CHECK_COMPLETED,
'resource_type' => 'check',
'object' => new Onfido\Model\WebhookEventPayloadObject(
[ 'id' => 'f2302f45-227d-413d-ad61-09ec077a086a',
'href' => 'https://api.onfido.com/v3.6/checks/f2302f45-227d-413d-ad61-09ec077a086a',
'status' => 'complete',
'completed_at_iso8601' => '2024-04-04T09:21:21Z' ])
])
]);
}

public function testValidSignature(): void
Expand All @@ -44,10 +30,10 @@ public function testValidSignature(): void
$object = $payload->getObject();

$this->assertSame(Onfido\Model\WebhookEventType::CHECK_COMPLETED, $payload->getAction());
$this->assertSame('check', $payload->getResourceType());
$this->assertSame(Onfido\Model\WebhookEventResourceType::CHECK, $payload->getResourceType());
$this->assertSame('f2302f45-227d-413d-ad61-09ec077a086a', $object->getId());
$this->assertSame('https://api.onfido.com/v3.6/checks/f2302f45-227d-413d-ad61-09ec077a086a', $object->getHref());
$this->assertSame('complete', $object->getStatus());
$this->assertSame(Onfido\Model\WebhookEventObjectStatus::COMPLETE, $object->getStatus());
$this->assertSame('2024-04-04T09:21:21+00:00', $object->getCompletedAtIso8601()->format('c'));
}

Expand Down
9 changes: 8 additions & 1 deletion test/OnfidoTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ protected function createApplicant(): Onfido\Model\Applicant
]),
'email' => '[email protected]',
'phone_number' => '351911111111',
'consents' => [
new Onfido\Model\ApplicantConsentBuilder([
'name' => Onfido\Model\ApplicantConsentName::PRIVACY_NOTICES_READ,
'granted' => true
])
]
]
),
);
Expand Down Expand Up @@ -106,7 +112,7 @@ protected static function cleanUpWebhooks(): void
protected function uploadDocument(string $applicantId): Onfido\Model\Document
{
return self::$onfido->uploadDocument(
'passport',
Onfido\Model\DocumentTypes::PASSPORT,
$applicantId,
new \SplFileObject('test/media/sample_driving_licence.png')
);
Expand Down Expand Up @@ -220,6 +226,7 @@ protected function repeatRequestUntilHttpCodeChanges(
$sleepTime = 1
)
{
$instance = null;
$iteration = 0;
while($iteration <= $maxRetries) {
try {
Expand Down
7 changes: 5 additions & 2 deletions test/Resource/ChecksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Onfido\Test\OnfidoTestCase as OnfidoTestCase;
use Onfido\Model\ReportName as ReportName;
use Onfido\Model\Check as Check;
use Onfido\Model\CheckStatus as CheckStatus;
use Onfido\Model\CheckBuilder as CheckBuilder;
use Onfido\Model\UsDrivingLicenceBuilder as UsDrivingLicenceBuilder;

Expand All @@ -31,7 +32,7 @@ public function setUp(): void
public function testCreateCheck(): void
{
$this->assertSame($this->applicantId, $this->check->getApplicantId());
$this->assertSame(Check::STATUS_IN_PROGRESS, $this->check->getStatus());
$this->assertSame(CheckStatus::IN_PROGRESS, $this->check->getStatus());
$this->assertSame(2, sizeOf($this->check->getReportIds()));
}

Expand All @@ -41,11 +42,13 @@ public function testCreateConsiderCheck(): void
'applicant_id' => $this->applicantId,
'document_ids' => [$this->documentId],
'report_names' => [ReportName::DOCUMENT],
'consider' => [ReportName::DOCUMENT]
'consider' => [ReportName::DOCUMENT],
'privacy_notices_read_consent_given' => true
]);

$check = $this->createCheck($checkBuilder);
$this->assertSame($this->applicantId, $check->getApplicantId());
$this->assertSame(true, $check->getPrivacyNoticesReadConsentGiven());
}

public function testCreateDrivingLicenceCheck(): void
Expand Down
4 changes: 2 additions & 2 deletions test/Resource/QualifiedElectronicSignatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public function testDownloadQesDocument(): void
]
]);
$workflowRun = $this->createWorkflowRun($workflowRunBuilder);
$task = self::$onfido->listTasks($workflowRun["id"])[0];

$task = self::$onfido->listTasks($workflowRun["id"])[1];
$workflowRunId = $workflowRun["id"];
$taskId = $task->getId();

Expand Down
9 changes: 5 additions & 4 deletions test/Resource/WorkflowRunOutputsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Onfido\Test\OnfidoTestCase as OnfidoTestCase;
use Onfido\Model\WorkflowRun as WorkflowRun;
use Onfido\Model\WorkflowRunStatus as WorkflowRunStatus;
use Onfido\Model\CompleteTaskBuilder as CompleteTaskBuilder;

class WorkflowRunOutputsTest extends OnfidoTestCase
Expand Down Expand Up @@ -56,13 +57,13 @@ public function testProfileDataCaptureAsOutput(): void
$workflowRunOutputs = $this->repeatRequestUntilStatusChanges(
$this->findWorkflowRunFn,
[$workflowRunId],
WorkflowRun::STATUS_APPROVED
WorkflowRunStatus::APPROVED
)->getOutput();

// Can't compare output with profileData directly due to issues with types,
// so converting the output into array and comparing key by key value
// (without the address which also causes issues and is compared separately)
$keysExceptAddress = array_filter(array_keys($this->profileData),
$keysExceptAddress = array_filter(array_keys($this->profileData),
function($key) {
return $key !== 'address';
}
Expand Down Expand Up @@ -96,7 +97,7 @@ public function testDocumentAndFacialSimilarityReportAsOutput()
'last_name' => 'Last'
]])
);

$tasks = self::$onfido->listTasks($workflowRunId);
$documentCaptureTaskId = $this->getTaskIdByPartialId($tasks, 'document');
self::$onfido->completeTask(
Expand All @@ -120,7 +121,7 @@ public function testDocumentAndFacialSimilarityReportAsOutput()
$workflowRunOutputs = $this->repeatRequestUntilStatusChanges(
$this->findWorkflowRunFn,
[$workflowRunId],
WorkflowRun::STATUS_APPROVED
WorkflowRunStatus::APPROVED
)->getOutput();

$documentReportOutput = (array) $workflowRunOutputs['doc'];
Expand Down
11 changes: 6 additions & 5 deletions test/Resource/WorkflowRunsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Onfido\Test\OnfidoTestCase as OnfidoTestCase;
use Onfido\Model\WorkflowRun as WorkflowRun;
use Onfido\Model\WorkflowRunStatus as WorkflowRunStatus;
use Onfido\Model\WorkflowRunBuilder as WorkflowRunBuilder;

class WorkflowRunsTest extends OnfidoTestCase
Expand All @@ -22,7 +23,7 @@ public function setUp(): void
public function testCreateWorkflowRun(): void
{
$this->assertSame($this->workflowId, $this->workflowRun->getWorkflowId());
$this->assertSame(WorkflowRun::STATUS_AWAITING_INPUT, $this->workflowRun->getStatus());
$this->assertSame(WorkflowRunStatus::AWAITING_INPUT, $this->workflowRun->getStatus());
}

public function testCreateWorkflowRunWithCustomInputs(): void
Expand All @@ -40,7 +41,7 @@ public function testCreateWorkflowRunWithCustomInputs(): void
$workflowRun = $this->createWorkflowRun($workflowRunBuilder);

$this->assertSame($workflowId, $workflowRun->getWorkflowId());
$this->assertSame(WorkflowRun::STATUS_APPROVED, $workflowRun->getStatus());
$this->assertSame(WorkflowRunStatus::APPROVED, $workflowRun->getStatus());
}

public function testListWorkflowRuns(): void
Expand Down Expand Up @@ -72,7 +73,7 @@ public function testGenerateTimelineFile(): void
$this->repeatRequestUntilStatusChanges(
$findWorkflowRunFn,
[$workflowRunId],
WorkflowRun::STATUS_APPROVED
WorkflowRunStatus::APPROVED
)->getOutput();

$workflowTimelineFileData = self::$onfido->createTimelineFile($workflowRunId);
Expand All @@ -96,7 +97,7 @@ public function testDownloadTimelineFile(): void
$this->repeatRequestUntilStatusChanges(
$findWorkflowRunFn,
[$workflowRunId],
WorkflowRun::STATUS_APPROVED
WorkflowRunStatus::APPROVED
)->getOutput();

$workflowTimelineFileId = self::$onfido->createTimelineFile($workflowRunId)
Expand All @@ -106,7 +107,7 @@ public function testDownloadTimelineFile(): void
$getTimelineFileFn,
[$workflowRunId, $workflowTimelineFileId]
);

$this->assertGreaterThan(0, $file->getSize());
}
}
Expand Down
57 changes: 57 additions & 0 deletions test/StudioWebhookEventVerifierTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Onfido\Test;

use PHPUnit\Framework\TestCase;

use Onfido as Onfido;

class StudioWebhookEventVerifierTest extends TestCase
{
private static $rawEvent = "{\"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}}}";
private static $webhookToken = "YKOC6mkBxi6yK2zlUIrLMvsJMFEZObP5";

private static $webhookEventVerifier;

/**
* Setup before running any test case
*/
public static function setUpBeforeClass(): void
{
self::$webhookEventVerifier = new Onfido\WebhookEventVerifier(self::$webhookToken);
}

public function testValidSignature(): void
{
$signature = "c95a5b785484f6fa1bc25f381b5595d66bf85cb442eefb06aa007802ee6a4dfa";

$event = self::$webhookEventVerifier->readPayload(self::$rawEvent, $signature);
$payload = $event->getPayload();
$object = $payload->getObject();

$this->assertSame(Onfido\Model\WebhookEventType::WORKFLOW_TASK_STARTED, $payload->getAction());
$this->assertSame(Onfido\Model\WebhookEventResourceType::WORKFLOW_TASK, $payload->getResourceType());
$this->assertSame('profile_1eb92', $object->getId());
$this->assertSame('https://api.eu.onfido.com/v3.6/workflow_runs/bc77c6e5-753a-4580-96a6-aaed3e5a8d19/tasks/profile_1eb92', $object->getHref());
$this->assertSame(Onfido\Model\WebhookEventObjectStatus::STARTED, $object->getStatus());
$this->assertSame('2024-07-10T12:49:09+00:00', $object->getStartedAtIso8601()->format('c'));

$resource = $payload->getResource();
$this->assertSame('2024-07-10T12:49:09+00:00', $resource->getCreatedAt()->format('c'));
$this->assertSame('profile_1eb92', $resource->getId());
$this->assertSame([], $resource->getInput());
$this->assertSame(null, $resource->getOutput());
$this->assertSame('profile_data', $resource->getTaskDefId());
$this->assertSame(null, $resource->getTaskDefVersion());
$this->assertSame('2024-07-10T12:49:09+00:00', $resource->getUpdatedAt()->format('c'));
$this->assertSame('bc77c6e5-753a-4580-96a6-aaed3e5a8d19', $resource->getWorkflowRunId());
}

public function testInvalidSignature()
{
$signature = "c95a5b785484f6fa1bc25f381b5595d66bf85cb442eefb06aa007802ee6a4dfb";

$this->expectException(Onfido\OnfidoInvalidSignatureError::class);
$event = self::$webhookEventVerifier->readPayload(self::$rawEvent, $signature);
}
}

0 comments on commit 9a991c5

Please sign in to comment.