addressfinder 1.6.2 → 1.8.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,95 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe AddressFinder::AddressAutocomplete do
4
+ before do
5
+ AddressFinder.configure do |af|
6
+ af.api_key = 'XXX'
7
+ af.api_secret = 'YYY'
8
+ af.default_country = 'au'
9
+ end
10
+ end
11
+
12
+ describe '#build_request' do
13
+ let(:locator){ AddressFinder::AddressAutocomplete.new(params: args, http: http) }
14
+ let(:http){ AddressFinder::HTTP.new(AddressFinder.configuration) }
15
+
16
+ subject(:request_uri){ locator.send(:build_request) }
17
+
18
+ context 'with minimal arguments' do
19
+ let(:args){ {q: '186 willis'} }
20
+
21
+ it { expect(request_uri).to eq('/api/au/address/autocomplete.json?q=186+willis&key=XXX&secret=YYY') }
22
+ end
23
+
24
+ context 'with more arguments' do
25
+ let(:args){ {q: '186 willis st', au_paf: 1, max: 10} }
26
+
27
+ it { expect(request_uri).to eq('/api/au/address/autocomplete.json?q=186+willis+st&au_paf=1&max=10&key=XXX&secret=YYY') }
28
+ end
29
+
30
+ context 'with a country override' do
31
+ let(:args){ {q: '186 willis st', country: 'au'} }
32
+
33
+ it { expect(request_uri).to eq('/api/au/address/autocomplete.json?q=186+willis+st&key=XXX&secret=YYY') }
34
+ end
35
+
36
+ context 'with a key override' do
37
+ let(:args){ {q: '186 willis st', key: 'AAA'} }
38
+
39
+ it { expect(request_uri).to eq('/api/au/address/autocomplete.json?q=186+willis+st&key=AAA&secret=YYY') }
40
+ end
41
+
42
+ context 'with a secret override' do
43
+ let(:args){ {q: '186 willis st', secret: 'BBB'} }
44
+
45
+ it { expect(request_uri).to eq('/api/au/address/autocomplete.json?q=186+willis+st&secret=BBB&key=XXX') }
46
+ end
47
+ end
48
+
49
+ describe '#validate_params' do
50
+ let(:locator){ AddressFinder::AddressAutocomplete.new(params: args, http: http) }
51
+ let(:http){ AddressFinder::HTTP.new(AddressFinder.configuration) }
52
+
53
+ subject(:validate_params){ locator.send(:validate_params) }
54
+
55
+ context 'with wrong country' do
56
+ let(:args){ {q: '186 willis st', country: 'nz'} }
57
+
58
+ it { expect{validate_params}.to raise_error(AddressFinder::RequestRejectedError) }
59
+ end
60
+
61
+ context 'with correct country' do
62
+ let(:args){ {q: '186 willis st', country: 'au'} }
63
+
64
+ it { expect{validate_params}.not_to raise_error }
65
+ end
66
+ end
67
+
68
+ describe '#build_result' do
69
+ let(:locator){ AddressFinder::AddressSearch.new(params: {q: 'ignored'}, http: nil) }
70
+
71
+ before do
72
+ locator.send('response_body=', body)
73
+ locator.send('response_status=', status)
74
+ locator.send(:build_result)
75
+ end
76
+
77
+ subject(:results){ locator.results }
78
+
79
+ context 'with completions' do
80
+ let(:body){ '{"completions":[{"a":"184 William Jones Drive, Otangarei, Whangarei 0112","pxid":"2-.9.2U.F.F.2I","v":1},{"a":"184 Williams Street, Kaiapoi 7630","pxid":"2-.3.1q.2.4G.4c","v":0},{"a":"184 Willis Street, Te Aro, Wellington 6011","pxid":"2-.F.1W.p.1D.1W","v":0}],"paid":true}' }
81
+ let(:status){ '200' }
82
+
83
+ it { expect(results.size).to eq(3) }
84
+ it { expect(results.first.class).to eq(AddressFinder::AddressSearch::Result) }
85
+ it { expect(results.first.a).to eq("184 William Jones Drive, Otangarei, Whangarei 0112") }
86
+ end
87
+
88
+ context 'with no completions' do
89
+ let(:body){ '{"completions":[],"paid":true}' }
90
+ let(:status){ '200' }
91
+
92
+ it { expect(results).to eq([]) }
93
+ end
94
+ end
95
+ end
@@ -28,9 +28,9 @@ RSpec.describe AddressFinder::Bulk do
28
28
  let(:response){ double(:response, body: %Q({"success": true}), code: "200") }
