123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530 |
- # frozen_string_literal: true
- require 'test_helper'
- require 'fulfillment_order_test_helper'
- class FulFillmentOrderTest < Test::Unit::TestCase
- include FulfillmentOrderTestHelper
- def setup
- super
- @url_prefix = url_prefix_for_activated_session_for('2020-01')
- fake(
- 'fulfillment_orders',
- url: "#{@url_prefix}/fulfillment_orders/519788021.json",
- method: :get,
- body: load_fixture('fulfillment_order')
- )
- end
- context "FulfillmentOrder" do
- context ".new" do
- should "raise NotImplementedError when api_version is older than 2020-01" do
- url_prefix_for_activated_session_for('2019-10')
- fulfillment_order = load_fixture('fulfillment_order')
- exception = assert_raises(NotImplementedError) do
- ShopifyAPI::FulfillmentOrder.new(ActiveSupport::JSON.decode(fulfillment_order))
- end
- assert_equal(
- "The minimum supported version is 2020-01.",
- exception.message
- )
- end
- end
- context ".find and .all" do
- should "raise NotImplementedError when api_version is older than 2020-01" do
- @url_prefix = url_prefix_for_activated_session_for('2019-10')
- fake(
- 'fulfillment_orders',
- url: "#{@url_prefix}/fulfillment_orders/519788021.json",
- method: :get,
- body: load_fixture('fulfillment_order')
- )
- exception = assert_raises(NotImplementedError) do
- ShopifyAPI::FulfillmentOrder.find(519788021)
- end
- assert_equal(
- "The minimum supported version is 2020-01.",
- exception.message
- )
- assert_raises(NotImplementedError) do
- ShopifyAPI::FulfillmentOrder.all(params: { order_id: 450789469 })
- end
- end
- end
- context "#find" do
- should "be able to find fulfillment order" do
- fulfillment_order = ShopifyAPI::FulfillmentOrder.find(519788021)
- assert(fulfillment_order.is_a?(ShopifyAPI::FulfillmentOrder))
- assert_equal(519788021, fulfillment_order.id)
- assert_equal(450789469, fulfillment_order.order_id)
- end
- end
- context "#all" do
- should "be able to list fulfillment orders for an order" do
- fake(
- 'orders',
- url: "#{@url_prefix}/orders/450789469/fulfillment_orders.json",
- method: :get,
- body: load_fixture('fulfillment_orders')
- )
- fulfillment_orders = ShopifyAPI::FulfillmentOrder.all(
- params: { order_id: 450789469 }
- )
- assert_equal([519788021, 519788022], fulfillment_orders.map(&:id).sort)
- fulfillment_orders.each do |fulfillment_order|
- assert(fulfillment_order.is_a?(ShopifyAPI::FulfillmentOrder))
- assert_equal(450789469, fulfillment_order.order_id)
- end
- end
- should "require order_id" do
- assert_raises(ShopifyAPI::ValidationException) do
- ShopifyAPI::FulfillmentOrder.all
- end
- end
- end
- context "#fulfillments" do
- should "be able to list fulfillments for a fulfillment order" do
- fulfillment_order = ShopifyAPI::FulfillmentOrder.find(519788021)
- fake(
- 'fulfillment_orders',
- url: "#{@url_prefix}/fulfillment_orders/#{fulfillment_order.id}/fulfillments.json",
- method: :get,
- body: load_fixture('fulfillments')
- )
- fulfillments = fulfillment_order.fulfillments
- assert_equal(1, fulfillments.count)
- fulfillment = fulfillments.first
- assert(fulfillment.is_a?(ShopifyAPI::Fulfillment))
- assert_equal(450789469, fulfillment.order_id)
- end
- end
- context "#locations_for_move" do
- should "be able to list locations for a fulfillment order" do
- fulfillment_order = ShopifyAPI::FulfillmentOrder.find(519788021)
- fake(
- 'fulfillment_orders',
- url: "#{@url_prefix}/fulfillment_orders/#{fulfillment_order.id}/locations_for_move.json",
- method: :get,
- body: load_fixture('fulfillment_order_locations_for_move')
- )
- locations_for_move = fulfillment_order.locations_for_move
- assert_equal(2, locations_for_move.count)
- location_for_move = locations_for_move.first
- assert(location_for_move.is_a?(ShopifyAPI::FulfillmentOrderLocationsForMove))
- location = location_for_move.location
- assert(location.is_a?(ShopifyAPI::Location))
- assert_equal(1059367776, location.id)
- end
- end
- context "#move" do
- should "move a fulfillment order to a new_location_id" do
- fulfillment_order = ShopifyAPI::FulfillmentOrder.find(519788021)
- new_location_id = 5
- fake_original_fulfillment_order = fulfillment_order.clone
- fake_original_fulfillment_order.status = 'closed'
- fake_moved_fulfillment_order = ActiveSupport::JSON.decode(load_fixture('fulfillment_order'))
- fake_moved_fulfillment_order['assigned_location_id'] = new_location_id
- request_body = { fulfillment_order: { new_location_id: 5 } }
- body = {
- original_fulfillment_order: fake_original_fulfillment_order,
- moved_fulfillment_order: fake_moved_fulfillment_order,
- remaining_fulfillment_order: nil,
- }
- fake(
- 'fulfillment_orders',
- url: "#{@url_prefix}/fulfillment_orders/519788021/move.json",
- method: :post,
- request_body: ActiveSupport::JSON.encode(request_body),
- body: ActiveSupport::JSON.encode(body)
- )
- response_fulfillment_orders = fulfillment_order.move(new_location_id: new_location_id)
- assert_equal('closed', fulfillment_order.status)
- assert_equal(3, response_fulfillment_orders.count)
- original_fulfillment_order = response_fulfillment_orders['original_fulfillment_order']
- refute_nil original_fulfillment_order
- assert(original_fulfillment_order.is_a?(ShopifyAPI::FulfillmentOrder))
- assert_equal('closed', original_fulfillment_order.status)
- moved_fulfillment_order = response_fulfillment_orders['moved_fulfillment_order']
- refute_nil moved_fulfillment_order
- assert(moved_fulfillment_order.is_a?(ShopifyAPI::FulfillmentOrder))
- assert_equal('open', moved_fulfillment_order.status)
- assert_equal(new_location_id, moved_fulfillment_order.assigned_location_id)
- remaining_fulfillment_order = response_fulfillment_orders['remaining_fulfillment_order']
- assert_nil(remaining_fulfillment_order)
- end
- end
- context "#cancel" do
- should "cancel a fulfillment order" do
- fulfillment_order = ShopifyAPI::FulfillmentOrder.find(519788021)
- assert_equal('open', fulfillment_order.status)
- cancelled = ActiveSupport::JSON.decode(load_fixture('fulfillment_order'))
- cancelled['status'] = 'cancelled'
- body = {
- fulfillment_order: cancelled,
- replacement_fulfillment_order: fulfillment_order,
- }
- fake(
- 'fulfillment_orders',
- url: "#{@url_prefix}/fulfillment_orders/519788021/cancel.json",
- method: :post,
- body: ActiveSupport::JSON.encode(body)
- )
- response_fulfillment_orders = fulfillment_order.cancel
- assert_equal('cancelled', fulfillment_order.status)
- assert_equal(2, response_fulfillment_orders.count)
- fulfillment_order = response_fulfillment_orders['fulfillment_order']
- assert_equal('cancelled', fulfillment_order.status)
- replacement_fulfillment_order = response_fulfillment_orders['replacement_fulfillment_order']
- assert_equal('open', replacement_fulfillment_order.status)
- end
- end
- context "#close" do
- should "be able to close fulfillment order" do
- fulfillment_order = ShopifyAPI::FulfillmentOrder.find(519788021)
- fulfillment_order.status = 'in_progress'
- closed = ActiveSupport::JSON.decode(load_fixture('fulfillment_order'))
- closed['status'] = 'incomplete'
- request_body = {
- fulfillment_order: {
- message: "Test close message.",
- },
- }
- fake(
- 'fulfillment_orders',
- url: "#{@url_prefix}/fulfillment_orders/519788021/close.json",
- method: :post,
- request_body: ActiveSupport::JSON.encode(request_body),
- body: ActiveSupport::JSON.encode(closed)
- )
- assert(fulfillment_order.close(message: "Test close message."))
- assert_equal('incomplete', fulfillment_order.status)
- end
- end
- context "#open" do
- should "be able to open fulfillment order" do
- fulfillment_order = ShopifyAPI::FulfillmentOrder.find(519788021)
- fulfillment_order.status = 'scheduled'
- opened = ActiveSupport::JSON.decode(load_fixture('fulfillment_order'))
- opened['status'] = 'open'
- body = {
- fulfillment_order: opened,
- }
- fake(
- 'fulfillment_orders',
- url: "#{@url_prefix}/fulfillment_orders/519788021/open.json",
- method: :post,
- body: ActiveSupport::JSON.encode(body)
- )
- assert(fulfillment_order.open)
- assert_equal('open', fulfillment_order.status)
- end
- end
- context "#reschedule" do
- should "be able to rescheduled fulfillment order" do
- fulfillment_order = ShopifyAPI::FulfillmentOrder.find(519788021)
- fulfillment_order.status = 'scheduled'
- new_fulfill_at = "2021-11-29"
- rescheduled = ActiveSupport::JSON.decode(load_fixture('fulfillment_order'))
- rescheduled['status'] = 'scheduled'
- rescheduled['fulfill_at'] = new_fulfill_at
- body = {
- fulfillment_order: rescheduled,
- }
- request_body = { fulfillment_order: { new_fulfill_at: new_fulfill_at } }
- fake(
- 'fulfillment_orders',
- url: "#{@url_prefix}/fulfillment_orders/519788021/reschedule.json",
- method: :post,
- request_body: ActiveSupport::JSON.encode(request_body),
- body: ActiveSupport::JSON.encode(body)
- )
- assert(fulfillment_order.reschedule(new_fulfill_at: new_fulfill_at))
- assert_equal('scheduled', fulfillment_order.status)
- assert_equal(new_fulfill_at, fulfillment_order.fulfill_at)
- end
- end
- context "#request_fulfillment" do
- should "make a fulfillment request for a fulfillment order including unsubmitted" do
- fake_original_fulfillment_order = ActiveSupport::JSON.decode(load_fixture('fulfillment_order'))
- fake_original_fulfillment_order['status'] = 'closed'
- fake_submitted_fulfillment_order = fake_original_fulfillment_order.clone
- fake_submitted_fulfillment_order['id'] = 2
- fake_submitted_fulfillment_order['status'] = 'open'
- fake_submitted_fulfillment_order['request_status'] = 'submitted'
- fake_unsubmitted_fulfillment_order = fake_original_fulfillment_order.clone
- fake_unsubmitted_fulfillment_order['id'] = 3
- fake_unsubmitted_fulfillment_order['status'] = 'open'
- fake_unsubmitted_fulfillment_order['request_status'] = 'unsubmitted'
- body = {
- original_fulfillment_order: fake_original_fulfillment_order,
- submitted_fulfillment_order: fake_submitted_fulfillment_order,
- unsubmitted_fulfillment_order: fake_unsubmitted_fulfillment_order,
- }
- request_body = {
- fulfillment_request: {
- fulfillment_order_line_items: [
- { id: 1, quantity: 1 },
- ],
- message: 'Fulfill this FO, please.',
- },
- }
- fake(
- 'fulfillment_orders',
- url: "#{@url_prefix}/fulfillment_orders/519788021/fulfillment_request.json",
- method: :post,
- request_body: ActiveSupport::JSON.encode(request_body),
- body: ActiveSupport::JSON.encode(body)
- )
- fulfillment_order = ShopifyAPI::FulfillmentOrder.find(519788021)
- params = {
- fulfillment_order_line_items: [{ id: 1, quantity: 1 }],
- message: "Fulfill this FO, please.",
- }
- response_fulfillment_orders = fulfillment_order.request_fulfillment(params)
- assert_equal('closed', fulfillment_order.status)
- assert_equal(3, response_fulfillment_orders.size)
- original_fulfillment_order = response_fulfillment_orders['original_fulfillment_order']
- assert_equal(519788021, original_fulfillment_order.id)
- assert_equal('closed', original_fulfillment_order.status)
- submitted_fulfillment_order = response_fulfillment_orders['submitted_fulfillment_order']
- assert_equal(2, submitted_fulfillment_order.id)
- assert_equal('open', submitted_fulfillment_order.status)
- assert_equal('submitted', submitted_fulfillment_order.request_status)
- unsubmitted_fulfillment_order = response_fulfillment_orders['unsubmitted_fulfillment_order']
- assert_equal(3, unsubmitted_fulfillment_order.id)
- assert_equal('open', unsubmitted_fulfillment_order.status)
- assert_equal('unsubmitted', unsubmitted_fulfillment_order.request_status)
- end
- should "make a fulfillment request for a fulfillment order excluding unsubmitted" do
- fake_original_fulfillment_order = ActiveSupport::JSON.decode(load_fixture('fulfillment_order'))
- fake_original_fulfillment_order['status'] = 'closed'
- fake_submitted_fulfillment_order = fake_original_fulfillment_order.clone
- fake_submitted_fulfillment_order['id'] = 2
- fake_submitted_fulfillment_order['status'] = 'open'
- fake_submitted_fulfillment_order['request_status'] = 'submitted'
- body = {
- original_fulfillment_order: fake_original_fulfillment_order,
- submitted_fulfillment_order: fake_submitted_fulfillment_order,
- unsubmitted_fulfillment_order: nil,
- }
- request_body = {
- fulfillment_request: {
- fulfillment_order_line_items: [
- { id: 1, quantity: 1 },
- ],
- message: 'Fulfill this FO, please.',
- },
- }
- fake(
- 'fulfillment_orders',
- url: "#{@url_prefix}/fulfillment_orders/519788021/fulfillment_request.json",
- method: :post,
- request_body: ActiveSupport::JSON.encode(request_body),
- body: ActiveSupport::JSON.encode(body)
- )
- fulfillment_order = ShopifyAPI::FulfillmentOrder.find(519788021)
- params = {
- fulfillment_order_line_items: [{ id: 1, quantity: 1 }],
- message: "Fulfill this FO, please.",
- }
- response_fulfillment_orders = fulfillment_order.request_fulfillment(params)
- assert_equal('closed', fulfillment_order.status)
- assert_equal(3, response_fulfillment_orders.size)
- original_fulfillment_order = response_fulfillment_orders['original_fulfillment_order']
- assert_equal(519788021, original_fulfillment_order.id)
- assert_equal('closed', original_fulfillment_order.status)
- submitted_fulfillment_order = response_fulfillment_orders['submitted_fulfillment_order']
- assert_equal(2, submitted_fulfillment_order.id)
- assert_equal('open', submitted_fulfillment_order.status)
- assert_equal('submitted', submitted_fulfillment_order.request_status)
- assert_nil(response_fulfillment_orders['unsubmitted_fulfillment_order'])
- end
- end
- context "#accept_fulfillment_request" do
- should "accept a fulfillment request for a fulfillment order" do
- fulfillment_order = ShopifyAPI::FulfillmentOrder.find(519788021)
- message = "LGTM. Accept this FO fulfillment request"
- request_body = {
- 'fulfillment_request' => {
- 'message' => message,
- },
- }
- fake_response = {
- fulfillment_order: fulfillment_order.attributes.merge(status: 'in_progress', request_status: 'accepted'),
- }
- fake(
- 'fulfillment_orders',
- url: "#{@url_prefix}/fulfillment_orders/519788021/fulfillment_request/accept.json",
- method: :post,
- request_body: ActiveSupport::JSON.encode(request_body),
- body: ActiveSupport::JSON.encode(fake_response)
- )
- accepted = fulfillment_order.accept_fulfillment_request(message: message)
- assert_equal(true, accepted)
- assert_equal('in_progress', fulfillment_order.status)
- assert_equal('accepted', fulfillment_order.request_status)
- end
- end
- context "#reject_fulfillment_request" do
- should "reject a fulfillment request for a fulfillment order" do
- fulfillment_order = ShopifyAPI::FulfillmentOrder.find(519788021)
- message = "LBTM. Reject this FO fulfillment request"
- request_body = {
- 'fulfillment_request' => {
- 'message' => message,
- },
- }
- fake_response = {
- fulfillment_order: fulfillment_order.attributes.merge(status: 'open', request_status: 'rejected'),
- }
- fake(
- 'fulfillment_orders',
- url: "#{@url_prefix}/fulfillment_orders/519788021/fulfillment_request/reject.json",
- method: :post,
- request_body: ActiveSupport::JSON.encode(request_body),
- body: ActiveSupport::JSON.encode(fake_response)
- )
- rejected = fulfillment_order.reject_fulfillment_request(message: message)
- assert_equal(true, rejected)
- assert_equal('open', fulfillment_order.status)
- assert_equal('rejected', fulfillment_order.request_status)
- end
- end
- context "#request_cancellation" do
- should "make a cancellation request for a fulfillment order" do
- fulfillment_order = ShopifyAPI::FulfillmentOrder.find(519788021)
- message = "Cancelling this please."
- request_body = {
- 'cancellation_request' => {
- 'message' => message,
- },
- }
- cancelling = ActiveSupport::JSON.decode(load_fixture('fulfillment_order'))
- cancelling['status'] = 'in_progress'
- cancelling['request_status'] = 'cancellation_requested'
- fake(
- 'fulfillment_orders',
- url: "#{@url_prefix}/fulfillment_orders/519788021/cancellation_request.json",
- method: :post,
- request_body: ActiveSupport::JSON.encode(request_body),
- body: ActiveSupport::JSON.encode({ fulfillment_order: cancelling })
- )
- cancelled = fulfillment_order.request_cancellation(message: "Cancelling this please.")
- assert_equal(true, cancelled)
- assert_equal('in_progress', fulfillment_order.status)
- assert_equal('cancellation_requested', fulfillment_order.request_status)
- end
- end
- context "#accept_cancellation_request" do
- should "accept a cancellation request for a fulfillment order" do
- fulfillment_order = ShopifyAPI::FulfillmentOrder.find(519788021)
- message = 'Already in-progress. Reject this FO cancellation request'
- request_body = {
- 'cancellation_request' => {
- 'message' => message,
- },
- }
- fake_response = {
- fulfillment_order: fulfillment_order.attributes.merge(status: 'cancelled',
- request_status: 'cancellation_accepted'),
- }
- fake(
- 'fulfillment_orders',
- url: "#{@url_prefix}/fulfillment_orders/519788021/cancellation_request/accept.json",
- method: :post,
- request_body: ActiveSupport::JSON.encode(request_body),
- body: ActiveSupport::JSON.encode(fake_response)
- )
- accepted = fulfillment_order.accept_cancellation_request(message: message)
- assert_equal(true, accepted)
- assert_equal('cancelled', fulfillment_order.status)
- assert_equal('cancellation_accepted', fulfillment_order.request_status)
- end
- end
- context "#reject_cancellation_request" do
- should "reject a cancellation request for a fulfillment order" do
- fulfillment_order = ShopifyAPI::FulfillmentOrder.find(519788021)
- message = "Already in-progress. Reject this FO cancellation request"
- request_body = {
- 'cancellation_request' => {
- 'message' => message,
- },
- }
- fake_response = {
- fulfillment_order: fulfillment_order.attributes.merge(status: 'in_progress',
- request_status: 'cancellation_rejected'),
- }
- fake(
- 'fulfillment_orders',
- url: "#{@url_prefix}/fulfillment_orders/519788021/cancellation_request/reject.json",
- method: :post,
- request_body: request_body,
- body: ActiveSupport::JSON.encode(fake_response)
- )
- rejected = fulfillment_order.reject_cancellation_request(message: message)
- assert_equal(true, rejected)
- assert_equal('in_progress', fulfillment_order.status)
- assert_equal('cancellation_rejected', fulfillment_order.request_status)
- end
- end
- end
- end
|