fulfillment_order_test.rb 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. # frozen_string_literal: true
  2. require 'test_helper'
  3. require 'fulfillment_order_test_helper'
  4. class FulFillmentOrderTest < Test::Unit::TestCase
  5. include FulfillmentOrderTestHelper
  6. def setup
  7. super
  8. @url_prefix = url_prefix_for_activated_session_for('2020-01')
  9. fake(
  10. 'fulfillment_orders',
  11. url: "#{@url_prefix}/fulfillment_orders/519788021.json",
  12. method: :get,
  13. body: load_fixture('fulfillment_order')
  14. )
  15. end
  16. context "FulfillmentOrder" do
  17. context ".new" do
  18. should "raise NotImplementedError when api_version is older than 2020-01" do
  19. url_prefix_for_activated_session_for('2019-10')
  20. fulfillment_order = load_fixture('fulfillment_order')
  21. exception = assert_raises(NotImplementedError) do
  22. ShopifyAPI::FulfillmentOrder.new(ActiveSupport::JSON.decode(fulfillment_order))
  23. end
  24. assert_equal(
  25. "The minimum supported version is 2020-01.",
  26. exception.message
  27. )
  28. end
  29. end
  30. context ".find and .all" do
  31. should "raise NotImplementedError when api_version is older than 2020-01" do
  32. @url_prefix = url_prefix_for_activated_session_for('2019-10')
  33. fake(
  34. 'fulfillment_orders',
  35. url: "#{@url_prefix}/fulfillment_orders/519788021.json",
  36. method: :get,
  37. body: load_fixture('fulfillment_order')
  38. )
  39. exception = assert_raises(NotImplementedError) do
  40. ShopifyAPI::FulfillmentOrder.find(519788021)
  41. end
  42. assert_equal(
  43. "The minimum supported version is 2020-01.",
  44. exception.message
  45. )
  46. assert_raises(NotImplementedError) do
  47. ShopifyAPI::FulfillmentOrder.all(params: { order_id: 450789469 })
  48. end
  49. end
  50. end
  51. context "#find" do
  52. should "be able to find fulfillment order" do
  53. fulfillment_order = ShopifyAPI::FulfillmentOrder.find(519788021)
  54. assert(fulfillment_order.is_a?(ShopifyAPI::FulfillmentOrder))
  55. assert_equal(519788021, fulfillment_order.id)
  56. assert_equal(450789469, fulfillment_order.order_id)
  57. end
  58. end
  59. context "#all" do
  60. should "be able to list fulfillment orders for an order" do
  61. fake(
  62. 'orders',
  63. url: "#{@url_prefix}/orders/450789469/fulfillment_orders.json",
  64. method: :get,
  65. body: load_fixture('fulfillment_orders')
  66. )
  67. fulfillment_orders = ShopifyAPI::FulfillmentOrder.all(
  68. params: { order_id: 450789469 }
  69. )
  70. assert_equal([519788021, 519788022], fulfillment_orders.map(&:id).sort)
  71. fulfillment_orders.each do |fulfillment_order|
  72. assert(fulfillment_order.is_a?(ShopifyAPI::FulfillmentOrder))
  73. assert_equal(450789469, fulfillment_order.order_id)
  74. end
  75. end
  76. should "require order_id" do
  77. assert_raises(ShopifyAPI::ValidationException) do
  78. ShopifyAPI::FulfillmentOrder.all
  79. end
  80. end
  81. end
  82. context "#fulfillments" do
  83. should "be able to list fulfillments for a fulfillment order" do
  84. fulfillment_order = ShopifyAPI::FulfillmentOrder.find(519788021)
  85. fake(
  86. 'fulfillment_orders',
  87. url: "#{@url_prefix}/fulfillment_orders/#{fulfillment_order.id}/fulfillments.json",
  88. method: :get,
  89. body: load_fixture('fulfillments')
  90. )
  91. fulfillments = fulfillment_order.fulfillments
  92. assert_equal(1, fulfillments.count)
  93. fulfillment = fulfillments.first
  94. assert(fulfillment.is_a?(ShopifyAPI::Fulfillment))
  95. assert_equal(450789469, fulfillment.order_id)
  96. end
  97. end
  98. context "#locations_for_move" do
  99. should "be able to list locations for a fulfillment order" do
  100. fulfillment_order = ShopifyAPI::FulfillmentOrder.find(519788021)
  101. fake(
  102. 'fulfillment_orders',
  103. url: "#{@url_prefix}/fulfillment_orders/#{fulfillment_order.id}/locations_for_move.json",
  104. method: :get,
  105. body: load_fixture('fulfillment_order_locations_for_move')
  106. )
  107. locations_for_move = fulfillment_order.locations_for_move
  108. assert_equal(2, locations_for_move.count)
  109. location_for_move = locations_for_move.first
  110. assert(location_for_move.is_a?(ShopifyAPI::FulfillmentOrderLocationsForMove))
  111. location = location_for_move.location
  112. assert(location.is_a?(ShopifyAPI::Location))
  113. assert_equal(1059367776, location.id)
  114. end
  115. end
  116. context "#move" do
  117. should "move a fulfillment order to a new_location_id" do
  118. fulfillment_order = ShopifyAPI::FulfillmentOrder.find(519788021)
  119. new_location_id = 5
  120. fake_original_fulfillment_order = fulfillment_order.clone
  121. fake_original_fulfillment_order.status = 'closed'
  122. fake_moved_fulfillment_order = ActiveSupport::JSON.decode(load_fixture('fulfillment_order'))
  123. fake_moved_fulfillment_order['assigned_location_id'] = new_location_id
  124. request_body = { fulfillment_order: { new_location_id: 5 } }
  125. body = {
  126. original_fulfillment_order: fake_original_fulfillment_order,
  127. moved_fulfillment_order: fake_moved_fulfillment_order,
  128. remaining_fulfillment_order: nil,
  129. }
  130. fake(
  131. 'fulfillment_orders',
  132. url: "#{@url_prefix}/fulfillment_orders/519788021/move.json",
  133. method: :post,
  134. request_body: ActiveSupport::JSON.encode(request_body),
  135. body: ActiveSupport::JSON.encode(body)
  136. )
  137. response_fulfillment_orders = fulfillment_order.move(new_location_id: new_location_id)
  138. assert_equal('closed', fulfillment_order.status)
  139. assert_equal(3, response_fulfillment_orders.count)
  140. original_fulfillment_order = response_fulfillment_orders['original_fulfillment_order']
  141. refute_nil original_fulfillment_order
  142. assert(original_fulfillment_order.is_a?(ShopifyAPI::FulfillmentOrder))
  143. assert_equal('closed', original_fulfillment_order.status)
  144. moved_fulfillment_order = response_fulfillment_orders['moved_fulfillment_order']
  145. refute_nil moved_fulfillment_order
  146. assert(moved_fulfillment_order.is_a?(ShopifyAPI::FulfillmentOrder))
  147. assert_equal('open', moved_fulfillment_order.status)
  148. assert_equal(new_location_id, moved_fulfillment_order.assigned_location_id)
  149. remaining_fulfillment_order = response_fulfillment_orders['remaining_fulfillment_order']
  150. assert_nil(remaining_fulfillment_order)
  151. end
  152. end
  153. context "#cancel" do
  154. should "cancel a fulfillment order" do
  155. fulfillment_order = ShopifyAPI::FulfillmentOrder.find(519788021)
  156. assert_equal('open', fulfillment_order.status)
  157. cancelled = ActiveSupport::JSON.decode(load_fixture('fulfillment_order'))
  158. cancelled['status'] = 'cancelled'
  159. body = {
  160. fulfillment_order: cancelled,
  161. replacement_fulfillment_order: fulfillment_order,
  162. }
  163. fake(
  164. 'fulfillment_orders',
  165. url: "#{@url_prefix}/fulfillment_orders/519788021/cancel.json",
  166. method: :post,
  167. body: ActiveSupport::JSON.encode(body)
  168. )
  169. response_fulfillment_orders = fulfillment_order.cancel
  170. assert_equal('cancelled', fulfillment_order.status)
  171. assert_equal(2, response_fulfillment_orders.count)
  172. fulfillment_order = response_fulfillment_orders['fulfillment_order']
  173. assert_equal('cancelled', fulfillment_order.status)
  174. replacement_fulfillment_order = response_fulfillment_orders['replacement_fulfillment_order']
  175. assert_equal('open', replacement_fulfillment_order.status)
  176. end
  177. end
  178. context "#close" do
  179. should "be able to close fulfillment order" do
  180. fulfillment_order = ShopifyAPI::FulfillmentOrder.find(519788021)
  181. fulfillment_order.status = 'in_progress'
  182. closed = ActiveSupport::JSON.decode(load_fixture('fulfillment_order'))
  183. closed['status'] = 'incomplete'
  184. request_body = {
  185. fulfillment_order: {
  186. message: "Test close message.",
  187. },
  188. }
  189. fake(
  190. 'fulfillment_orders',
  191. url: "#{@url_prefix}/fulfillment_orders/519788021/close.json",
  192. method: :post,
  193. request_body: ActiveSupport::JSON.encode(request_body),
  194. body: ActiveSupport::JSON.encode(closed)
  195. )
  196. assert(fulfillment_order.close(message: "Test close message."))
  197. assert_equal('incomplete', fulfillment_order.status)
  198. end
  199. end
  200. context "#request_fulfillment" do
  201. should "make a fulfillment request for a fulfillment order including unsubmitted" do
  202. fake_original_fulfillment_order = ActiveSupport::JSON.decode(load_fixture('fulfillment_order'))
  203. fake_original_fulfillment_order['status'] = 'closed'
  204. fake_submitted_fulfillment_order = fake_original_fulfillment_order.clone
  205. fake_submitted_fulfillment_order['id'] = 2
  206. fake_submitted_fulfillment_order['status'] = 'open'
  207. fake_submitted_fulfillment_order['request_status'] = 'submitted'
  208. fake_unsubmitted_fulfillment_order = fake_original_fulfillment_order.clone
  209. fake_unsubmitted_fulfillment_order['id'] = 3
  210. fake_unsubmitted_fulfillment_order['status'] = 'open'
  211. fake_unsubmitted_fulfillment_order['request_status'] = 'unsubmitted'
  212. body = {
  213. original_fulfillment_order: fake_original_fulfillment_order,
  214. submitted_fulfillment_order: fake_submitted_fulfillment_order,
  215. unsubmitted_fulfillment_order: fake_unsubmitted_fulfillment_order,
  216. }
  217. request_body = {
  218. fulfillment_request: {
  219. fulfillment_order_line_items: [
  220. { id: 1, quantity: 1 },
  221. ],
  222. message: 'Fulfill this FO, please.',
  223. },
  224. }
  225. fake(
  226. 'fulfillment_orders',
  227. url: "#{@url_prefix}/fulfillment_orders/519788021/fulfillment_request.json",
  228. method: :post,
  229. request_body: ActiveSupport::JSON.encode(request_body),
  230. body: ActiveSupport::JSON.encode(body)
  231. )
  232. fulfillment_order = ShopifyAPI::FulfillmentOrder.find(519788021)
  233. params = {
  234. fulfillment_order_line_items: [{ id: 1, quantity: 1 }],
  235. message: "Fulfill this FO, please.",
  236. }
  237. response_fulfillment_orders = fulfillment_order.request_fulfillment(params)
  238. assert_equal('closed', fulfillment_order.status)
  239. assert_equal(3, response_fulfillment_orders.size)
  240. original_fulfillment_order = response_fulfillment_orders['original_fulfillment_order']
  241. assert_equal(519788021, original_fulfillment_order.id)
  242. assert_equal('closed', original_fulfillment_order.status)
  243. submitted_fulfillment_order = response_fulfillment_orders['submitted_fulfillment_order']
  244. assert_equal(2, submitted_fulfillment_order.id)
  245. assert_equal('open', submitted_fulfillment_order.status)
  246. assert_equal('submitted', submitted_fulfillment_order.request_status)
  247. unsubmitted_fulfillment_order = response_fulfillment_orders['unsubmitted_fulfillment_order']
  248. assert_equal(3, unsubmitted_fulfillment_order.id)
  249. assert_equal('open', unsubmitted_fulfillment_order.status)
  250. assert_equal('unsubmitted', unsubmitted_fulfillment_order.request_status)
  251. end
  252. should "make a fulfillment request for a fulfillment order excluding unsubmitted" do
  253. fake_original_fulfillment_order = ActiveSupport::JSON.decode(load_fixture('fulfillment_order'))
  254. fake_original_fulfillment_order['status'] = 'closed'
  255. fake_submitted_fulfillment_order = fake_original_fulfillment_order.clone
  256. fake_submitted_fulfillment_order['id'] = 2
  257. fake_submitted_fulfillment_order['status'] = 'open'
  258. fake_submitted_fulfillment_order['request_status'] = 'submitted'
  259. body = {
  260. original_fulfillment_order: fake_original_fulfillment_order,
  261. submitted_fulfillment_order: fake_submitted_fulfillment_order,
  262. unsubmitted_fulfillment_order: nil,
  263. }
  264. request_body = {
  265. fulfillment_request: {
  266. fulfillment_order_line_items: [
  267. { id: 1, quantity: 1 },
  268. ],
  269. message: 'Fulfill this FO, please.',
  270. },
  271. }
  272. fake(
  273. 'fulfillment_orders',
  274. url: "#{@url_prefix}/fulfillment_orders/519788021/fulfillment_request.json",
  275. method: :post,
  276. request_body: ActiveSupport::JSON.encode(request_body),
  277. body: ActiveSupport::JSON.encode(body)
  278. )
  279. fulfillment_order = ShopifyAPI::FulfillmentOrder.find(519788021)
  280. params = {
  281. fulfillment_order_line_items: [{ id: 1, quantity: 1 }],
  282. message: "Fulfill this FO, please.",
  283. }
  284. response_fulfillment_orders = fulfillment_order.request_fulfillment(params)
  285. assert_equal('closed', fulfillment_order.status)
  286. assert_equal(3, response_fulfillment_orders.size)
  287. original_fulfillment_order = response_fulfillment_orders['original_fulfillment_order']
  288. assert_equal(519788021, original_fulfillment_order.id)
  289. assert_equal('closed', original_fulfillment_order.status)
  290. submitted_fulfillment_order = response_fulfillment_orders['submitted_fulfillment_order']
  291. assert_equal(2, submitted_fulfillment_order.id)
  292. assert_equal('open', submitted_fulfillment_order.status)
  293. assert_equal('submitted', submitted_fulfillment_order.request_status)
  294. assert_nil(response_fulfillment_orders['unsubmitted_fulfillment_order'])
  295. end
  296. end
  297. context "#accept_fulfillment_request" do
  298. should "accept a fulfillment request for a fulfillment order" do
  299. fulfillment_order = ShopifyAPI::FulfillmentOrder.find(519788021)
  300. message = "LGTM. Accept this FO fulfillment request"
  301. request_body = {
  302. 'fulfillment_request' => {
  303. 'message' => message,
  304. },
  305. }
  306. fake_response = {
  307. fulfillment_order: fulfillment_order.attributes.merge(status: 'in_progress', request_status: 'accepted'),
  308. }
  309. fake(
  310. 'fulfillment_orders',
  311. url: "#{@url_prefix}/fulfillment_orders/519788021/fulfillment_request/accept.json",
  312. method: :post,
  313. request_body: ActiveSupport::JSON.encode(request_body),
  314. body: ActiveSupport::JSON.encode(fake_response)
  315. )
  316. accepted = fulfillment_order.accept_fulfillment_request(message: message)
  317. assert_equal(true, accepted)
  318. assert_equal('in_progress', fulfillment_order.status)
  319. assert_equal('accepted', fulfillment_order.request_status)
  320. end
  321. end
  322. context "#reject_fulfillment_request" do
  323. should "reject a fulfillment request for a fulfillment order" do
  324. fulfillment_order = ShopifyAPI::FulfillmentOrder.find(519788021)
  325. message = "LBTM. Reject this FO fulfillment request"
  326. request_body = {
  327. 'fulfillment_request' => {
  328. 'message' => message,
  329. },
  330. }
  331. fake_response = {
  332. fulfillment_order: fulfillment_order.attributes.merge(status: 'open', request_status: 'rejected'),
  333. }
  334. fake(
  335. 'fulfillment_orders',
  336. url: "#{@url_prefix}/fulfillment_orders/519788021/fulfillment_request/reject.json",
  337. method: :post,
  338. request_body: ActiveSupport::JSON.encode(request_body),
  339. body: ActiveSupport::JSON.encode(fake_response)
  340. )
  341. rejected = fulfillment_order.reject_fulfillment_request(message: message)
  342. assert_equal(true, rejected)
  343. assert_equal('open', fulfillment_order.status)
  344. assert_equal('rejected', fulfillment_order.request_status)
  345. end
  346. end
  347. context "#request_cancellation" do
  348. should "make a cancellation request for a fulfillment order" do
  349. fulfillment_order = ShopifyAPI::FulfillmentOrder.find(519788021)
  350. message = "Cancelling this please."
  351. request_body = {
  352. 'cancellation_request' => {
  353. 'message' => message,
  354. },
  355. }
  356. cancelling = ActiveSupport::JSON.decode(load_fixture('fulfillment_order'))
  357. cancelling['status'] = 'in_progress'
  358. cancelling['request_status'] = 'cancellation_requested'
  359. fake(
  360. 'fulfillment_orders',
  361. url: "#{@url_prefix}/fulfillment_orders/519788021/cancellation_request.json",
  362. method: :post,
  363. request_body: ActiveSupport::JSON.encode(request_body),
  364. body: ActiveSupport::JSON.encode({ fulfillment_order: cancelling })
  365. )
  366. cancelled = fulfillment_order.request_cancellation(message: "Cancelling this please.")
  367. assert_equal(true, cancelled)
  368. assert_equal('in_progress', fulfillment_order.status)
  369. assert_equal('cancellation_requested', fulfillment_order.request_status)
  370. end
  371. end
  372. context "#accept_cancellation_request" do
  373. should "accept a cancellation request for a fulfillment order" do
  374. fulfillment_order = ShopifyAPI::FulfillmentOrder.find(519788021)
  375. message = 'Already in-progress. Reject this FO cancellation request'
  376. request_body = {
  377. 'cancellation_request' => {
  378. 'message' => message,
  379. },
  380. }
  381. fake_response = {
  382. fulfillment_order: fulfillment_order.attributes.merge(status: 'cancelled',
  383. request_status: 'cancellation_accepted'),
  384. }
  385. fake(
  386. 'fulfillment_orders',
  387. url: "#{@url_prefix}/fulfillment_orders/519788021/cancellation_request/accept.json",
  388. method: :post,
  389. request_body: ActiveSupport::JSON.encode(request_body),
  390. body: ActiveSupport::JSON.encode(fake_response)
  391. )
  392. accepted = fulfillment_order.accept_cancellation_request(message: message)
  393. assert_equal(true, accepted)
  394. assert_equal('cancelled', fulfillment_order.status)
  395. assert_equal('cancellation_accepted', fulfillment_order.request_status)
  396. end
  397. end
  398. context "#reject_cancellation_request" do
  399. should "reject a cancellation request for a fulfillment order" do
  400. fulfillment_order = ShopifyAPI::FulfillmentOrder.find(519788021)
  401. message = "Already in-progress. Reject this FO cancellation request"
  402. request_body = {
  403. 'cancellation_request' => {
  404. 'message' => message,
  405. },
  406. }
  407. fake_response = {
  408. fulfillment_order: fulfillment_order.attributes.merge(status: 'in_progress',
  409. request_status: 'cancellation_rejected'),
  410. }
  411. fake(
  412. 'fulfillment_orders',
  413. url: "#{@url_prefix}/fulfillment_orders/519788021/cancellation_request/reject.json",
  414. method: :post,
  415. request_body: request_body,
  416. body: ActiveSupport::JSON.encode(fake_response)
  417. )
  418. rejected = fulfillment_order.reject_cancellation_request(message: message)
  419. assert_equal(true, rejected)
  420. assert_equal('in_progress', fulfillment_order.status)
  421. assert_equal('cancellation_rejected', fulfillment_order.request_status)
  422. end
  423. end
  424. end
  425. end