test_payment.rb 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # rubocop:disable all
  2. require 'test_helper'
  3. require 'bigdecimal'
  4. class PaymentTest < Minitest::Test
  5. def setup
  6. Paratika.configure do |config|
  7. config.merchant_id = '10002036'
  8. config.merchant_user = 'paytentest@akillibulut.net'
  9. config.merchant_password = '3mzVyRKuwnttqRC.'
  10. config.url = 'https://entegrasyon.paratika.com.tr'
  11. end
  12. excluded_files = ["base.rb", "errors.rb", "test_cards.rb", "test_cards", "version.rb"]
  13. @modules = (Dir.children("lib/payment") - excluded_files).map { |file| "Payment::#{file.split(".")[0].camelcase}" }
  14. @mandatory_methods = %w(charge_from_card cancel_transaction refund_money query_saved_cards delete_card currency_exchange)
  15. @customer = { id: 'c1', name: 'John Doe', email: 'test@example.com', phone: '905531781020', ip: '7.11.7.11' }
  16. @card = { name: 'John Doe', number: '4355084355084358', expiry: '12/2030', ccv: '000', save: 'yes' }
  17. @pid = -> { { id: SecureRandom.hex(16), amount: BigDecimal('1.00'), currency: 'TRY' } }
  18. @refundable, @payment = @pid.call, @pid.call
  19. @new_result_keys = %w(success amount currency card_token)
  20. @saved_result_keys = %w(success amount currency)
  21. end
  22. def test_version_number
  23. refute_nil ::Payment::VERSION
  24. end
  25. def test_every_billing_module_contains_mandatory_methods
  26. @modules.each do |module_name|
  27. @mandatory_methods.each do |method_name|
  28. assert module_name.constantize.respond_to?(method_name.to_sym), "#{module_name} does not have #{method_name} method"
  29. end
  30. end
  31. end
  32. def test_pre_auth_method_is_responding_hash
  33. @modules.each do |module_name|
  34. request, response = module_name.constantize.try(:charge_from_card, @payment, @customer, @card, true, false)
  35. check_response(response, module_name)
  36. 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}"
  37. @payment = @pid.call
  38. request, response = module_name.constantize.try(:charge_from_card, @payment, @customer, response[:card_token], true, true)
  39. check_response(response, module_name)
  40. 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}"
  41. end
  42. end
  43. def test_sale_method_is_responding_hash
  44. @modules.each do |module_name|
  45. request, response = module_name.constantize.try(:charge_from_card, @payment, @customer, @card, false, false)
  46. check_response(response, module_name)
  47. 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}"
  48. @payment = @pid.call
  49. request, response = module_name.constantize.try(:charge_from_card, @payment, @customer, response[:card_token], false, true)
  50. check_response(response, module_name)
  51. 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}"
  52. end
  53. end
  54. def test_cancel_transaction_and_refund_money_methods_are_responding_hash
  55. @methods = %w(refund_money cancel_transaction)
  56. @modules.each do |module_name|
  57. @methods.each do |method_name|
  58. @refundable = @pid.call
  59. module_name.constantize.charge_from_card(@refundable, @customer, @card, false, false)
  60. request, response = send_method_to_gateway(module_name, method_name, @refundable)
  61. assert_equal response[:success], true, "api call must be successful on module #{module_name}" if module_name == "TestGateway"
  62. assert_equal Hash.new.class, response.class, "return value must be a Hash object on module #{module_name}"
  63. end
  64. end
  65. end
  66. def test_query_saved_cards_method_is_responding_hash
  67. @modules.each do |module_name|
  68. request, response = module_name.constantize.query_saved_cards(@customer)
  69. check_response(response, module_name)
  70. assert_equal response.key?(:card_list), true, "return value must contain :card_list key as a symbol on module #{module_name}"
  71. assert_equal [].class, response[:card_list].class, "card_list must be an array on #{module_name} module"
  72. assert_equal response[:card_list].all? {|a| a.class == Hash.new.class }, true, "card_list must contain Hash objects on #{module_name} module"
  73. assert_equal response[:card_list].all? {|a| a.key? "cardToken" }, true, "card_list must contain cardToken key on #{module_name} module"
  74. end
  75. end
  76. def test_delete_card_method_is_responding_hash
  77. @modules.each do |module_name|
  78. module_name.constantize.charge_from_card(@payment, @customer, @card, false, false)
  79. request, response = module_name.constantize.query_saved_cards( @customer)
  80. @token = response[:card_list].first["cardToken"]
  81. request, response = module_name.constantize.delete_card(@token)
  82. check_response(response, module_name)
  83. end
  84. end
  85. def test_currency_exchange_method_is_responding_hash
  86. @modules.each do |module_name|
  87. request, response = module_name.constantize.currency_exchange "TRY", "USD", BigDecimal("1.00")
  88. check_response(response, module_name)
  89. 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"
  90. end
  91. end
  92. private
  93. def send_method_to_gateway(module_name, method_name, *args)
  94. request, response = module_name.constantize.send(method_name.to_sym, *args)
  95. end
  96. def check_response(response, module_name)
  97. assert_equal response[:success], true, "api call must be successful on module #{module_name}"
  98. assert_equal Hash.new.class, response.class, "return value must be a Hash object on module #{module_name}"
  99. end
  100. end