recurring_application_charge_test.rb 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. require 'test_helper'
  2. class RecurringApplicationChargeTest < Test::Unit::TestCase
  3. def test_recurring_application_charges_create
  4. fake(
  5. "recurring_application_charges", method: :post, status: 201, body: load_fixture('recurring_application_charge')
  6. )
  7. charge = ShopifyAPI::RecurringApplicationCharge.create(
  8. name: "Default Plan",
  9. price: 10.0,
  10. return_url: "http://test.com/confirm"
  11. )
  12. assert_equal(
  13. 'http://apple.myshopify.com/admin/charges/654381177/confirm_recurring_application_charge?' \
  14. 'signature=BAhpBHkQASc%3D--419fc7424f8c290ac2b21b9004ed223e35b52164',
  15. charge.confirmation_url
  16. )
  17. end
  18. def test_get_recurring_application_charges
  19. fake(
  20. "recurring_application_charges/654381177",
  21. method: :get,
  22. status: 201,
  23. body: load_fixture('recurring_application_charge')
  24. )
  25. charge = ShopifyAPI::RecurringApplicationCharge.find(654381177)
  26. assert_equal("Super Duper Plan", charge.name)
  27. end
  28. def test_list_recurring_application_charges
  29. fake(
  30. "recurring_application_charges", method: :get, status: 201, body: load_fixture('recurring_application_charges')
  31. )
  32. charge = ShopifyAPI::RecurringApplicationCharge.find(:all)
  33. assert_equal("Super Mega Plan", charge.last.name)
  34. end
  35. def test_list_since_recurring_application_charges
  36. fake(
  37. "recurring_application_charges.json?since_id=64512345",
  38. extension: false,
  39. method: :get,
  40. status: 201,
  41. body: load_fixture('recurring_application_charges')
  42. )
  43. charge = ShopifyAPI::RecurringApplicationCharge.find(:all, params: { since_id: '64512345' })
  44. assert_equal("Super Mega Plan", charge.last.name)
  45. end
  46. def test_list_fields_recurring_application_charges
  47. fake(
  48. "recurring_application_charges.json?fields=name",
  49. extension: false,
  50. method: :get,
  51. status: 201,
  52. body: load_fixture('recurring_application_charges')
  53. )
  54. charge = ShopifyAPI::RecurringApplicationCharge.find(:all, params: { fields: 'name' })
  55. assert_equal("Super Mega Plan", charge.last.name)
  56. end
  57. def test_pending_recurring_application_charge
  58. fake(
  59. "recurring_application_charges", method: :get, status: 201, body: load_fixture('recurring_application_charges')
  60. )
  61. charge = ShopifyAPI::RecurringApplicationCharge.pending
  62. assert_equal("Super Mega Plan2", charge.last.name)
  63. end
  64. def test_cancelled_recurring_application_charge
  65. fake(
  66. "recurring_application_charges", method: :get, status: 201, body: load_fixture('recurring_application_charges')
  67. )
  68. charge = ShopifyAPI::RecurringApplicationCharge.cancelled
  69. assert_equal("Super Mega Plan3", charge.last.name)
  70. end
  71. def test_accepted_recurring_application_charge
  72. fake(
  73. "recurring_application_charges", method: :get, status: 201, body: load_fixture('recurring_application_charges')
  74. )
  75. charge = ShopifyAPI::RecurringApplicationCharge.accepted
  76. assert_equal("Super Mega Plan4", charge.first.name)
  77. assert_equal("Super Mega Plan", charge.last.name)
  78. end
  79. def test_declined_recurring_application_charge
  80. fake(
  81. "recurring_application_charges", method: :get, status: 201, body: load_fixture('recurring_application_charges')
  82. )
  83. charge = ShopifyAPI::RecurringApplicationCharge.declined
  84. assert_equal("Super Mega Plan5", charge.last.name)
  85. end
  86. def test_activate_recurring_application_charge
  87. fake(
  88. "recurring_application_charges", method: :get, status: 201, body: load_fixture('recurring_application_charges')
  89. )
  90. fake("recurring_application_charges/455696199/activate", method: :post, status: 200, body: "{}")
  91. charge = ShopifyAPI::RecurringApplicationCharge.accepted
  92. assert(charge.last.activate)
  93. end
  94. def test_adjust_recurring_application_charge
  95. fake(
  96. "recurring_application_charges/654381177",
  97. method: :get,
  98. status: 201,
  99. body: load_fixture('recurring_application_charge')
  100. )
  101. fake(
  102. "recurring_application_charges/654381177/customize.json?recurring_application_charge%5Bcapped_amount%5D=200",
  103. method: :put,
  104. body: load_fixture('recurring_application_charge_adjustment'),
  105. extension: false
  106. )
  107. charge = ShopifyAPI::RecurringApplicationCharge.find(654381177)
  108. assert(charge.customize(capped_amount: 200))
  109. end
  110. def test_cancel_recurring_application_charge
  111. fake(
  112. "recurring_application_charges", method: :get, status: 201, body: load_fixture('recurring_application_charges')
  113. )
  114. fake("recurring_application_charges/455696194", method: :delete, status: 200, body: "{}")
  115. charge = ShopifyAPI::RecurringApplicationCharge.current
  116. assert(charge.cancel)
  117. end
  118. def test_usage_charges_recurring_application_charge_found
  119. fake(
  120. "recurring_application_charges/654381177",
  121. method: :get,
  122. status: 201,
  123. body: load_fixture('recurring_application_charge')
  124. )
  125. fake(
  126. "recurring_application_charges/654381177/usage_charges",
  127. method: :get,
  128. status: 201,
  129. body: load_fixture('usage_charges')
  130. )
  131. charge = ShopifyAPI::RecurringApplicationCharge.find(654381177)
  132. usage_charges = charge.usage_charges
  133. assert_equal(2, usage_charges.length)
  134. end
  135. def test_usage_charges_recurring_application_charge_not_found
  136. fake(
  137. "recurring_application_charges/654381177",
  138. method: :get,
  139. status: 201,
  140. body: load_fixture('recurring_application_charge')
  141. )
  142. fake("recurring_application_charges/654381177/usage_charges", method: :get, status: 201, body: "[]")
  143. charge = ShopifyAPI::RecurringApplicationCharge.find(654381177)
  144. usage_charges = charge.usage_charges
  145. assert_equal(0, usage_charges.length)
  146. end
  147. def test_no_recurring_application_charge_found
  148. fake("recurring_application_charges", body: { recurring_application_charges: [] }.to_json)
  149. assert_equal(0, ShopifyAPI::RecurringApplicationCharge.all.count)
  150. assert_nil(ShopifyAPI::RecurringApplicationCharge.current)
  151. assert_equal([], ShopifyAPI::RecurringApplicationCharge.pending)
  152. end
  153. def test_recurring_application_charge_not_found_error
  154. fake("recurring_application_charges", body: '{"errors":"Not Found"}', status: 404)
  155. assert_nil(ShopifyAPI::RecurringApplicationCharge.all)
  156. assert_nil(ShopifyAPI::RecurringApplicationCharge.current)
  157. assert_equal([], ShopifyAPI::RecurringApplicationCharge.pending)
  158. end
  159. end