Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds utility methods to find current shop, current recurring application charge #951

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Note: For changes to the API, see https://shopify.dev/changelog?filter=api
- [#933](https://github.com/Shopify/shopify-api-ruby/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-ruby/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-ruby/issues/930)
- [#959](https://github.com/Shopify/shopify-api-ruby/pull/959) Update `LATEST_SUPPORTED_ADMIN_VERSION` to `2022-04` to align it with the current value
- [#951](https://github.com/Shopify/shopify-api-ruby/pull/951) 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
25 changes: 25 additions & 0 deletions lib/shopify_api/utils.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# typed: strict
# frozen_string_literal: true

module ShopifyAPI
module Utils
class << self
extend T::Sig

sig do
params(
session: T.nilable(Auth::Session),
fields: T.untyped,
).returns(T.nilable(Shop))
end
def current_shop(session: Context.active_session, fields: nil)
Shop.all(session: T.must(session), fields: fields).first
end

sig { returns(T.nilable(RecurringApplicationCharge)) }
def current_recurring_application_charge
RecurringApplicationCharge.all.find { |c| c.status == "active" }
end
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"
}
}
12 changes: 11 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def setup
ShopifyAPI::Context.setup(
api_key: "API_KEY",
api_secret_key: "API_SECRET_KEY",
api_version: "unstable",
api_version: ShopifyAPI::LATEST_SUPPORTED_ADMIN_VERSION,
host_name: "app-address.com",
scope: ["scope1", "scope2"],
is_private: false,
Expand Down Expand Up @@ -81,6 +81,16 @@ def modify_context(
old_api_secret_key: old_api_secret_key ? old_api_secret_key : ShopifyAPI::Context.old_api_secret_key,
)
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
143 changes: 143 additions & 0 deletions test/utils_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
# typed: true
# 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)
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/#{ShopifyAPI::Context.api_version}/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/#{ShopifyAPI::Context.api_version}/shop.json")
assert_equal(548380009, T.must(current_shop).id)
assert_equal("John Smith Test Store", T.must(current_shop).name)
assert_equal("j.smith@example.com", T.must(current_shop).email)
assert_equal("shop.apple.com", T.must(current_shop).domain)
assert_equal("1 Infinite Loop", T.must(current_shop).address1)
assert_equal("Suite 100", T.must(current_shop).address2)
assert_equal("Cupertino", T.must(current_shop).city)
assert_equal("California", T.must(current_shop).province)
assert_equal("US", T.must(current_shop).country)
assert_equal("95014", T.must(current_shop).zip)
end

sig { void }
def test_current_shop_with_fields
fields = "id,address1,address2,city,province,country"
test_shop = JSON.parse(T.must(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/#{ShopifyAPI::Context.api_version}/shop.json?fields=id%2Caddress1%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)

assert_requested(:get, "https://test-shop.myshopify.io/admin/api/#{ShopifyAPI::Context.api_version}/shop.json?fields=id%2Caddress1%2Caddress2%2Ccity%2Cprovince%2Ccountry")
assert_equal("1 Infinite Loop", T.must(current_shop).address1)
assert_equal("Suite 100", T.must(current_shop).address2)
assert_equal("Cupertino", T.must(current_shop).city)
assert_equal("California", T.must(current_shop).province)
assert_equal("US", T.must(current_shop).country)
end

sig { void }
def test_current_shop_passed_session
different_session = ShopifyAPI::Auth::Session.new(
id: "id",
shop: "not-a-test-shop.myshopify.io",
access_token: "this_is_a_different_token",
)

stub_request(:get, "https://not-a-test-shop.myshopify.io/admin/api/#{ShopifyAPI::Context.api_version}/shop.json")
.with(
headers: { "X-Shopify-Access-Token" => "this_is_a_different_token", "Accept" => "application/json" },
body: {},
)
.to_return(status: 200, body: load_fixture("shop"), headers: {})

current_shop = ShopifyAPI::Utils.current_shop(session: different_session)

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

sig { void }
def test_current_recurring_application_charge
stub_request(:get, "https://test-shop.myshopify.io/admin/api/#{ShopifyAPI::Context.api_version}/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/#{ShopifyAPI::Context.api_version}/recurring_application_charges.json")
assert_equal(455696194, T.must(current_recurring_application_charge).id)
end

sig { void }
def test_current_recurring_application_charge_no_active
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this test, we should probably have a similar one for the shop method!

recurring_application_charges = JSON.parse(T.must(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/#{ShopifyAPI::Context.api_version}/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()

assert_requested(:get, "https://test-shop.myshopify.io/admin/api/#{ShopifyAPI::Context.api_version}/recurring_application_charges.json")
assert_nil(current_recurring_application_charge)
end
end
end