onfido 1.1.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +5 -49
  3. data/.travis.yml +2 -8
  4. data/CHANGELOG.md +7 -0
  5. data/Gemfile +2 -0
  6. data/README.md +37 -148
  7. data/lib/onfido.rb +3 -2
  8. data/lib/onfido/api.rb +18 -12
  9. data/lib/onfido/errors/connection_error.rb +2 -0
  10. data/lib/onfido/errors/onfido_error.rb +2 -0
  11. data/lib/onfido/errors/request_error.rb +2 -0
  12. data/lib/onfido/errors/server_error.rb +2 -0
  13. data/lib/onfido/options.rb +38 -0
  14. data/lib/onfido/resource.rb +45 -59
  15. data/lib/onfido/resources/address.rb +2 -0
  16. data/lib/onfido/resources/applicant.rb +2 -0
  17. data/lib/onfido/resources/check.rb +6 -0
  18. data/lib/onfido/resources/document.rb +2 -0
  19. data/lib/onfido/resources/extraction.rb +2 -0
  20. data/lib/onfido/resources/live_photo.rb +2 -0
  21. data/lib/onfido/resources/live_video.rb +2 -0
  22. data/lib/onfido/resources/report.rb +2 -0
  23. data/lib/onfido/resources/sdk_token.rb +2 -0
  24. data/lib/onfido/resources/webhook.rb +4 -2
  25. data/lib/onfido/version.rb +3 -1
  26. data/onfido.gemspec +5 -6
  27. data/spec/integrations/address_spec.rb +4 -2
  28. data/spec/integrations/applicant_spec.rb +12 -7
  29. data/spec/integrations/check_spec.rb +17 -4
  30. data/spec/integrations/document_spec.rb +7 -3
  31. data/spec/integrations/extraction_spec.rb +6 -2
  32. data/spec/integrations/live_photo_spec.rb +7 -3
  33. data/spec/integrations/live_video_spec.rb +6 -1
  34. data/spec/integrations/report_spec.rb +6 -1
  35. data/spec/integrations/resource_spec.rb +93 -0
  36. data/spec/integrations/sdk_token_spec.rb +5 -1
  37. data/spec/integrations/webhook_spec.rb +28 -24
  38. data/spec/onfido/api_spec.rb +14 -25
  39. data/spec/onfido/connection_error_spec.rb +4 -2
  40. data/spec/onfido/options_spec.rb +39 -0
  41. data/spec/onfido/request_error_spec.rb +4 -2
  42. data/spec/spec_helper.rb +3 -5
  43. data/spec/support/fake_onfido_api.rb +63 -49
  44. data/spec/support/fixtures/applicant.json +1 -1
  45. data/spec/support/fixtures/check.json +1 -1
  46. data/spec/support/fixtures/checks.json +1 -1
  47. data/spec/support/fixtures/document.json +1 -1
  48. data/spec/support/fixtures/documents.json +2 -2
  49. data/spec/support/fixtures/live_photo.json +2 -2
  50. data/spec/support/fixtures/live_photos.json +4 -4
  51. data/spec/support/fixtures/live_video.json +2 -2
  52. data/spec/support/fixtures/live_videos.json +2 -2
  53. data/spec/support/fixtures/report.json +1 -1
  54. data/spec/support/fixtures/reports.json +2 -2
  55. data/spec/support/fixtures/webhook.json +1 -1
  56. data/spec/support/fixtures/webhooks.json +2 -2
  57. metadata +11 -29
  58. data/Rakefile +0 -1
  59. data/lib/onfido/configuration.rb +0 -47
  60. data/lib/onfido/null_logger.rb +0 -5
  61. data/spec/integrations/exceptions_spec.rb +0 -72
  62. data/spec/onfido/resource_spec.rb +0 -133
  63. data/spec/onfido_spec.rb +0 -83
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
- ca: "api.ca.onfido.com"
6
- }.freeze
7
-
8
- attr_accessor :api_key, :region, :open_timeout, :read_timeout
9
-
10
- def self.extended(base)
11
- base.reset
12
- end
13
-
14
- def configure
15
- yield self
16
- end
17
-
18
- def reset
19
- self.api_key = nil
20
- self.region = nil
21
- self.open_timeout = 30
22
- self.read_timeout = 80
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}/v3/"
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,72 +0,0 @@
1
- describe Onfido::Resource do
2
- let(:resource) { described_class.new }
3
- let(:api_key) { 'some_key' }
4
-
5
- before { allow(Onfido).to receive(:api_key).and_return(api_key) }
6
-
7
- context '4xx response' do
8
- it 'raises a custom error' do
9
- path = '4xx_response'
10
-
11
- expect { resource.get(path: path) }.
12
- to raise_error(Onfido::RequestError, 'Something went wrong')
13
- end
14
- end
15
-
16
- context 'unexpected error format' do
17
- it 'raises a custom error' do
18
- path = 'unexpected_error_format'
19
-
20
- expect { resource.get(path: path) }.
21
- to raise_error(Onfido::RequestError, /response code was 400/)
22
- end
23
- end
24
-
25
- context 'unparseable JSON 5xx' do
26
- it 'raises a server error' do
27
- path = 'unparseable_response'
28
-
29
- expect { resource.get(path: path) }.
30
- to raise_error(Onfido::ServerError, /response code was 504/)
31
- end
32
- end
33
-
34
- context 'timeout' do
35
- before do
36
- allow(RestClient::Request).
37
- to receive(:execute).
38
- and_raise(RestClient::RequestTimeout)
39
- end
40
-
41
- it 'raises a ConnectionError' do
42
- expect { resource.get(path: Onfido.endpoint) }.
43
- to raise_error(Onfido::ConnectionError, /Could not connect/)
44
- end
45
- end
46
-
47
- context 'broken connection' do
48
- before do
49
- allow(RestClient::Request).
50
- to receive(:execute).
51
- and_raise(RestClient::ServerBrokeConnection)
52
- end
53
-
54
- it 'raises a ConnectionError' do
55
- expect { resource.get(path: Onfido.endpoint) }.
56
- to raise_error(Onfido::ConnectionError, /connection to the server/)
57
- end
58
- end
59
-
60
- context "bad SSL certificate" do
61
- before do
62
- allow(RestClient::Request).
63
- to receive(:execute).
64
- and_raise(RestClient::SSLCertificateNotVerified.new(nil))
65
- end
66
-
67
- it 'raises a ConnectionError' do
68
- expect { resource.get(path: Onfido.endpoint) }.
69
- to raise_error(Onfido::ConnectionError, /SSL certificate/)
70
- end
71
- end
72
- end
@@ -1,133 +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/v3/' }
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 '#method_missing' do
29
- %i(patch).each do |method|
30
- context "for unsupported HTTP method: #{method}" do
31
- it 'raises an error' do
32
- expect do
33
- resource.public_send(method, path: endpoint)
34
- end.to raise_error(NoMethodError)
35
- end
36
- end
37
- end
38
- end
39
-
40
- describe "API key" do
41
- subject(:resource) { described_class.new(specific_api_key) }
42
-
43
- before do
44
- expect(RestClient::Request).to receive(:execute).with(
45
- url: url,
46
- payload: Rack::Utils.build_query(payload),
47
- method: :get,
48
- headers: resource.send(:headers),
49
- open_timeout: 30,
50
- timeout: 80
51
- ).and_call_original
52
-
53
- WebMock.stub_request(:get, url).
54
- to_return(body: response.to_json, status: 200)
55
- end
56
-
57
- context "when using a specific key" do
58
- let(:specific_api_key) { "specific_key" }
59
-
60
- it "uses that key when making the request" do
61
- resource.get(path: path, payload: payload)
62
-
63
- expect(WebMock).to have_requested(:get, url).with(
64
- headers: {
65
- 'Authorization' => "Token token=#{specific_api_key}",
66
- 'Accept' => "application/json",
67
- 'User-Agent' => "onfido-ruby/#{Onfido::VERSION}"
68
- }
69
- )
70
- end
71
- end
72
-
73
- context "when not using a specific key" do
74
- let(:specific_api_key) { nil }
75
-
76
- it "uses the general config key when making the request" do
77
- resource.get(path: path, payload: payload)
78
-
79
- expect(WebMock).to have_requested(:get, url).with(
80
- headers: {
81
- 'Authorization' => "Token token=#{api_key}",
82
- 'Accept' => "application/json",
83
- 'User-Agent' => "onfido-ruby/#{Onfido::VERSION}"
84
- }
85
- )
86
- end
87
- end
88
- end
89
-
90
- describe "valid http methods" do
91
- %i(get post put delete).each do |method|
92
- context "for supported HTTP method: #{method}" do
93
- context "with a success response" do
94
- before do
95
- expect(RestClient::Request).to receive(:execute).
96
- with(
97
- url: url,
98
- payload: Rack::Utils.build_query(payload),
99
- method: method,
100
- headers: resource.send(:headers),
101
- open_timeout: 30,
102
- timeout: 80
103
- ).and_call_original
104
-
105
- WebMock.stub_request(method, url).
106
- to_return(body: response.to_json,
107
- status: 200,
108
- headers: { "Content-Type" => "application/json" })
109
- end
110
-
111
- it 'makes a request to an endpoint' do
112
- expect(resource.public_send(method, path: path, payload: payload)).
113
- to eq(response)
114
- end
115
- end
116
-
117
- context "with a timeout error response" do
118
- before do
119
- allow_any_instance_of(RestClient::ExceptionWithResponse).
120
- to receive(:response).and_return(double(body: "", code: "408"))
121
- expect(RestClient::Request).to receive(:execute).
122
- and_raise(RestClient::ExceptionWithResponse)
123
- end
124
-
125
- it "raises a ConnectionError" do
126
- expect { resource.public_send(method, path: path, payload: payload) }.
127
- to raise_error(Onfido::ConnectionError)
128
- end
129
- end
130
- end
131
- end
132
- end
133
- end
data/spec/onfido_spec.rb DELETED
@@ -1,83 +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/v3/') }
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 'using the US region' do
31
- it 'should change endpoint' do
32
- onfido.region = 'us'
33
- expect(onfido.endpoint).to eq('https://api.us.onfido.com/v3/')
34
- end
35
- end
36
-
37
- describe 'using the CA region' do
38
- it 'should change endpoint' do
39
- onfido.region = 'ca'
40
- expect(onfido.endpoint).to eq('https://api.ca.onfido.com/v3/')
41
- end
42
- end
43
-
44
- describe 'using an unsupported region' do
45
- it 'should change endpoint' do
46
- onfido.region = 'de'
47
- expect { onfido.endpoint }.
48
- to raise_error('The region "de" is not currently supported')
49
- end
50
- end
51
-
52
- describe 'using an old API token' do
53
- it 'should use old endpoint' do
54
- onfido.api_key = "live_asdfghjkl1234567890qwertyuiop"
55
- expect(onfido.endpoint).to eq('https://api.onfido.com/v3/')
56
- end
57
- end
58
-
59
- describe '.logger' do
60
- context 'when an option is passed' do
61
- context 'when the option passed behaves like a logger' do
62
- let(:logger_like) { double('LoggerLike', :<< => nil) }
63
-
64
- it 'returns the option' do
65
- onfido.logger = logger_like
66
- expect(onfido.logger).to eq(logger_like)
67
- end
68
- end
69
-
70
- context 'when the option passed does not behave like a logger' do
71
- let(:non_logger) { double('NotLogger') }
72
-
73
- it 'raises an error' do
74
- expect { onfido.logger = non_logger }.
75
- to raise_error(
76
- "#{non_logger.class} doesn't seem to behave like a logger!"
77
- )
78
- end
79
- end
80
- end
81
- end
82
- end
83
- end