diff --git a/lib/shopify-cli/helpers/partners_api.rb b/lib/shopify-cli/helpers/partners_api.rb index 50a53dbb1b..8f13c52db6 100644 --- a/lib/shopify-cli/helpers/partners_api.rb +++ b/lib/shopify-cli/helpers/partners_api.rb @@ -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) diff --git a/lib/shopify-cli/oauth.rb b/lib/shopify-cli/oauth.rb index 3bca8f49f7..ecf9d9eb02 100644 --- a/lib/shopify-cli/oauth.rb +++ b/lib/shopify-cli/oauth.rb @@ -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}" @@ -89,16 +90,16 @@ 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 @@ -106,6 +107,24 @@ def listen_local 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) diff --git a/shopify.fish b/shopify.fish index eba6e743af..fdef6afe82 100755 --- a/shopify.fish +++ b/shopify.fish @@ -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" diff --git a/test/shopify-cli/helpers/partners_api_test.rb b/test/shopify-cli/helpers/partners_api_test.rb index 6498b46b09..e85defaaaa 100644 --- a/test/shopify-cli/helpers/partners_api_test.rb +++ b/test/shopify-cli/helpers/partners_api_test.rb @@ -62,7 +62,7 @@ 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, @@ -70,10 +70,13 @@ def test_query_fails_gracefully_without_partners_account 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