29
29
  let(:block){
30
30
  Proc.new do |proxy|
31
- proxy.cleanse(q: "1 Willis")
32
- proxy.cleanse(q: "2 Willis")
33
- proxy.cleanse(q: "3 Willis")
31
+ proxy.verification(q: "1 Willis")
32
+ proxy.verification(q: "2 Willis")
33
+ proxy.verification(q: "3 Willis")
34
34
  end
35
35
  }
36
36
 
@@ -38,7 +38,31 @@ RSpec.describe AddressFinder::Bulk do
38
38
  expect(net_http).to receive(:do_start).once.and_call_original
39
39
  expect(net_http).to receive(:transport_request).exactly(3).times.and_return(response)
40
40
  expect(net_http).to receive(:do_finish).once.and_call_original
41
- AddressFinder::Bulk.new(http: http, &block).perform
41
+ AddressFinder::Bulk.new(http: http, verification_version: 'v2', default_country: 'au', &block).perform
42
+ end
43
+
44
+ it "calls the correct class with v2 verification and au default" do
45
+ allow(net_http).to receive(:do_start).once.and_call_original
46
+ allow(net_http).to receive(:transport_request).exactly(3).times.and_return(response)
47
+ allow(net_http).to receive(:do_finish).once.and_call_original
48
+ expect(AddressFinder::V2::Au::Verification).to receive(:new).exactly(3).times.and_call_original
49
+ AddressFinder::Bulk.new(http: http, verification_version: 'v2', default_country: 'au', &block).perform
50
+ end
51
+
52
+ it "calls the correct class with v2 verification and nz default" do
53
+ allow(net_http).to receive(:do_start).once.and_call_original
54
+ allow(net_http).to receive(:transport_request).exactly(3).times.and_return(response)
55
+ allow(net_http).to receive(:do_finish).once.and_call_original
56
+ expect(AddressFinder::Verification).to receive(:new).exactly(3).times.and_call_original
57
+ AddressFinder::Bulk.new(http: http, verification_version: 'v2', default_country: 'nz', &block).perform
58
+ end
59
+
60
+ it "calls the correct class without a verification version" do
61
+ allow(net_http).to receive(:do_start).once.and_call_original
62
+ allow(net_http).to receive(:transport_request).exactly(3).times.and_return(response)
63
+ allow(net_http).to receive(:do_finish).once.and_call_original
64
+ expect(AddressFinder::Verification).to receive(:new).exactly(3).times.and_call_original
65
+ AddressFinder::Bulk.new(http: http, verification_version: nil, default_country: 'au', &block).perform
42
66
  end
43
67
 
44
68
  it "re-establishes the http connection and continues where we left off when a Net::OpenTimeout, Net::ReadTimeout or SocketError is raised" do
@@ -50,8 +74,42 @@ RSpec.describe AddressFinder::Bulk do
50
74
  expect(net_http).to receive(:transport_request).once.and_raise(SocketError) # Retry 2 Willis (error)
51
75
  expect(net_http).to receive(:transport_request).exactly(2).and_return(response) # Retry 2 Willis (success) & 3 Willis (success)
52
76
  expect(net_http).to receive(:do_finish).exactly(4).times.and_call_original
53
- AddressFinder::Bulk.new(http: http, &block).perform
77
+ AddressFinder::Bulk.new(http: http, verification_version: 'v2', default_country: 'au', &block).perform
78
+ end
79
+ end
80
+
81
+ context "with the deprecated cleanse method" do
82
+ let(:response){ double(:response, body: %Q({"success": true}), code: "200") }
83
+ let(:block){
84
+ Proc.new do |proxy|
85
+ proxy.cleanse(q: "1 Willis")
86
+ end
87
+ }
88
+
89
+ it "has the same behaviour as the verification method" do
90
+ expect(net_http).to receive(:do_start).once.and_call_original
91
+ expect(net_http).to receive(:transport_request).once.and_return(response)
92
+ expect(net_http).to receive(:do_finish).once.and_call_original
93
+ expect(AddressFinder::Verification).to receive(:new).exactly(1).times.and_call_original
94
+ AddressFinder::Bulk.new(http: http, verification_version: nil, default_country: 'au', &block).perform
95
+ end
96
+ end
97
+
98
+ context "with a country override and v2 in the config" do
99
+ let(:response){ double(:response, body: %Q({"success": true}), code: "200") }
100
+ let(:block){
101
+ Proc.new do |proxy|
102
+ proxy.verification(q: "1 Willis", country: "au")
103
+ end
104
+ }
105
+
106
+ it "has the same behaviour as the verification method" do
107
+ expect(net_http).to receive(:do_start).once.and_call_original
108
+ expect(net_http).to receive(:transport_request).once.and_return(response)
109
+ expect(net_http).to receive(:do_finish).once.and_call_original
110
+ expect(AddressFinder::V2::Au::Verification).to receive(:new).exactly(1).times.and_call_original
111
+ AddressFinder::Bulk.new(http: http, verification_version: "v2", default_country: 'nz', &block).perform
54
112
  end
