Skip to content

Commit 11c8fbe

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

File tree

2 files changed

+35
-15
lines changed

2 files changed

+35
-15
lines changed

lib/xero-ruby/api_client.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ def append_to_default_config(default_config, user_config)
5656
config
5757
end
5858

59+
def client_id_valid?
60+
@client_id.match?(/^[A-F,0-9]{32}$/)
61+
end
62+
5963
def authorization_url
6064
url = URI.parse(@config.login_url)
6165
url.query = URI.encode_www_form(

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)