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

host_name or host is required not both #1045

Merged
merged 6 commits into from
Oct 20, 2022
Merged
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 @@ -3,6 +3,7 @@
Note: For changes to the API, see https://shopify.dev/changelog?filter=api

## Unreleased
- [#1045](https://github.com/Shopify/shopify-api-ruby/pull/1045) Fixes bug with host/host_name requirement.

## Version 12.2.0

Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ group :test do
gem "fakefs", require: false
gem "webmock"
gem "mocha"
gem "pry-byebug"
end
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ GEM
addressable (2.8.0)
public_suffix (>= 2.0.2, < 5.0)
ast (2.4.2)
byebug (11.1.3)
coderay (1.1.3)
concurrent-ruby (1.1.9)
crack (0.4.5)
Expand Down Expand Up @@ -56,6 +57,9 @@ GEM
pry (0.14.1)
coderay (~> 1.1)
method_source (~> 1.0)
pry-byebug (3.10.1)
byebug (~> 11.0)
pry (>= 0.13, < 0.15)
public_suffix (4.0.6)
rainbow (3.1.1)
rake (13.0.6)
Expand Down Expand Up @@ -140,6 +144,7 @@ DEPENDENCIES
fakefs
minitest
mocha
pry-byebug
rake
rubocop
rubocop-shopify
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Start by initializing the `ShopifyAPI::Context` with the parameters of your app
ShopifyAPI::Context.setup(
api_key: "<api-key>",
api_secret_key: "<api-secret-key>",
host_name: "<application-host-name>",
host: "<https://application-host-name.com>",
scope: "read_orders,read_products,etc",
session_storage: ShopifyAPI::Auth::FileSessionStorage.new, # See more details below
is_embedded: true, # Set to true if you are building an embedded app
Expand Down
15 changes: 9 additions & 6 deletions lib/shopify_api/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ class Context
@api_key = T.let("", String)
@api_secret_key = T.let("", String)
@api_version = T.let(LATEST_SUPPORTED_ADMIN_VERSION, String)
@host_name = T.let("", String)
@scope = T.let(Auth::AuthScopes.new, Auth::AuthScopes)
@session_storage = T.let(ShopifyAPI::Auth::FileSessionStorage.new, ShopifyAPI::Auth::SessionStorage)
@is_private = T.let(false, T::Boolean)
Expand All @@ -30,12 +29,12 @@ class << self
api_key: String,
api_secret_key: String,
api_version: String,
host_name: String,
scope: T.any(T::Array[String], String),
is_private: T::Boolean,
is_embedded: T::Boolean,
session_storage: ShopifyAPI::Auth::SessionStorage,
logger: Logger,
host_name: T.nilable(String),
host: T.nilable(String),
private_shop: T.nilable(String),
user_agent_prefix: T.nilable(String),
Expand All @@ -46,12 +45,12 @@ def setup(
api_key:,
api_secret_key:,
api_version:,
host_name:,
scope:,
is_private:,
is_embedded:,
session_storage:,
logger: Logger.new($stdout),
host_name: nil,
host: ENV["HOST"] || "https://#{host_name}",
private_shop: nil,
user_agent_prefix: nil,
Expand All @@ -65,7 +64,6 @@ def setup(
@api_key = api_key
@api_secret_key = api_secret_key
@api_version = api_version
@host_name = host_name
@host = T.let(host, T.nilable(String))
@is_private = is_private
@scope = Auth::AuthScopes.new(scope)
Expand Down Expand Up @@ -108,7 +106,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 All @@ -134,7 +132,7 @@ def embedded?

sig { returns(T::Boolean) }
def setup?
[api_key, api_secret_key, host_name, T.must(host)].none?(&:empty?)
[api_key, api_secret_key, T.must(host)].none?(&:empty?)
end

sig { returns(T.nilable(Auth::Session)) }
Expand All @@ -158,6 +156,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
1 change: 1 addition & 0 deletions shopify_api.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Gem::Specification.new do |s|
s.add_runtime_dependency("zeitwerk", "~> 2.5")

s.add_development_dependency("activesupport")
s.add_development_dependency("pry-byebug")
s.add_development_dependency("rake")
s.add_development_dependency("rubocop")
s.add_development_dependency("rubocop-shopify")
Expand Down
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
21 changes: 11 additions & 10 deletions test/context_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ module ShopifyAPITest
class ContextTest < Minitest::Test
def setup
@reader, writer = IO.pipe
ENV["HOST"] = "http://localhost:3000"

ShopifyAPI::Context.setup(
api_key: "key",
api_secret_key: "secret",
api_version: "unstable",
host_name: "host",
scope: ["scope1", "scope2"],
is_private: true,
is_embedded: true,
Expand All @@ -33,7 +34,7 @@ def test_setup
assert_equal("key", ShopifyAPI::Context.api_key)
assert_equal("secret", ShopifyAPI::Context.api_secret_key)
assert_equal("unstable", ShopifyAPI::Context.api_version)
assert_equal("host", ShopifyAPI::Context.host_name)
assert_equal("http://localhost:3000", ShopifyAPI::Context.host)
assert_equal(ShopifyAPI::Auth::AuthScopes.new(["scope1", "scope2"]), ShopifyAPI::Context.scope)
assert(ShopifyAPI::Context.private?)
assert_equal(ShopifyAPI::Auth::FileSessionStorage.new, ShopifyAPI::Context.session_storage)
Expand All @@ -42,8 +43,8 @@ def test_setup
assert_equal("privateshop.myshopify.com", ShopifyAPI::Context.private_shop)
assert_equal("user_agent_prefix1", ShopifyAPI::Context.user_agent_prefix)
assert_equal("old_secret", ShopifyAPI::Context.old_api_secret_key)
assert_equal("https", ShopifyAPI::Context.host_scheme)
assert_equal("https://host", ShopifyAPI::Context.host)
assert_equal("http", ShopifyAPI::Context.host_scheme)
assert_equal("localhost", ShopifyAPI::Context.host_name)
end

def test_active_session_is_thread_safe
Expand Down Expand Up @@ -113,17 +114,16 @@ def test_unsupported_api_version
end
end

def test_host_scheme_with_localhost
def test_with_host_name_and_no_host_env
clear_context

old_host = ENV["HOST"]
ENV["HOST"] = "http://localhost:3000"
ENV["HOST"] = nil

ShopifyAPI::Context.setup(
api_key: "key",
api_secret_key: "secret",
api_version: "unstable",
host_name: "host",
host_name: "tunnel-o-security.com",
scope: ["scope1", "scope2"],
is_private: true,
is_embedded: true,
Expand All @@ -132,8 +132,9 @@ def test_host_scheme_with_localhost
user_agent_prefix: "user_agent_prefix1",
old_api_secret_key: "old_secret",
)
assert_equal("http", ShopifyAPI::Context.host_scheme)
assert_equal("http://localhost:3000", ShopifyAPI::Context.host)
assert_equal("https", ShopifyAPI::Context.host_scheme)
assert_equal("https://tunnel-o-security.com", ShopifyAPI::Context.host)
assert_equal("tunnel-o-security.com", ShopifyAPI::Context.host_name)
ENV["HOST"] = old_host
end

Expand Down
9 changes: 5 additions & 4 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
require "mocha/minitest"

require "shopify_api"
require "pry-byebug"

require_relative("./test_helpers/constants.rb")

Expand All @@ -26,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 @@ -41,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 @@ -56,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 @@ -70,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