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 640198f commit 433ebd9
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 31 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ on:
release:
types:
- published
schedule:
- cron: "0 14 * * 0" # Every Sunday, 2 hours after midday

jobs:
integration-tests:
Expand Down
7 changes: 4 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def create_applicant(onfido_api, applicant_builder=None):
def upload_document(onfido_api, applicant_id):
return onfido_api.upload_document(
applicant_id=applicant_id,
type="passport",
type=onfido.DocumentTypes.PASSPORT,
side="front",
file="tests/media/sample_driving_licence.png",
)
Expand Down Expand Up @@ -93,6 +93,7 @@ def create_check(
applicant_id=applicant_id,
document_ids=document_ids,
report_names=report_names,
privacy_notices_read_consent_given=True,
)
)

Expand Down Expand Up @@ -142,13 +143,13 @@ def repeat_request_until_task_output_changes(
instance = function(*params)

iteration = 0
while instance.output == None:
while instance.output is None:
if iteration > max_retries:
pytest.fail("Task output did not change in time")

iteration += 1
sleep(sleep_time)

instance = function(*params)

return instance
Expand Down
6 changes: 6 additions & 0 deletions tests/test_applicants.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ def test_create_applicant(onfido_api):
country=onfido.CountryCodes.FRA,
line1="My wonderful address",
),
consents=[
onfido.ApplicantConsentBuilder(
name=onfido.ApplicantConsentName.PRIVACY_NOTICES_READ,
granted=True
)
]
)

applicant = create_applicant(onfido_api, applicant_builder)
Expand Down
5 changes: 3 additions & 2 deletions tests/test_checks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest
from onfido import Check, ChecksList, ReportName, CheckBuilder, UsDrivingLicenceBuilder
from onfido import (Check, ChecksList, ReportName, CheckBuilder,
UsDrivingLicenceBuilder, CheckStatus)
from tests.conftest import create_applicant, create_check, upload_document


Expand Down Expand Up @@ -28,7 +29,7 @@ def test_create_check(check, applicant_id):
assert isinstance(check, Check)
assert check.applicant_id == applicant_id
assert len(check.report_ids) == 2
assert check.status == "in_progress"
assert check.status == CheckStatus.IN_PROGRESS


def test_create_consider_check(onfido_api, applicant_id, document_id):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_documents.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from onfido import ApiException, Document, DocumentsList
from onfido import ApiException, Document, DocumentsList, DocumentTypes
from tests.conftest import create_applicant, upload_document


Expand All @@ -17,7 +17,7 @@ def document(onfido_api, applicant_id):
def test_create_document(applicant_id, document):
assert document is not None
assert document.applicant_id == applicant_id
assert document.type == "passport"
assert document.type == DocumentTypes.PASSPORT
assert document.side == "front"
assert isinstance(document, Document)

Expand Down
3 changes: 1 addition & 2 deletions tests/test_qualified_electronic_signatures.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ def workflow_run(onfido_api, applicant_id, workflow_id):

@pytest.fixture(scope="function")
def file_id(onfido_api, workflow_run):

task = onfido_api.list_tasks(workflow_run.id)[0]
task = onfido_api.list_tasks(workflow_run.id)[1]

