Browse Source

Add payments tests

Emmanuel Milou 6 years ago
parent
commit
7068ce96c2

+ 23 - 6
test/abandoned_checkouts_test.rb

@@ -1,12 +1,29 @@
 # frozen_string_literal: true
-
 require 'test_helper'
 
 class AbandonedCheckoutsTest < Test::Unit::TestCase
-  test ":get lists all abandoned checkouts indexed by id" do
-    fake 'checkouts', method: :get, status: 200, body: load_fixture('checkouts')
-    abandoned_checkouts = ShopifyAPI::AbandonedCheckout.all
-    assert_equal 450789469, abandoned_checkouts.first.id
-    assert_equal JSON.parse(load_fixture('checkouts'))['checkouts'].size, abandoned_checkouts.size
+  def setup
+    super
+
+    @expected_checkouts = JSON.parse(load_fixture('abandoned_checkouts'))['checkouts']
+    @expected_checkout_id = JSON.parse(load_fixture('abandoned_checkout'))['checkout']['id']
+  end
+
+  test ":create creates a checkout" do
+    fake 'checkouts', method: :post, status: 201, body: load_fixture('abandoned_checkout')
+
+    checkout = ShopifyAPI::AbandonedCheckout.create
+
+    assert_equal @expected_checkout_id, checkout.id
+    assert_equal true, checkout.attributes.include?(:abandoned_checkout_url)
+  end
+
+  test "get all checkouts indexed by token" do
+    fake 'checkouts', method: :get, status: 200, body: load_fixture('abandoned_checkouts')
+
+    checkouts = ShopifyAPI::AbandonedCheckout.all
+
+    assert_equal @expected_checkout_id, checkouts.first.id
+    assert_equal @expected_checkouts.size, checkouts.size
   end
 end

+ 19 - 7
test/checkouts_test.rb

@@ -1,26 +1,38 @@
+# frozen_string_literal: true
 require 'test_helper'
 
 class CheckoutsTest < Test::Unit::TestCase
+  def setup
+    super
+
+    @expected_checkouts = JSON.parse(load_fixture('checkouts'))['checkouts']
+    @expected_checkout_id = JSON.parse(load_fixture('checkout'))['checkout']['token']
+  end
+
   test ":create creates a checkout" do
     fake 'checkouts', method: :post, status: 201, body: load_fixture('checkout')
+
     checkout = ShopifyAPI::Checkout.create
-    assert_equal "2a1ace52255252df566af0faaedfbfa7", checkout.id
+
+    assert_equal @expected_checkout_id, checkout.id
   end
 
   test "get all checkouts indexed by token" do
     fake 'checkouts', method: :get, status: 200, body: load_fixture('checkouts')
+
     checkouts = ShopifyAPI::Checkout.all
-    assert_equal '2a1ace52255252df566af0faaedfbfa7', checkouts.first.id
-    assert_equal JSON.parse(load_fixture('checkouts'))['checkouts'].size, checkouts.size
+
+    assert_equal @expected_checkout_id, checkouts.first.id
+    assert_equal @expected_checkouts.size, checkouts.size
   end
 
   test ":complete completes a checkout" do
-    checkout_id = JSON.parse(load_fixture('checkout'))['checkout']['id']
+    fake "checkouts/#{@expected_checkout_id}", method: :get, status: 200, body: load_fixture('checkout')
+
+    checkout = ShopifyAPI::Checkout.find(@expected_checkout_id)
 
-    fake "checkouts/#{checkout_id}", method: :get, status: 200, body: load_fixture('checkout')
-    checkout = ShopifyAPI::Checkout.find(checkout_id)
+    fake "checkouts/#{@expected_checkout_id}/complete", method: :post, status: 200, body: load_fixture('checkouts')
 
-    fake "checkouts/#{checkout.id}/complete", method: :post, status: 200, body: load_fixture('checkouts')
     checkout.complete
   end
 end

+ 13 - 49
test/fixtures/checkout.json

