From 9e47058a1236ba500882207b6345e6677b1a9761 Mon Sep 17 00:00:00 2001 From: Kevin O'Sullivan Date: Fri, 6 May 2022 20:45:31 -0400 Subject: [PATCH] Adds current shop, recurring application charges 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 --- CHANGELOG.md | 3 +- lib/shopify_api/utils.rb | 22 ++++ .../recurring_application_charges.json | 106 ++++++++++++++++ test/fixtures/shop.json | 23 ++++ test/test_helper.rb | 10 ++ test/utils_test.rb | 118 ++++++++++++++++++ 6 files changed, 281 insertions(+), 1 deletion(-) create mode 100644 lib/shopify_api/utils.rb create mode 100644 test/fixtures/recurring_application_charges.json create mode 100644 test/fixtures/shop.json create mode 100644 test/utils_test.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c294f17e..ba4d6911e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/shopify_api/utils.rb b/lib/shopify_api/utils.rb new file mode 100644 index 000000000..e72e15a0c --- /dev/null +++ b/lib/shopify_api/utils.rb @@ -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 diff --git a/test/fixtures/recurring_application_charges.json b/test/fixtures/recurring_application_charges.json new file mode 100644 index 000000000..8f671d737 --- /dev/null +++ b/test/fixtures/recurring_application_charges.json @@ -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" + } + ] +} diff --git a/test/fixtures/shop.json b/test/fixtures/shop.json new file mode 100644 index 000000000..245b8187d --- /dev/null +++ b/test/fixtures/shop.json @@ -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" + } +} \ No newline at end of file diff --git a/test/test_helper.rb b/test/test_helper.rb index daa24fcc7..217ee024c 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -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 diff --git a/test/utils_test.rb b/test/utils_test.rb new file mode 100644 index 000000000..577737215 --- /dev/null +++ b/test/utils_test.rb @@ -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