55
113
  end
56
114
  end
57
- end
115
+ end
@@ -0,0 +1,187 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe AddressFinder::V2::Au::Verification do
4
+ before do
5
+ AddressFinder.configure do |af|
6
+ af.api_key = 'XXX'
7
+ af.api_secret = 'YYY'
8
+ af.timeout = 5
9
+ af.retries = 3
10
+ end
11
+ end
12
+
13
+ let(:verification_module){ AddressFinder::V2::Au::Verification.new(args) }
14
+ let(:http){ AddressFinder::HTTP.new(AddressFinder.configuration) }
15
+ let(:net_http){ http.send(:net_http) }
16
+
17
+ describe '#execute_request' do
18
+ let(:args){ {q: "186 Willis Street", http: http} }
19
+
20
+ before do
21
+ WebMock.allow_net_connect!(net_http_connect_on_start: true)
22
+ allow(http).to receive(:sleep)
23
+ allow(verification_module).to receive(:request_uri).and_return("/test/path")
24
+ expect(http).to_not receive(:re_establish_connection)
25
+ end
26
+
27
+ after do
28
+ WebMock.disable_net_connect!
29
+ end
30
+
31
+ subject(:execute_request){ verification_module.send(:execute_request) }
32
+
33
+ it "retries an errored request another time before succeeding" do
34
+ expect(net_http).to receive(:do_start).twice.and_call_original
35
+ expect(net_http).to receive(:transport_request).once.and_raise(Net::OpenTimeout)
36
+ expect(net_http).to receive(:transport_request).once.and_return(double(:response, body: "OK", code: "200"))
37
+ expect(net_http).to receive(:do_finish).twice.and_call_original
38
+ execute_request
39
+ end
40
+
41
+ it "re-raises a Net::OpenTimeout error after 3 retries" do
42
+ expect(net_http).to receive(:do_start).exactly(4).times.and_call_original
43
+ expect(net_http).to receive(:transport_request).exactly(4).times.and_raise(Net::OpenTimeout)
44
+ expect(net_http).to receive(:do_finish).exactly(4).times.and_call_original
45
+ expect{execute_request}.to raise_error(Net::OpenTimeout)
46
+ end
47
+
48
+ it "re-raises a Net::ReadTimeout error after 3 retries" do
49
+ expect(net_http).to receive(:do_start).exactly(4).times.and_call_original
50
+ expect(net_http).to receive(:transport_request).exactly(4).times.and_raise(Net::ReadTimeout)
51
+ expect(net_http).to receive(:do_finish).exactly(4).times.and_call_original
52
+ expect{execute_request}.to raise_error(Net::ReadTimeout)
53
+ end
54
+
55
+ it "re-raises a SocketError error after 3 retries" do
56
+ expect(net_http).to receive(:do_start).exactly(4).times.and_call_original
57
+ expect(net_http).to receive(:transport_request).exactly(4).times.and_raise(SocketError)
58
+ expect(net_http).to receive(:do_finish).exactly(4).times.and_call_original
59
+ expect{execute_request}.to raise_error(SocketError)
60
+ end
61
+ end
62
+
63
+ describe '#build_request' do
64
+ subject(:request_uri){ verification_module.send(:build_request) }
65
+
66
+ context 'with minimal arguments' do
67
+ let(:args){ {q: '186 willis st', http: http} }
68
+
69
+ it { expect(request_uri).to eq('/api/au/address/v2/verification?q=186+willis+st&key=XXX&secret=YYY&format=json') }
70
+ end
71
+
72
+ context 'with more arguments' do
73
+ let(:args){ {q: '186 willis st', census: '2011', http: http} }
74
+
75
+ it { expect(request_uri).to eq('/api/au/address/v2/verification?q=186+willis+st&census=2011&key=XXX&secret=YYY&format=json') }
76
+ end
77
+
78
+ context 'with a state codes as an array' do
79
+ let(:args){ {q: '186 willis st', state_codes: ['ACT','NSW'], http: http} }
80
+
81
+ it { expect(request_uri).to eq('/api/au/address/v2/verification?q=186+willis+st&key=XXX&secret=YYY&state_codes[]=ACT&state_codes[]=NSW&format=json') }
82
+ end
83
+
84
+ context 'with a reserved character in the query' do
85
+ let(:args){ {q: '186=willis st', state_codes: ['ACT','NSW'], http: http} }
86
+
87
+ it { expect(request_uri).to eq('/api/au/address/v2/verification?q=186%3Dwillis+st&key=XXX&secret=YYY&state_codes[]=ACT&state_codes[]=NSW&format=json') }
88
+ end
89
+
90
+ context 'with a state codes as a string' do
91
+ let(:args){ {q: '186 willis st', state_codes: 'ACT,NSW', http: http} }
92
+
93
+ it { expect(request_uri).to eq('/api/au/address/v2/verification?q=186+willis+st&key=XXX&secret=YYY&state_codes=ACT%2CNSW&format=json') }
94
+ end
95
+
96
+ context 'with a key override' do
97
+ let(:args){ {q: '186 willis st', key: 'AAA', http: http} }
98
+
99
+ it { expect(request_uri).to eq('/api/au/address/v2/verification?q=186+willis+st&key=AAA&secret=YYY&format=json') }
100
+ end
101
+
102
+ context 'with a secret override' do
103
+ let(:args){ {q: '186 willis st', secret: 'BBB', http: http} }
104
+
105
+ it { expect(request_uri).to eq('/api/au/address/v2/verification?q=186+willis+st&key=XXX&secret=BBB&format=json') }
106
+ end
107
+
108
+ context 'with a domain given' do
109
+ let(:args){ {q: '123', domain: 'testdomain.com', http: http} }
110
+
111
+ it { expect(request_uri).to eq('/api/au/address/v2/verification?q=123&domain=testdomain.com&key=XXX&secret=YYY&format=json') }
112
+
113
+ context 'given in the AF configuration' do
114
+
115
+ let(:args){ {q: '123', http: http} }
116
+
117
+ it 'should use the config domain if set' do
118
+ AddressFinder.configuration.domain = 'anotherdomain.com'
119
+ # expect(request_uri).to eq('/api/au/address/v2/verification?q=123&domain=anotherdomain.com&key=XXX&secret=YYY&format=json')
120
+ AddressFinder.configuration.domain = nil # set back to nil after
121
+ end
122
+ end
123
+ end
124
+
125
+ context 'with a post_box exclusion' do
126
+ let(:args){ {q: '186 willis st', post_box: '0', http: http} }
127
+
128
+ it { expect(request_uri).to eq('/api/au/address/v2/verification?q=186+willis+st&post_box=0&key=XXX&secret=YYY&format=json') }
129
+ end
130
+
131
+ context 'with a gnaf request' do
132
+ let(:args){ {q: '186 willis st', gnaf: '1', http: http} }
133
+
134
+ it { expect(request_uri).to eq('/api/au/address/v2/verification?q=186+willis+st&key=XXX&secret=YYY&gnaf=1&format=json') }
135
+ end
136
+
137
+ context 'with a paf request' do
138
+ let(:args){ {q: '186 willis st', paf: '1', http: http} }
139
+
140
+ it { expect(request_uri).to eq('/api/au/address/v2/verification?q=186+willis+st&key=XXX&secret=YYY&paf=1&format=json') }
141
+ end
142
+
143
+ context 'with a all args included request' do
144
+ let(:args){ {q: '186 willis st', paf: '1', gnaf:'1', post_box:'0', state_codes:'ACT', census: '2016', domain: 'mysite.com', gps: '1', extended: '1', http: http} }
145
+
146
+ it { expect(request_uri).to eq('/api/au/address/v2/verification?q=186+willis+st&post_box=0&census=2016&domain=mysite.com&key=XXX&secret=YYY&paf=1&gnaf=1&gps=1&extended=1&state_codes=ACT&format=json') }
147
+ end
148
+ end
149
+
150
+ describe '#build_result' do
151
+ let(:args){ {q: 'ignored', http: nil} }
152
+
153
+ before do
154
+ verification_module.send('response_body=', body)
155
+ verification_module.send('response_status=', status)
156
+ end
157
+
158
+ subject(:result){ verification_module.send(:build_result) }
159
+
160
+ context 'with a successful nz result' do
161
+ let(:body){ '{"matched": true, "postal_address": "Texas"}' }
162
+ let(:status){ '200' }
163
+
164
+ it { expect(result.class).to eq(AddressFinder::V2::Au::Verification::Result) }
165
+
166
+ it { expect(result.matched).to eq(true) }
167
+
168
+ it { expect(result.postal_address).to eq("Texas") }
169
+ end
170
+
171
+ context 'with a successful au result' do
172
+ let(:body){ %Q({"matched": true, "success": true, "address": {"full_address": "Texas"}}) }
173
+ let(:status){ '200' }
174
+
175
+ it { expect(result.class).to eq(AddressFinder::V2::Au::Verification::Result) }
176
+
177
+ it { expect(result.full_address).to eq("Texas") }
178
+ end
179
+
180
+ context 'with an unfound result' do
181
+ let(:body){ '{"matched": false}' }
182
+ let(:status){ '200' }
183
+
184
+ it { expect(result).to eq(nil) }
185
+ end
186
+ end
187
+ end
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- RSpec.describe AddressFinder::Cleanse do
3
+ RSpec.describe AddressFinder::Verification do
4
4
  before do
