Skip to content

Commit

Permalink
add a validation helper for client_id
Browse files Browse the repository at this point in the history
  • Loading branch information
Rimian Perkins committed Sep 9, 2022
1 parent 9eba2a1 commit 9017835
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
6 changes: 3 additions & 3 deletions lib/xero-ruby/api_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def payroll_nz_api
@config.base_url = @config.payroll_nz_url
XeroRuby::PayrollNzApi.new(self)
end

def payroll_uk_api
@config.base_url = @config.payroll_uk_url
XeroRuby::PayrollUkApi.new(self)
Expand Down Expand Up @@ -142,7 +142,7 @@ def set_token_set(token_set)

set_access_token(token_set[:access_token]) if token_set[:access_token]
set_id_token(token_set[:id_token]) if token_set[:id_token]

return true
end

Expand Down Expand Up @@ -460,7 +460,7 @@ def deserialize(response, return_type, api_client)
else
raise e
end
end
end

convert_to_type(data, return_type, api_client)
end
Expand Down
46 changes: 31 additions & 15 deletions spec/api_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,22 @@
end
end

describe 'validations' do
subject { XeroRuby::ApiClient.new(config: {}, credentials: credentials) }

context 'when the client id is not valid' do
let(:credentials) { { client_id: '' } }

it { is_expected.not_to be_client_id_valid }
end

context 'when the client id is valid' do
let(:credentials) { { client_id: 'AAAAAA9E3FC416CF84283851A1BB7185' } }

it { is_expected.to be_client_id_valid }
end
end

describe 'api_client helper functions' do
let(:api_client) { XeroRuby::ApiClient.new }
let(:token_set) { {'access_token': 'eyx.authorization.data', 'id_token': 'eyx.authentication.data', 'refresh_token': 'REFRESHMENTS'} }
Expand All @@ -114,7 +130,7 @@
}]
}

before do
before do
allow(api_client).to receive(:token_request).and_return(token_set)
end

Expand Down Expand Up @@ -153,33 +169,33 @@

it "sets and resets the base url based on endpoint usage of the same client" do
expect(api_client).to receive(:call_api).and_return(connections)

api_client.accounting_api
expect(api_client.config.base_url).to eq('https://api.xero.com/api.xro/2.0')

api_client.asset_api
expect(api_client.config.base_url).to eq('https://api.xero.com/assets.xro/1.0')

api_client.project_api
expect(api_client.config.base_url).to eq('https://api.xero.com/projects.xro/2.0/')

api_client.files_api
expect(api_client.config.base_url).to eq('https://api.xero.com/files.xro/1.0/')

api_client.payroll_au_api
expect(api_client.config.base_url).to eq('https://api.xero.com/payroll.xro/1.0/')

api_client.payroll_nz_api
expect(api_client.config.base_url).to eq('https://api.xero.com/payroll.xro/2.0/')

api_client.payroll_uk_api
expect(api_client.config.base_url).to eq('https://api.xero.com/payroll.xro/2.0/')

api_client.finance_api
expect(api_client.config.base_url).to eq('https://api.xero.com/finance.xro/1.0/')

api_client.connections
expect(api_client.config.base_url).to eq('https://api.xero.com')
expect(api_client.config.base_url).to eq('https://api.xero.com')
end

it "does not mutate the original opts hash" do
Expand Down Expand Up @@ -282,7 +298,7 @@
}
]
}

json_after = {
"LineItems":[
{
Expand Down Expand Up @@ -486,7 +502,7 @@

let(:tkn_set_1){{'id_token': "abc.123.1", 'access_token': "xxx.yyy.zzz.111"}}
let(:tkn_set_2){{'id_token': "efg.456.2", 'access_token': "xxx.yyy.zzz.222"}}

describe 'when configuration is changed, other instantiations of the client are not affected' do
it 'applies to #set_access_token' do
expect(api_client_1.access_token).to eq(nil)
Expand Down Expand Up @@ -522,7 +538,7 @@
expect(api_client_2.id_token).to eq("id_token_2")
end

it 'applies to #set_token_set' do
it 'applies to #set_token_set' do
expect(api_client_1.token_set).to eq(nil)
expect(api_client_2.token_set).to eq(nil)

Expand All @@ -534,15 +550,15 @@
expect(api_client_1.token_set).to eq(tkn_set_1.with_indifferent_access)
expect(api_client_2.token_set).to eq(tkn_set_2.with_indifferent_access)
end

it 'applies to #base_url' do
expect(api_client_1.config.base_url).to eq(nil)
expect(api_client_2.config.base_url).to eq(nil)

api_client_1.accounting_api
expect(api_client_1.config.base_url).to eq(api_client_1.config.accounting_url)
expect(api_client_2.config.base_url).to eq(nil)

api_client_2.files_api
expect(api_client_1.config.base_url).to eq(api_client_1.config.accounting_url)
expect(api_client_2.config.base_url).to eq(api_client_1.config.files_url)
Expand Down

0 comments on commit 9017835

Please sign in to comment.