fulfillment_test.rb 9.2 KB

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