onfido 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +161 -0
  3. data/.rspec +3 -0
  4. data/.ruby-version +1 -0
  5. data/Gemfile +14 -0
  6. data/LICENSE +22 -0
  7. data/LICENSE.txt +22 -0
  8. data/README.md +154 -0
  9. data/Rakefile +2 -0
  10. data/lib/onfido.rb +23 -0
  11. data/lib/onfido/address.rb +10 -0
  12. data/lib/onfido/api.rb +18 -0
  13. data/lib/onfido/applicant.rb +18 -0
  14. data/lib/onfido/check.rb +21 -0
  15. data/lib/onfido/configuration.rb +35 -0
  16. data/lib/onfido/document.rb +13 -0
  17. data/lib/onfido/null_logger.rb +6 -0
  18. data/lib/onfido/report.rb +11 -0
  19. data/lib/onfido/request_error.rb +23 -0
  20. data/lib/onfido/requestable.rb +12 -0
  21. data/lib/onfido/resource.rb +48 -0
  22. data/lib/onfido/response_handler.rb +38 -0
  23. data/lib/onfido/version.rb +3 -0
  24. data/onfido.gemspec +25 -0
  25. data/spec/integrations/4xx_response_spec.rb +35 -0
  26. data/spec/integrations/address_spec.rb +10 -0
  27. data/spec/integrations/applicant_spec.rb +73 -0
  28. data/spec/integrations/check_spec.rb +36 -0
  29. data/spec/integrations/document_spec.rb +27 -0
  30. data/spec/integrations/report_spec.rb +20 -0
  31. data/spec/onfido/request_error_spec.rb +39 -0
  32. data/spec/onfido/resource_spec.rb +63 -0
  33. data/spec/onfido/response_handler_spec.rb +79 -0
  34. data/spec/onfido_spec.rb +55 -0
  35. data/spec/spec_helper.rb +45 -0
  36. data/spec/support/fake_onfido_api.rb +55 -0
  37. data/spec/support/fixtures/4xx_response.json +14 -0
  38. data/spec/support/fixtures/addresses.json +24 -0
  39. data/spec/support/fixtures/applicant.json +44 -0
  40. data/spec/support/fixtures/applicants.json +12 -0
  41. data/spec/support/fixtures/check.json +30 -0
  42. data/spec/support/fixtures/checks.json +34 -0
  43. data/spec/support/fixtures/document.json +10 -0
  44. data/spec/support/fixtures/report.json +10 -0
  45. data/spec/support/fixtures/reports.json +24 -0
  46. metadata +151 -0
