fulfillment_test.rb 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. require 'test_helper'
  2. class FulFillmentTest < Test::Unit::TestCase
  3. def setup
  4. super
  5. fake "orders/450789469/fulfillments/255858046", :method => :get, :body => load_fixture('fulfillment')
  6. end
  7. context "Fulfillment" do
  8. context "#complete" do
  9. should "be able to complete fulfillment" do
  10. fulfillment = ShopifyAPI::Fulfillment.find(255858046, :params => {:order_id => 450789469})
  11. success = ActiveSupport::JSON.decode(load_fixture('fulfillment'))
  12. success['fulfillment']['status'] = 'success'
  13. fake "orders/450789469/fulfillments/255858046/complete", :method => :post, :body => ActiveSupport::JSON.encode(success)
  14. assert_equal 'pending', fulfillment.status
  15. assert fulfillment.complete
  16. assert_equal 'success', fulfillment.status
  17. end
  18. end
  19. context "#cancel" do
  20. should "be able to cancel fulfillment" do
  21. fulfillment = ShopifyAPI::Fulfillment.find(255858046, :params => {:order_id => 450789469})
  22. cancelled = ActiveSupport::JSON.decode(load_fixture('fulfillment'))
  23. cancelled['fulfillment']['status'] = 'cancelled'
  24. fake "orders/450789469/fulfillments/255858046/cancel", :method => :post, :body => ActiveSupport::JSON.encode(cancelled)
  25. assert_equal 'pending', fulfillment.status
  26. assert fulfillment.cancel
  27. assert_equal 'cancelled', fulfillment.status
  28. end
  29. end
  30. context "#open" do
  31. should "be able to open a fulfillment" do
  32. fulfillment = ShopifyAPI::Fulfillment.find(255858046, :params => {:order_id => 450789469})
  33. open_fulfillment = ActiveSupport::JSON.decode(load_fixture('fulfillment'))
  34. open_fulfillment['fulfillment']['status'] = 'open'
  35. fake "orders/450789469/fulfillments/255858046/open", :method => :post, :body => ActiveSupport::JSON.encode(open_fulfillment)
  36. assert_equal 'pending', fulfillment.status
  37. assert fulfillment.open
  38. assert_equal 'open', fulfillment.status
  39. end
  40. end
  41. context "#find" do
  42. should "be able to find fulfillment" do
  43. fulfillment = ShopifyAPI::Fulfillment.find(255858046, :params => {:order_id => 450789469})
  44. assert_equal 255858046, fulfillment.id
  45. assert_equal 450789469, fulfillment.order_id
  46. end
  47. end
  48. context "#create" do
  49. should "create a fulfillment with line_items_by_fulfillment_order" do
  50. create_fulfillment_attributes = {
  51. message: "The message for this FO fulfillment",
  52. notify_customer: true,
  53. tracking_info: {
  54. number: "XSDFHYR23475",
  55. url: "https://tracking.example.com/XSDFHYR23475",
  56. company: "TFTC - the fulfillment/tracking company"
  57. },
  58. line_items_by_fulfillment_order: [
  59. {
  60. fulfillment_order_id: 3,
  61. fulfillment_order_line_items: [{ id: 2, quantity: 1 }]
  62. }
  63. ]
  64. }
  65. request_body = { fulfillment: create_fulfillment_attributes }
  66. response_body = { fulfillment: create_fulfillment_attributes.merge(id: 346743624) }
  67. fake "fulfillments", :method => :post,
  68. request_body: ActiveSupport::JSON.encode(request_body),
  69. body: ActiveSupport::JSON.encode(response_body)
  70. fulfillment = ShopifyAPI::Fulfillment.create(create_fulfillment_attributes)
  71. assert fulfillment.is_a?(ShopifyAPI::Fulfillment)
  72. assert fulfillment.persisted?
  73. assert_equal 346743624, fulfillment.id
  74. end
  75. end
  76. context "#save" do
  77. should "save a fulfillment with line_items_by_fulfillment_order" do
  78. create_fulfillment_attributes = {
  79. message: "The message for this FO fulfillment",
  80. notify_customer: true,
  81. tracking_info: {
  82. number: "XSDFHYR23475",
  83. url: "https://tracking.example.com/XSDFHYR23475",
  84. company: "TFTC - the fulfillment/tracking company"
  85. },
  86. line_items_by_fulfillment_order: [
  87. {
  88. fulfillment_order_id: 3,
  89. fulfillment_order_line_items: [{ id: 2, quantity: 1 }]
  90. }
  91. ]
  92. }
  93. request_body = { fulfillment: create_fulfillment_attributes }
  94. response_body = { fulfillment: create_fulfillment_attributes.merge(id: 346743624) }
  95. fake "fulfillments", :method => :post,
  96. request_body: ActiveSupport::JSON.encode(request_body),
  97. body: ActiveSupport::JSON.encode(response_body)
  98. fulfillment = ShopifyAPI::Fulfillment.new(create_fulfillment_attributes)
  99. assert fulfillment.save
  100. assert fulfillment.is_a?(ShopifyAPI::Fulfillment)
  101. assert fulfillment.persisted?
  102. assert_equal 346743624, fulfillment.id
  103. end
  104. should "save a fulfillment without line_items_by_fulfillment_order" do
  105. order_id = 8
  106. create_fulfillment_attributes = {
  107. message: "The message for this FO fulfillment",
  108. notify_customer: true,
  109. tracking_info: {
  110. number: "XSDFHYR23475",
  111. url: "https://tracking.example.com/XSDFHYR23475",
  112. company: "TFTC - the fulfillment/tracking company"
  113. }
  114. }
  115. request_body = { fulfillment: create_fulfillment_attributes }
  116. response_body = { fulfillment: create_fulfillment_attributes.merge(id: 346743624) }
  117. fake "orders/#{order_id}/fulfillments", :method => :post,
  118. request_body: ActiveSupport::JSON.encode(request_body),
  119. body: ActiveSupport::JSON.encode(response_body)
  120. fulfillment = ShopifyAPI::Fulfillment.new(create_fulfillment_attributes)
  121. fulfillment.prefix_options[:order_id] = order_id
  122. assert fulfillment.save
  123. assert fulfillment.is_a?(ShopifyAPI::Fulfillment)
  124. assert fulfillment.persisted?
  125. assert_equal 346743624, fulfillment.id
  126. end
  127. end
  128. context "#update_tracking" do
  129. should "be able to update tracking info for a fulfillment" do
  130. tracking_info = {
  131. number: 'JSDHFHAG',
  132. url: 'https://example.com/fulfillment_tracking/JSDHFHAG',
  133. company: 'ACME co',
  134. }
  135. fake_fulfillment = ActiveSupport::JSON.decode(load_fixture('fulfillment'))['fulfillment']
  136. fake_fulfillment['tracking_number'] = tracking_info[:number]
  137. fake_fulfillment['tracking_numbers'] = [tracking_info[:number]]
  138. fake_fulfillment['tracking_url'] = tracking_info[:url]
  139. fake_fulfillment['tracking_urls'] = [tracking_info[:url]]
  140. fake_fulfillment['tracking_company'] = tracking_info[:company]
  141. request_body = {
  142. fulfillment: {
  143. tracking_info: tracking_info,
  144. notify_customer: true
  145. }
  146. }
  147. fake "fulfillments/#{fake_fulfillment['id']}/update_tracking", method: :post,
  148. request_body: ActiveSupport::JSON.encode(request_body),
  149. body: ActiveSupport::JSON.encode(fulfillment: fake_fulfillment)
  150. fulfillment = ShopifyAPI::Fulfillment.new(id: fake_fulfillment['id'])
  151. assert fulfillment.update_tracking(tracking_info: tracking_info, notify_customer: true)
  152. assert_equal tracking_info[:number], fulfillment.tracking_number
  153. assert_equal [tracking_info[:number]], fulfillment.tracking_numbers
  154. assert_equal tracking_info[:url], fulfillment.tracking_url
  155. assert_equal [tracking_info[:url]], fulfillment.tracking_urls
  156. assert_equal tracking_info[:company], fulfillment.tracking_company
  157. end
  158. end
  159. end
  160. end