test_payment.rb 5.5 KB

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