Skip to content

Loading…

Adding tests for Webhooks, updated Order tests #120

Merged
merged 3 commits into from

3 participants

@benjlcox
Shopify member

Adding tests for the Webhooks endpoint, updated Order tests that haven't been touched in 4 years

@pickle27

@maartenvg maartenvg commented on an outdated diff
test/order_test.rb
((10 lines not shown))
- should "be loaded correctly from order xml" do
- order_xml = <<-XML
- <?xml version="1.0" encoding="UTF-8"?>
- <order>
- <note-attributes type="array">
- <note-attribute>
- <name>size</name>
- <value>large</value>
- </note-attribute>
- </note-attributes>
- </order>
- XML
+ test "create should create order" do
+ fake 'orders', :method => :post, :status => 201, :body => load_fixture('order')
+ order = ShopifyAPI::Order.create(line_items: [{quantity:1,variant_id:39072856}],financial_status:"authorized")
@maartenvg Shopify member

Can you put spaces between the hash elements (after the comma)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@maartenvg maartenvg commented on an outdated diff
test/webhook_test.rb
@@ -0,0 +1,21 @@
+require 'test_helper'
+
+class WebhookTest < Test::Unit::TestCase
+ test 'create should create a webhook' do
+ fake 'webhooks', :method => :post, :status => 201, :body => load_fixture('webhook')
+ webhook = ShopifyAPI::Webhook.create(address: "http://www.yoloship.it/webhook",topic: "orders/create",format: "json")
@maartenvg Shopify member

here too please.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@maartenvg
Shopify member

Looks good to me, just a little nitpick

@benjlcox benjlcox merged commit 298f9cd into master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Showing with 669 additions and 40 deletions.
  1. +297 −0 test/fixtures/order.json
  2. +299 −0 test/fixtures/orders.json
  3. +10 −0 test/fixtures/webhook.json
  4. +18 −0 test/fixtures/webhooks.json
  5. +24 −40 test/order_test.rb
  6. +21 −0 test/webhook_test.rb
