diff --git a/lib/mangopay.rb b/lib/mangopay.rb index b512e23..fb1a794 100644 --- a/lib/mangopay.rb +++ b/lib/mangopay.rb @@ -49,6 +49,8 @@ module MangoPay # temporary autoload :Temp, 'mangopay/temp' + @configurations = {} + class Configuration attr_accessor :preproduction, :root_url, :client_id, :client_apiKey, @@ -56,6 +58,20 @@ class Configuration :http_max_retries, :http_open_timeout, :logger, :use_ssl + def apply_configuration + MangoPay.configure do |config| + config.preproduction = @preproduction + config.client_id = @client_id + config.client_apiKey = @client_apiKey + config.log_file = @log_file + config.http_timeout = @http_timeout + config.http_max_retries = @http_max_retries + config.http_open_timeout = @http_open_timeout + config.use_ssl = @use_ssl + config.logger = @logger + end + end + def preproduction @preproduction || false end @@ -138,6 +154,23 @@ def ratelimit=(obj) @ratelimit = obj end + # Add MangoPay.Configuration to the list of configs + def add_config(name, config) + @configurations[name] = config + end + + # Fetch a MangoPay configuration from the list of configs. Throw error if not found + def get_config(name) + config = @configurations[name] + raise "Could not find any configuration with name '#{name}'" unless config + config + end + + def remove_config(name) + raise "Could not find any configuration with name '#{name}'" unless @configurations[name] + @configurations[name] = nil + end + # # - +method+: HTTP method; lowercase symbol, e.g. :get, :post etc. # - +url+: the part after Configuration#root_url diff --git a/spec/mangopay/configuration_spec.rb b/spec/mangopay/configuration_spec.rb index 2b23ee7..51d34bd 100644 --- a/spec/mangopay/configuration_spec.rb +++ b/spec/mangopay/configuration_spec.rb @@ -13,6 +13,74 @@ } end + it 'fails when calling with wrong client credentials, but succeeds after applying a new (correct) config' do + # create a wrong configuration + wrong_config = MangoPay::Configuration.new + wrong_config.client_id = 'placeholder' + wrong_config.client_apiKey = '0000' + wrong_config.preproduction = true + + # create a valid configuration + valid_config = MangoPay::Configuration.new + valid_config.client_id = 'sdk-unit-tests' + valid_config.client_apiKey = 'cqFfFrWfCcb7UadHNxx2C9Lo6Djw8ZduLi7J9USTmu8bhxxpju' + valid_config.preproduction = true + + # add the 2 configs to the list of MangoPay configs + MangoPay::add_config('wrong', wrong_config) + MangoPay::add_config('valid', valid_config) + + # apply wrong config + MangoPay::get_config('wrong').apply_configuration + + expect { + MangoPay::User.fetch() + }.to raise_error { |err| + expect(err).to be_a MangoPay::ResponseError + expect(err.code).to eq '401' + expect(err.message).to eq 'invalid_client' + } + + # apply valid configuration + MangoPay::get_config('valid').apply_configuration + + # expect success + users = MangoPay::User.fetch() + expect(users).to be_kind_of(Array) + end + + it 'fails when fetching a config that does not exist' do + expect { + MangoPay::get_config('placeholder') + }.to raise_error { |err| + expect(err).to be_a RuntimeError + expect(err.message).to eq "Could not find any configuration with name 'placeholder'" + } + end + + it 'succeeds when removing config' do + wrong_config = MangoPay::Configuration.new + wrong_config.client_id = 'placeholder' + wrong_config.client_apiKey = '0000' + wrong_config.preproduction = true + + MangoPay::add_config('wrong', wrong_config) + + # pass when fetching config before removing it + MangoPay::get_config('wrong') + + # remove config + MangoPay::remove_config('wrong') + + # fail when trying to fetch after removal + expect { + MangoPay::get_config('wrong') + }.to raise_error { |err| + expect(err).to be_a RuntimeError + expect(err.message).to eq "Could not find any configuration with name 'wrong'" + } + end + it 'goes ok when calling with correct client credentials' do reset_mangopay_configuration users = MangoPay::User.fetch()