onfido 0.15.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +5 -49
  3. data/.travis.yml +3 -10
  4. data/CHANGELOG.md +24 -0
  5. data/Gemfile +2 -0
  6. data/LICENSE +2 -1
  7. data/README.md +46 -172
  8. data/lib/onfido.rb +4 -3
  9. data/lib/onfido/api.rb +21 -15
  10. data/lib/onfido/errors/connection_error.rb +2 -0
  11. data/lib/onfido/errors/onfido_error.rb +2 -0
  12. data/lib/onfido/errors/request_error.rb +2 -0
  13. data/lib/onfido/errors/server_error.rb +2 -0
  14. data/lib/onfido/options.rb +38 -0
  15. data/lib/onfido/resource.rb +44 -61
  16. data/lib/onfido/resources/address.rb +3 -4
  17. data/lib/onfido/resources/applicant.rb +8 -6
  18. data/lib/onfido/resources/check.rb +15 -19
  19. data/lib/onfido/resources/document.rb +13 -11
  20. data/lib/onfido/resources/extraction.rb +11 -0
  21. data/lib/onfido/resources/live_photo.rb +11 -14
  22. data/lib/onfido/resources/live_video.rb +7 -8
  23. data/lib/onfido/resources/report.rb +10 -9
  24. data/lib/onfido/resources/sdk_token.rb +5 -5
  25. data/lib/onfido/resources/webhook.rb +15 -11
  26. data/lib/onfido/version.rb +3 -1
  27. data/onfido.gemspec +10 -12
  28. data/spec/integrations/address_spec.rb +5 -2
  29. data/spec/integrations/applicant_spec.rb +29 -42
  30. data/spec/integrations/check_spec.rb +28 -69
  31. data/spec/integrations/document_spec.rb +22 -19
  32. data/spec/integrations/extraction_spec.rb +23 -0
  33. data/spec/integrations/live_photo_spec.rb +18 -15
  34. data/spec/integrations/live_video_spec.rb +13 -11
  35. data/spec/integrations/report_spec.rb +16 -13
  36. data/spec/integrations/resource_spec.rb +93 -0
  37. data/spec/integrations/sdk_token_spec.rb +10 -6
  38. data/spec/integrations/webhook_spec.rb +56 -37
  39. data/spec/onfido/api_spec.rb +14 -25
  40. data/spec/onfido/connection_error_spec.rb +4 -2
  41. data/spec/onfido/options_spec.rb +39 -0
  42. data/spec/onfido/request_error_spec.rb +4 -2
  43. data/spec/spec_helper.rb +3 -5
  44. data/spec/support/fake_onfido_api.rb +77 -88
  45. data/spec/support/fixtures/applicant.json +21 -42
  46. data/spec/support/fixtures/check.json +4 -4
  47. data/spec/support/fixtures/checks.json +4 -4
  48. data/spec/support/fixtures/document.json +2 -2
  49. data/spec/support/fixtures/documents.json +8 -8
  50. data/spec/support/fixtures/extraction.json +23 -0
  51. data/spec/support/fixtures/live_photo.json +3 -3
  52. data/spec/support/fixtures/live_photos.json +6 -6
  53. data/spec/support/fixtures/live_video.json +3 -3
  54. data/spec/support/fixtures/live_videos.json +4 -4
  55. data/spec/support/fixtures/report.json +4 -4
  56. data/spec/support/fixtures/reports.json +8 -8
  57. data/spec/support/fixtures/webhook.json +6 -5
  58. data/spec/support/fixtures/webhooks.json +17 -12
  59. metadata +25 -65
  60. data/Rakefile +0 -1
  61. data/lib/onfido/configuration.rb +0 -47
  62. data/lib/onfido/null_logger.rb +0 -5
  63. data/lib/onfido/resources/report_type_group.rb +0 -11
  64. data/spec/integrations/exceptions_spec.rb +0 -74
  65. data/spec/integrations/report_type_group_spec.rb +0 -19
  66. data/spec/onfido/resource_spec.rb +0 -137
  67. data/spec/onfido_spec.rb +0 -84
  68. data/spec/support/fixtures/check_with_expanded_reports.json +0 -30
  69. data/spec/support/fixtures/checks_with_expanded_reports.json +0 -34
  70. data/spec/support/fixtures/report_type_group.json +0 -25
  71. data/spec/support/fixtures/report_type_groups.json +0 -30
