diff --git a/spec/integrations/address_picker_spec.rb b/spec/integrations/address_picker_spec.rb new file mode 100644 index 0000000..2d930c0 --- /dev/null +++ b/spec/integrations/address_picker_spec.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +require_relative '../shared_contexts/with_onfido' + +describe Onfido::Address do + describe 'Address picker' do + include_context 'with onfido' + + it 'finds an address based on post code' do + post_code = 'S2 2DF' + addresses_with_post_code = onfido_api.find_addresses(post_code).addresses + + expect(addresses_with_post_code[0].postcode).to eq post_code + end + end +end diff --git a/spec/integrations/applicant_spec.rb b/spec/integrations/applicant_spec.rb index 0db55f2..3c04b8b 100644 --- a/spec/integrations/applicant_spec.rb +++ b/spec/integrations/applicant_spec.rb @@ -1,46 +1,35 @@ # frozen_string_literal: true -require 'onfido' -Onfido.configure do |config| - config.api_token = ENV["ONFIDO_API_TOKEN"] - config.debugging = true - config.region = config.region[:EU] -end +require_relative '../shared_contexts/with_applicant' describe Onfido::Applicant do + describe 'Applicants' do + include_context 'with applicant' - let(:applicant_id) { '61f659cb-c90b-4067-808a-6136b5c01351' } - let(:params) do - { - 'title' => 'Mr', - 'first_name' => 'Chandler', - 'last_name' => 'Bing', - 'middle_name' => 'Muriel', - 'dob' => '1968-04-08', - 'email' => 'chandler_bing_6@friends.com', - 'address' => { - 'flat_number' => '4', - 'building_number' => '100', - 'building_name' => 'Awesome Building', - 'street' => 'Main Street', - 'sub_street' => 'A sub street', - 'town' => 'London', - 'postcode' => 'SW4 6EH', - 'country' => 'GBR' - }, - 'location' => { - 'ip_address' => '127.0.0.1', - 'country_of_residence' => 'GBR' + it 'creates an applicant' do + params = { + 'title' => 'Mr', + 'first_name' => 'Chandler', + 'last_name' => 'Bing', + 'middle_name' => 'Muriel', + 'dob' => '1968-04-08', + 'email' => 'chandler_bing_6@friends.com', + 'address' => { + 'flat_number' => '4', + 'building_number' => '100', + 'building_name' => 'Awesome Building', + 'street' => 'Main Street', + 'sub_street' => 'A sub street', + 'town' => 'London', + 'postcode' => 'SW4 6EH', + 'country' => 'GBR' + }, + 'location' => { + 'ip_address' => '127.0.0.1', + 'country_of_residence' => 'GBR' + } } - } - end - let(:onfido_api) do - Onfido::DefaultApi.new - end - - describe '#create' do - it 'creates an applicant' do applicant = onfido_api.create_applicant(params) expect(applicant.id).not_to be_nil expect(applicant.first_name).to eq 'Chandler' @@ -58,5 +47,48 @@ expect(applicant.location.ip_address).to eq '127.0.0.1' expect(applicant.location.country_of_residence).to eq 'GBR' end + + it 'lists applicants' do + list_of_applicants = onfido_api.list_applicants() + + expect(list_of_applicants).to be_an_instance_of Onfido::ApplicantsList + expect(list_of_applicants.applicants.size).to be > 0 + end + + it 'finds an applicant' do + applicant = onfido_api.find_applicant(applicant_id) + + expect(applicant).to be_an_instance_of Onfido::Applicant + expect(applicant.id).to eq applicant_id + expect(applicant.first_name).to eq 'Test' + expect(applicant.last_name).to eq 'Applicant' + end + + it 'updates an applicant' do + new_applicant_data = Onfido::ApplicantUpdater.new( + first_name: 'Jane', + last_name: 'Doe', + dob: '1968-04-08' + ) + updated_applicant = onfido_api.update_applicant(applicant_id, new_applicant_data) + expect(updated_applicant.first_name).to eq 'Jane' + expect(updated_applicant.last_name).to eq 'Doe' + expect(updated_applicant.dob).to eq Date.parse('1968-04-08') + end + + it 'deletes an applicant' do + onfido_api.delete_applicant(applicant_id) + + expect { + onfido_api.find_applicant(applicant_id) + }.to raise_error(Onfido::ApiError) { |e| + expect(e.code).to eq(410) + } + end + + it 'restores an applicant' do + onfido_api.delete_applicant(applicant_id) + onfido_api.restore_applicant(applicant_id) + end end -end \ No newline at end of file +end diff --git a/spec/integrations/check_spec.rb b/spec/integrations/check_spec.rb new file mode 100644 index 0000000..ca3823c --- /dev/null +++ b/spec/integrations/check_spec.rb @@ -0,0 +1,83 @@ +# frozen_string_literal: true + +require_relative '../shared_contexts/with_check' + +describe Onfido::Check do + describe 'Checks' do + include_context 'with check' + + it 'creates a check' do + expect(check).not_to be_nil + expect(check).to be_an_instance_of Onfido::Check + expect(check.applicant_id).to eq applicant_id + expect(check.report_ids.size).to eq 2 + expect(check.status).to eq 'in_progress' + end + + context 'consider check' do + let(:check_builder) do + check_builder = Onfido::CheckBuilder.new({ + applicant_id: applicant_id, + document_ids: [document_id], + report_names: [Onfido::ReportName::DOCUMENT], + consider: [Onfido::ReportName::DOCUMENT], + }) + end + + it 'creates a consider check' do + expect(check).not_to be_nil + expect(check).to be_an_instance_of Onfido::Check + expect(check.applicant_id).to eq applicant_id + end + end + + context 'US driving licence check' do + let(:us_driving_licence_builder) do + Onfido::UsDrivingLicenceBuilder.new({ + id_number: '12345', + issue_state: 'GA', + }) + end + + let(:check_builder) do + Onfido::CheckBuilder.new({ + applicant_id: applicant_id, + document_ids: [document_id], + report_names: [Onfido::ReportName::DOCUMENT], + us_driving_licence: us_driving_licence_builder, + }) + end + + it 'creates a driving licence check' do + expect(check).not_to be_nil + expect(check).to be_an_instance_of Onfido::Check + expect(check.applicant_id).to eq applicant_id + end + end + + it 'lists checks' do + list_of_checks = onfido_api.list_checks(applicant_id) + + expect(list_of_checks).not_to be_nil + expect(list_of_checks).to be_an_instance_of Onfido::ChecksList + expect(list_of_checks.checks.size).to be > 0 + end + + it 'finds a check' do + get_check = onfido_api.find_check(check_id) + + expect(get_check).to be_an_instance_of(Onfido::Check) + expect(get_check.id).to eq check_id + end + + it 'restarts a check' do + onfido_api.resume_check(check_id) + end + + it 'downloads a check' do + file = onfido_api.download_check(check_id) + + expect(file.size).to be > 0 + end + end +end diff --git a/spec/integrations/document_spec.rb b/spec/integrations/document_spec.rb new file mode 100644 index 0000000..1ca9692 --- /dev/null +++ b/spec/integrations/document_spec.rb @@ -0,0 +1,46 @@ +# frozen_string_literal: true + +require_relative '../shared_contexts/with_document' + +describe Onfido::Document do + describe 'Documents' do + include_context 'with document' + + it 'uploads a document' do + expect(document).not_to be_nil + expect(document.id).not_to be_empty + expect(document.file_name).to eq('sample_driving_licence.png') + expect(document).to be_an_instance_of(Onfido::Document) + end + + it 'lists documents' do + list_of_documents = onfido_api.list_documents(applicant_id) + + expect(list_of_documents).to be_an_instance_of(Onfido::DocumentsList) + expect(list_of_documents.documents.size).to be > 0 + end + + it 'finds a document' do + get_document = onfido_api.find_document(document_id) + + expect(get_document).to be_an_instance_of(Onfido::Document) + expect(get_document.id).to eq document_id + end + + it 'downloads a document' do + file = onfido_api.download_document(document_id) + + expect(file.size).to be > 0 + end + + it 'cannot download an inexistent document' do + inexistent_document_id = '00000000-0000-0000-0000-000000000000' + + expect { + onfido_api.download_document(inexistent_document_id) + }.to raise_error(Onfido::ApiError) { |e| + expect(e.code).to eq(404) + } + end + end +end diff --git a/spec/integrations/extraction_spec.rb b/spec/integrations/extraction_spec.rb new file mode 100644 index 0000000..d7dd290 --- /dev/null +++ b/spec/integrations/extraction_spec.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +require_relative '../shared_contexts/with_document' + +describe Onfido::Extraction do + describe 'Extraction' do + include_context 'with document' + + it 'performs extraction on a document' do + extraction = onfido_api.extract( + Onfido::ExtractRequest.new({ + 'document_id': document_id + }) + ) + + expect(extraction).not_to be_nil + expect(extraction).to be_an_instance_of Onfido::Extraction + expect(extraction.document_id).to eq document_id + + document_classification = extraction.document_classification + extracted_data = extraction.extracted_data + + # Check response body: document classification + expect(document_classification).not_to be_nil + expect(document_classification.document_type).to eq Onfido::DocumentTypes::DRIVING_LICENCE + expect(document_classification.issuing_country).to eq Onfido::CountryCodes::GBR + + # Check response body: extracted data + expect(extracted_data).not_to be_nil + expect(extracted_data.date_of_birth).to eq Date.parse('1976-03-11') + expect(extracted_data.date_of_expiry).to eq Date.parse('2031-05-28') + expect(extracted_data.document_number).to eq '200407512345' + expect(extracted_data.first_name).to eq 'SARAH' + expect(extracted_data.last_name).to eq 'MORGAN' + expect(extracted_data.gender).to eq 'Female' + end + end +end diff --git a/spec/integrations/id_photo_spec.rb b/spec/integrations/id_photo_spec.rb new file mode 100644 index 0000000..5b3f4d6 --- /dev/null +++ b/spec/integrations/id_photo_spec.rb @@ -0,0 +1,55 @@ +# frozen_string_literal: true + +require_relative '../shared_contexts/with_applicant' + +describe Onfido::IdPhoto do + describe 'Id Photo' do + include_context 'with applicant' + + let(:id_photo) do + onfido_api.upload_id_photo( + applicant_id: applicant_id, + file: File.new('spec/integrations/media/sample_photo.png') + ) + end + let!(:id_photo_id) { id_photo.id } + + it 'uploads a Id photo' do + expect(id_photo).not_to be_nil + expect(id_photo_id).not_to be_empty + expect(id_photo).to be_an_instance_of Onfido::IdPhoto + expect(id_photo.file_name).to eq "#{id_photo_id}.png" + end + + it 'lists id photos' do + id_photos = onfido_api.list_id_photos(applicant_id) + + expect(id_photos.id_photos.length).to be > 0 + expect(id_photos).to be_an_instance_of Onfido::IDPhotosList + end + + it 'retrieves id photo' do + get_id_photo = onfido_api.find_id_photo(id_photo_id) + + expect(get_id_photo).to be_an_instance_of Onfido::IdPhoto + expect(get_id_photo.id).to eq id_photo_id + end + + it 'downloads id photo' do + file = onfido_api.download_id_photo(id_photo_id) + + expect(file.length).to be > 0 + end + + it 'raises an error with the correct status code when trying to download an inexistent id photo' do + inexistent_id_photo_id = '00000000-0000-0000-0000-000000000000' + + expect { + onfido_api.download_id_photo(inexistent_id_photo_id) + }.to raise_error(Onfido::ApiError) { |e| + expect(e.message).to match(/the server returns an error/) + expect(e.code).to eq(404) + } + end + end +end diff --git a/spec/integrations/live_photo_spec.rb b/spec/integrations/live_photo_spec.rb new file mode 100644 index 0000000..de152af --- /dev/null +++ b/spec/integrations/live_photo_spec.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +require_relative '../shared_contexts/with_live_photo' + +describe Onfido::LivePhoto do + describe 'Live Photo' do + include_context 'with live photo' + + it 'uploads a live photo' do + expect(live_photo).not_to be_nil + expect(live_photo_id).not_to be_empty + expect(live_photo.file_name).to eq('sample_photo.png') + expect(live_photo).to be_an_instance_of Onfido::LivePhoto + end + + it 'lists live photos' do + live_photos = onfido_api.list_live_photos(applicant_id) + + expect(live_photos.live_photos.length).to be > 0 + expect(live_photos).to be_an_instance_of Onfido::LivePhotosList + end + + it 'retrieves live photo' do + live_photo = onfido_api.find_live_photo(live_photo_id) + + expect(live_photo_id).to eq(live_photo_id) + expect(live_photo).to be_an_instance_of Onfido::LivePhoto + end + + it 'downloads live photo' do + file = onfido_api.download_live_photo(live_photo_id) + + expect(file.length).to be > 0 + end + + it 'raises an error with the correct status code when trying to download an inexistent live photo' do + inexistent_live_photo_id = '00000000-0000-0000-0000-000000000000' + + expect { + onfido_api.download_live_photo(inexistent_live_photo_id) + }.to raise_error(Onfido::ApiError) { |e| + expect(e.message).to match(/the server returns an error/) + expect(e.code).to eq(404) + } + end + end +end diff --git a/spec/integrations/live_video_spec.rb b/spec/integrations/live_video_spec.rb new file mode 100644 index 0000000..b667ee1 --- /dev/null +++ b/spec/integrations/live_video_spec.rb @@ -0,0 +1,50 @@ +# frozen_string_literal: true + +require_relative '../shared_contexts/with_onfido' + +describe Onfido::LiveVideo do + describe 'Live Video' do + include_context 'with onfido' + + let(:applicant_id) { ENV['ONFIDO_SAMPLE_APPLICANT_ID'] } + let(:live_video_id) { ENV['ONFIDO_SAMPLE_VIDEO_ID_1'] } + + it 'lists live videos' do + live_videos = onfido_api.list_live_videos(applicant_id) + + expect(live_videos.live_videos.length).to be > 0 + expect(live_videos).to be_an_instance_of Onfido::LiveVideosList + expect(live_videos.live_videos.first).to be_an_instance_of Onfido::LiveVideo + end + + it 'retrieves live videos' do + get_live_video = onfido_api.find_live_video(live_video_id) + + expect(get_live_video.id).to eq(live_video_id) + expect(get_live_video).to be_an_instance_of Onfido::LiveVideo + end + + it 'downloads live video' do + file = onfido_api.download_live_video(live_video_id) + + expect(file.length).to be > 0 + end + + it 'downloads live video frame' do + file = onfido_api.download_live_video_frame(live_video_id) + + expect(file.length).to be > 0 + end + + it 'raises an error with the correct status code when trying to download an inexistent live video' do + inexistent_live_video_id = '00000000-0000-0000-0000-000000000000' + + expect { + onfido_api.download_live_video(inexistent_live_video_id) + }.to raise_error(Onfido::ApiError) { |e| + expect(e.message).to match(/the server returns an error/) + expect(e.code).to eq(404) + } + end + end +end diff --git a/spec/integrations/media/sample_driving_licence.png b/spec/integrations/media/sample_driving_licence.png new file mode 100644 index 0000000..07bb4fe Binary files /dev/null and b/spec/integrations/media/sample_driving_licence.png differ diff --git a/spec/integrations/media/sample_photo.png b/spec/integrations/media/sample_photo.png new file mode 100644 index 0000000..240349f Binary files /dev/null and b/spec/integrations/media/sample_photo.png differ diff --git a/spec/integrations/motion_capture_spec.rb b/spec/integrations/motion_capture_spec.rb new file mode 100644 index 0000000..0f29e2b --- /dev/null +++ b/spec/integrations/motion_capture_spec.rb @@ -0,0 +1,50 @@ +# frozen_string_literal: true + +require_relative '../shared_contexts/with_onfido' + +describe Onfido::MotionCapture do + describe 'Motion Capture' do + include_context 'with onfido' + + let(:applicant_id) { ENV['ONFIDO_SAMPLE_APPLICANT_ID'] } + let(:motion_id) { ENV['ONFIDO_SAMPLE_MOTION_ID_1'] } + + it 'lists motion captures' do + motion_captures = onfido_api.list_motion_captures(applicant_id) + + expect(motion_captures.motion_captures.length).to be > 0 + expect(motion_captures).to be_an_instance_of Onfido::MotionCapturesList + expect(motion_captures.motion_captures.first).to be_an_instance_of Onfido::MotionCapture + end + + it 'retrieves motion capture' do + get_motion_capture = onfido_api.find_motion_capture(motion_id) + + expect(get_motion_capture.id).to eq(motion_id) + expect(get_motion_capture).to be_an_instance_of Onfido::MotionCapture + end + + it 'downloads motion capture' do + file = onfido_api.download_motion_capture(motion_id) + + expect(file.length).to be > 0 + end + + it 'downloads motion capture frame' do + file = onfido_api.download_motion_capture_frame(motion_id) + + expect(file.length).to be > 0 + end + + it 'raises an error with the correct status code when trying to download an inexistent motion capture' do + inexistent_motion_id = '00000000-0000-0000-0000-000000000000' + + expect { + onfido_api.download_motion_capture(inexistent_motion_id) + }.to raise_error(Onfido::ApiError) { |e| + expect(e.message).to match(/the server returns an error/) + expect(e.code).to eq(404) + } + end + end +end diff --git a/spec/integrations/report_schema_spec.rb b/spec/integrations/report_schema_spec.rb new file mode 100644 index 0000000..e8ba7b1 --- /dev/null +++ b/spec/integrations/report_schema_spec.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +require_relative '../shared_contexts/with_document' +require_relative '../shared_contexts/with_live_photo' + +describe Onfido::Report do + describe 'Report schemas' do + include_context 'with document' + include_context 'with live photo' + + it 'schema of Document report is valid' do + document_report_id = onfido_api.create_check( + Onfido::CheckBuilder.new( + applicant_id: applicant_id, + document_ids: [document_id], + report_names: [Onfido::ReportName::DOCUMENT], + )).report_ids[0] + + document_report = repeat_request_until_status_changes( + Onfido::ReportStatus::COMPLETE + ) { onfido_api.find_report(document_report_id) } + + expect(document_report).to be_an_instance_of Onfido::DocumentReport + end + + it 'schema of Facial Similarity report is valid' do + facial_similarity_report_id = onfido_api.create_check( + Onfido::CheckBuilder.new( + applicant_id: applicant_id, + document_ids: [document_id], + report_names: [Onfido::ReportName::FACIAL_SIMILARITY_PHOTO], + )).report_ids[0] + + facial_similarity_report = repeat_request_until_status_changes( + Onfido::ReportStatus::COMPLETE + ) { onfido_api.find_report(facial_similarity_report_id) } + + expect(facial_similarity_report).to be_an_instance_of Onfido::FacialSimilarityPhotoReport + end + end +end diff --git a/spec/integrations/report_spec.rb b/spec/integrations/report_spec.rb new file mode 100644 index 0000000..2c2054d --- /dev/null +++ b/spec/integrations/report_spec.rb @@ -0,0 +1,50 @@ +# frozen_string_literal: true + +require_relative '../shared_contexts/with_check' + +describe Onfido::Report do + describe 'Report' do + include_context 'with check' + + let(:sorted_reports) do + sort_reports(onfido_api.list_reports(check.id).reports) + end + let(:document_report_id) { sorted_reports[0].id } + let(:identity_report_id) { sorted_reports[1].id } + + + def sort_reports(reports_list) + reports_list.sort_by { |report| report.name } + end + + it 'lists reports' do + expect(sorted_reports[0].name).to eq Onfido::ReportName::DOCUMENT + expect(sorted_reports[1].name).to eq Onfido::ReportName::IDENTITY_ENHANCED + end + + it 'finds a report' do + get_document_report = onfido_api.find_report(document_report_id) + get_identity_report = onfido_api.find_report(identity_report_id) + + expect(get_document_report).to be_an_instance_of Onfido::DocumentReport + expect(get_document_report.id).to eq document_report_id + expect(get_document_report.name).to eq Onfido::ReportName::DOCUMENT + expect(get_document_report.status).to eq Onfido::ReportStatus::AWAITING_DATA + + expect(get_identity_report).to be_an_instance_of Onfido::IdentityEnhancedReport + expect(get_identity_report.id).to eq identity_report_id + expect(get_identity_report.name).to eq Onfido::ReportName::IDENTITY_ENHANCED + expect(get_identity_report.status).to eq Onfido::ReportStatus::COMPLETE + expect(get_identity_report.breakdown.date_of_birth).to be_nil + expect(get_identity_report.breakdown.address).to be_nil + end + + it 'resumes a report' do + onfido_api.resume_report(document_report_id) + end + + it 'cancels a report' do + onfido_api.cancel_report(document_report_id) + end + end +end diff --git a/spec/integrations/sdk_token_spec.rb b/spec/integrations/sdk_token_spec.rb new file mode 100644 index 0000000..035e310 --- /dev/null +++ b/spec/integrations/sdk_token_spec.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +require_relative '../shared_contexts/with_applicant' + +describe Onfido::SdkToken do + describe 'SDK token' do + include_context 'with applicant' + + it 'generates a SDK token' do + token_builder = Onfido::SdkTokenBuilder.new({ + applicant_id: applicant_id, + referrer: 'https://*.example.com/example_page/*', + }) + token = onfido_api.generate_sdk_token(token_builder) + + expect(token).to be_an_instance_of Onfido::SdkToken + expect(token.token.size).to be > 0 + end + end +end diff --git a/spec/integrations/tasks_spec.rb b/spec/integrations/tasks_spec.rb new file mode 100644 index 0000000..a936c12 --- /dev/null +++ b/spec/integrations/tasks_spec.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +require_relative '../shared_contexts/with_workflow_run' + +describe Onfido::Task do + describe 'Tasks' do + include_context 'with workflow run' + + it 'lists tasks' do + list_of_tasks = onfido_api.list_tasks(workflow_run_id) + + expect(list_of_tasks).to_not be_nil + expect(list_of_tasks[0]).to be_an_instance_of Onfido::TaskItem + expect(list_of_tasks.size).to eq 2 + end + + it 'finds a task' do + task = onfido_api.list_tasks(workflow_run_id)[0] + get_task = onfido_api.find_task(workflow_run_id, task.id) + + expect(get_task).to_not be_nil + expect(get_task).to be_an_instance_of Onfido::Task + expect(get_task.id).to eq task.id + expect(get_task.task_def_id).to eq task.task_def_id + end + + it 'completes a task' do + tasks = onfido_api.list_tasks(workflow_run_id) + profile_data_task_id = (tasks.select { |task| task.id.include? 'profile' })[0].id + + complete_task_builder = Onfido::CompleteTaskBuilder.new({ + data: Onfido::CompleteTaskDataBuilder.build({ + first_name: 'Jane', + last_name: 'Doe', + }) + }) + + onfido_api.complete_task(workflow_run_id, profile_data_task_id, complete_task_builder) + task_outputs = onfido_api.find_task(workflow_run_id, profile_data_task_id).output + + expect(task_outputs[:first_name]).to eq 'Jane' + expect(task_outputs[:last_name]).to eq 'Doe' + end + end +end diff --git a/spec/integrations/watchlist_monitor_spec.rb b/spec/integrations/watchlist_monitor_spec.rb new file mode 100644 index 0000000..6dbfd2a --- /dev/null +++ b/spec/integrations/watchlist_monitor_spec.rb @@ -0,0 +1,73 @@ +# frozen_string_literal: true + +require_relative '../shared_contexts/with_applicant' + +describe Onfido::WatchlistMonitor do + describe 'Watchlist monitor' do + include_context 'with applicant' + + let(:watchlist_monitor_builder) do + Onfido::WatchlistMonitorBuilder.new({ + applicant_id: applicant_id, + report_name: Onfido::ReportName::WATCHLIST_STANDARD + }) + end + let(:watchlist_monitor) do + onfido_api.create_watchlist_monitor(watchlist_monitor_builder) + end + let!(:watchlist_monitor_id) { watchlist_monitor.id } + + + it 'creates a watchlist standard monitor' do + expect(watchlist_monitor).to be_an_instance_of Onfido::WatchlistMonitor + expect(watchlist_monitor.applicant_id).to eq applicant_id + expect(watchlist_monitor.report_name).to eq Onfido::ReportName::WATCHLIST_STANDARD + end + + context 'AML monitor' do + let(:watchlist_monitor_builder) do + Onfido::WatchlistMonitorBuilder.new({ + applicant_id: applicant_id, + report_name: Onfido::ReportName::WATCHLIST_AML + }) + end + + it 'creates a watchlist AML monitor' do + expect(watchlist_monitor).to be_an_instance_of Onfido::WatchlistMonitor + expect(watchlist_monitor.applicant_id).to eq applicant_id + expect(watchlist_monitor.report_name).to eq Onfido::ReportName::WATCHLIST_AML + end + end + + it 'lists watchlist monitors' do + list_of_monitors = onfido_api.list_watchlist_monitors(applicant_id) + + expect(list_of_monitors).to be_an_instance_of Onfido::WatchlistMonitorsList + expect(list_of_monitors.monitors.size).to be > 0 + end + + it 'finds a watchlist monitor' do + get_watchlist_monitor = onfido_api.find_watchlist_monitor(watchlist_monitor_id) + + expect(get_watchlist_monitor.id).to eq watchlist_monitor_id + end + + it 'deletes a watchlist monitor' do + onfido_api.delete_watchlist_monitor(watchlist_monitor_id) + end + + it 'lists watchlist monitor matches' do + matches_list = onfido_api.list_watchlist_monitor_matches(watchlist_monitor_id) + + expect(matches_list).to be_an_instance_of Onfido::WatchlistMonitorMatchesList + expect(matches_list.matches.size).to eq 0 + end + + it 'forces report creation' do + onfido_api.force_report_creation_from_watchlist_monitor(watchlist_monitor_id) + + checks = onfido_api.list_checks(applicant_id).checks + expect(checks.size).to eq 2 + end + end +end diff --git a/spec/integrations/webhook_spec.rb b/spec/integrations/webhook_spec.rb new file mode 100644 index 0000000..fccc6fb --- /dev/null +++ b/spec/integrations/webhook_spec.rb @@ -0,0 +1,62 @@ +# frozen_string_literal: true + +require_relative '../shared_contexts/with_onfido' + +describe Onfido::Webhook do + describe 'Webhook' do + include_context 'with onfido' + + let(:webhook_builder) do + Onfido::WebhookBuilder.new({ + url: 'https://example.com', + events: ['check.completed', 'report.completed'], + }) + end + + let(:webhook) { onfido_api.create_webhook(webhook_builder) } + let(:webhook_id) { webhook.id } + + it 'creates a webhook' do + expect(webhook).to be_an_instance_of Onfido::Webhook + expect(webhook_id).to_not be_nil + expect(webhook.url).to eq 'https://example.com' + expect(webhook.events).to eq ['check.completed', 'report.completed'] + end + + it 'updates a webhook' do + webhook_updater = Onfido::WebhookUpdater.new({ + url: 'https://example.co.uk', + events: ['check.completed'], + }) + + updated_webhook = onfido_api.update_webhook(webhook_id, webhook_updater) + expect(updated_webhook.id).to eq webhook_id + expect(updated_webhook.url).to eq 'https://example.co.uk' + expect(updated_webhook.events).to eq ['check.completed'] + end + + it 'lists webhooks' do + list_of_webhooks = onfido_api.list_webhooks() + + expect(list_of_webhooks).to be_an_instance_of Onfido::WebhooksList + expect(list_of_webhooks.webhooks.size).to be > 0 + end + + it 'finds a webhook' do + get_webhook = onfido_api.find_webhook(webhook_id) + + expect(get_webhook).to be_an_instance_of(Onfido::Webhook) + expect(get_webhook.id).to eq webhook_id + end + + it 'deletes a webhook' do + onfido_api.delete_webhook(webhook_id) + + expect { + onfido_api.find_webhook(webhook_id) + }.to raise_error(Onfido::ApiError) { |e| + expect(e.code).to eq(404) + } + end + end +end diff --git a/spec/integrations/workflow_run_output_spec.rb b/spec/integrations/workflow_run_output_spec.rb new file mode 100644 index 0000000..27eb960 --- /dev/null +++ b/spec/integrations/workflow_run_output_spec.rb @@ -0,0 +1,117 @@ +# frozen_string_literal: true + +require_relative '../shared_contexts/with_document' +require_relative '../shared_contexts/with_live_photo' +require_relative '../shared_contexts/with_workflow_run' + +describe Onfido::WorkflowRun do + describe 'Workflow run outputs' do + include_context 'with workflow run' + + context 'workflow with profile data as output' do + let(:workflow_id) { 'd27e510b-27a8-44c3-a3cc-bf4c0648a4ba' } + + let(:profile_data) do + { + country_residence: 'GBR', + first_name: 'First', + last_name: 'Last', + dob: '2000-01-01', + email: 'first.last@gmail.com', + phone_number: '+351911111111', + nationality: 'GBR', + phone_number_consent_granted: true, + address: { + country: 'GBR', + line1: '123rd Street', + line2: '2nd Floor', + line3: '23', + town: 'London', + postcode: 'S2 2DF', + } + } + end + + it 'returns profile data as output' do + tasks = onfido_api.list_tasks(workflow_run_id) + profile_data_task_id = (tasks.select { |task| task.id.include? 'profile' })[0].id + + complete_task_builder = Onfido::CompleteTaskBuilder.new({ + data: Onfido::CompleteTaskDataBuilder.build(profile_data) + }) + onfido_api.complete_task(workflow_run_id, profile_data_task_id, complete_task_builder) + + workflow_run_outputs = onfido_api.find_workflow_run(workflow_run_id).output + + expect(workflow_run_outputs[:profile_capture_data]).to eq profile_data + end + end + + context 'workflow run with document and facial similarity reports as output' do + include_context 'with document' + include_context 'with live photo' + + let(:workflow_id) { '5025d9fd-7842-4805-bce1-a7bfd7131b4e' } + let(:profile_data) { { first_name: 'Jane', last_name: 'Doe' } } + + it 'returns document and facial similarity reports' do + tasks = onfido_api.list_tasks(workflow_run_id) + profile_data_task_id = (tasks.select { |task| task.id.include? 'profile' })[0].id + + complete_profile_task_builder = Onfido::CompleteTaskBuilder.new({ + data: Onfido::CompleteTaskDataBuilder.build(profile_data) + }) + onfido_api.complete_task( + workflow_run_id, + profile_data_task_id, + complete_profile_task_builder, + ) + + tasks = onfido_api.list_tasks(workflow_run_id) + document_capture_task_id = (tasks.select { |task| task.id.include? 'document_photo' })[0].id + + complete_document_capture_task_builder = Onfido::CompleteTaskBuilder.new( + data: Onfido::CompleteTaskDataBuilder.build([{ id: document_id }]) + ) + onfido_api.complete_task( + workflow_run_id, + document_capture_task_id, + complete_document_capture_task_builder, + ) + + tasks = onfido_api.list_tasks(workflow_run_id) + live_photo_capture_task_id = (tasks.select { |task| task.id.include? 'face_photo' })[0].id + + complete_live_photo_capture_task_request = Onfido::CompleteTaskBuilder.new( + data: Onfido::CompleteTaskDataBuilder.build([{ id: live_photo_id }]) + ) + onfido_api.complete_task( + workflow_run_id, + live_photo_capture_task_id, + complete_live_photo_capture_task_request, + ) + + workflow_run = repeat_request_until_status_changes('approved') do + onfido_api.find_workflow_run(workflow_run_id) + end + + document_report_output = workflow_run.output[:doc] + facial_similarity_report_output = workflow_run.output[:selfie] + + expect(document_report_output[:breakdown]).to_not be_nil + expect(document_report_output[:properties]).to_not be_nil + expect(document_report_output[:repeat_attempts]).to_not be_nil + expect(document_report_output[:result]).to_not be_nil + expect(document_report_output[:status]).to_not be_nil + expect(document_report_output[:sub_result]).to_not be_nil + expect(document_report_output[:uuid]).to_not be_nil + + expect(facial_similarity_report_output[:breakdown]).to_not be_nil + expect(facial_similarity_report_output[:properties]).to_not be_nil + expect(facial_similarity_report_output[:result]).to_not be_nil + expect(facial_similarity_report_output[:status]).to_not be_nil + expect(facial_similarity_report_output[:uuid]).to_not be_nil + end + end + end +end diff --git a/spec/integrations/workflow_run_spec.rb b/spec/integrations/workflow_run_spec.rb new file mode 100644 index 0000000..2688f67 --- /dev/null +++ b/spec/integrations/workflow_run_spec.rb @@ -0,0 +1,84 @@ +# frozen_string_literal: true + +require_relative '../shared_contexts/with_workflow_run' + +describe Onfido::WorkflowRun do + describe 'Workflow run' do + include_context 'with workflow run' + + it 'creates a workflow run' do + expect(workflow_run).to be_an_instance_of Onfido::WorkflowRun + expect(workflow_run.applicant_id).to eq applicant_id + expect(workflow_run.status).to eq 'awaiting_input' + end + + context 'workflow run with custom inputs' do + let(:workflow_id) { '45092b29-f220-479e-aa6f-a6f989baac4c' } + let(:workflow_run_builder) do + Onfido::WorkflowRunBuilder.new({ + applicant_id: applicant_id, + workflow_id: workflow_id, + custom_data: { + age: 18, + is_employed: false + } + }) + end + + it 'creates a workflow run with custom inputs' do + expect(workflow_run).to be_an_instance_of Onfido::WorkflowRun + expect(workflow_run.workflow_id).to eq workflow_id + expect(workflow_run.status).to eq 'approved' + end + end + + it 'lists workflow runs' do + list_of_workflow_runs = onfido_api.list_workflow_runs() + + expect(list_of_workflow_runs[0]).to be_an_instance_of Onfido::WorkflowRun + expect(list_of_workflow_runs.size).to be > 0 + end + + it 'finds a workflow run' do + get_workflow_run = onfido_api.find_workflow_run(workflow_run_id) + + expect(get_workflow_run).to be_an_instance_of Onfido::WorkflowRun + expect(get_workflow_run.id).to eq workflow_run_id + end + + it 'downloads evidence file' do + file = onfido_api.download_signed_evidence_file(workflow_run_id) + + expect(file.size).to be > 0 + end + + context 'with start -> approved workflow' do + let(:workflow_id) { '221f9d24-cf72-4762-ac4a-01bf3ccc09dd' } + + it 'generates a timeline file' do + repeat_request_until_status_changes('approved') do + onfido_api.find_workflow_run(workflow_run_id) + end + + workflow_timeline_file_data = onfido_api.create_timeline_file(workflow_run_id) + + expect(workflow_timeline_file_data).to be_an_instance_of Onfido::TimelineFileReference + expect(workflow_timeline_file_data.workflow_timeline_file_id).to_not be_nil + expect(workflow_timeline_file_data.href).to_not be_nil + end + + it 'finds a timeline file' do + repeat_request_until_status_changes('approved') do + onfido_api.find_workflow_run(workflow_run_id) + end + + timeline_file_id = onfido_api.create_timeline_file(workflow_run_id).workflow_timeline_file_id + file = repeat_request_unti_http_code_changes do + onfido_api.find_timeline_file(workflow_run_id, timeline_file_id) + end + + expect(file.size).to be > 0 + end + end + end +end diff --git a/spec/shared_contexts/with_applicant.rb b/spec/shared_contexts/with_applicant.rb new file mode 100644 index 0000000..d2512c1 --- /dev/null +++ b/spec/shared_contexts/with_applicant.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +require_relative '../shared_contexts/with_onfido' + +RSpec.shared_context 'with applicant', shared_context: :metadata do + include_context 'with onfido' + + let(:applicant_id) do + applicant = onfido_api.create_applicant(default_applicant_body) + + applicant.id + end + + let(:default_applicant_body) do + { + 'first_name' => 'Test', + 'last_name' => 'Applicant', + 'location' => { + 'ip_address' => '127.0.0.1', + 'country_of_residence' => 'GBR' + } + } + end +end diff --git a/spec/shared_contexts/with_check.rb b/spec/shared_contexts/with_check.rb new file mode 100644 index 0000000..f40ddfd --- /dev/null +++ b/spec/shared_contexts/with_check.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +require_relative '../shared_contexts/with_document' + +RSpec.shared_context 'with check', shared_context: :metadata do + include_context 'with document' + + let(:check_builder) do + Onfido::CheckBuilder.new({ + applicant_id: applicant_id, + document_ids: [document_id], + report_names: [Onfido::ReportName::DOCUMENT, Onfido::ReportName::IDENTITY_ENHANCED], + }) + end + + let(:check) { onfido_api.create_check(check_builder) } + let!(:check_id) { check.id } +end diff --git a/spec/shared_contexts/with_document.rb b/spec/shared_contexts/with_document.rb new file mode 100644 index 0000000..95a6b64 --- /dev/null +++ b/spec/shared_contexts/with_document.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +require_relative '../shared_contexts/with_applicant' + +RSpec.shared_context 'with document', shared_context: :metadata do + include_context 'with applicant' + + let(:document_file_name) { 'sample_driving_licence.png' } + let(:document_file) { File.open("spec/integrations/media/#{document_file_name}") } + + let(:document) { onfido_api.upload_document('passport', applicant_id, document_file) } + let!(:document_id) { document.id } +end diff --git a/spec/shared_contexts/with_live_photo.rb b/spec/shared_contexts/with_live_photo.rb new file mode 100644 index 0000000..045e75d --- /dev/null +++ b/spec/shared_contexts/with_live_photo.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +require_relative '../shared_contexts/with_applicant' + +RSpec.shared_context 'with live photo', shared_context: :metadata do + include_context 'with applicant' + + let(:live_photo_file_name) { 'sample_photo.png' } + let(:live_photo_file) { File.open("spec/integrations/media/#{live_photo_file_name}") } + + let(:live_photo) do + onfido_api.upload_live_photo( + applicant_id: applicant_id, + file: live_photo_file, + ) + end + let!(:live_photo_id) { live_photo.id } +end diff --git a/spec/shared_contexts/with_onfido.rb b/spec/shared_contexts/with_onfido.rb new file mode 100644 index 0000000..0188b4a --- /dev/null +++ b/spec/shared_contexts/with_onfido.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +require 'onfido' + +RSpec.shared_context 'with onfido' do + let(:onfido_api) do + Onfido::DefaultApi.new + end + + Onfido.configure do |config| + config.api_token = ENV['ONFIDO_API_TOKEN'] + config.region = config.region[:EU] + end +end diff --git a/spec/shared_contexts/with_workflow_run.rb b/spec/shared_contexts/with_workflow_run.rb new file mode 100644 index 0000000..ac28789 --- /dev/null +++ b/spec/shared_contexts/with_workflow_run.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +require_relative '../shared_contexts/with_applicant' + +RSpec.shared_context 'with workflow run', shared_context: :metadata do + include_context 'with applicant' + + let(:workflow_id) { 'e8c921eb-0495-44fe-b655-bcdcaffdafe5' } + + let(:workflow_run_builder) do + Onfido::WorkflowRunBuilder.new({ + applicant_id: applicant_id, + workflow_id: workflow_id, + }) + end + + let(:workflow_run) { onfido_api.create_workflow_run(workflow_run_builder) } + let!(:workflow_run_id) { workflow_run.id } +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index d10aca3..6905ee1 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -26,9 +26,82 @@ config.warnings = false config.expose_dsl_globally = true + config.after(:suite) do + # clean-up applicants and webhooks + sample_applicant_id = ENV['ONFIDO_SAMPLE_APPLICANT_ID'] + onfido_api = Onfido::DefaultApi.new + + applicants = onfido_api.list_applicants( + page: 1, + per_page: 100, + include_deleted: false, + ).applicants + + applicants.each do |applicant| + if applicant.id != sample_applicant_id + begin + onfido_api.delete_applicant(applicant.id) + rescue Onfido::ApiError + # ignore failures during cleanup + end + end + end + + webhooks = onfido_api.list_webhooks().webhooks + + webhooks.each do |webhook| + begin + onfido_api.delete_webhook(webhook.id) + rescue Onfido::ApiError + # ignore failures during cleanup + end + end + end + # Seed global randomization in this process using the `--seed` CLI option. # Setting this allows you to use `--seed` to deterministically reproduce # test failures related to randomization by passing the same `--seed` value # as the one that triggered the failure. Kernel.srand config.seed end + +def repeat_request_until_status_changes(expected_status, max_retries = 10, + interval = 1, &proc) + # expected_status --> desired status + # max_retries --> how many times to retry the request + # interval --> how many seconds to wait until the next retry + # proc --> code containing the request + + instance = proc.call + + iteration = 0 + while instance.status != expected_status + raise "status did not change in time" if iteration > max_retries + + iteration += 1 + sleep(interval) + instance = proc.call + end + + instance +end + +def repeat_request_unti_http_code_changes(max_retries = 10, + interval = 1, &proc) + # max_retries --> how many times to retry the request + # interval --> how many seconds to wait until the next retry + # proc --> code containing the request + + iteration = 0 + while iteration <= max_retries + begin + instance = proc.call + break + rescue Onfido::ApiError + sleep(interval) + iteration += 1 + end + end + + instance +end diff --git a/spec/webhook_event_verifier_spec.rb b/spec/webhook_event_verifier_spec.rb index a2150ac..3829669 100644 --- a/spec/webhook_event_verifier_spec.rb +++ b/spec/webhook_event_verifier_spec.rb @@ -14,7 +14,7 @@ it "returns the decoded event payload" do expected_payload = JSON.parse(event_body)["payload"] - expected_webhook_event = Onfido::WebhookEvent.new({payload: Onfido::WebhookEventPayload.build_from_hash(expected_payload)}) + expected_webhook_event = Onfido::WebhookEvent.new({ payload: Onfido::WebhookEventPayload.build_from_hash(expected_payload) }) webhook_event = subject.read_payload(event_body, sig) expect(webhook_event.payload).to eq(expected_webhook_event.payload) @@ -26,4 +26,4 @@ expect(webhook_event.payload.object.href).to eq('https://api.onfido.com/v3.6/checks/f2302f45-227d-413d-ad61-09ec077a086a') end end -end \ No newline at end of file +end