Ver Fonte

Merge pull request #199 from discolabs/fix_198_helper_methods

Add missing helper method on ApplicationCharge
Adam Doeler há 9 anos atrás
pai
commit
06aa940127

+ 1 - 0
.gitignore

@@ -6,3 +6,4 @@ doc
 pkg
 .ruby-version
 *.lock
+.idea

+ 6 - 0
lib/shopify_api/resources/application_charge.rb

@@ -2,6 +2,12 @@ module ShopifyAPI
   class ApplicationCharge < Base
     undef_method :test
 
+    class << self
+      [:pending, :cancelled, :accepted, :declined].each do |status|
+        define_method(status) { (all || []).select { |c| c.status == status.to_s } }
+      end
+    end
+
     def activate
       load_attributes_from_response(post(:activate))
     end

+ 79 - 0
test/application_charge_test.rb

@@ -0,0 +1,79 @@
+require 'test_helper'
+
+class ApplicationChargeTest < Test::Unit::TestCase
+
+  def test_application_charge_create
+    fake "application_charges", :method => :post, :status => 201, :body => load_fixture('application_charge')
+
+    charge = ShopifyAPI::ApplicationCharge.create(
+        name: "iPod Cleaning",
+        price: 5.00,
+        return_url: "http://google.com"
+    )
+
+    assert_equal 'https://this-is-my-test-shop.myshopify.com/admin/charges/803742/confirm_application_charge?signature=BAhpA55DDA%3D%3D--55b44e274e438c619be4631c804abcbcb6ee915a', charge.confirmation_url
+  end
+
+  def test_get_application_charge
+    fake "application_charges/803742", :method => :get, :status => 201, :body => load_fixture('application_charge')
+
+    charge = ShopifyAPI::ApplicationCharge.find(803742)
+
+    assert_equal "iPod Cleaning", charge.name
+  end
+
+  def test_list_application_charges
+    fake "application_charges", :method => :get, :status => 201, :body => load_fixture('application_charges')
+
+    charges = ShopifyAPI::ApplicationCharge.find(:all)
+
+    assert_equal 4, charges.size
+    assert_equal "iPhone Case", charges.last.name
+  end
+
+  def test_list_pending_application_charges
+    fake "application_charges", :method => :get, :status => 201, :body => load_fixture('application_charges')
+
+    pending_charges = ShopifyAPI::ApplicationCharge.pending
+
+    assert_equal 1, pending_charges.size
+    assert_equal "Screen Replacement", pending_charges.first.name
+  end
+
+  def test_list_cancelled_application_charges
+    fake "application_charges", :method => :get, :status => 201, :body => load_fixture('application_charges')
+
+    cancelled_charges = ShopifyAPI::ApplicationCharge.cancelled
+
+    assert_equal 1, cancelled_charges.size
+    assert_equal "iPod Cleaning", cancelled_charges.first.name
+  end
+
+  def test_list_accepted_application_charges
+    fake "application_charges", :method => :get, :status => 201, :body => load_fixture('application_charges')
+
+    accepted_charges = ShopifyAPI::ApplicationCharge.accepted
+
+    assert_equal 1, accepted_charges.size
+    assert_equal "iPhone Case", accepted_charges.first.name
+  end
+
+  def test_list_declined_application_charges
+    fake "application_charges", :method => :get, :status => 201, :body => load_fixture('application_charges')
+
+    declined_charges = ShopifyAPI::ApplicationCharge.declined
+
+    assert_equal 1, declined_charges.size
+    assert_equal "Magic Mouse", declined_charges.first.name
+  end
+
+  def test_activate_application_charge
+    fake "application_charges", :method => :get, :status => 201, :body => load_fixture('application_charges')
+    fake "application_charges/803740/activate", :method => :post, :status => 200, :body => "{}"
+
+    charge = ShopifyAPI::ApplicationCharge.accepted
+
+    assert charge.last.activate
+  end
+
+end

+ 16 - 0
test/fixtures/application_charge.json

@@ -0,0 +1,16 @@
+{
+  "application_charge": {
+    "api_client_id": 861378,
+    "created_at": "2015-07-10T15:03:51+10:00",
+    "id": 803742,
+    "name": "iPod Cleaning",
+    "price": "5.00",
+    "return_url": "http:\/\/google.com",
+    "status": "pending",
+    "test": null,
+    "updated_at": "2015-07-10T15:03:51+10:00",
+    "charge_type": null,
+    "decorated_return_url": "http:\/\/google.com?charge_id=803742",
+    "confirmation_url": "https://this-is-my-test-shop.myshopify.com\/admin\/charges\/803742\/confirm_application_charge?signature=BAhpA55DDA%3D%3D--55b44e274e438c619be4631c804abcbcb6ee915a"
+  }
+}

+ 57 - 0
test/fixtures/application_charges.json

@@ -0,0 +1,57 @@
+{
+  "application_charges": [
+    {
+      "api_client_id": 861378,
+      "created_at": "2015-07-10T15:16:14+10:00",
+      "id": 803750,
+      "name": "Magic Mouse",
+      "price": "99.00",
+      "return_url": "http:\/\/google.com",
+      "status": "declined",
+      "test": null,
+      "updated_at": "2015-07-10T15:18:09+10:00",
+      "charge_type": null,
+      "decorated_return_url": "http:\/\/google.com?charge_id=803750"
+    },
+    {
+      "api_client_id": 861378,
+      "created_at": "2015-07-10T15:10:10+10:00",
+      "id": 803748,
+      "name": "Screen Replacement",
+      "price": "499.00",
+      "return_url": "http:\/\/google.com",
+      "status": "pending",
+      "test": null,
+      "updated_at": "2015-07-10T15:10:10+10:00",
+      "charge_type": null,
+      "decorated_return_url": "http:\/\/google.com?charge_id=803748",
+      "confirmation_url": "https:\/\/this-is-my-test-shop.myshopify.com\/admin\/charges\/803748\/confirm_application_charge?signature=BAhpA6RDDA%3D%3D--ef4769e2c1149de0ed19dee9ecddaa155baa7baf"
+    },
+    {
+      "api_client_id": 861378,
+      "created_at": "2015-07-10T15:03:51+10:00",
+      "id": 803742,
+      "name": "iPod Cleaning",
+      "price": "5.00",
+      "return_url": "http:\/\/google.com",
+      "status": "cancelled",
+      "test": null,
+      "updated_at": "2015-07-10T15:03:51+10:00",
+      "charge_type": null,
+      "decorated_return_url": "http:\/\/google.com?charge_id=803742"
+    },
+    {
+      "api_client_id": 861378,
+      "created_at": "2015-07-10T15:03:51+10:00",
+      "id": 803740,
+      "name": "iPhone Case",
+      "price": "23.00",
+      "return_url": "http:\/\/google.com",
+      "status": "accepted",
+      "test": null,
+      "updated_at": "2015-07-10T15:03:51+10:00",
+      "charge_type": null,
+      "decorated_return_url": "http:\/\/google.com?charge_id=803740"
+    }
+  ]
+}