Skip to content

Commit

Permalink
poc for multiple configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
Iulian Masar committed Nov 14, 2023
1 parent 7c0cc95 commit 935cae6
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
33 changes: 33 additions & 0 deletions lib/mangopay.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,29 @@ module MangoPay
# temporary
autoload :Temp, 'mangopay/temp'

@configurations = {}

class Configuration
attr_accessor :preproduction, :root_url,
:client_id, :client_apiKey,
:temp_dir, :log_file, :http_timeout,
: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
Expand Down Expand Up @@ -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
Expand Down
68 changes: 68 additions & 0 deletions spec/mangopay/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 935cae6

Please sign in to comment.