From 433ebd91bac46b2afdeddce0a8ee85af93a59861 Mon Sep 17 00:00:00 2001 From: Davide Vacca Date: Mon, 21 Oct 2024 14:29:23 +0200 Subject: [PATCH] Add schedule to CI, fix and improve tests after library refresh --- .github/workflows/python.yml | 2 + tests/conftest.py | 7 +- tests/test_applicants.py | 6 ++ tests/test_checks.py | 5 +- tests/test_documents.py | 4 +- tests/test_qualified_electronic_signatures.py | 3 +- tests/test_webhook_verification.py | 88 +++++++++++++++---- tests/test_workflow_runs.py | 10 +-- 8 files changed, 94 insertions(+), 31 deletions(-) diff --git a/.github/workflows/python.yml b/.github/workflows/python.yml index 7dc6819..c31ffa1 100644 --- a/.github/workflows/python.yml +++ b/.github/workflows/python.yml @@ -16,6 +16,8 @@ on: release: types: - published + schedule: + - cron: "0 14 * * 0" # Every Sunday, 2 hours after midday jobs: integration-tests: diff --git a/tests/conftest.py b/tests/conftest.py index e1a34ee..ee04ef7 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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", ) @@ -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, ) ) @@ -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 diff --git a/tests/test_applicants.py b/tests/test_applicants.py index 7c07610..d76b00d 100644 --- a/tests/test_applicants.py +++ b/tests/test_applicants.py @@ -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) diff --git a/tests/test_checks.py b/tests/test_checks.py index 55553d9..d264cb6 100644 --- a/tests/test_checks.py +++ b/tests/test_checks.py @@ -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 @@ -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): diff --git a/tests/test_documents.py b/tests/test_documents.py index 934102b..1696f87 100644 --- a/tests/test_documents.py +++ b/tests/test_documents.py @@ -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 @@ -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) diff --git a/tests/test_qualified_electronic_signatures.py b/tests/test_qualified_electronic_signatures.py index 44d9c85..5fad1fc 100644 --- a/tests/test_qualified_electronic_signatures.py +++ b/tests/test_qualified_electronic_signatures.py @@ -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 diff --git a/tests/test_webhook_verification.py b/tests/test_webhook_verification.py index d11f5fc..d531272 100644 --- a/tests/test_webhook_verification.py +++ b/tests/test_webhook_verification.py @@ -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) diff --git a/tests/test_workflow_runs.py b/tests/test_workflow_runs.py index d18334a..29403f5 100644 --- a/tests/test_workflow_runs.py +++ b/tests/test_workflow_runs.py @@ -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, @@ -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): @@ -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): @@ -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) @@ -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(