checkouts_test.rb 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # frozen_string_literal: true
  2. require 'test_helper'
  3. class CheckoutsTest < Test::Unit::TestCase
  4. def setup
  5. super
  6. @expected_checkouts = JSON.parse(load_fixture('checkouts'))['checkouts']
  7. @expected_checkout_id = JSON.parse(load_fixture('checkout'))['checkout']['token']
  8. end
  9. test ".headers includes a version" do
  10. assert_equal "2016-09-06", ShopifyAPI::Checkout.headers["X-Shopify-Checkout-Version"]
  11. assert_nil ShopifyAPI::Base.headers["X-Shopify-Checkout-Version"]
  12. end
  13. test ":create creates a checkout" do
  14. fake 'checkouts', method: :post, status: 201, body: load_fixture('checkout')
  15. checkout = ShopifyAPI::Checkout.create
  16. assert_equal @expected_checkout_id, checkout.id
  17. end
  18. test "get all checkouts indexed by token" do
  19. fake 'checkouts', method: :get, status: 200, body: load_fixture('checkouts')
  20. checkouts = ShopifyAPI::Checkout.all
  21. assert_equal @expected_checkout_id, checkouts.first.id
  22. assert_equal @expected_checkouts.size, checkouts.size
  23. end
  24. test ":complete completes a checkout" do
  25. fake "checkouts/#{@expected_checkout_id}", method: :get, status: 200, body: load_fixture('checkout')
  26. checkout = ShopifyAPI::Checkout.find(@expected_checkout_id)
  27. fake "checkouts/#{@expected_checkout_id}/complete", method: :post, status: 200, body: load_fixture('checkouts')
  28. checkout.complete
  29. end
  30. test ":ready? returns true when status is 201" do
  31. fake "checkouts/#{@expected_checkout_id}", method: :get, status: 201, body: load_fixture('checkout')
  32. checkout = ShopifyAPI::Checkout.find(@expected_checkout_id)
  33. assert_predicate checkout, :ready?
  34. end
  35. test ":ready? returns false when status is 202" do
  36. fake "checkouts/#{@expected_checkout_id}", method: :get, status: 202, body: load_fixture('checkout')
  37. checkout = ShopifyAPI::Checkout.find(@expected_checkout_id)
  38. refute_predicate checkout, :ready?
  39. end
  40. test ":payments returns payments for a checkout" do
  41. fake "checkouts/#{@expected_checkout_id}", method: :get, status: 200, body: load_fixture('checkout')
  42. checkout = ShopifyAPI::Checkout.find(@expected_checkout_id)
  43. fake "checkouts/#{@expected_checkout_id}/payments", method: :get, status: 202, body: load_fixture('payments')
  44. assert_equal 10.00, checkout.payments.first.amount
  45. end
  46. test ":shipping_rates returns shipping rates for a checkout" do
  47. fake "checkouts/#{@expected_checkout_id}", method: :get, status: 200, body: load_fixture('checkout')
  48. checkout = ShopifyAPI::Checkout.find(@expected_checkout_id)
  49. fake("checkouts/#{@expected_checkout_id}/shipping_rates",
  50. method: :get,
  51. status: 202,
  52. body: load_fixture('shipping_rates'))
  53. assert_equal "canada_post-INT.TP.BOGUS-4.00", checkout.shipping_rates.first.id
  54. end
  55. end