From 1694bc25423bbe66687348d24d06209cfdfdcb3f Mon Sep 17 00:00:00 2001 From: Nelson Wittwer Date: Thu, 20 Oct 2022 11:26:12 -0400 Subject: [PATCH] interface for Context.host_name for webhooks --- lib/shopify_api/context.rb | 9 +++++++-- test/auth/oauth_test.rb | 4 ++-- test/auth_test.rb | 2 +- test/context_test.rb | 1 + test/test_helper.rb | 8 ++++---- 5 files changed, 15 insertions(+), 9 deletions(-) diff --git a/lib/shopify_api/context.rb b/lib/shopify_api/context.rb index 337ab7b0d..3c9b3962f 100644 --- a/lib/shopify_api/context.rb +++ b/lib/shopify_api/context.rb @@ -65,7 +65,7 @@ def setup( @api_key = api_key @api_secret_key = api_secret_key @api_version = api_version - @host_name = host_name + @host_name = T.must(host_name) @host = T.let(host, T.nilable(String)) @is_private = is_private @scope = Auth::AuthScopes.new(scope) @@ -108,7 +108,7 @@ def load_rest_resources(api_version:) end sig { returns(String) } - attr_reader :api_key, :api_secret_key, :api_version, :host_name + attr_reader :api_key, :api_secret_key, :api_version sig { returns(Auth::AuthScopes) } attr_reader :scope @@ -158,6 +158,11 @@ def deactivate_session def host_scheme T.must(URI.parse(T.must(host)).scheme) end + + sig { returns(String) } + def host_name + T.must(URI(T.must(host)).host) + end end end end diff --git a/test/auth/oauth_test.rb b/test/auth/oauth_test.rb index b24b42b89..7d89a820c 100644 --- a/test/auth/oauth_test.rb +++ b/test/auth/oauth_test.rb @@ -107,7 +107,7 @@ def test_custom_scope_with_a_comma_separated_string end def test_begin_auth_context_not_setup - modify_context(api_key: "", api_secret_key: "", host_name: "") + modify_context(api_key: "", api_secret_key: "", host: "") assert_raises(ShopifyAPI::Errors::ContextNotSetupError) do ShopifyAPI::Auth::Oauth.begin_auth(shop: @shop, redirect_path: "/redirect") @@ -242,7 +242,7 @@ def test_validate_auth_callback_invalid_hmac end def test_validate_auth_context_not_setup - modify_context(api_key: "", api_secret_key: "", host_name: "") + modify_context(api_key: "", api_secret_key: "", host: "") assert_raises(ShopifyAPI::Errors::ContextNotSetupError) do ShopifyAPI::Auth::Oauth.validate_auth_callback(cookies: @cookies, auth_query: @auth_query) diff --git a/test/auth_test.rb b/test/auth_test.rb index 56a7b5c74..695682e1d 100644 --- a/test/auth_test.rb +++ b/test/auth_test.rb @@ -25,7 +25,7 @@ def test_no_host end def test_context_not_setup - modify_context(api_key: "", api_secret_key: "", host_name: "") + modify_context(api_key: "", api_secret_key: "", host: "") assert_raises(ShopifyAPI::Errors::ContextNotSetupError) do ShopifyAPI::Auth.embedded_app_url(@encoded_host) diff --git a/test/context_test.rb b/test/context_test.rb index 91e96e7fa..906b11cb5 100644 --- a/test/context_test.rb +++ b/test/context_test.rb @@ -44,6 +44,7 @@ def test_setup assert_equal("user_agent_prefix1", ShopifyAPI::Context.user_agent_prefix) assert_equal("old_secret", ShopifyAPI::Context.old_api_secret_key) assert_equal("http", ShopifyAPI::Context.host_scheme) + assert_equal("localhost", ShopifyAPI::Context.host_name) end def test_active_session_is_thread_safe diff --git a/test/test_helper.rb b/test/test_helper.rb index 886c4a8eb..65dc065ab 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -27,7 +27,7 @@ def setup api_key: "API_KEY", api_secret_key: "API_SECRET_KEY", api_version: "unstable", - host_name: "app-address.com", + host: "https://app-address.com", scope: ["scope1", "scope2"], is_private: false, is_embedded: false, @@ -42,7 +42,7 @@ def setup api_key: T.nilable(String), api_secret_key: T.nilable(String), api_version: T.nilable(String), - host_name: T.nilable(String), + host: T.nilable(String), scope: T.nilable(T.any(T::Array[String], String)), is_private: T.nilable(T::Boolean), is_embedded: T.nilable(T::Boolean), @@ -57,7 +57,7 @@ def modify_context( api_key: nil, api_secret_key: nil, api_version: nil, - host_name: nil, + host: nil, scope: nil, is_private: nil, is_embedded: nil, @@ -71,7 +71,7 @@ def modify_context( api_key: api_key ? api_key : ShopifyAPI::Context.api_key, api_secret_key: api_secret_key ? api_secret_key : ShopifyAPI::Context.api_secret_key, api_version: api_version ? api_version : ShopifyAPI::Context.api_version, - host_name: host_name ? host_name : ShopifyAPI::Context.host_name, + host: host ? host : ShopifyAPI::Context.host, scope: scope ? scope : ShopifyAPI::Context.scope.to_s, is_private: !is_private.nil? ? is_private : ShopifyAPI::Context.private?, is_embedded: !is_embedded.nil? ? is_embedded : ShopifyAPI::Context.embedded?,