output = repeat_request_until_task_output_changes(
onfido_api.find_task, [workflow_run.id, task.id], max_retries=10, sleep_time=3
Expand Down
88 changes: 71 additions & 17 deletions tests/test_webhook_verification.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,94 @@
from onfido.webhook_event_verifier import WebhookEventVerifier, OnfidoInvalidSignatureError
from onfido import WebhookEvent, WebhookEventPayload, WebhookEventPayloadObject
from onfido.webhook_event_verifier import (
WebhookEventVerifier, OnfidoInvalidSignatureError
)
from onfido import (
WebhookEvent, WebhookEventPayload, WebhookEventPayloadObject,
WebhookEventResourceType, WebhookEventType, WebhookEventObjectStatus,
WebhookEventPayloadResource
)

import pytest

raw_event = ("{\"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\"}}}")
classic_raw_event = ("{\"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\"}}}")

secret_token = "wU99mE6jJ7nXOLFwZ0tJymM1lpI15pZh"
studio_raw_event = ("{\"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}}}")

expected_event = WebhookEvent(
classic_expected_event = WebhookEvent(
payload=WebhookEventPayload(
action="check.completed",
resource_type="check",
action=WebhookEventType.CHECK_DOT_COMPLETED,
resource_type=WebhookEventResourceType.CHECK,
object=WebhookEventPayloadObject(
id='f2302f45-227d-413d-ad61-09ec077a086a',
href='https://api.onfido.com/v3.6/checks/f2302f45-227d-413d-ad61-09ec077a086a',
status="complete",
status=WebhookEventObjectStatus.COMPLETE,
completed_at_iso8601='2024-04-04T09:21:21Z'
)
)
)

verifier = WebhookEventVerifier(secret_token)
studio_expected_event = WebhookEvent(
payload=WebhookEventPayload(
action=WebhookEventType.WORKFLOW_TASK_DOT_STARTED,
resource_type=WebhookEventResourceType.WORKFLOW_TASK,
object=WebhookEventPayloadObject(
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=WebhookEventPayloadResource(
created_at='2024-07-10T12:49:09Z',
id='profile_1eb92',
input={},
output=None,
task_def_id='profile_data',
task_def_version=None,
updated_at='2024-07-10T12:49:09Z',
workflow_run_id='bc77c6e5-753a-4580-96a6-aaed3e5a8d19'
)
)
)


classic_verifier = WebhookEventVerifier("wU99mE6jJ7nXOLFwZ0tJymM1lpI15pZh")
studio_verifier = WebhookEventVerifier("YKOC6mkBxi6yK2zlUIrLMvsJMFEZObP5")

def test_webhook_verification():

def test_classic_webhook_verification():
signature = "77ebc3e418f26be6eebb47f7ebe551321de26734fc273961e075fc9ab163d9c7"

event = verifier.read_payload(raw_event, signature)
assert event == expected_event
event = classic_verifier.read_payload(classic_raw_event, signature)
assert event == classic_expected_event


def test_webhook_verification_invalid_signature():
def test_classic_webhook_verification_invalid_signature():
signature = "77ebc3e418f26be6eebb47f7ebe551321de26734fc273961e075fc9ab163d9c8"

with pytest.raises(OnfidoInvalidSignatureError):
verifier.read_payload(raw_event, signature)
classic_verifier.read_payload(classic_raw_event, signature)


def test_studio_webhook_verification():
signature = "c95a5b785484f6fa1bc25f381b5595d66bf85cb442eefb06aa007802ee6a4dfa"

event = studio_verifier.read_payload(studio_raw_event, signature)
event.payload.object.additional_properties = {} # Suppress any additional property in object
assert event == studio_expected_event


def test_studio_webhook_verification_invalid_signature():
signature = "c95a5b785484f6fa1bc25f381b5595d66bf85cb442eefb06aa007802ee6a4dfb"

with pytest.raises(OnfidoInvalidSignatureError):
studio_verifier.read_payload(studio_raw_event, signature)
10 changes: 5 additions & 5 deletions tests/test_workflow_runs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from onfido import TimelineFileReference, WorkflowRun, WorkflowRunBuilder
from onfido import TimelineFileReference, WorkflowRun, WorkflowRunBuilder, WorkflowRunStatus
from tests.conftest import (
create_applicant,
create_workflow_run,
Expand Down Expand Up @@ -30,7 +30,7 @@ def test_create_workflow_run(workflow_run, workflow_id):
assert workflow_run is not None
assert isinstance(workflow_run, WorkflowRun)
assert workflow_run.workflow_id == workflow_id
assert workflow_run.status == "awaiting_input"
assert workflow_run.status == WorkflowRunStatus.AWAITING_INPUT


def test_create_workflow_run_with_custom_inputs(onfido_api, applicant_id):
Expand All @@ -46,7 +46,7 @@ def test_create_workflow_run_with_custom_inputs(onfido_api, applicant_id):
)
assert isinstance(workflow_run, WorkflowRun)
assert workflow_run.workflow_id == workflow_id
assert workflow_run.status == "approved"
assert workflow_run.status == WorkflowRunStatus.APPROVED


def test_list_workflow_runs(onfido_api):
Expand Down Expand Up @@ -76,7 +76,7 @@ def test_generate_timeline_file(onfido_api, applicant_id):
onfido_api, applicant_id=applicant_id, workflow_id=workflow_id
).id
repeat_request_until_status_changes(
onfido_api.find_workflow_run, [workflow_run_id], "approved"
onfido_api.find_workflow_run, [workflow_run_id], WorkflowRunStatus.APPROVED
)

workflow_timeline_file_data = onfido_api.create_timeline_file(workflow_run_id)
Expand All @@ -92,7 +92,7 @@ def test_find_timeline_file(onfido_api, applicant_id):
onfido_api, applicant_id=applicant_id, workflow_id=workflow_id
).id
repeat_request_until_status_changes(
onfido_api.find_workflow_run, [workflow_run_id], "approved"
onfido_api.find_workflow_run, [workflow_run_id], WorkflowRunStatus.APPROVED
)

timeline_file_id = onfido_api.create_timeline_file(
Expand Down

0 comments on commit 433ebd9

Please sign in to comment.