View
297 test/fixtures/order.json
@@ -0,0 +1,297 @@
+{
+ "order": {
+ "buyer_accepts_marketing": false,
+ "cancel_reason": null,
+ "cancelled_at": null,
+ "cart_token": "68778783ad298f1c80c3bafcddeea02f",
+ "checkout_token": null,
+ "closed_at": null,
+ "confirmed": false,
+ "created_at": "2008-01-10T11:00:00-05:00",
+ "currency": "USD",
+ "email": "bob.norman@hostmail.com",
+ "financial_status": "authorized",
+ "fulfillment_status": null,
+ "gateway": "authorize_net",
+ "id": 450789469,
+ "landing_site": "http://www.example.com?source=abc",
+ "location_id": null,
+ "name": "#1001",
+ "note": "Test note",
+ "number": 1,
+ "reference": "fhwdgads",
+ "referring_site": "http://www.otherexample.com",
+ "source": null,
+ "source_identifier": "fhwdgads",
+ "source_name": "web",
+ "source_url": null,
+ "subtotal_price": "398.00",
+ "taxes_included": false,
+ "test": false,
+ "token": "b1946ac92492d2347c6235b4d2611184",
+ "total_discounts": "0.00",
+ "total_line_items_price": "398.00",
+ "total_price": "409.94",
+ "total_price_usd": "409.94",
+ "total_tax": "11.94",
+ "total_weight": 0,
+ "updated_at": "2008-01-10T11:00:00-05:00",
+ "user_id": null,
+ "browser_ip": null,
+ "landing_site_ref": "abc",
+ "order_number": 1001,
+ "discount_codes": [
+ {
+ "code": "TENOFF",
+ "amount": "10.00"
+ }
+ ],
+ "note_attributes": [
+ {
+ "name": "custom engraving",
+ "value": "Happy Birthday"
+ },
+ {
+ "name": "colour",
+ "value": "green"
+ }
+ ],
+ "processing_method": "direct",
+ "checkout_id": 450789469,
+ "tax_lines": [
+ {
+ "price": "11.94",
+ "rate": 0.06,
+ "title": "State Tax"
+ }
+ ],
+ "tags": "",
+ "line_items": [
+ {
+ "fulfillment_service": "manual",
+ "fulfillment_status": null,
+ "grams": 200,
+ "id": 466157049,
+ "price": "199.00",
+ "product_id": 632910392,
+ "quantity": 1,
+ "requires_shipping": true,
+ "sku": "IPOD2008GREEN",
+ "taxable": true,
+ "title": "IPod Nano - 8gb",
+ "variant_id": 39072856,
+ "variant_title": "green",
+ "vendor": null,
+ "name": "IPod Nano - 8gb - green",
+ "variant_inventory_management": "shopify",
+ "properties": [
+ {
+ "name": "Custom Engraving",
+ "value": "Happy Birthday"
+ }
+ ],
+ "product_exists": true,
+ "tax_lines": [
+
+ ]
+ },
+ {
+ "fulfillment_service": "manual",
+ "fulfillment_status": null,
+ "grams": 200,
+ "id": 518995019,
+ "price": "199.00",
+ "product_id": 632910392,
+ "quantity": 1,
+ "requires_shipping": true,
+ "sku": "IPOD2008RED",
+ "taxable": true,
+ "title": "IPod Nano - 8gb",
+ "variant_id": 49148385,
+ "variant_title": "red",
+ "vendor": null,
+ "name": "IPod Nano - 8gb - red",
+ "variant_inventory_management": "shopify",
+ "properties": [
+
+ ],
+ "product_exists": true,
+ "tax_lines": [
+
+ ]
+ },
+ {
+ "fulfillment_service": "manual",
+ "fulfillment_status": null,
+ "grams": 200,
+ "id": 703073504,
+ "price": "199.00",
+ "product_id": 632910392,
+ "quantity": 1,
+ "requires_shipping": true,
+ "sku": "IPOD2008BLACK",
+ "taxable": true,
+ "title": "IPod Nano - 8gb",
+ "variant_id": 457924702,
+ "variant_title": "black",
+ "vendor": null,
+ "name": "IPod Nano - 8gb - black",
+ "variant_inventory_management": "shopify",
+ "properties": [
+
+ ],
+ "product_exists": true,
+ "tax_lines": [
+
+ ]
+ }
+ ],
+ "shipping_lines": [
+ {
+ "code": "Free Shipping",
+ "price": "0.00",
+ "source": "shopify",
+ "title": "Free Shipping",
+ "tax_lines": [
+
+ ]
+ }
+ ],
+ "payment_details": {
+ "avs_result_code": null,
+ "credit_card_bin": null,
+ "cvv_result_code": null,
+ "credit_card_number": "XXXX-XXXX-XXXX-4242",
+ "credit_card_company": "Visa"
+ },
+ "billing_address": {
+ "address1": "Chestnut Street 92",
+ "address2": "",
+ "city": "Louisville",
+ "company": null,
+ "country": "United States",
+ "first_name": "Bob",
+ "last_name": "Norman",
+ "latitude": "45.41634",
+ "longitude": "-75.6868",
+ "phone": "555-625-1199",
+ "province": "Kentucky",
+ "zip": "40202",
+ "name": "Bob Norman",
+ "country_code": "US",
+ "province_code": "KY"
+ },
+ "shipping_address": {
+ "address1": "Chestnut Street 92",
+ "address2": "",
+ "city": "Louisville",
+ "company": null,
+ "country": "United States",
+ "first_name": "Bob",
+ "last_name": "Norman",
+ "latitude": "45.41634",
+ "longitude": "-75.6868",
+ "phone": "555-625-1199",
+ "province": "Kentucky",
+ "zip": "40202",
+ "name": "Bob Norman",
+ "country_code": "US",
+ "province_code": "KY"
+ },
+ "fulfillments": [
+ {
+ "created_at": "2014-03-07T16:14:08-05:00",
+ "id": 255858046,
+ "order_id": 450789469,
+ "service": "manual",
+ "status": "failure",
+ "tracking_company": null,
+ "updated_at": "2014-03-07T16:14:08-05:00",
+ "tracking_number": "1Z2345",
+ "tracking_numbers": [
+ "1Z2345"
+ ],
+ "tracking_url": "http://wwwapps.ups.com/etracking/tracking.cgi?InquiryNumber1=1Z2345&TypeOfInquiryNumber=T&AcceptUPSLicenseAgreement=yes&submit=Track",
+ "tracking_urls": [
+ "http://wwwapps.ups.com/etracking/tracking.cgi?InquiryNumber1=1Z2345&TypeOfInquiryNumber=T&AcceptUPSLicenseAgreement=yes&submit=Track"
+ ],
+ "receipt": {
+ "testcase": true,
+ "authorization": "123456"
+ },
+ "line_items": [
+ {
+ "fulfillment_service": "manual",
+ "fulfillment_status": null,
+ "grams": 200,
+ "id": 466157049,
+ "price": "199.00",
+ "product_id": 632910392,
+ "quantity": 1,
+ "requires_shipping": true,
+ "sku": "IPOD2008GREEN",
+ "taxable": true,
+ "title": "IPod Nano - 8gb",
+ "variant_id": 39072856,
+ "variant_title": "green",
+ "vendor": null,
+ "name": "IPod Nano - 8gb - green",
+ "variant_inventory_management": "shopify",
+ "properties": [
+ {
+ "name": "Custom Engraving",
+ "value": "Happy Birthday"
+ }
+ ],
+ "product_exists": true,
+ "tax_lines": [
+
+ ]
+ }
+ ]
+ }
+ ],
+ "client_details": {
+ "accept_language": null,
+ "browser_ip": "0.0.0.0",
+ "session_hash": null,
+ "user_agent": null
+ },
+ "customer": {
+ "accepts_marketing": false,
+ "created_at": "2014-03-07T16:14:08-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:14:08-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
+ }
+ }
+ }
+}
View
299 test/fixtures/orders.json
@@ -0,0 +1,299 @@
+{
+ "orders": [
+ {
+ "buyer_accepts_marketing": false,
+ "cancel_reason": null,
+ "cancelled_at": null,
+ "cart_token": "68778783ad298f1c80c3bafcddeea02f",
+ "checkout_token": null,
+ "closed_at": null,
+ "confirmed": false,
+ "created_at": "2008-01-10T11:00:00-05:00",
+ "currency": "USD",
+ "email": "bob.norman@hostmail.com",
+ "financial_status": "authorized",
+ "fulfillment_status": null,
+ "gateway": "authorize_net",
+ "id": 450789469,
+ "landing_site": "http://www.example.com?source=abc",
+ "location_id": null,
+ "name": "#1001",
+ "note": null,
+ "number": 1,
+ "reference": "fhwdgads",
+ "referring_site": "http://www.otherexample.com",
+ "source": null,
+ "source_identifier": "fhwdgads",
+ "source_name": "web",
+ "source_url": null,
+ "subtotal_price": "398.00",
+ "taxes_included": false,
+ "test": false,
+ "token": "b1946ac92492d2347c6235b4d2611184",
+ "total_discounts": "0.00",
+ "total_line_items_price": "398.00",
+ "total_price": "409.94",
+ "total_price_usd": "409.94",
+ "total_tax": "11.94",
+ "total_weight": 0,
+ "updated_at": "2008-01-10T11:00:00-05:00",
+ "user_id": null,
+ "browser_ip": null,
+ "landing_site_ref": "abc",
+ "order_number": 1001,
+ "discount_codes": [
+ {
+ "code": "TENOFF",
+ "amount": "10.00"
+ }
+ ],
+ "note_attributes": [
+ {
+ "name": "custom engraving",
+ "value": "Happy Birthday"
+ },
+ {
+ "name": "colour",
+ "value": "green"
+ }
+ ],
+ "processing_method": "direct",
+ "checkout_id": 450789469,
+ "tax_lines": [
+ {
+ "price": "11.94",
+ "rate": 0.06,
+ "title": "State Tax"
+ }
+ ],
+ "tags": "",
+ "line_items": [
+ {
+ "fulfillment_service": "manual",
+ "fulfillment_status": null,
+ "grams": 200,
+ "id": 466157049,
+ "price": "199.00",
+ "product_id": 632910392,
+ "quantity": 1,
+ "requires_shipping": true,
+ "sku": "IPOD2008GREEN",
+ "taxable": true,
+ "title": "IPod Nano - 8gb",
+ "variant_id": 39072856,
+ "variant_title": "green",
+ "vendor": null,
+ "name": "IPod Nano - 8gb - green",
+ "variant_inventory_management": "shopify",
+ "properties": [
+ {
+ "name": "Custom Engraving",
+ "value": "Happy Birthday"
+ }
+ ],
+ "product_exists": true,
+ "tax_lines": [
+
+ ]
+ },
+ {
+ "fulfillment_service": "manual",
+ "fulfillment_status": null,
+ "grams": 200,
+ "id": 518995019,
+ "price": "199.00",
+ "product_id": 632910392,
+ "quantity": 1,
+ "requires_shipping": true,
+ "sku": "IPOD2008RED",
+ "taxable": true,
+ "title": "IPod Nano - 8gb",
+ "variant_id": 49148385,
+ "variant_title": "red",
+ "vendor": null,
+ "name": "IPod Nano - 8gb - red",
+ "variant_inventory_management": "shopify",
+ "properties": [
+
+ ],
+ "product_exists": true,
+ "tax_lines": [
+
+ ]
+ },
+ {
+ "fulfillment_service": "manual",
+ "fulfillment_status": null,
+ "grams": 200,
+ "id": 703073504,
+ "price": "199.00",
+ "product_id": 632910392,
+ "quantity": 1,
+ "requires_shipping": true,
+ "sku": "IPOD2008BLACK",
+ "taxable": true,
+ "title": "IPod Nano - 8gb",
+ "variant_id": 457924702,
+ "variant_title": "black",
+ "vendor": null,
+ "name": "IPod Nano - 8gb - black",
+ "variant_inventory_management": "shopify",
+ "properties": [
+
+ ],
+ "product_exists": true,
+ "tax_lines": [
+
+ ]
+ }
+ ],
+ "shipping_lines": [
+ {
+ "code": "Free Shipping",
+ "price": "0.00",
+ "source": "shopify",
+ "title": "Free Shipping",
+ "tax_lines": [
+
+ ]
+ }
+ ],
+ "payment_details": {
+ "avs_result_code": null,
+ "credit_card_bin": null,
+ "cvv_result_code": null,
+ "credit_card_number": "XXXX-XXXX-XXXX-4242",
+ "credit_card_company": "Visa"
+ },
+ "billing_address": {
+ "address1": "Chestnut Street 92",
+ "address2": "",
+ "city": "Louisville",
+ "company": null,
+ "country": "United States",
+ "first_name": "Bob",
+ "last_name": "Norman",
+ "latitude": "45.41634",
+ "longitude": "-75.6868",
+ "phone": "555-625-1199",
+ "province": "Kentucky",
+ "zip": "40202",
+ "name": "Bob Norman",
+ "country_code": "US",
+ "province_code": "KY"
+ },
+ "shipping_address": {
+ "address1": "Chestnut Street 92",
+ "address2": "",
+ "city": "Louisville",
+ "company": null,
+ "country": "United States",
+ "first_name": "Bob",
+ "last_name": "Norman",
+ "latitude": "45.41634",
+ "longitude": "-75.6868",
+ "phone": "555-625-1199",
+ "province": "Kentucky",
+ "zip": "40202",
+ "name": "Bob Norman",
+ "country_code": "US",
+ "province_code": "KY"
+ },
+ "fulfillments": [
+ {
+ "created_at": "2014-03-07T16:14:08-05:00",
+ "id": 255858046,
+ "order_id": 450789469,
+ "service": "manual",
+ "status": "failure",
+ "tracking_company": null,
+ "updated_at": "2014-03-07T16:14:08-05:00",
+ "tracking_number": "1Z2345",
+ "tracking_numbers": [
+ "1Z2345"
+ ],
+ "tracking_url": "http://wwwapps.ups.com/etracking/tracking.cgi?InquiryNumber1=1Z2345&TypeOfInquiryNumber=T&AcceptUPSLicenseAgreement=yes&submit=Track",
+ "tracking_urls": [
+ "http://wwwapps.ups.com/etracking/tracking.cgi?InquiryNumber1=1Z2345&TypeOfInquiryNumber=T&AcceptUPSLicenseAgreement=yes&submit=Track"
+ ],
+ "receipt": {
+ "testcase": true,
+ "authorization": "123456"
+ },
+ "line_items": [
+ {
+ "fulfillment_service": "manual",
+ "fulfillment_status": null,
+ "grams": 200,
+ "id": 466157049,
+ "price": "199.00",
+ "product_id": 632910392,
+ "quantity": 1,
+ "requires_shipping": true,
+ "sku": "IPOD2008GREEN",
+ "taxable": true,
+ "title": "IPod Nano - 8gb",
+ "variant_id": 39072856,
+ "variant_title": "green",
+ "vendor": null,
+ "name": "IPod Nano - 8gb - green",
+ "variant_inventory_management": "shopify",
+ "properties": [
+ {
+ "name": "Custom Engraving",
+ "value": "Happy Birthday"
+ }
+ ],
+ "product_exists": true,
+ "tax_lines": [
+
+ ]
+ }
+ ]
+ }
+ ],
+ "client_details": {
+ "accept_language": null,
+ "browser_ip": "0.0.0.0",
+ "session_hash": null,
+ "user_agent": null
+ },
+ "customer": {
+ "accepts_marketing": false,
+ "created_at": "2014-03-07T16:14:08-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:14:08-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
+ }
+ }
+ }
+ ]
+}
View
10 test/fixtures/webhook.json
@@ -0,0 +1,10 @@
+{
+ "webhooks":{
+ "address":"http://www.yoloship.it/webhook",
+ "created_at":"2014-02-11T12:13:55-05:00",
+ "format":"json",
+ "id":123456,
+ "topic":"orders/create",
+ "updated_at":"2014-02-11T12:13:55-05:00"
+ }
+}
View
18 test/fixtures/webhooks.json
@@ -0,0 +1,18 @@
+{
+ "webhooks":[{
+ "address":"http://www.yoloship.it/webhook",
+ "created_at":"2014-02-11T12:13:55-05:00",
+ "format":"json",
+ "id":123456,
+ "topic":"orders/create",
+ "updated_at":"2014-02-11T12:13:55-05:00"
+ },
+ {
+ "address":"http://www.yoloship.it/webhook",
+ "created_at":"2014-02-11T12:13:55-05:00",
+ "format":"json",
+ "id":123456,
+ "topic":"orders/create",
+ "updated_at":"2014-02-11T12:13:55-05:00"
+ }]
+}
View
64 test/order_test.rb
@@ -1,48 +1,32 @@
require 'test_helper'
class OrderTest < Test::Unit::TestCase
- def setup
- ActiveResource::Base.site = "http://localhost"
- end
-
- context "Order" do
- context "#note_attributes" do
- should "be loaded correctly from order xml" do
- order_xml = <<-XML
- <?xml version="1.0" encoding="UTF-8"?>
- <order>
- <note-attributes type="array">
- <note-attribute>
- <name>size</name>
- <value>large</value>
- </note-attribute>
- </note-attributes>
- </order>
- XML
+ test "create should create order" do
+ fake 'orders', :method => :post, :status => 201, :body => load_fixture('order')
+ order = ShopifyAPI::Order.create(line_items: [{quantity:1, variant_id:39072856}], financial_status:"authorized")
+ assert_equal 39072856, order.line_items.first.variant_id
+ end
- order = ShopifyAPI::Order.new(Hash.from_xml(order_xml)["order"])
+ test "get should get an order" do
+ fake 'orders/450789469', :method => :get, :status => 200, :body => load_fixture('order')
+ order = ShopifyAPI::Order.find(450789469)
+ assert_equal 450789469, order.id
+ end
- assert_equal 1, order.note_attributes.size
+ test "get all should get all orders" do
+ fake 'orders', :method => :get, :status => 200, :body => load_fixture('orders')
+ order = ShopifyAPI::Order.all
+ assert_equal 450789469, order.first.id
+ end
- note_attribute = order.note_attributes.first
- assert_equal "size", note_attribute.name
- assert_equal "large", note_attribute.value
- end
-
- should "be able to add note attributes to an order" do
- order = ShopifyAPI::Order.new
- order.note_attributes = []
- order.note_attributes << ShopifyAPI::NoteAttribute.new(:name => "color", :value => "blue")
-
- order_xml = Hash.from_xml(order.to_xml)
- assert note_attributes = order_xml["order"]["note_attributes"]
- assert_instance_of Array, note_attributes
-
- attribute = note_attributes.first
- assert_equal "color", attribute["name"]
- assert_equal "blue", attribute["value"]
- end
- end
+ test "add note should add a note to order" do
+ fake 'orders/450789469', :method => :get, :status => 200, :body => load_fixture('order')
+ order = ShopifyAPI::Order.find(450789469)
+ order.note = "Test note"
+ fake 'orders/450789469', :method => :put, :status => 200, :body => load_fixture('order')
+ order.save
+ assert_equal "Test note", order.note
end
-end
+end
+
View
21 test/webhook_test.rb
@@ -0,0 +1,21 @@
+require 'test_helper'
+
+class WebhookTest < Test::Unit::TestCase
+ test 'create should create a webhook' do
+ fake 'webhooks', :method => :post, :status => 201, :body => load_fixture('webhook')
+ webhook = ShopifyAPI::Webhook.create(address: "http://www.yoloship.it/webhook", topic: "orders/create", format: "json")
+ assert_equal "http://www.yoloship.it/webhook", webhook.address
+ end
+
+ test 'get should retrieve a webhook' do
+ fake 'webhooks/123456', :method => :get, :status => 200, :body => load_fixture('webhook')
+ webhook = ShopifyAPI::Webhook.find(123456)
+ assert_equal "http://www.yoloship.it/webhook", webhook.address
+ end
+
+ test 'find all should return all webhooks' do
+ fake 'webhooks', :method => :get, :status => 200, :body => load_fixture('webhooks')
+ webhook = ShopifyAPI::Webhook.all
+ assert_equal 123456, webhook.first.id
+ end
+end
Something went wrong with that request. Please try again.