fulfillment_test.rb 9.0 KB

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