Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add further workflow run support - create, find, and all #47

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions onfido/resources/workflow_runs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,16 @@


class WorkflowRuns(Resource):
def create(self, request_body: dict):
return self._post("workflow_runs/", **request_body)

def all(self, **user_payload: dict):
payload = {"page": 1, "sort": "desc"}
payload.update(user_payload)
return self._get("workflow_runs", payload=payload)

def find(self, workflow_run_id: str):
return self._get(f"workflow_runs/{workflow_run_id}")

def evidence(self, workflow_run_id: str):
return self._download_request(f"workflow_runs/{workflow_run_id}/signed_evidence_file")
11 changes: 11 additions & 0 deletions onfido/resources_aio/workflow_runs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,16 @@


class WorkflowRuns(Resource):
def create(self, request_body: dict):
return self._post("workflow_runs/", **request_body)

def all(self, **user_payload: dict):
payload = {"page": 1, "sort": "desc"}
payload.update(user_payload)
return self._get("workflow_runs", payload=payload)

def find(self, workflow_run_id: str):
return self._get(f"workflow_runs/{workflow_run_id}")

def evidence(self, workflow_run_id: str):
return self._download_request(f"workflow_runs/{workflow_run_id}/signed_evidence_file")
44 changes: 43 additions & 1 deletion tests/test_workflow_runs.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,53 @@
import onfido
from onfido.regions import Region

from urllib.parse import urlparse, parse_qs

api = onfido.Api("<AN_API_TOKEN>", region=Region.EU)

workflow_run_details = {
"applicant_id": "12345",
"completed_redirect_url": "https://<completed URL>",
"expired_redirect_url": "https://<expired URL>",
"expires_at": "2023-11-17T16:39:04Z",
"language": "en_US"
}

fake_uuid = "58a9c6d2-8661-4dbd-96dc-b9b9d344a7ce"

def test_create_workflow_run(requests_mock):
mock_create = requests_mock.post("https://api.eu.onfido.com/v3.6/workflow_runs/", json=[])
api.workflowrun.create(workflow_run_details)
assert mock_create.called is True

def test_list_workflow_runs(requests_mock):
mock_list = requests_mock.get("https://api.eu.onfido.com/v3.6/workflow_runs", json=[])
api.workflowrun.all(
page=2,
status="approved,declined",
tags="dummy_tag1,dummy_tag2",
created_at_gt="2023-11-17T16:39:04Z",
created_at_lt="2008-02-29T02:56:37Z",
sort='asc'
)
history = mock_list.request_history
assert mock_list.called is True
assert history[0].method == 'GET'
query_params = urlparse(history[0].url).query
assert history[0].url == "https://api.eu.onfido.com/v3.6/workflow_runs?" + query_params
assert parse_qs(query_params) == {
"page": ['2'],
"status": ["approved,declined"],
"tags": ["dummy_tag1,dummy_tag2"],
"created_at_gt": ["2023-11-17T16:39:04Z"],
"created_at_lt": ["2008-02-29T02:56:37Z"],
"sort": ['asc']
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are other tests simply compare the fully-formed url with query parameters, such as:

assert history[0].url == "https://api.eu.onfido.com/v3.6/applicants?include_deleted=True&per_page=5&page=2"

in test_applicants.py > test_list_applicants.

However, this is a slightly different case because there are more params, and there are url-escaped values such as commas in properties like tags and status:

tags (optional): a list of comma separated tags to filter the results.
(docs)

Comparing the parsed url query params as a dict might be more readable and maintainable than something like:

assert history[0].url == "https://api.eu.onfido.com/v3.6/workflow_runs?page=2&sort=asc&status=approved%2Cdeclined&tags=dummy_tag1%2Cdummy_tag2&created_at_gt=2023-11-17T16%3A39%3A04Z&created_at_lt=2008-02-29T02%3A56%3A37Z


def test_find_workflow_run(requests_mock):
mock_find = requests_mock.get(f"https://api.eu.onfido.com/v3.6/workflow_runs/{fake_uuid}", json=[])
api.workflowrun.find(fake_uuid)
assert mock_find.called is True

def test_evidence_workflowrun(requests_mock):
mock_download = requests_mock.get(f"https://api.eu.onfido.com/v3.6/workflow_runs/{fake_uuid}/signed_evidence_file", text="FAKE PDF BINARY", headers={"Content-type": "application/pdf"})
onfido_download = api.workflowrun.evidence(fake_uuid)
Expand Down
Loading