Skip to content
This repository has been archived by the owner on Jun 1, 2023. It is now read-only.

Commit

Permalink
Merge branch 'master' into shopify_cli_v2_feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Anema authored Apr 13, 2020
2 parents 7d78d8a + c0e6b16 commit d71d601
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/shopify-cli/helpers/partners_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def authenticated_req(ctx)
Tasks::AuthenticateIdentity.call(ctx)
retry
rescue API::APIRequestNotFoundError
ctx.puts("{{x}} error: Your account was not found. Please sign up at https://partners.shopify.com/signup")
ctx.error("error: Your account was not found. Please sign up at https://partners.shopify.com/signup")
end

def api_client(ctx)
Expand Down
31 changes: 25 additions & 6 deletions lib/shopify-cli/oauth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class OAuth
include SmartProperties

class Error < StandardError; end
LocalRequest = Struct.new(:method, :path, :query, :protocol)

DEFAULT_PORT = 3456
REDIRECT_HOST = "http://app-cli-loopback.shopifyapps.com:#{DEFAULT_PORT}"
Expand Down Expand Up @@ -89,23 +90,41 @@ def listen_local
Thread.current.abort_on_exception = true
begin
socket = server.accept
query = Hash[URI.decode_www_form(socket.gets.split[1][2..-1])]
if !query['error'].nil?
respond_with(socket, 400, "Invalid Request: #{query['error_description']}")
elsif query['state'] != state_token
query.merge!('error' => 'invalid_state', 'error_description' => INVALID_STATE_RESP)
req = decode_request(socket.gets)
if !req.query['error'].nil?
respond_with(socket, 400, "Invalid Request: #{req.query['error_description']}")
elsif req.query['state'] != state_token
req.query.merge!('error' => 'invalid_state', 'error_description' => INVALID_STATE_RESP)
respond_with(socket, 403, INVALID_STATE_RESP)
else
respond_with(socket, 200, SUCCESS_RESP)
end
query
req.query
ensure
socket.close_write
server.close
end
end
end

def decode_request(req)
data = LocalRequest.new
data.method, path, data.protocol = req.split(' ')
data.path, _sep, query = path.partition("?")
data.query = decode_request_params(query)
data
end

def decode_request_params(str)
str.b.split('&').each_with_object({}) do |string, params|
key, _sep, val = string.partition('=')
key = URI.decode_www_form_component(key)
val = URI.decode_www_form_component(val)
params[key] = val
params
end
end

def receive_access_code
@access_code ||= begin
server = @server_thread.join(60)
Expand Down
4 changes: 2 additions & 2 deletions shopify.fish
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ function __shopify__
case setenv:\*
set -l assignment (echo "$fin" | sed 's/^setenv://' | tr = \\n)
set -x "$assignment[1]" "$assignment[2]"
case reload_shopify_from:\*
set -l root (echo "$fin" | sed 's/^reload_shopify_from://')
case reload_shopify_cli_from:\*
set -l root (echo "$fin" | sed 's/^reload_shopify_cli_from://')
source "$root/shopify.fish"
end
end < "$finalizers"
Expand Down
11 changes: 7 additions & 4 deletions test/shopify-cli/helpers/partners_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,21 @@ def test_query_can_reauth
PartnersAPI.query(@context, 'query')
end

def test_query_fails_gracefully_without_partners_account
def test_query_aborts_without_partners_account
api_stub = Object.new
PartnersAPI.expects(:new).with(
ctx: @context,
token: 'token123',
url: "#{PartnersAPI.endpoint}/api/cli/graphql",
).returns(api_stub)
api_stub.expects(:query).raises(API::APIRequestNotFoundError)
@context.expects(:puts).with(
"{{x}} error: Your account was not found. Please sign up at https://partners.shopify.com/signup",
e = assert_raises ShopifyCli::Abort do
PartnersAPI.query(@context, 'query')
end
assert_match(
'error: Your account was not found. Please sign up at https://partners.shopify.com/signup',
e.message
)
PartnersAPI.query(@context, 'query')
end

def test_query
Expand Down

0 comments on commit d71d601

Please sign in to comment.