recurring_application_charge_test.rb 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. require 'test_helper'
  2. class RecurringApplicationChargeTest < Test::Unit::TestCase
  3. def test_recurring_application_charges_create
  4. fake "recurring_application_charges", method: :post, status: 201, body: load_fixture('recurring_application_charge')
  5. charge = ShopifyAPI::RecurringApplicationCharge.create(
  6. name: "Default Plan",
  7. price: 10.0,
  8. return_url: "http://test.com/confirm"
  9. )
  10. assert_equal 'http://apple.myshopify.com/admin/charges/654381177/confirm_recurring_application_charge?signature=BAhpBHkQASc%3D--419fc7424f8c290ac2b21b9004ed223e35b52164', charge.confirmation_url
  11. end
  12. def test_get_recurring_application_charges
  13. fake "recurring_application_charges/654381177", method: :get, status: 201, body: load_fixture('recurring_application_charge')
  14. charge = ShopifyAPI::RecurringApplicationCharge.find(654381177)
  15. assert_equal "Super Duper Plan", charge.name
  16. end
  17. def test_list_recurring_application_charges
  18. fake "recurring_application_charges", method: :get, status: 201, body: load_fixture('recurring_application_charges')
  19. charge = ShopifyAPI::RecurringApplicationCharge.find(:all)
  20. assert_equal "Super Mega Plan", charge.last.name
  21. end
  22. def test_list_since_recurring_application_charges
  23. fake "recurring_application_charges.json?since_id=64512345",extension: false, method: :get, status: 201, body: load_fixture('recurring_application_charges')
  24. charge = ShopifyAPI::RecurringApplicationCharge.find(:all, params: { since_id: '64512345' })
  25. assert_equal "Super Mega Plan", charge.last.name
  26. end
  27. def test_list_fields_recurring_application_charges
  28. fake "recurring_application_charges.json?fields=name",extension: false, method: :get, status: 201, body: load_fixture('recurring_application_charges')
  29. charge = ShopifyAPI::RecurringApplicationCharge.find(:all, params: { fields: 'name' })
  30. assert_equal "Super Mega Plan", charge.last.name
  31. end
  32. def test_pending_recurring_application_charge
  33. fake "recurring_application_charges", method: :get, status: 201, body: load_fixture('recurring_application_charges')
  34. charge = ShopifyAPI::RecurringApplicationCharge.pending
  35. assert_equal "Super Mega Plan2", charge.last.name
  36. end
  37. def test_cancelled_recurring_application_charge
  38. fake "recurring_application_charges", method: :get, status: 201, body: load_fixture('recurring_application_charges')
  39. charge = ShopifyAPI::RecurringApplicationCharge.cancelled
  40. assert_equal "Super Mega Plan3", charge.last.name
  41. end
  42. def test_accepted_recurring_application_charge
  43. fake "recurring_application_charges", method: :get, status: 201, body: load_fixture('recurring_application_charges')
  44. charge = ShopifyAPI::RecurringApplicationCharge.accepted
  45. assert_equal "Super Mega Plan4", charge.first.name
  46. assert_equal "Super Mega Plan", charge.last.name
  47. end
  48. def test_declined_recurring_application_charge
  49. fake "recurring_application_charges", method: :get, status: 201, body: load_fixture('recurring_application_charges')
  50. charge = ShopifyAPI::RecurringApplicationCharge.declined
  51. assert_equal "Super Mega Plan5", charge.last.name
  52. end
  53. def test_activate_recurring_application_charge
  54. fake "recurring_application_charges", method: :get, status: 201, body: load_fixture('recurring_application_charges')
  55. fake "recurring_application_charges/455696199/activate", method: :post, status: 200, body: "{}"
  56. charge = ShopifyAPI::RecurringApplicationCharge.accepted
  57. assert charge.last.activate
  58. end
  59. def test_adjust_recurring_application_charge
  60. fake "recurring_application_charges/654381177", method: :get, status: 201, body: load_fixture('recurring_application_charge')
  61. fake "recurring_application_charges/654381177/customize.json?recurring_application_charge%5Bcapped_amount%5D=200", method: :put, body: load_fixture('recurring_application_charge_adjustment'), extension: false
  62. charge = ShopifyAPI::RecurringApplicationCharge.find(654381177)
  63. assert charge.customize(capped_amount: 200)
  64. end
  65. def test_cancel_recurring_application_charge
  66. fake "recurring_application_charges", method: :get, status: 201, body: load_fixture('recurring_application_charges')
  67. fake "recurring_application_charges/455696194", method: :delete, status: 200, body: "{}"
  68. charge = ShopifyAPI::RecurringApplicationCharge.current
  69. assert charge.cancel
  70. end
  71. def test_usage_charges_recurring_application_charge_found
  72. fake "recurring_application_charges/654381177", method: :get, status: 201, body: load_fixture('recurring_application_charge')
  73. fake "recurring_application_charges/654381177/usage_charges", method: :get, status: 201, body: load_fixture('usage_charges')
  74. charge = ShopifyAPI::RecurringApplicationCharge.find(654381177)
  75. usage_charges = charge.usage_charges
  76. assert_equal 2, usage_charges.length
  77. end
  78. def test_usage_charges_recurring_application_charge_not_found
  79. fake "recurring_application_charges/654381177", method: :get, status: 201, body: load_fixture('recurring_application_charge')
  80. fake "recurring_application_charges/654381177/usage_charges", method: :get, status: 201, body: "[]"
  81. charge = ShopifyAPI::RecurringApplicationCharge.find(654381177)
  82. usage_charges = charge.usage_charges
  83. assert_equal 0, usage_charges.length
  84. end
  85. def test_no_recurring_application_charge_found
  86. fake "recurring_application_charges", body: {recurring_application_charges: []}.to_json
  87. assert_equal 0, ShopifyAPI::RecurringApplicationCharge.all.count
  88. assert_equal nil, ShopifyAPI::RecurringApplicationCharge.current
  89. assert_equal [], ShopifyAPI::RecurringApplicationCharge.pending
  90. end
  91. def test_recurring_application_charge_not_found_error
  92. fake "recurring_application_charges", body: '{"errors":"Not Found"}', status: 404
  93. all_application_charges = ShopifyAPI::RecurringApplicationCharge.all
  94. if ActiveResource::VERSION::MAJOR >= 5 || (ActiveResource::VERSION::MAJOR == 4 && ActiveResource::VERSION::MINOR >= 2)
  95. assert_equal [], all_application_charges
  96. else
  97. assert_equal nil, all_application_charges
  98. end
  99. assert_equal nil, ShopifyAPI::RecurringApplicationCharge.current
  100. assert_equal [], ShopifyAPI::RecurringApplicationCharge.pending
  101. end
  102. end