order_test.rb 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. # frozen_string_literal: true
  2. require 'test_helper'
  3. require 'fulfillment_order_test_helper'
  4. class OrderTest < Test::Unit::TestCase
  5. include FulfillmentOrderTestHelper
  6. test "create should create order" do
  7. fake('orders', method: :post, status: 201, body: load_fixture('order'))
  8. order = ShopifyAPI::Order.create(
  9. line_items: [{ quantity: 1, variant_id: 39072856 }],
  10. financial_status: "authorized"
  11. )
  12. assert_equal(39072856, order.line_items.first.variant_id)
  13. end
  14. test "create should create an order with custom properties" do
  15. props = [
  16. {
  17. "By default may label with \"Roasted for ": {
  18. "Your First Name": {
  19. "\". If you want something specific on the label, enter it here:": "",
  20. },
  21. },
  22. },
  23. ]
  24. fake('orders', method: :post, status: 201, body: load_fixture('order_with_properties'))
  25. order = ShopifyAPI::Order.create(
  26. line_items: [{ quantity: 1, variant_id: 39072856, properties: props }],
  27. financial_status: "authorized"
  28. )
  29. assert_equal(39072856, order.line_items.first.variant_id)
  30. end
  31. test "get should get an order" do
  32. fake('orders/450789469', method: :get, status: 200, body: load_fixture('order'))
  33. order = ShopifyAPI::Order.find(450789469)
  34. assert_equal(450789469, order.id)
  35. end
  36. test "get should get an order with custom properties" do
  37. fake('orders/450789469', method: :get, status: 200, body: load_fixture('order_with_properties'))
  38. order = ShopifyAPI::Order.find(450789469)
  39. assert_equal(450789469, order.id)
  40. end
  41. test "get all should get all orders" do
  42. fake('orders', method: :get, status: 200, body: load_fixture('orders'))
  43. order = ShopifyAPI::Order.all
  44. assert_equal(450789469, order.first.id)
  45. end
  46. test "add note should add a note to order" do
  47. fake('orders/450789469', method: :get, status: 200, body: load_fixture('order'))
  48. order = ShopifyAPI::Order.find(450789469)
  49. order.note = "Test note"
  50. fake('orders/450789469', method: :put, status: 200, body: load_fixture('order'))
  51. order.save
  52. assert_equal("Test note", order.note)
  53. end
  54. test "delete should delete an order" do
  55. fake('orders/450789469', method: :get, status: 200, body: load_fixture('order'))
  56. fake('orders/450789469', method: :delete, body: 'destroyed')
  57. order = ShopifyAPI::Order.find(450789469)
  58. assert(order.destroy)
  59. end
  60. test "cancel an order with params" do
  61. fake('orders/450789469', method: :get, status: 200, body: load_fixture('order'))
  62. fake('orders/450789469/cancel', method: :post, body: load_fixture('order'))
  63. order = ShopifyAPI::Order.find(450789469)
  64. order.cancel(email: false, restock: true)
  65. assert_request_body({ 'email' => false, 'restock' => true }.to_json)
  66. end
  67. test "capture an order with currency param" do
  68. fake('orders/450789469', body: load_fixture('order'))
  69. order = ShopifyAPI::Order.find(450789469)
  70. fake('orders/450789469/transactions', method: :post, status: 201, body: load_fixture('transaction'))
  71. order.capture(100.00, currency: 'CAD')
  72. assert_request_body({
  73. transaction: {
  74. amount: 100.00,
  75. kind: 'capture',
  76. currency: 'CAD',
  77. },
  78. }.to_json)
  79. end
  80. test "fulfillment_orders should get fulfillment orders for an order with 2020-01 version" do
  81. url_prefix = url_prefix_for_activated_session_for('2020-01')
  82. fake(
  83. 'orders',
  84. url: "#{url_prefix}/orders/450789469.json",
  85. method: :get,
  86. status: 200,
  87. body: load_fixture('order'),
  88. extension: false
  89. )
  90. order = ShopifyAPI::Order.find(450789469)
  91. fake(
  92. 'orders',
  93. url: "#{url_prefix}/orders/450789469/fulfillment_orders.json",
  94. method: :get,
  95. status: 200,
  96. body: load_fixture('fulfillment_orders'),
  97. extension: false
  98. )
  99. fulfillment_orders = order.fulfillment_orders
  100. assert_equal([519788021, 519788022], fulfillment_orders.map(&:id).sort)
  101. fulfillment_orders.each do |fulfillment_order|
  102. assert(fulfillment_order.is_a?(ShopifyAPI::FulfillmentOrder))
  103. assert_equal(450789469, fulfillment_order.order_id)
  104. end
  105. end
  106. test "fulfillment_orders raises NotImplementedError when api_version is older than 2020-01" do
  107. url_prefix = url_prefix_for_activated_session_for('2019-10')
  108. fake(
  109. 'orders',
  110. url: "#{url_prefix}/orders/450789469.json",
  111. method: :get,
  112. status: 200,
  113. body: load_fixture('order'),
  114. extension: false
  115. )
  116. order = ShopifyAPI::Order.find(450789469)
  117. assert_raises(NotImplementedError) do
  118. order.fulfillment_orders
  119. end
  120. end
  121. end