5
5
  AddressFinder.configure do |af|
6
6
  af.api_key = 'XXX'
@@ -11,7 +11,7 @@ RSpec.describe AddressFinder::Cleanse do
11
11
  end
12
12
  end
13
13
 
14
- let(:cleanser){ AddressFinder::Cleanse.new(args) }
14
+ let(:verification_module){ AddressFinder::Verification.new(args) }
15
15
  let(:http){ AddressFinder::HTTP.new(AddressFinder.configuration) }
16
16
  let(:net_http){ http.send(:net_http) }
17
17
 
@@ -21,7 +21,7 @@ RSpec.describe AddressFinder::Cleanse do
21
21
  before do
22
22
  WebMock.allow_net_connect!(net_http_connect_on_start: true)
23
23
  allow(http).to receive(:sleep)
24
- allow(cleanser).to receive(:request_uri).and_return("/test/path")
24
+ allow(verification_module).to receive(:request_uri).and_return("/test/path")
25
25
  expect(http).to_not receive(:re_establish_connection)
26
26
  end
27
27
 
@@ -29,7 +29,7 @@ RSpec.describe AddressFinder::Cleanse do
29
29
  WebMock.disable_net_connect!
30
30
  end
31
31
 
32
- subject(:execute_request){ cleanser.send(:execute_request) }
32
+ subject(:execute_request){ verification_module.send(:execute_request) }
33
33
 