data/Rakefile DELETED
@@ -1 +0,0 @@
1
- require 'bundler/gem_tasks'
@@ -1,47 +0,0 @@
1
- module Onfido
2
- module Configuration
3
- REGION_HOSTS = {
4
- us: "api.us.onfido.com"
5
- }.freeze
6
-
7
- attr_accessor :api_key, :region, :open_timeout, :read_timeout, :api_version
8
-
9
- def self.extended(base)
10
- base.reset
11
- end
12
-
13
- def configure
14
- yield self
15
- end
16
-
17
- def reset
18
- self.api_key = nil
19
- self.region = nil
20
- self.open_timeout = 30
21
- self.read_timeout = 80
22
- self.api_version = 'v2'
23
- RestClient.log = nil
24
- end
25
-
26
- def logger=(log)
27
- unless log.respond_to?(:<<)
28
- raise "#{log.class} doesn't seem to behave like a logger!"
29
- end
30
-
31
- RestClient.log = log
32
- end
33
-
34
- def logger
35
- RestClient.log ||= NullLogger.new
36
- end
37
-
38
- def endpoint
39
- region_host = region ? REGION_HOSTS[region.downcase.to_sym] : "api.onfido.com"
40
- unless region_host
41
- raise "The region \"#{region.downcase}\" is not currently supported"
42
- end
43
-
44
- "https://#{region_host}/#{api_version}/"
45
- end
46
- end
47
- end
@@ -1,5 +0,0 @@
1
- module Onfido
2
- class NullLogger
3
- def <<(*args); end
4
- end
5
- end
@@ -1,11 +0,0 @@
1
- module Onfido
2
- class ReportTypeGroup < Resource
3
- def find(report_type_group_id)
4
- get(url: url_for("report_type_groups/#{report_type_group_id}"))
5
- end
6
-
7
- def all
8
- get(url: url_for("report_type_groups"))
9
- end
10
- end
11
- end
@@ -1,74 +0,0 @@
1
- describe Onfido::Resource do
2
- let(:resource) { described_class.new }
3
- let(:url) { Onfido.endpoint + path }
4
- let(:api_key) { 'some_key' }
5
- let(:payload) { { postcode: 'SE1 4NG' } }
6
-
7
- before { allow(Onfido).to receive(:api_key).and_return(api_key) }
8
-
9
- context '4xx response' do
10
- let(:path) { '4xx_response' }
11
-
12
- it 'raises a custom error' do
13
- expect { resource.get(url: url, payload: payload) }.
14
- to raise_error(Onfido::RequestError, 'Something went wrong')
15
- end
16
- end
17
-
18
- context 'unexpected error format' do
19
- let(:path) { 'unexpected_error_format' }
20
-
21
- it 'raises a custom error' do
22
- expect { resource.get(url: url, payload: payload) }.
23
- to raise_error(Onfido::RequestError, /response code was 400/)
24
- end
25
- end
26
-
27
- context 'unparseable JSON 5xx' do
28
- let(:path) { 'unparseable_response' }
29
-
30
- it 'raises a server error' do
31
- expect { resource.get(url: url, payload: payload) }.
32
- to raise_error(Onfido::ServerError, /response code was 504/)
33
- end
34
- end
35
-
36
- context 'timeout' do
37
- before do
38
- allow(RestClient::Request).
39
- to receive(:execute).
40
- and_raise(RestClient::RequestTimeout)
41
- end
42
-
43
- it 'raises a ConnectionError' do
44
- expect { resource.get(url: Onfido.endpoint, payload: payload) }.
45
- to raise_error(Onfido::ConnectionError, /Could not connect/)
46
- end
47
- end
48
-
49
- context 'broken connection' do
50
- before do
51
- allow(RestClient::Request).
52
- to receive(:execute).
53
- and_raise(RestClient::ServerBrokeConnection)
54
- end
55
-
56
- it 'raises a ConnectionError' do
57
- expect { resource.get(url: Onfido.endpoint, payload: payload) }.
58
- to raise_error(Onfido::ConnectionError, /connection to the server/)
59
- end
60
- end
61
-
62
- context "bad SSL certificate" do
63
- before do
64
- allow(RestClient::Request).
65
- to receive(:execute).
66
- and_raise(RestClient::SSLCertificateNotVerified.new(nil))
67
- end
68
-
69
- it 'raises a ConnectionError' do
70
- expect { resource.get(url: Onfido.endpoint, payload: payload) }.
71
- to raise_error(Onfido::ConnectionError, /SSL certificate/)
72
- end
73
- end
74
- end
@@ -1,19 +0,0 @@
1
- describe Onfido::ReportTypeGroup do
2
- subject(:report_type_group) { described_class.new }
3
-
4
- describe '#find' do
5
- let(:id) { '8546921-123123-123123' }
6
-
7
- it 'return a retport type group' do
8
- response = report_type_group.find(id)
9
- expect(response['id']).not_to be_nil
10
- end
11
- end
12
-
13
- describe '#all' do
14
- it 'return a list of report type group' do
15
- response = report_type_group.all
16
- expect(response['report_type_groups']).not_to be_empty
17
- end
18
- end
19
- end
@@ -1,137 +0,0 @@
1
- require 'onfido/errors/connection_error'
2
-
3
- describe Onfido::Resource do
4
- subject(:resource) { described_class.new }
5
-
6
- let(:endpoint) { 'https://api.onfido.com/v2/' }
7
- let(:path) { 'addresses/pick' }
8
- let(:url) { endpoint + path }
9
- let(:payload) { { postcode: 'SE1 4NG' } }
10
- let(:api_key) { 'some_key' }
11
-
12
- let(:response) do
13
- {
14
- 'addresses' => [
15
- {
16
- 'street' => 'Main Street',
17
- 'town' => 'London',
18
- 'postcode' => 'SW4 6EH',
19
- 'country' => 'GBR'
20
- }
21
- ]
22
- }
23
- end
24
-
25
- before { allow(Onfido).to receive(:endpoint).and_return(endpoint) }
26
- before { allow(Onfido).to receive(:api_key).and_return(api_key) }
27
-
28
- describe '#url_for' do
29
- it 'composes the full api url' do
30
- expect(resource.url_for(path)).to eq(endpoint + path)
31
- end
32
- end
33
-
34
- describe '#method_missing' do
35
- %i(patch).each do |method|
36
- context "for unsupported HTTP method: #{method}" do
37
- it 'raises an error' do
38
- expect do
39
- resource.public_send(method, url: endpoint)
40
- end.to raise_error(NoMethodError)
41
- end
42
- end
43
- end
44
- end
45
-
46
- describe "API key" do
47
- subject(:resource) { described_class.new(specific_api_key) }
48
-
49
- before do
50
- expect(RestClient::Request).to receive(:execute).with(
51
- url: url,
52
- payload: Rack::Utils.build_query(payload),
53
- method: :get,
54
- headers: resource.send(:headers),
55
- open_timeout: 30,
56
- timeout: 80
57
- ).and_call_original
58
-
59
- WebMock.stub_request(:get, url).
60
- to_return(body: response.to_json, status: 200)
61
- end
62
-
63
- context "when using a specific key" do
64
- let(:specific_api_key) { "specific_key" }
65
-
66
- it "uses that key when making the request" do
67
- resource.get(url: url, payload: payload)
68
-
69
- expect(WebMock).to have_requested(:get, url).with(
70
- headers: {
71
- 'Authorization' => "Token token=#{specific_api_key}",
72
- 'Accept' => "application/json"
73
- }
74
- )
75
- end
76
- end
77
-
78
- context "when not using a specific key" do
79
- let(:specific_api_key) { nil }
80
-
81
- it "uses the general config key when making the request" do
82
- resource.get(url: url, payload: payload)
83
-
84
- expect(WebMock).to have_requested(:get, url).with(
85
- headers: {
86
- 'Authorization' => "Token token=#{api_key}",
87
- 'Accept' => "application/json"
88
- }
89
- )
90
- end
91
- end
92
- end
93
-
94
- describe "valid http methods" do
95
- %i(get post put delete).each do |method|
96
- context "for supported HTTP method: #{method}" do
97
- context "with a success response" do
98
- before do
99
- expect(RestClient::Request).to receive(:execute).
100
- with(
101
- url: url,
102
- payload: Rack::Utils.build_query(payload),
103
- method: method,
104
- headers: resource.send(:headers),
105
- open_timeout: 30,
106
- timeout: 80
107
- ).and_call_original
108
-
109
- WebMock.stub_request(method, url).
110
- to_return(body: response.to_json,
111
- status: 200,
112
- headers: { "Content-Type" => "application/json" })
113
- end
114
-
115
- it 'makes a request to an endpoint' do
116
- expect(resource.public_send(method, url: url, payload: payload)).
117
- to eq(response)
118
- end
119
- end
120
-
121
- context "with a timeout error response" do
122
- before do
123
- allow_any_instance_of(RestClient::ExceptionWithResponse).
124
- to receive(:response).and_return(double(body: "", code: "408"))
125
- expect(RestClient::Request).to receive(:execute).
126
- and_raise(RestClient::ExceptionWithResponse)
127
- end
128
-
129
- it "raises a ConnectionError" do
130
- expect { resource.public_send(method, url: url, payload: payload) }.
131
- to raise_error(Onfido::ConnectionError)
132
- end
133
- end
134
- end
135
- end
136
- end
137
- end
data/spec/onfido_spec.rb DELETED
@@ -1,84 +0,0 @@
1
- describe Onfido do
2
- subject(:onfido) { described_class }
3
- after(:each) { onfido.reset }
4
-
5
- context 'configuration' do
6
- describe "default values" do
7
- describe ".api_key" do
8
- subject { onfido.api_key }
9
- it { is_expected.to be_nil }
10
- end
11
-
12
- describe ".endpoint" do
13
- subject { onfido.endpoint }
14
- it { is_expected.to eq('https://api.onfido.com/v2/') }
15
- end
16
-
17
- describe ".logger" do
18
- subject { onfido.logger }
19
- it { is_expected.to be_an_instance_of(Onfido::NullLogger) }
20
- end
21
- end
22
-
23
- describe "setting an API key" do
24
- it 'changes the configuration to the new value' do
25
- onfido.api_key = 'some_key'
26
- expect(onfido.api_key).to eq('some_key')
27
- end
28
- end
29
-
30
- describe "setting the API version" do
31
- it 'changes the configuration to the new value' do
32
- onfido.api_version = 'v1'
33
- expect(onfido.api_version).to eq('v1')
34
- expect(onfido.endpoint).to eq('https://api.onfido.com/v1/')
35
- end
36
- end
37
-
38
- describe 'using the US region' do
39
- it 'should change endpoint' do
40
- onfido.region = 'us'
41
- expect(onfido.endpoint).to eq('https://api.us.onfido.com/v2/')
42
- end
43
- end
44
-
45
- describe 'using an unsupported region' do
46
- it 'should change endpoint' do
47
- onfido.region = 'de'
48
- expect { onfido.endpoint }.
49
- to raise_error('The region "de" is not currently supported')
50
- end
51
- end
52
-
53
- describe 'using an old API token' do
54
- it 'should use old endpoint' do
55
- onfido.api_key = "live_asdfghjkl1234567890qwertyuiop"
56
- expect(onfido.endpoint).to eq('https://api.onfido.com/v2/')
57
- end
58
- end
59
-
60
- describe '.logger' do
61
- context 'when an option is passed' do
62
- context 'when the option passed behaves like a logger' do
63
- let(:logger_like) { double('LoggerLike', :<< => nil) }
64
-
65
- it 'returns the option' do
66
- onfido.logger = logger_like
67
- expect(onfido.logger).to eq(logger_like)
68
- end
69
- end
70
-
71
- context 'when the option passed does not behave like a logger' do
72
- let(:non_logger) { double('NotLogger') }
73
-
74
- it 'raises an error' do
75
- expect { onfido.logger = non_logger }.
76
- to raise_error(
77
- "#{non_logger.class} doesn't seem to behave like a logger!"
78
- )
79
- end
80
- end
81
- end
82
- end
83
- end
84
- end
@@ -1,30 +0,0 @@
1
- {
2
- "id": "8546921-123123-123123",
3
- "created_at": "2014-05-23T13:50:33Z",
4
- "href": "/v2/applicants/61f659cb-c90b-4067-808a-6136b5c01351/checks/8546921-123123-123123",
5
- "type": "standard",
6
- "status": "pending",
7
- "result": "pending",
8
- "reports": [
9
- {
10
- "id": "6951786-123123-422221",
11
- "name": "identity",
12
- "created_at": "2014-05-23T13:50:33Z",
13
- "status": "awaiting_applicant",
14
- "result": "pending",
15
- "href": "/v2/checks/8546921-123123-123123/reports/6951786-123123-422221",
16
- "breakdown": {},
17
- "properties": {}
18
- },
19
- {
20
- "id": "6951786-123123-316712",
21
- "name": "document",
22
- "created_at": "2014-05-23T13:50:33Z",
23
- "status": "awaiting_applicant",
24
- "result": "pending",
25
- "href": "/v2/checks/8546921-123123-123123/reports/6951786-123123-316712",
26
- "breakdown": {},
27
- "properties": {}
28
- }
29
- ]
30
- }
@@ -1,34 +0,0 @@
1
- {
2
- "checks": [
3
- {
4
- "id": "8546921-123123-123123",
5
- "created_at": "2014-05-23T13:50:33Z",
6
- "href": "/v2/applicants/61f659cb-c90b-4067-808a-6136b5c01351/checks/8546921-123123-123123",
7
- "type": "standard",
8
- "status": "pending",
9
- "result": "pending",
10
- "reports": [
11
- {
12
- "id": "6951786-123123-422221",
13
- "name": "identity",
14
- "created_at": "2014-05-23T13:50:33Z",
15
- "status": "awaiting_applicant",
16
- "result": "pending",
17
- "href": "/v2/checks/8546921-123123-123123/reports/6951786-123123-422221",
18
- "breakdown": {},
19
- "properties": {}
20
- },
21
- {
22
- "id": "6951786-123123-316712",
23
- "name": "document",
24
- "created_at": "2014-05-23T13:50:33Z",
25
- "status": "awaiting_applicant",
26
- "result": "pending",
27
- "href": "/v2/checks/8546921-123123-123123/reports/6951786-123123-316712",
28
- "breakdown": {},
29
- "properties": {}
30
- }
31
- ]
32
- }
33
- ]
34
- }