draft_order_test.rb 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. # frozen_string_literal: true
  2. require 'test_helper'
  3. class DraftOrderTest < Test::Unit::TestCase
  4. def setup
  5. super
  6. fake('draft_orders/517119332', body: load_fixture('draft_order'))
  7. @draft_order = ShopifyAPI::DraftOrder.find(517119332)
  8. end
  9. def test_get_draft_order
  10. fake('draft_orders/517119332', method: :get, status: 200, body: load_fixture('draft_order'))
  11. draft_order = ShopifyAPI::DraftOrder.find(517119332)
  12. assert_equal(517119332, draft_order.id)
  13. end
  14. def test_get_all_draft_orders
  15. fake('draft_orders', method: :get, status: 200, body: load_fixture('draft_orders'))
  16. draft_orders = ShopifyAPI::DraftOrder.all
  17. assert_equal(1, draft_orders.length)
  18. assert_equal(517119332, draft_orders.first.id)
  19. end
  20. def test_get_count_draft_orders
  21. fake('draft_orders/count', method: :get, status: 200, body: '{"count": 16}')
  22. draft_orders_count = ShopifyAPI::DraftOrder.count
  23. assert_equal(16, draft_orders_count)
  24. end
  25. def test_create_draft_order
  26. fake('draft_orders', method: :post, status: 201, body: load_fixture('draft_order'))
  27. draft_order = ShopifyAPI::DraftOrder.create(line_items: [{ quantity: 1, variant_id: 39072856 }])
  28. assert_equal('{"draft_order":{"line_items":[{"quantity":1,"variant_id":39072856}]}}', WebMock.last_request.body)
  29. assert_equal(39072856, draft_order.line_items.first.variant_id)
  30. end
  31. def test_create_draft_order_202
  32. fake('draft_orders', method: :post, status: 202, body: load_fixture('draft_order'))
  33. draft_order = ShopifyAPI::DraftOrder.create(line_items: [{ quantity: 1, variant_id: 39072856 }])
  34. assert_equal(39072856, draft_order.line_items.first.variant_id)
  35. end
  36. def test_update_draft_order
  37. draft_order_response = ActiveSupport::JSON.decode(load_fixture('draft_order'))
  38. draft_order_response['draft_order']['note'] = 'Test new note'
  39. @draft_order.note = 'Test new note'
  40. fake('draft_orders/517119332', method: :put, status: 200, body: ActiveSupport::JSON.encode(draft_order_response))
  41. @draft_order.save
  42. assert_equal(draft_order_response['draft_order']['note'], @draft_order.note)
  43. end
  44. def test_send_invoice_with_no_params
  45. draft_order_invoice_fixture = load_fixture('draft_order_invoice')
  46. draft_order_invoice = ActiveSupport::JSON.decode(draft_order_invoice_fixture)
  47. fake('draft_orders/517119332/send_invoice', method: :post, body: draft_order_invoice_fixture)
  48. draft_order_invoice_response = @draft_order.send_invoice
  49. assert_equal('{"draft_order_invoice":{}}', WebMock.last_request.body)
  50. assert_kind_of(ShopifyAPI::DraftOrderInvoice, draft_order_invoice_response)
  51. assert_equal(draft_order_invoice['draft_order_invoice']['to'], draft_order_invoice_response.to)
  52. end
  53. def test_send_invoice_with_params
  54. draft_order_invoice_fixture = load_fixture('draft_order_invoice')
  55. draft_order_invoice = ActiveSupport::JSON.decode(draft_order_invoice_fixture)
  56. fake('draft_orders/517119332/send_invoice', method: :post, body: draft_order_invoice_fixture)
  57. draft_order_invoice_response = @draft_order.send_invoice(
  58. ShopifyAPI::DraftOrderInvoice.new(draft_order_invoice['draft_order_invoice'])
  59. )
  60. assert_equal(draft_order_invoice, ActiveSupport::JSON.decode(WebMock.last_request.body))
  61. assert_kind_of(ShopifyAPI::DraftOrderInvoice, draft_order_invoice_response)
  62. assert_equal(draft_order_invoice['draft_order_invoice']['to'], draft_order_invoice_response.to)
  63. end
  64. def test_delete_draft_order
  65. fake('draft_orders/517119332', method: :delete, body: 'destroyed')
  66. assert(@draft_order.destroy)
  67. end
  68. def test_add_metafields_to_draft_order
  69. fake('draft_orders/517119332/metafields', method: :post, status: 201, body: load_fixture('metafield'))
  70. field = @draft_order.add_metafield(
  71. ShopifyAPI::Metafield.new(namespace: 'contact', key: 'email', value: '123@example.com', value_type: 'string')
  72. )
  73. assert_equal(
  74. ActiveSupport::JSON.decode(
  75. '{"metafield":{"namespace":"contact","key":"email","value":"123@example.com","value_type":"string"}}'
  76. ),
  77. ActiveSupport::JSON.decode(WebMock.last_request.body)
  78. )
  79. assert(!field.new_record?)
  80. assert_equal('contact', field.namespace)
  81. assert_equal('email', field.key)
  82. assert_equal('123@example.com', field.value)
  83. end
  84. def test_get_all_metafields_for_draft_order
  85. fake('draft_orders/517119332/metafields', body: load_fixture('metafields'))
  86. metafields = @draft_order.metafields
  87. assert_equal(3, metafields.length)
  88. assert(metafields.all? { |m| m.is_a?(ShopifyAPI::Metafield) })
  89. end
  90. def test_get_2_metafields_for_draft_order
  91. body = ActiveSupport::JSON.decode(load_fixture('metafields'))
  92. body['metafields'].slice!(2, 1)
  93. fake('draft_orders/517119332/metafields.json?limit=2', body: body.to_json, extension: false)
  94. metafields = @draft_order.metafields(limit: 2)
  95. assert_equal(2, metafields.length)
  96. assert(metafields.all? { |m| m.is_a?(ShopifyAPI::Metafield) })
  97. end
  98. def test_complete_draft_order_with_no_params
  99. completed_fixture = load_fixture('draft_order_completed')
  100. completed_draft = ActiveSupport::JSON.decode(completed_fixture)['draft_order']
  101. fake('draft_orders/517119332/complete', method: :put, status: 200, body: completed_fixture)
  102. @draft_order.complete
  103. assert_equal(completed_draft['status'], @draft_order.status)
  104. assert_equal(completed_draft['order_id'], @draft_order.order_id)
  105. refute_nil(@draft_order.completed_at)
  106. end
  107. def test_complete_draft_order_with_params
  108. completed_fixture = load_fixture('draft_order_completed')
  109. completed_draft = ActiveSupport::JSON.decode(completed_fixture)['draft_order']
  110. complete_params = { payment_pending: true }
  111. fake(
  112. 'draft_orders/517119332/complete.json?payment_pending=true',
  113. extension: false,
  114. method: :put,
  115. status: 200,
  116. body: completed_fixture
  117. )
  118. @draft_order.complete(complete_params)
  119. assert_equal(completed_draft['status'], @draft_order.status)
  120. assert_equal(completed_draft['order_id'], @draft_order.order_id)
  121. refute_nil(@draft_order.completed_at)
  122. end
  123. end