34
34
  it "retries an errored request another time before succeeding" do
35
35
  expect(net_http).to receive(:do_start).twice.and_call_original
@@ -62,60 +62,60 @@ RSpec.describe AddressFinder::Cleanse do
62
62
  end
63
63
 
64
64
  describe '#build_request' do
65
- subject(:request_uri){ cleanser.send(:build_request) }
65
+ subject(:request_uri){ verification_module.send(:build_request) }
66
66
 
67
67
  context 'with minimal arguments' do
68
68
  let(:args){ {q: '186 willis st', http: http} }
69
69
 
70
- it { expect(request_uri).to eq('/api/nz/address/cleanse?q=186+willis+st&format=json&key=XXX&secret=YYY') }
70
+ it { expect(request_uri).to eq('/api/nz/address/verification?q=186+willis+st&key=XXX&secret=YYY&format=json') }
71
71
  end
72
72
 
73
73
  context 'with more arguments' do
74
74
  let(:args){ {q: '186 willis st', delivered: true, region_code: 'A', census: '2013', http: http} }
75
75
 
76
- it { expect(request_uri).to eq('/api/nz/address/cleanse?q=186+willis+st&delivered=true&region_code=A&census=2013&format=json&key=XXX&secret=YYY') }
76
+ it { expect(request_uri).to eq('/api/nz/address/verification?q=186+willis+st&census=2013&key=XXX&secret=YYY&delivered=true&region_code=A&format=json') }
77
77
  end
