Skip to content

Commit

Permalink
Adds current shop, recurring application charges
Browse files Browse the repository at this point in the history
Adds utility methods to find current shop and current recurring
application charges, functionality that had been available in v9 of API
when it used ActiveResource.

Fixes 923
  • Loading branch information
mkevinosullivan committed May 9, 2022
1 parent 5c7e1b6 commit 9e47058
Show file tree
Hide file tree
Showing 6 changed files with 281 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Changelog

## Unreleased
- [#933](https://github.com/Shopify/shopify_api/pull/933) Fix syntax of GraphQL query in `Webhooks.get_webhook_id` method by removing extra curly brace

- [#933](https://github.com/Shopify/shopify_api/pull/933) Fix syntax of GraphQL query in `Webhooks.get_webhook_id` method by removing extra curly brace
- [#941](https://github.com/Shopify/shopify_api/pull/941) Fix `to_hash` to return readonly attributes, unless being used for serialize the object for saving - fix issue [#930](https://github.com/Shopify/shopify_api/issues/930)
- [#951](https://github.com/Shopify/shopify_api/pull/941) Adds utility methods to find current shop, current recurring application charge - fix for issue [#923](https://github.com/Shopify/shopify_api/issues/923)

## Version 10.0.3

Expand Down
22 changes: 22 additions & 0 deletions lib/shopify_api/utils.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# typed: strict
# frozen_string_literal: true

module ShopifyAPI
module Utils
extend T::Sig

sig do
params(
fields: T.untyped
).returns(T.nilable(ShopifyAPI::Shop))
end
def self.current_shop(fields: nil)
ShopifyAPI::Shop.all(fields: fields).first
end

sig { returns(T.nilable(ShopifyAPI::RecurringApplicationCharge)) }
def self.current_recurring_application_charge
ShopifyAPI::RecurringApplicationCharge.all.find { |c| c.status == "active" }
end
end
end
106 changes: 106 additions & 0 deletions test/fixtures/recurring_application_charges.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
{
"recurring_application_charges": [
{
"activated_on": null,
"api_client_id": 755357713,
"billing_on": "2014-01-19T00:00:00+00:00",
"cancelled_on": null,
"created_at": "2014-01-20T13:41:37-05:00",
"id": 455696194,
"name": "Super Mega Plan2",
"price": "15.00",
"return_url": "http://yourapp.com",
"status": "active",
"test": null,
"trial_days": 0,
"trial_ends_on": null,
"updated_at": "2014-01-20T13:42:20-05:00",
"decorated_return_url": "http://yourapp.com?charge_id=455696194"
},
{
"activated_on": null,
"api_client_id": 755357713,
"billing_on": "2014-01-19T00:00:00+00:00",
"cancelled_on": null,
"created_at": "2014-01-20T13:41:37-05:00",
"id": 455696195,
"name": "Super Mega Plan2",
"price": "15.00",
"return_url": "http://yourapp.com",
"status": "pending",
"test": null,
"trial_days": 0,
"trial_ends_on": null,
"updated_at": "2014-01-20T13:42:20-05:00",
"decorated_return_url": "http://yourapp.com?charge_id=455696195"
},
{
"activated_on": null,
"api_client_id": 755357713,
"billing_on": "2014-01-19T00:00:00+00:00",
"cancelled_on": null,
"created_at": "2014-01-20T13:41:37-05:00",
"id": 455696196,
"name": "Super Mega Plan3",
"price": "15.00",
"return_url": "http://yourapp.com",
"status": "cancelled",
"test": null,
"trial_days": 0,
"trial_ends_on": null,
"updated_at": "2014-01-20T13:42:20-05:00",
"decorated_return_url": "http://yourapp.com?charge_id=455696196"
},
{
"activated_on": null,
"api_client_id": 755357713,
"billing_on": "2014-01-19T00:00:00+00:00",
"cancelled_on": null,
"created_at": "2014-01-20T13:41:37-05:00",
"id": 455696197,
"name": "Super Mega Plan4",
"price": "15.00",
"return_url": "http://yourapp.com",
"status": "accepted",
"test": null,
"trial_days": 0,
"trial_ends_on": null,
"updated_at": "2014-01-20T13:42:20-05:00",
"decorated_return_url": "http://yourapp.com?charge_id=455696197"
},
{
"activated_on": null,
"api_client_id": 755357713,
"billing_on": "2014-01-19T00:00:00+00:00",
"cancelled_on": null,
"created_at": "2014-01-20T13:41:37-05:00",
"id": 455696198,
"name": "Super Mega Plan5",
"price": "15.00",
"return_url": "http://yourapp.com",
"status": "declined",
"test": null,
"trial_days": 0,
"trial_ends_on": null,
"updated_at": "2014-01-20T13:42:20-05:00",
"decorated_return_url": "http://yourapp.com?charge_id=455696198"
},
{
"activated_on": null,
"api_client_id": 755357713,
"billing_on": "2014-01-19T00:00:00+00:00",
"cancelled_on": null,
"created_at": "2014-01-20T13:41:37-05:00",
"id": 455696199,
"name": "Super Mega Plan",
"price": "15.00",
"return_url": "http://yourapp.com",
"status": "accepted",
"test": null,
"trial_days": 0,
"trial_ends_on": null,
"updated_at": "2014-01-20T13:42:20-05:00",
"decorated_return_url": "http://yourapp.com?charge_id=455696199"
}
]
}
23 changes: 23 additions & 0 deletions test/fixtures/shop.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"shop": {
"id": 548380009,
"name": "John Smith Test Store",
"email": "j.smith@example.com",
"domain": "shop.apple.com",
"province": "California",
"country": "US",
"address1": "1 Infinite Loop",
"zip": "95014",
"city": "Cupertino",
"address2": "Suite 100",
"created_at": "2007-12-31T19:00:00-05:00",
"updated_at": "2022-04-05T12:52:43-04:00",
"country_code": "US",
"country_name": "United States",
"currency": "USD",
"customer_email": "customers@apple.com",
"timezone": "(GMT-05:00) Eastern Time (US & Canada)",
"iana_timezone": "America/New_York",
"shop_owner": "John Smith"
}
}
10 changes: 10 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ def modify_context(
user_agent_prefix: user_agent_prefix ? user_agent_prefix : ShopifyAPI::Context.user_agent_prefix
)
end

sig do
params(
name: String,
format: Symbol
).returns(T.nilable(String))
end
def load_fixture(name, format = :json)
File.read(File.dirname(__FILE__) + "/fixtures/#{name}.#{format}")
end
end
end
end
118 changes: 118 additions & 0 deletions test/utils_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# typed: false
# frozen_string_literal: true

require_relative "./test_helper"

module ShopifyAPITest
class UtilsTest < Test::Unit::TestCase
extend T::Sig

def setup
super

test_session = ShopifyAPI::Auth::Session.new(
id: "id",
shop: "test-shop.myshopify.io",
access_token: "this_is_a_test_token"
)
ShopifyAPI::Context.activate_session(test_session)
modify_context(api_version: "2022-04")
end

def teardown
super

ShopifyAPI::Context.deactivate_session
end

sig { void }
def test_current_shop
stub_request(:get, "https://test-shop.myshopify.io/admin/api/2022-04/shop.json")
.with(
headers: { "X-Shopify-Access-Token" => "this_is_a_test_token", "Accept" => "application/json" },
body: {}
)
.to_return(status: 200, body: load_fixture("shop"), headers: {})

current_shop = ShopifyAPI::Utils.current_shop()

assert_requested(:get, "https://test-shop.myshopify.io/admin/api/2022-04/shop.json")
assert_equal(548380009, current_shop.id)
assert_equal("John Smith Test Store", current_shop.name)
assert_equal("j.smith@example.com", current_shop.email)
assert_equal("shop.apple.com", current_shop.domain)
assert_equal("1 Infinite Loop", current_shop.address1)
assert_equal("Suite 100", current_shop.address2)
assert_equal("Cupertino", current_shop.city)
assert_equal("California", current_shop.province)
assert_equal("US", current_shop.country)
assert_equal("95014", current_shop.zip)
end

sig { void }
def test_current_shop_with_fields
fields = "address1,address2,city,province,country"
test_shop = JSON.parse(load_fixture("shop"))
shop_with_fields_only = {
"shop" => test_shop["shop"].select { |k, _v| fields.split(",").include?(k) },
}

stub_request(:get, "https://test-shop.myshopify.io/admin/api/2022-04/shop.json?fields=address1%2Caddress2%2Ccity%2Cprovince%2Ccountry")
.with(
headers: { "X-Shopify-Access-Token" => "this_is_a_test_token", "Accept" => "application/json" },
body: {}
)
.to_return(status: 200, body: JSON.generate(shop_with_fields_only), headers: {})

# current_shop = ShopifyAPI::Utils.current_shop(fields: fields)
current_shop = ShopifyAPI::Shop.all(fields: fields).first

assert_requested(:get, "https://test-shop.myshopify.io/admin/api/2022-04/shop.json?fields=address1%2Caddress2%2Ccity%2Cprovince%2Ccountry")
assert_equal("1 Infinite Loop", current_shop.address1)
assert_equal("Suite 100", current_shop.address2)
assert_equal("Cupertino", current_shop.city)
assert_equal("California", current_shop.province)
assert_equal("US", current_shop.country)
end

sig { void }
def test_current_recurring_application_charge
stub_request(:get, "https://test-shop.myshopify.io/admin/api/2022-04/recurring_application_charges.json")
.with(
headers: { "X-Shopify-Access-Token" => "this_is_a_test_token", "Accept" => "application/json" },
body: {}
)
.to_return(status: 200, body: load_fixture("recurring_application_charges"), headers: {})

current_recurring_application_charge = ShopifyAPI::Utils.current_recurring_application_charge()

assert_requested(:get, "https://test-shop.myshopify.io/admin/api/2022-04/recurring_application_charges.json")
assert_equal(455696194, current_recurring_application_charge.id)
end

sig { void }
def test_current_recurring_application_charge_no_active
recurring_application_charges = JSON.parse(load_fixture("recurring_application_charges"))

no_active_recurring_application_charges = {
"recurring_application_charges" =>
recurring_application_charges["recurring_application_charges"].select { |c| c["status"] != "active" },
}

stub_request(:get, "https://test-shop.myshopify.io/admin/api/2022-04/recurring_application_charges.json")
.with(
headers: { "X-Shopify-Access-Token" => "this_is_a_test_token", "Accept" => "application/json" },
body: {}
)
.to_return(status: 200, body: JSON.generate(no_active_recurring_application_charges), headers: {})

# current_recurring_application_charge = ShopifyAPI::Utils.current_recurring_application_charge()
current_recurring_application_charge = ShopifyAPI::RecurringApplicationCharge.all.find do |c|
c.status == "active"
end

assert_requested(:get, "https://test-shop.myshopify.io/admin/api/2022-04/recurring_application_charges.json")
assert_nil(current_recurring_application_charge)
end
end
end

0 comments on commit 9e47058

Please sign in to comment.