Skip to content

Commit 9017835

Browse files
author
Rimian Perkins
committed
add a validation helper for client_id
1 parent 9eba2a1 commit 9017835

File tree

2 files changed

+34
-18
lines changed

2 files changed

+34
-18
lines changed

lib/xero-ruby/api_client.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def payroll_nz_api
9999
@config.base_url = @config.payroll_nz_url
100100
XeroRuby::PayrollNzApi.new(self)
101101
end
102-
102+
103103
def payroll_uk_api
104104
@config.base_url = @config.payroll_uk_url
105105
XeroRuby::PayrollUkApi.new(self)
@@ -142,7 +142,7 @@ def set_token_set(token_set)
142142

143143
set_access_token(token_set[:access_token]) if token_set[:access_token]
144144
set_id_token(token_set[:id_token]) if token_set[:id_token]
145-
145+
146146
return true
147147
end
148148

@@ -460,7 +460,7 @@ def deserialize(response, return_type, api_client)
460460
else
461461
raise e
462462
end
463-
end
463+
end
464464

465465
convert_to_type(data, return_type, api_client)
466466
end

spec/api_client_spec.rb

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,22 @@
100100
end
101101
end
102102

103+
describe 'validations' do
104+
subject { XeroRuby::ApiClient.new(config: {}, credentials: credentials) }
105+
106+
context 'when the client id is not valid' do
107+
let(:credentials) { { client_id: '' } }
108+
109+
it { is_expected.not_to be_client_id_valid }
110+
end
111+
112+
context 'when the client id is valid' do
113+
let(:credentials) { { client_id: 'AAAAAA9E3FC416CF84283851A1BB7185' } }
114+
115+
it { is_expected.to be_client_id_valid }
116+
end
117+
end
118+
103119
describe 'api_client helper functions' do
104120
let(:api_client) { XeroRuby::ApiClient.new }
105121
let(:token_set) { {'access_token': 'eyx.authorization.data', 'id_token': 'eyx.authentication.data', 'refresh_token': 'REFRESHMENTS'} }
@@ -114,7 +130,7 @@
114130
}]
115131
}
116132

117-
before do
133+
before do
118134
allow(api_client).to receive(:token_request).and_return(token_set)
119135
end
120136

@@ -153,33 +169,33 @@
153169

154170
it "sets and resets the base url based on endpoint usage of the same client" do
155171
expect(api_client).to receive(:call_api).and_return(connections)
156-
172+
157173
api_client.accounting_api
158174
expect(api_client.config.base_url).to eq('https://api.xero.com/api.xro/2.0')
159-
175+
160176
api_client.asset_api
161177
expect(api_client.config.base_url).to eq('https://api.xero.com/assets.xro/1.0')
162-
178+
163179
api_client.project_api
164180
expect(api_client.config.base_url).to eq('https://api.xero.com/projects.xro/2.0/')
165-
181+
166182
api_client.files_api
167183
expect(api_client.config.base_url).to eq('https://api.xero.com/files.xro/1.0/')
168-
184+
169185
api_client.payroll_au_api
170186
expect(api_client.config.base_url).to eq('https://api.xero.com/payroll.xro/1.0/')
171-
187+
172188
api_client.payroll_nz_api
173189
expect(api_client.config.base_url).to eq('https://api.xero.com/payroll.xro/2.0/')
174-
190+
175191
api_client.payroll_uk_api
176192
expect(api_client.config.base_url).to eq('https://api.xero.com/payroll.xro/2.0/')
177193

178194
api_client.finance_api
179195
expect(api_client.config.base_url).to eq('https://api.xero.com/finance.xro/1.0/')
180-
196+
181197
api_client.connections
182-
expect(api_client.config.base_url).to eq('https://api.xero.com')
198+
expect(api_client.config.base_url).to eq('https://api.xero.com')
183199
end
184200

185201
it "does not mutate the original opts hash" do
@@ -282,7 +298,7 @@
282298
}
283299
]
284300
}
285-
301+
286302
json_after = {
287303
"LineItems":[
288304
{
@@ -486,7 +502,7 @@
486502

487503
let(:tkn_set_1){{'id_token': "abc.123.1", 'access_token': "xxx.yyy.zzz.111"}}
488504
let(:tkn_set_2){{'id_token': "efg.456.2", 'access_token': "xxx.yyy.zzz.222"}}
489-
505+
490506
describe 'when configuration is changed, other instantiations of the client are not affected' do
491507
it 'applies to #set_access_token' do
492508
expect(api_client_1.access_token).to eq(nil)
@@ -522,7 +538,7 @@
522538
expect(api_client_2.id_token).to eq("id_token_2")
523539
end
524540

525-
it 'applies to #set_token_set' do
541+
it 'applies to #set_token_set' do
526542
expect(api_client_1.token_set).to eq(nil)
527543
expect(api_client_2.token_set).to eq(nil)
528544

@@ -534,15 +550,15 @@
534550
expect(api_client_1.token_set).to eq(tkn_set_1.with_indifferent_access)
535551
expect(api_client_2.token_set).to eq(tkn_set_2.with_indifferent_access)
536552
end
537-
553+
538554
it 'applies to #base_url' do
539555
expect(api_client_1.config.base_url).to eq(nil)
540556
expect(api_client_2.config.base_url).to eq(nil)
541557

542558
api_client_1.accounting_api
543559
expect(api_client_1.config.base_url).to eq(api_client_1.config.accounting_url)
544560
expect(api_client_2.config.base_url).to eq(nil)
545-
561+
546562
api_client_2.files_api
547563
expect(api_client_1.config.base_url).to eq(api_client_1.config.accounting_url)
548564
expect(api_client_2.config.base_url).to eq(api_client_1.config.files_url)

0 commit comments

Comments
 (0)