78
78
 
79
79
  context 'with a country override' do
80
80
  let(:args){ {q: '186 willis st', country: 'au', http: http} }
81
81
 
82
- it { expect(request_uri).to eq('/api/au/address/cleanse?q=186+willis+st&format=json&key=XXX&secret=YYY') }
82
+ it { expect(request_uri).to eq('/api/au/address/verification?q=186+willis+st&key=XXX&secret=YYY&format=json') }
83
83
  end
84
84
 
85
85
  context 'with a state codes as an array' do
86
86
  let(:args){ {q: '186 willis st', country: 'au', state_codes: ['ACT','NSW'], http: http} }
87
87
 
88
- it { expect(request_uri).to eq('/api/au/address/cleanse?q=186+willis+st&state_codes[]=ACT&state_codes[]=NSW&format=json&key=XXX&secret=YYY') }
88
+ it { expect(request_uri).to eq('/api/au/address/verification?q=186+willis+st&key=XXX&secret=YYY&state_codes[]=ACT&state_codes[]=NSW&format=json') }
89
89
  end
90
90
 
91
91
  context 'with a reserved character in the query' do
92
92
  let(:args){ {q: '186=willis st', country: 'au', state_codes: ['ACT','NSW'], http: http} }
93
93
 
94
- it { expect(request_uri).to eq('/api/au/address/cleanse?q=186%3Dwillis+st&state_codes[]=ACT&state_codes[]=NSW&format=json&key=XXX&secret=YYY') }
94
+ it { expect(request_uri).to eq('/api/au/address/verification?q=186%3Dwillis+st&key=XXX&secret=YYY&state_codes[]=ACT&state_codes[]=NSW&format=json') }
95
95
  end
96
96
 
97
97
  context 'with a state codes as a string' do
98
98
  let(:args){ {q: '186 willis st', country: 'au', state_codes: 'ACT,NSW', http: http} }
99
99
 
100
- it { expect(request_uri).to eq('/api/au/address/cleanse?q=186+willis+st&state_codes=ACT%2CNSW&format=json&key=XXX&secret=YYY') }
100
+ it { expect(request_uri).to eq('/api/au/address/verification?q=186+willis+st&key=XXX&secret=YYY&state_codes=ACT%2CNSW&format=json') }
101
101
  end
102
102
 
103
103
  context 'with a key override' do
104
104
  let(:args){ {q: '186 willis st', key: 'AAA', http: http} }
105
105
 
106
- it { expect(request_uri).to eq('/api/nz/address/cleanse?q=186+willis+st&format=json&key=AAA&secret=YYY') }
106
+ it { expect(request_uri).to eq('/api/nz/address/verification?q=186+willis+st&key=AAA&secret=YYY&format=json') }
107
107
  end
108
108
 
109
109
  context 'with a secret override' do
110
110
  let(:args){ {q: '186 willis st', secret: 'BBB', http: http} }
111
111
 
112
- it { expect(request_uri).to eq('/api/nz/address/cleanse?q=186+willis+st&format=json&key=XXX&secret=BBB') }
112
+ it { expect(request_uri).to eq('/api/nz/address/verification?q=186+willis+st&key=XXX&secret=BBB&format=json') }
113
113
  end
114
114
 
115
115
  context 'with a domain given' do
116
116
  let(:args){ {q: '123', domain: 'testdomain.com', http: http} }
117
117
 
118
- it { expect(request_uri).to eq('/api/nz/address/cleanse?q=123&domain=testdomain.com&format=json&key=XXX&secret=YYY') }
118
+ it { expect(request_uri).to eq('/api/nz/address/verification?q=123&domain=testdomain.com&key=XXX&secret=YYY&format=json') }
119
119
 
120
120
  context 'given in the AF configuration' do
121
121
 
@@ -123,7 +123,7 @@ RSpec.describe AddressFinder::Cleanse do
123
123
 
124
124
  it 'should use the config domain if set' do
125
125
  AddressFinder.configuration.domain = 'anotherdomain.com'
126
- expect(request_uri).to eq('/api/nz/address/cleanse?q=123&domain=anotherdomain.com&format=json&key=XXX&secret=YYY')
126
+ expect(request_uri).to eq('/api/nz/address/verification?q=123&domain=anotherdomain.com&key=XXX&secret=YYY&format=json')
127
127
  AddressFinder.configuration.domain = nil # set back to nil after
