fulfillment_test.rb 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. require 'test_helper'
  2. require 'fulfillment_order_test_helper'
  3. class FulFillmentTest < Test::Unit::TestCase
  4. include FulfillmentOrderTestHelper
  5. def setup
  6. super
  7. fake("orders/450789469/fulfillments/255858046", method: :get, body: load_fixture('fulfillment'))
  8. end
  9. context "Fulfillment" do
  10. context "#complete" do
  11. should "be able to complete fulfillment" do
  12. fulfillment = ShopifyAPI::Fulfillment.find(255858046, params: { order_id: 450789469 })
  13. success = ActiveSupport::JSON.decode(load_fixture('fulfillment'))
  14. success['fulfillment']['status'] = 'success'
  15. fake(
  16. "orders/450789469/fulfillments/255858046/complete", method: :post, body: ActiveSupport::JSON.encode(success)
  17. )
  18. assert_equal('pending', fulfillment.status)
  19. assert(fulfillment.complete)
  20. assert_equal('success', fulfillment.status)
  21. end
  22. end
  23. context "#cancel" do
  24. should "be able to cancel fulfillment" do
  25. fulfillment = ShopifyAPI::Fulfillment.find(255858046, params: { order_id: 450789469 })
  26. cancelled = ActiveSupport::JSON.decode(load_fixture('fulfillment'))
  27. cancelled['fulfillment']['status'] = 'cancelled'
  28. fake(
  29. "orders/450789469/fulfillments/255858046/cancel", method: :post, body: ActiveSupport::JSON.encode(cancelled)
  30. )
  31. assert_equal('pending', fulfillment.status)
  32. assert(fulfillment.cancel)
  33. assert_equal('cancelled', fulfillment.status)
  34. end
  35. end
  36. context "#open" do
  37. should "be able to open a fulfillment" do
  38. fulfillment = ShopifyAPI::Fulfillment.find(255858046, params: { order_id: 450789469 })
  39. open_fulfillment = ActiveSupport::JSON.decode(load_fixture('fulfillment'))
  40. open_fulfillment['fulfillment']['status'] = 'open'
  41. fake(
  42. "orders/450789469/fulfillments/255858046/open",
  43. method: :post,
  44. body: ActiveSupport::JSON.encode(open_fulfillment)
  45. )
  46. assert_equal('pending', fulfillment.status)
  47. assert(fulfillment.open)
  48. assert_equal('open', fulfillment.status)
  49. end
  50. end
  51. context "#find" do
  52. should "be able to find fulfillment" do
  53. fulfillment = ShopifyAPI::Fulfillment.find(255858046, params: { order_id: 450789469 })
  54. assert_equal(255858046, fulfillment.id)
  55. assert_equal(450789469, fulfillment.order_id)
  56. end
  57. end
  58. context "#create" do
  59. should "create a fulfillment with line_items_by_fulfillment_order" do
  60. create_fulfillment_attributes = {
  61. message: "The message for this FO fulfillment",
  62. notify_customer: true,
  63. tracking_info: {
  64. number: "XSDFHYR23475",
  65. url: "https://tracking.example.com/XSDFHYR23475",
  66. company: "TFTC - the fulfillment/tracking company"
  67. },
  68. line_items_by_fulfillment_order: [
  69. {
  70. fulfillment_order_id: 3,
  71. fulfillment_order_line_items: [{ id: 2, quantity: 1 }]
  72. }
  73. ]
  74. }
  75. request_body = { fulfillment: create_fulfillment_attributes }
  76. response_body = { fulfillment: create_fulfillment_attributes.merge(id: 346743624) }
  77. url_prefix = url_prefix_for_activated_session_for('2020-01')
  78. fake(
  79. 'fulfillments',
  80. url: "#{url_prefix}/fulfillments.json",
  81. method: :post,
  82. request_body: ActiveSupport::JSON.encode(request_body),
  83. body: ActiveSupport::JSON.encode(response_body)
  84. )
  85. fulfillment = ShopifyAPI::Fulfillment.create(create_fulfillment_attributes)
  86. assert(fulfillment.is_a?(ShopifyAPI::Fulfillment))
  87. assert(fulfillment.persisted?)
  88. assert_equal(346743624, fulfillment.id)
  89. end
  90. should "raise NotImplementedError when api_version is older than 2020-01" do
  91. create_fulfillment_attributes = {
  92. message: "The message for this FO fulfillment",
  93. notify_customer: true,
  94. tracking_info: {
  95. number: "XSDFHYR23475",
  96. url: "https://tracking.example.com/XSDFHYR23475",
  97. company: "TFTC - the fulfillment/tracking company"
  98. },
  99. line_items_by_fulfillment_order: [
  100. {
  101. fulfillment_order_id: 3,
  102. fulfillment_order_line_items: [{ id: 2, quantity: 1 }]
  103. }
  104. ]
  105. }
  106. request_body = { fulfillment: create_fulfillment_attributes }
  107. response_body = { fulfillment: create_fulfillment_attributes.merge(id: 346743624) }
  108. url_prefix = url_prefix_for_activated_session_for('2019-10')
  109. fake(
  110. 'fulfillments',
  111. url: "#{url_prefix}/fulfillments.json",
  112. method: :post,
  113. request_body: ActiveSupport::JSON.encode(request_body),
  114. body: ActiveSupport::JSON.encode(response_body)
  115. )
  116. assert_raises(NotImplementedError) do
  117. ShopifyAPI::Fulfillment.create(create_fulfillment_attributes)
  118. end
  119. end
  120. end
  121. context "#save" do
  122. should "save a fulfillment with line_items_by_fulfillment_order" do
  123. create_fulfillment_attributes = {
  124. message: "The message for this FO fulfillment",
  125. notify_customer: true,
  126. tracking_info: {
  127. number: "XSDFHYR23475",
  128. url: "https://tracking.example.com/XSDFHYR23475",
  129. company: "TFTC - the fulfillment/tracking company"
  130. },
  131. line_items_by_fulfillment_order: [
  132. {
  133. fulfillment_order_id: 3,
  134. fulfillment_order_line_items: [{ id: 2, quantity: 1 }]
  135. }
  136. ]
  137. }
  138. request_body = { fulfillment: create_fulfillment_attributes }
  139. response_body = { fulfillment: create_fulfillment_attributes.merge(id: 346743624) }
  140. url_prefix = url_prefix_for_activated_session_for('2020-01')
  141. fake(
  142. 'fulfillments',
  143. url: "#{url_prefix}/fulfillments.json",
  144. method: :post,
  145. request_body: ActiveSupport::JSON.encode(request_body),
  146. body: ActiveSupport::JSON.encode(response_body)
  147. )
  148. fulfillment = ShopifyAPI::Fulfillment.new(create_fulfillment_attributes)
  149. assert(fulfillment.save)
  150. assert(fulfillment.is_a?(ShopifyAPI::Fulfillment))
  151. assert(fulfillment.persisted?)
  152. assert_equal(346743624, fulfillment.id)
  153. end
  154. should "save a fulfillment without line_items_by_fulfillment_order" do
  155. order_id = 8
  156. create_fulfillment_attributes = {
  157. message: "The message for this FO fulfillment",
  158. notify_customer: true,
  159. tracking_info: {
  160. number: "XSDFHYR23475",
  161. url: "https://tracking.example.com/XSDFHYR23475",
  162. company: "TFTC - the fulfillment/tracking company"
  163. }
  164. }
  165. request_body = { fulfillment: create_fulfillment_attributes }
  166. response_body = { fulfillment: create_fulfillment_attributes.merge(id: 346743624) }
  167. fake(
  168. "orders/#{order_id}/fulfillments",
  169. method: :post,
  170. request_body: ActiveSupport::JSON.encode(request_body),
  171. body: ActiveSupport::JSON.encode(response_body)
  172. )
  173. fulfillment = ShopifyAPI::Fulfillment.new(create_fulfillment_attributes)
  174. fulfillment.prefix_options[:order_id] = order_id
  175. assert(fulfillment.save)
  176. assert(fulfillment.is_a?(ShopifyAPI::Fulfillment))
  177. assert(fulfillment.persisted?)
  178. assert_equal(346743624, fulfillment.id)
  179. end
  180. end
  181. context "#update_tracking" do
  182. should "be able to update tracking info for a fulfillment" do
  183. tracking_info = {
  184. number: 'JSDHFHAG',
  185. url: 'https://example.com/fulfillment_tracking/JSDHFHAG',
  186. company: 'ACME co',
  187. }
  188. fake_fulfillment = ActiveSupport::JSON.decode(load_fixture('fulfillment'))['fulfillment']
  189. fake_fulfillment['tracking_number'] = tracking_info[:number]
  190. fake_fulfillment['tracking_numbers'] = [tracking_info[:number]]
  191. fake_fulfillment['tracking_url'] = tracking_info[:url]
  192. fake_fulfillment['tracking_urls'] = [tracking_info[:url]]
  193. fake_fulfillment['tracking_company'] = tracking_info[:company]
  194. request_body = {
  195. fulfillment: {
  196. tracking_info: tracking_info,
  197. notify_customer: true
  198. }
  199. }
  200. url_prefix = url_prefix_for_activated_session_for('2020-01')
  201. fake(
  202. 'fulfillments',
  203. url: "#{url_prefix}/fulfillments/#{fake_fulfillment['id']}/update_tracking.json",
  204. method: :post,
  205. request_body: ActiveSupport::JSON.encode(request_body),
  206. body: ActiveSupport::JSON.encode(fulfillment: fake_fulfillment)
  207. )
  208. fulfillment = ShopifyAPI::Fulfillment.new(id: fake_fulfillment['id'])
  209. assert(fulfillment.update_tracking(tracking_info: tracking_info, notify_customer: true))
  210. assert_equal(tracking_info[:number], fulfillment.tracking_number)
  211. assert_equal([tracking_info[:number]], fulfillment.tracking_numbers)
  212. assert_equal(tracking_info[:url], fulfillment.tracking_url)
  213. assert_equal([tracking_info[:url]], fulfillment.tracking_urls)
  214. assert_equal(tracking_info[:company], fulfillment.tracking_company)
  215. end
  216. end
  217. end
  218. end