Skip to content

Commit

Permalink
Merge pull request #53 from onfido/add-tests
Browse files Browse the repository at this point in the history
test: add tests for onfido-ruby library
  • Loading branch information
sofia-gomes-onfido authored Jun 4, 2024
2 parents 09db667 + 167c866 commit 1bb444e
Show file tree
Hide file tree
Showing 27 changed files with 1,127 additions and 39 deletions.
16 changes: 16 additions & 0 deletions spec/integrations/address_picker_spec.rb
Original file line number Diff line number Diff line change
@@ -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
106 changes: 69 additions & 37 deletions spec/integrations/applicant_spec.rb
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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
end
83 changes: 83 additions & 0 deletions spec/integrations/check_spec.rb
Original file line number Diff line number Diff line change
@@ -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
46 changes: 46 additions & 0 deletions spec/integrations/document_spec.rb
Original file line number Diff line number Diff line change
@@ -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
38 changes: 38 additions & 0 deletions spec/integrations/extraction_spec.rb
Original file line number Diff line number Diff line change
@@ -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
55 changes: 55 additions & 0 deletions spec/integrations/id_photo_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit 1bb444e

Please sign in to comment.