128
128
  end
129
129
  end
@@ -134,17 +134,17 @@ RSpec.describe AddressFinder::Cleanse do
134
134
  let(:args){ {q: 'ignored', http: nil} }
135
135
 
136
136
  before do
137
- cleanser.send('response_body=', body)
138
- cleanser.send('response_status=', status)
137
+ verification_module.send('response_body=', body)
138
+ verification_module.send('response_status=', status)
139
139
  end
140
140
 
141
- subject(:result){ cleanser.send(:build_result) }
141
+ subject(:result){ verification_module.send(:build_result) }
142
142
 
143
143
  context 'with a successful nz result' do
144
144
  let(:body){ '{"matched": true, "postal_address": "Texas"}' }
145
145
  let(:status){ '200' }
146
146
 
147
- it { expect(result.class).to eq(AddressFinder::Cleanse::Result) }
147
+ it { expect(result.class).to eq(AddressFinder::Verification::Result) }
148
148
 
149
149
  it { expect(result.matched).to eq(true) }
150
150
 
@@ -155,7 +155,7 @@ RSpec.describe AddressFinder::Cleanse do
155
155
  let(:body){ %Q({"matched": true, "success": true, "address": {"full_address": "Texas"}}) }
156
156
  let(:status){ '200' }
157
157
 
158
- it { expect(result.class).to eq(AddressFinder::Cleanse::Result) }
158
+ it { expect(result.class).to eq(AddressFinder::Verification::Result) }
159
159
 
160
160
  it { expect(result.full_address).to eq("Texas") }
161
161
  end
@@ -0,0 +1,64 @@
1
+ require 'spec_helper'
2
+ require 'fileutils'
3
+
4
+ RSpec.describe AddressFinder do
5
+ before do
6
+ AddressFinder.configure do |af|
7
+ af.api_key = 'XXX'
8
+ af.api_secret = 'YYY'
9
+ af.default_country = 'nz'
10
+ af.timeout = 5
11
+ af.retries = 5
12
+ end
13
+ end
14
+
15
+ describe '#verification with verification_version configured to "v2"' do
16
+ before do
17
+ AddressFinder.configuration.verification_version = "v2"
18
+ end
19
+
20
+ after do
21
+ AddressFinder.configuration.verification_version = nil # set back to nil after
22
+ end
23
+
24
+ subject(:verification){ AddressFinder.verification(args) }
25
+
26
+ context "with country set to nz" do
27
+ let(:args){ {country: "nz", q: "12 high street sydney"} }
28
+ it "calls the old class" do
29
+ expect(AddressFinder::Verification).to receive_message_chain(:new, :perform, :result)
30
+ subject
31
+ end
32
+ end
33
+
34
+ context "with country set to au" do
35
+ let(:args){ {country: "au", q: "12 high street sydney"} }
36
+ it "calls the v2::Au class" do
37
+ expect(AddressFinder::V2::Au::Verification).to receive_message_chain(:new, :perform, :result)
38
+ subject
39
+ end
40
+ end
41
+ end
42
+
43
+ describe '#verification with verification_version not configured' do
44
+ subject(:verification){ AddressFinder.verification(args) }
45
+
46
+ context "with country set to nz" do
47
+ let(:args){ {country: "nz", q: "12 high street sydney"} }
48
+
49
+ it "calls the old class" do
50
+ expect(AddressFinder::Verification).to receive_message_chain(:new, :perform, :result)
51
+ subject
52
+ end
53
+ end
54
+
55
+ context "with country set to au" do
56
+ let(:args){ {country: "au", q: "12 high street sydney"} }
57
+
58
+ it "calls the old class" do
59
+ expect(AddressFinder::Verification).to receive_message_chain(:new, :perform, :result)
60
+ subject
61
+ end
62
+ end
63
+ end
64
+ end
data/spec/spec_helper.rb CHANGED
@@ -12,6 +12,9 @@ require 'addressfinder'
12
12
 
13
13
  RSpec.configure do |config|
14
14
  config.mock_with :rspec
15
+
16
+ config.filter_run :focus => true
17
+ config.run_all_when_everything_filtered = true
15
18
  end
16
19
 
17
20
  WebMock.disable_net_connect!