| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- # rubocop:disable all
- require 'test_helper'
- require 'bigdecimal'
- class PaymentTest < Minitest::Test
- def setup
- excluded_files = ["base.rb", "errors.rb", "paratika_gateway.rb", "test_cards.rb", "test_cards", "version.rb"]
- @modules = (Dir.children("lib/payment") - excluded_files).map { |file| "Payment::#{file.split(".")[0].camelcase}" }
- @mandatory_methods = %w(charge_from_card cancel_transaction refund_money query_saved_cards delete_card currency_exchange)
- @customer = { id: 'c1', name: 'John Doe', email: 'test@example.com', phone: '905531781020', ip: '7.11.7.11' }
- @card = { name: 'John Doe', number: '4355084355084358', expiry: '12/2030', ccv: '000', save: 'yes' }
- @pid = -> { { id: SecureRandom.hex(16), amount: BigDecimal('1.00'), currency: 'TRY' } }
- @refundable, @payment = @pid.call, @pid.call
- @new_result_keys = %w(success amount currency card_token)
- @saved_result_keys = %w(success amount currency)
- end
- def test_version_number
- refute_nil ::Payment::VERSION
- end
- def test_every_billing_module_contains_mandatory_methods
- @modules.each do |module_name|
- @mandatory_methods.each do |method_name|
- assert module_name.constantize.respond_to?(method_name.to_sym), "#{module_name} does not have #{method_name} method"
- end
- end
- end
- def test_pre_auth_method_is_responding_hash
- @modules.each do |module_name|
- binding.irb
- request, response = module_name.constantize.try(:charge_from_card, @payment, @customer, @card, true, false)
- check_response(response, module_name)
- assert_equal @new_result_keys.all? { |s| response.key? s.to_sym }, true, "return value must contain all of the following keys: #{@new_result_keys}"
- @payment = @pid.call
- request, response = module_name.constantize.try(:charge_from_card, @payment, @customer, response[:card_token], true, true)
- check_response(response, module_name)
- assert_equal @saved_result_keys.all? { |s| response.key? s.to_sym }, true, "return value must contain all of the following keys: #{@saved_result_keys}"
- end
- end
- def test_sale_method_is_responding_hash
- @modules.each do |module_name|
- request, response = module_name.constantize.try(:charge_from_card, @payment, @customer, @card, false, false)
- check_response(response, module_name)
- assert_equal @new_result_keys.all? {|s| response.key? s.to_sym}, true, "return value must contain all of the following keys: #{@new_result_keys}"
- @payment = @pid.call
- request, response = module_name.constantize.try(:charge_from_card, @payment, @customer, response[:card_token], false, true)
- check_response(response, module_name)
- assert_equal @saved_result_keys.all? {|s| response.key? s.to_sym}, true, "return value must contain all of the following keys: #{@saved_result_keys}"
- end
- end
- def test_cancel_transaction_and_refund_money_methods_are_responding_hash
- @methods = %w(refund_money cancel_transaction)
- @modules.each do |module_name|
- @methods.each do |method_name|
- @refundable = @pid.call
- module_name.constantize.charge_from_card(@refundable, @customer, @card, false, false)
- request, response = send_method_to_gateway(module_name, method_name, @refundable)
- assert_equal response[:success], true, "api call must be successful on module #{module_name}" if module_name == "TestGateway"
- assert_equal Hash.new.class, response.class, "return value must be a Hash object on module #{module_name}"
- end
- end
- end
- def test_query_saved_cards_method_is_responding_hash
- @modules.each do |module_name|
- request, response = module_name.constantize.query_saved_cards(@customer)
- check_response(response, module_name)
- assert_equal response.key?(:card_list), true, "return value must contain :card_list key as a symbol on module #{module_name}"
- assert_equal [].class, response[:card_list].class, "card_list must be an array on #{module_name} module"
- assert_equal response[:card_list].all? {|a| a.class == Hash.new.class }, true, "card_list must contain Hash objects on #{module_name} module"
- assert_equal response[:card_list].all? {|a| a.key? "cardToken" }, true, "card_list must contain cardToken key on #{module_name} module"
- end
- end
- def test_delete_card_method_is_responding_hash
- @modules.each do |module_name|
- module_name.constantize.charge_from_card(@payment, @customer, @card, false, false)
- request, response = module_name.constantize.query_saved_cards( @customer)
- @token = response[:card_list].first["cardToken"]
- request, response = module_name.constantize.delete_card(@token)
- check_response(response, module_name)
- end
- end
- def test_currency_exchange_method_is_responding_hash
- @modules.each do |module_name|
- request, response = module_name.constantize.currency_exchange "TRY", "USD", BigDecimal("1.00")
- check_response(response, module_name)
- assert_equal %w(amount converted_amount from_currency to_currency success).all? { |a| response.key? a.to_sym }, true, "return value must contain #{%w(amount converted_amount from_currency to_currency success)} keys"
- end
- end
- private
- def send_method_to_gateway(module_name, method_name, *args)
- request, response = module_name.constantize.send(method_name.to_sym, *args)
- end
- def check_response(response, module_name)
- assert_equal response[:success], true, "api call must be successful on module #{module_name}"
- assert_equal Hash.new.class, response.class, "return value must be a Hash object on module #{module_name}"
- end
- end
|