@@ -0,0 +1,20 @@
1
+ describe Onfido::Report do
2
+ subject(:report) { described_class.new }
3
+ let(:check_id) { '8546921-123123-123123' }
4
+
5
+ describe '#find' do
6
+ let(:report_id) {'6951786-123123-422221' }
7
+
8
+ it 'returns a report for an existing check' do
9
+ response = report.find(check_id, report_id)
10
+ expect(response['id']).to eq(report_id)
11
+ end
12
+ end
13
+
14
+ describe '#all' do
15
+ it 'lists all reports for an existing check' do
16
+ response = report.all(check_id)
17
+ expect(response['reports'].count).to eq(2)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,39 @@
1
+ describe Onfido::RequestError do
2
+ subject(:error) do
3
+ described_class.new(failed_response['error']['message']).tap do |e|
4
+ e.type = failed_response['error']['type']
5
+ e.fields = failed_response['error']['fields']
6
+ e.response_code = 401
7
+ end
8
+ end
9
+
10
+ let(:failed_response) do
11
+ {
12
+ 'error' =>
13
+ {
14
+ 'id' => '551722cc964860653c00c202',
15
+ 'type' => 'authorization_error',
16
+ 'message' => 'Authorization error: please re-check your credentials',
17
+ 'fields' => {'name' => {'messages' => ['cannot be blank']} }
18
+ }
19
+ }
20
+ end
21
+
22
+ context 'when there is a request error' do
23
+ it 'returns the right message' do
24
+ expect { raise error }.to raise_error('Authorization error: please re-check your credentials')
25
+ end
26
+
27
+ it 'has the right error type' do
28
+ expect(error.type).to eq('authorization_error')
29
+ end
30
+
31
+ it 'has the right affected fields' do
32
+ expect(error.fields).to eq({'name' => {'messages' => ['cannot be blank']} })
33
+ end
34
+
35
+ it 'has the right response code' do
36
+ expect(error.response_code).to eq(401)
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,63 @@
1
+ describe Onfido::Resource do
2
+ subject(:resource) { described_class.new }
3
+ let(:endpoint) { 'https://api.onfido.com/v1/' }
4
+ let(:path) { 'addresses/pick' }
5
+ let(:api_key) { 'some_key' }
6
+
7
+ before do
8
+ allow(Onfido).to receive(:endpoint).and_return(endpoint)
9
+ allow(Onfido).to receive(:api_key).and_return(api_key)
10
+ end
11
+
12
+ describe '#url_for' do
13
+ it 'composes the full api url' do
14
+ expect(resource.url_for(path)).to eq(endpoint + path)
15
+ end
16
+ end
17
+
18
+ describe '#method_missing' do
19
+ %i(put delete patch).each do |method|
20
+ context "for unsupported HTTP method: #{method}" do
21
+ it 'raises an error' do
22
+ expect {
23
+ resource.public_send(method)
24
+ }.to raise_error(NoMethodError)
25
+ end
26
+ end
27
+ end
28
+
29
+ %i(get post).each do |method|
30
+ context "for supported HTTP method: #{method}" do
31
+ let(:url) { endpoint + path }
32
+ let(:payload) { {postcode: 'SE1 4NG'} }
33
+ let(:response) do
34
+ {
35
+ 'addresses' => [
36
+ {
37
+ 'street' => 'Main Street',
38
+ 'town' => 'London',
39
+ 'postcode' => 'SW4 6EH',
40
+ 'country' => 'GBR'
41
+ }
42
+ ]
43
+ }
44
+ end
45
+
46
+ before do
47
+ expect(RestClient::Request).to receive(:execute)
48
+ .with(
49
+ url: url,
50
+ payload: Rack::Utils.build_query(payload),
51
+ method: method,
52
+ headers: resource.headers
53
+ )
54
+ .and_return(response)
55
+ end
56
+
57
+ it 'makes a request to an endpoint' do
58
+ expect(resource.public_send(method, {url: url, payload: payload})).to eq(response)
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,79 @@
1
+ describe Onfido::ResponseHandler do
2
+ subject(:handler) { described_class.new(response) }
3
+
4
+ describe '#parse!' do
5
+ before do
6
+ allow(Onfido).to receive(:throws_exceptions).and_return(throws_exceptions)
7
+ end
8
+
9
+ let(:throws_exceptions) { true }
10
+
11
+ context 'when the response is successful' do
12
+ let(:response) { hashified_response.to_json }
13
+ let(:hashified_response) do
14
+ {'success' => 'applicant was successfully created'}
15
+ end
16
+
17
+ it 'parses the body and returns it' do
18
+ expect(handler.parse!).to eq(hashified_response)
19
+ end
20
+ end
21
+
22
+ context 'when the response has gracefully errored' do
23
+ let(:response) { hashified_response.to_json }
24
+ let(:hashified_response) do
25
+ {
26
+ 'error' => {
27
+ 'message' => 'Authorization error: please re-check your credentials'
28
+ }
29
+ }
30
+ end
31
+
32
+ context 'when throw_exceptions configuration is set to true' do
33
+ it 'raises an error' do
34
+ expect { handler.parse! }.to raise_error(
35
+ Onfido::RequestError,
36
+ 'Authorization error: please re-check your credentials'
37
+ )
38
+ end
39
+ end
40
+
41
+ context 'when throw_exceptions configuration is set to false' do
42
+ let(:throws_exceptions) { false }
43
+
44
+
45
+ it 'parses the body and returns it' do
46
+ expect(handler.parse!).to eq(hashified_response)
47
+ end
48
+ end
49
+ end
50
+
51
+ context 'when the response is unparseable' do
52
+ let(:response) { 'something : wrong' }
53
+
54
+ context 'when throw_exceptions configuration is set to true' do
55
+ it 'raises an error' do
56
+ expect { handler.parse! }.to raise_error(
57
+ Onfido::RequestError,
58
+ 'Unparseable response: something : wrong'
59
+ )
60
+ end
61
+ end
62
+
63
+ context 'when throw_exceptions configuration is set to false' do
64
+ let(:throws_exceptions) { false }
65
+
66
+
67
+ it 'parses the body and returns it' do
68
+ expect(handler.parse!).to eq(
69
+ {
70
+ 'error' => {
71
+ 'message' => 'Unparseable response: something : wrong'
72
+ }
73
+ }
74
+ )
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,55 @@
1
+ describe Onfido do
2
+ subject(:onfido) { described_class }
3
+
4
+ context 'configuration' do
5
+ before do
6
+ onfido.reset
7
+ end
8
+
9
+ {api_key: nil, endpoint: 'https://api.onfido.com/v1/', throws_exceptions: true}.each do |config_key, value|
10
+ describe ".#{config_key}" do
11
+ it 'returns the default value' do
12
+ expect(onfido.public_send(config_key)).to eq(value)
13
+ end
14
+ end
15
+ end
16
+
17
+ {api_key: 'some_key', throws_exceptions: false}.each do |config_key, new_value|
18
+ describe ".#{config_key}=" do
19
+ it 'changes the configuration to the new value' do
20
+ onfido.public_send("#{config_key}=", new_value)
21
+ expect(onfido.public_send(config_key)).to eq(new_value)
22
+ end
23
+ end
24
+ end
25
+
26
+ describe '.logger' do
27
+ context 'when no option is passed' do
28
+ it 'returns the default value' do
29
+ expect(onfido.logger).to be_an_instance_of(Onfido::NullLogger)
30
+ end
31
+ end
32
+
33
+ context 'when an option is passed' do
34
+ context 'when the option passed behaves like a logger' do
35
+ let(:logger_like) { double('LoggerLike', :<< => nil) }
36
+
37
+ it 'returns the option' do
38
+ onfido.logger = logger_like
39
+ expect(onfido.logger).to eq(logger_like)
40
+ end
41
+ end
42
+
43
+ context 'when the option passed does not behave like a logger' do
44
+ let(:non_logger) { double('NotLogger') }
45
+
46
+ it 'raises an error' do
47
+ expect {
48
+ onfido.logger = non_logger
49
+ }.to raise_error("#{non_logger.class} doesn't seem to behave like a logger!")
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,45 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ Dir[File.dirname(__FILE__).concat("/support/**/*.rb")].each {|f| require f}
4
+
5
+ require 'onfido'
6
+ require 'webmock/rspec'
7
+
8
+ WebMock.disable_net_connect!(allow_localhost: true)
9
+
10
+ RSpec.configure do |config|
11
+ config.expect_with :rspec do |expectations|
12
+ # This option will default to `true` in RSpec 4. It makes the `description`
13
+ # and `failure_message` of custom matchers include text for helper methods
14
+ # defined using `chain`, e.g.:
15
+ # be_bigger_than(2).and_smaller_than(4).description
16
+ # # => "be bigger than 2 and smaller than 4"
17
+ # ...rather than:
18
+ # # => "be bigger than 2"
19
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
20
+ end
21
+
22
+ config.mock_with :rspec do |mocks|
23
+ # Prevents you from mocking or stubbing a method that does not exist on
24
+ # a real object. This is generally recommended, and will default to
25
+ # `true` in RSpec 4.
26
+ mocks.verify_partial_doubles = true
27
+ end
28
+
29
+ config.order = :random
30
+ config.filter_run :focus
31
+ config.run_all_when_everything_filtered = true
32
+ config.disable_monkey_patching!
33
+ config.warnings = false
34
+ config.expose_dsl_globally = true
35
+
36
+ # Seed global randomization in this process using the `--seed` CLI option.
37
+ # Setting this allows you to use `--seed` to deterministically reproduce
38
+ # test failures related to randomization by passing the same `--seed` value
39
+ # as the one that triggered the failure.
40
+ Kernel.srand config.seed
41
+
42
+ config.before(:each) do
43
+ stub_request(:any, /onfido.com/).to_rack(FakeOnfidoAPI)
44
+ end
45
+ end
@@ -0,0 +1,55 @@
1
+ require 'sinatra/base'
2
+
3
+ class FakeOnfidoAPI < Sinatra::Base
4
+ get '/v1/addresses/pick' do
5
+ json_response(200, 'addresses.json')
6
+ end
7
+
8
+ post '/v1/applicants' do
9
+ json_response(201, 'applicant.json')
10
+ end
11
+
12
+ get '/v1/applicants/:id' do
13
+ json_response(200, 'applicant.json')
14
+ end
15
+
16
+ get '/v1/applicants' do
17
+ json_response(200, 'applicants.json')
18
+ end
19
+
20
+ post '/v1/applicants/:id/documents' do
21
+ json_response(201, 'document.json')
22
+ end
23
+
24
+ post '/v1/applicants/:id/checks' do
25
+ json_response(201, 'check.json')
26
+ end
27
+
28
+ get '/v1/applicants/:id/checks/:id' do
29
+ json_response(200, 'check.json')
30
+ end
31
+
32
+ get '/v1/applicants/:id/checks' do
33
+ json_response(200, 'checks.json')
34
+ end
35
+
36
+ get '/v1/checks/:id/reports' do
37
+ json_response(200, 'reports.json')
38
+ end
39
+
40
+ get '/v1/checks/:id/reports/:id' do
41
+ json_response(200, 'report.json')
42
+ end
43
+
44
+ get '/v1/4xx_response' do
45
+ json_response(422, '4xx_response.json')
46
+ end
47
+
48
+ private
49
+
50
+ def json_response(response_code, file_name)
51
+ content_type :json
52
+ status response_code
53
+ File.open(File.dirname(__FILE__) + '/fixtures/' + file_name, 'rb').read
54
+ end
55
+ end
@@ -0,0 +1,14 @@
1
+ {
2
+ "error": {
3
+ "id": "544f95539648607d03001542",
4
+ "type": "validation_error",
5
+ "message": "Something went wrong",
6
+ "fields": {
7
+ "email": {
8
+ "messages": [
9
+ "invalid format"
10
+ ]
11
+ }
12
+ }
13
+ }
14
+ }
@@ -0,0 +1,24 @@
1
+ {
2
+ "addresses": [
3
+ {
4
+ "flat_number": "",
5
+ "building_number": "100",
6
+ "building_name": "",
7
+ "street": "Main Street",
8
+ "sub_street": "",
9
+ "town": "London",
10
+ "postcode": "SW4 6EH",
11
+ "country": "GBR"
12
+ },
13
+ {
14
+ "flat_number": "",
15
+ "building_number": "101",
16
+ "building_name": "",
17
+ "street": "Main Street",
18
+ "sub_street": "",
19
+ "town": "London",
20
+ "postcode": "SW4 6EH",
21
+ "country": "GBR"
22
+ }
23
+ ]
24
+ }
@@ -0,0 +1,44 @@
1
+ {
2
+ "id":"61f659cb-c90b-4067-808a-6136b5c01351",
3
+ "created_at":"2015-04-10T13:16:01Z",
4
+ "title":"Mr",
5
+ "first_name":"Chandler",
6
+ "middle_name":"Muriel",
7
+ "last_name":"Bing",
8
+ "email":"chandler_bing_6@friends.com",
9
+ "gender":"Male",
10
+ "dob":"1968-04-08",
11
+ "telephone":"555555555",
12
+ "mobile":"77777777",
13
+ "country":"gbr",
14
+ "href":"/v1/applicants/61f659cb-c90b-4067-808a-6136b5c01351",
15
+ "id_numbers":[],
16
+ "addresses": [
17
+ {
18
+ "flat_number":"4",
19
+ "building_number":"100",
20
+ "building_name":"Awesome Building",
21
+ "street":"Main Street",
22
+ "sub_street":"A sub street",
23
+ "town":"London",
24
+ "state":"",
25
+ "postcode":"SW4 6EH",
26
+ "country":"GBR",
27
+ "start_date":"",
28
+ "end_date":""
29
+ },
30
+ {
31
+ "flat_number":"1",
32
+ "building_number":"10",
33
+ "building_name":"Great Building",
34
+ "street":"Old Street",
35
+ "sub_street":"Sub Street",
36
+ "town":"London",
37
+ "state":"",
38
+ "postcode":"SW1 4NG",
39
+ "country":"GBR",
40
+ "start_date":"",
41
+ "end_date":""
42
+ }
43
+ ]
44
+ }