Skip to content

Commit

Permalink
interface for Context.host_name for webhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonwittwer committed Oct 20, 2022
1 parent 6bf9538 commit 1694bc2
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 9 deletions.
9 changes: 7 additions & 2 deletions lib/shopify_api/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions test/auth/oauth_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test/auth_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions test/context_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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),
Expand All @@ -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,
Expand All @@ -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?,
Expand Down

0 comments on commit 1694bc2

Please sign in to comment.