recurring_application_charge_test.rb 6.3 KB

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