@@ -1,18 +1,15 @@
 {
   "checkout": {
-    "buyer_accepts_marketing": false,
-    "cart_token": "68778783ad298f1c80c3bafcddeea02f",
-    "closed_at": null,
+    "applied_discount": "0.00",
+    "clone_url": "",
     "completed_at": null,
     "created_at": "2012-10-12T07:05:27-04:00",
+    "credit_card": null,
     "currency": "USD",
+    "presentment_currency": "USD",
     "email": "bob.norman@hostmail.com",
-    "gateway": null,
-    "id": 450789469,
-    "landing_site": null,
     "note": null,
-    "referring_site": null,
-    "shipping_lines": [
+    "shipping_line": [
       {
         "title": "Free Shipping",
         "price": "0.00",
@@ -32,18 +29,16 @@
         "title": "Medium Packet International Air Bogus"
       }
     ],
-    "source": null,
     "source_identifier": null,
     "source_name": "web",
     "source_url": null,
     "subtotal_price": "398.00",
+    "shipping_policy_url": null,
     "taxes_included": false,
     "token": "2a1ace52255252df566af0faaedfbfa7",
-    "total_discounts": "0.00",
     "total_line_items_price": "398.00",
     "total_price": "409.94",
     "total_tax": "11.94",
-    "total_weight": 400,
     "updated_at": "2012-10-12T07:05:27-04:00",
     "line_items": [
       {
@@ -108,13 +103,12 @@
         "value": "green"
       }
     ],
-    "discount_codes": [
+    "discount_code": [
       {
         "code": "TENOFF",
         "amount": "10.00"
       }
     ],
-    "abandoned_checkout_url": "https://checkout.local/orders/690933842/2a1ace52255252df566af0faaedfbfa7?recovered=1",
     "tax_lines": [
       {
         "price": "11.94",
@@ -156,41 +150,11 @@
       "country_code": "US",
       "province_code": "KY"
     },
-    "customer": {
-      "accepts_marketing": false,
-      "created_at": "2014-03-07T16:12:37-05:00",
-      "email": "bob.norman@hostmail.com",
-      "first_name": "Bob",
-      "id": 207119551,
-      "last_name": "Norman",
-      "last_order_id": null,
-      "multipass_identifier": null,
-      "note": null,
-      "orders_count": 0,
-      "state": "disabled",
-      "total_spent": "0.00",
-      "updated_at": "2014-03-07T16:12:37-05:00",
-      "verified_email": true,
-      "tags": "",
-      "last_order_name": null,
-      "default_address": {
-        "address1": "Chestnut Street 92",
-        "address2": "",
-        "city": "Louisville",
-        "company": null,
-        "country": "United States",
-        "first_name": null,
-        "id": 207119551,
-        "last_name": null,
-        "phone": "555-625-1199",
-        "province": "Kentucky",
-        "zip": "40202",
-        "name": null,
-        "province_code": "KY",
-        "country_code": "US",
-        "country_name": "United States",
-        "default": true
-      }
-    }
+    "customer_id": null,
+    "customer_locale": "en-CA",
+    "device_id": "12",
+    "order": null,
+    "order_id": null,
+    "order_status_url": ""
   }
 }

+ 7 - 0
test/fixtures/payment.json

@@ -0,0 +1,7 @@
+{
+  "payment" : {
+    "amount": 10.00,
+    "session_id": "east-44a400ef20b36e38f10b882cb7260796",
+    "unique_token": "my-client-generated-idempotency-token"
+  }
+}

+ 19 - 0
test/payment_test.rb

@@ -0,0 +1,19 @@
+# frozen_string_literal: true
+require 'test_helper'
+
+class PaymentTest < Test::Unit::TestCase
+  def setup
+    super
+
+    @checkout_id = JSON.parse(load_fixture('checkout'))['checkout']['token']
+    @expected_payment = JSON.parse(load_fixture('payment'))['payment']
+  end
+
+  test ":create creates a new payment" do
+    fake "checkouts/#{@checkout_id}/payments", method: :post, status: 201, body: load_fixture('payment')
+
+    new_payment = ShopifyAPI::Payment.create(checkout_id: @checkout_id)
+
+    assert_equal @expected_payment['unique_token'], new_payment.attributes['unique_token']
+  end
+end