From 4d145079caa460f1b33447b70d09c22dd445a87a Mon Sep 17 00:00:00 2001 From: Dave Goddard Date: Fri, 12 Jun 2020 19:28:12 +1000 Subject: [PATCH 1/6] update ruby versions for travis to supported --- .travis.yml | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index 9b9f3b7..70c1660 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,7 @@ language: ruby rvm: - - 2.0.0 - - 2.1.5 - - 2.2.1 - - 2.3.0 - - ruby-head + - 2.4 + - 2.5 + - 2.6 + - 2.7 script: bundle exec rake test From 9fe12e6f1a4d8ee5aa0fcdc16be4a5a325a4c88d Mon Sep 17 00:00:00 2001 From: Dave Goddard Date: Fri, 12 Jun 2020 19:29:18 +1000 Subject: [PATCH 2/6] update to latest bundler version 2.0 --- Gemfile.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile.lock b/Gemfile.lock index b42e040..29c1355 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -20,4 +20,4 @@ DEPENDENCIES rake BUNDLED WITH - 1.12.5 + 2.1.4 From 5cce943ea4fb03b9bb4f85c23143732e4af9adbc Mon Sep 17 00:00:00 2001 From: Dave Goddard Date: Fri, 12 Jun 2020 20:43:53 +1000 Subject: [PATCH 3/6] Switch to webmock from fakeweb Fakeweb hasn't had a release since 2010 and doesn't work with latest ruby --- Gemfile | 2 +- Gemfile.lock | 14 +++++++-- test/test.rb | 88 ++++++++++++++++++++++++++++++++-------------------- 3 files changed, 67 insertions(+), 37 deletions(-) diff --git a/Gemfile b/Gemfile index 9c451ee..7844371 100644 --- a/Gemfile +++ b/Gemfile @@ -6,5 +6,5 @@ gem "json" group :test do gem "rake" gem "minitest" - gem "fakeweb" + gem "webmock" end diff --git a/Gemfile.lock b/Gemfile.lock index 29c1355..823951e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,23 +1,33 @@ GEM remote: https://rubygems.org/ specs: - fakeweb (1.3.0) + addressable (2.7.0) + public_suffix (>= 2.0.2, < 5.0) + crack (0.4.3) + safe_yaml (~> 1.0.0) + hashdiff (1.0.1) httparty (0.14.0) multi_xml (>= 0.5.2) json (2.0.2) minitest (5.10.1) multi_xml (0.6.0) + public_suffix (4.0.5) rake (12.0.0) + safe_yaml (1.0.5) + webmock (3.8.3) + addressable (>= 2.3.6) + crack (>= 0.3.2) + hashdiff (>= 0.4.0, < 2.0.0) PLATFORMS ruby DEPENDENCIES - fakeweb httparty json minitest rake + webmock BUNDLED WITH 2.1.4 diff --git a/test/test.rb b/test/test.rb index 58f25f8..0b262af 100644 --- a/test/test.rb +++ b/test/test.rb @@ -1,5 +1,5 @@ require "minitest/autorun" -require "fakeweb" +require 'webmock/minitest' require "json" require "woocommerce_api" @@ -19,9 +19,11 @@ def setup end def test_basic_auth_get - FakeWeb.register_uri(:get, "https://user:pass@dev.test/wc-api/v3/customers", - body: '{"customers":[]}', - content_type: "application/json" + stub_request(:get, "https://dev.test/wc-api/v3/customers") + .with(headers: { 'Authorization'=>'Basic dXNlcjpwYXNz' }) + .to_return( + body: '{"customers":[]}', + headers: { content_type: "application/json" }, ) response = @basic_auth.get "customers" @@ -29,9 +31,9 @@ def test_basic_auth_get end def test_oauth_get - FakeWeb.register_uri(:get, /http:\/\/dev\.test\/wc-api\/v3\/customers\?oauth_consumer_key=user&oauth_nonce=(.*)&(.*)oauth_signature_method=HMAC-SHA256&oauth_timestamp=(.*)/, + stub_request(:get, /http:\/\/dev\.test\/wc-api\/v3\/customers\?oauth_consumer_key=user&oauth_nonce=(.*)&(.*)oauth_signature_method=HMAC-SHA256&oauth_timestamp=(.*)/).and_return( body: '{"customers":[]}', - content_type: "application/json" + headers: { content_type: "application/json" }, ) response = @oauth.get "customers" @@ -39,9 +41,9 @@ def test_oauth_get end def test_oauth_get_puts_data_in_alpha_order - FakeWeb.register_uri(:get, /http:\/\/dev\.test\/wc-api\/v3\/customers\?abc=123&oauth_consumer_key=user&oauth_d=456&oauth_nonce=(.*)&(.*)oauth_signature_method=HMAC-SHA256&oauth_timestamp=(.*)&xyz=789/, + stub_request(:get, /http:\/\/dev\.test\/wc-api\/v3\/customers\?abc=123&oauth_consumer_key=user&oauth_d=456&oauth_nonce=(.*)&(.*)oauth_signature_method=HMAC-SHA256&oauth_timestamp=(.*)&xyz=789/).and_return( body: '{"customers":[]}', - content_type: "application/json" + headers: { content_type: "application/json" }, ) response = @oauth.get "customers", abc: '123', oauth_d: '456', xyz: '789' @@ -49,10 +51,12 @@ def test_oauth_get_puts_data_in_alpha_order end def test_basic_auth_post - FakeWeb.register_uri(:post, "https://user:pass@dev.test/wc-api/v3/products", - body: '{"products":[]}', - content_type: "application/json", - status: ["201", "Created"] + stub_request(:post, "https://dev.test/wc-api/v3/products") + .with(headers: { 'Authorization'=>'Basic dXNlcjpwYXNz' }) + .to_return( + body: '{"products":[]}', + headers: { content_type: "application/json" }, + status: 201 ) data = { @@ -66,10 +70,10 @@ def test_basic_auth_post end def test_oauth_post - FakeWeb.register_uri(:post, /http:\/\/dev\.test\/wc-api\/v3\/products\?oauth_consumer_key=user&oauth_nonce=(.*)&(.*)oauth_signature_method=HMAC-SHA256&oauth_timestamp=(.*)/, + stub_request(:post, /http:\/\/dev\.test\/wc-api\/v3\/products\?oauth_consumer_key=user&oauth_nonce=(.*)&(.*)oauth_signature_method=HMAC-SHA256&oauth_timestamp=(.*)/).and_return( body: '{"products":[]}', - content_type: "application/json", - status: ["201", "Created"] + headers: { content_type: "application/json" }, + status: 201 ) data = { @@ -83,10 +87,16 @@ def test_oauth_post end def test_basic_auth_put - FakeWeb.register_uri(:put, "https://user:pass@dev.test/wc-api/v3/products/1234", - body: '{"customers":[]}', - content_type: "application/json" - ) + stub_request(:put, "https://dev.test/wc-api/v3/products/1234"). + with( + body: "{\"product\":{\"title\":\"Updating product title\"}}", + headers: { + 'Authorization'=>'Basic dXNlcjpwYXNz', + }). + to_return( + body: '{"customers":[]}', + headers: { content_type: "application/json" } + ) data = { product: { @@ -99,9 +109,9 @@ def test_basic_auth_put end def test_oauth_put - FakeWeb.register_uri(:put, /http:\/\/dev\.test\/wc-api\/v3\/products\?oauth_consumer_key=user&oauth_nonce=(.*)&(.*)oauth_signature_method=HMAC-SHA256&oauth_timestamp=(.*)/, + stub_request(:put, /http:\/\/dev\.test\/wc-api\/v3\/products\?oauth_consumer_key=user&oauth_nonce=(.*)&(.*)oauth_signature_method=HMAC-SHA256&oauth_timestamp=(.*)/).and_return( body: '{"products":[]}', - content_type: "application/json" + headers: { content_type: "application/json" }, ) data = { @@ -115,11 +125,16 @@ def test_oauth_put end def test_basic_auth_delete - FakeWeb.register_uri(:delete, "https://user:pass@dev.test/wc-api/v3/products/1234?force=true", - body: '{"message":"Permanently deleted product"}', - content_type: "application/json", - status: ["202", "Accepted"] - ) + stub_request(:delete, "https://dev.test/wc-api/v3/products/1234?force=true"). + with( + headers: { + 'Authorization'=>'Basic dXNlcjpwYXNz', + }). + to_return( + body: '{"message":"Permanently deleted product"}', + headers: { content_type: "application/json" }, + status: 202, + ) response = @basic_auth.delete "products/1234?force=true" @@ -128,11 +143,16 @@ def test_basic_auth_delete end def test_basic_auth_delete_params - FakeWeb.register_uri(:delete, "https://user:pass@dev.test/wc-api/v3/products/1234?force=true", - body: '{"message":"Permanently deleted product"}', - content_type: "application/json", - status: ["202", "Accepted"] - ) + stub_request(:delete, "https://dev.test/wc-api/v3/products/1234?force=true"). + with( + headers: { + 'Authorization'=>'Basic dXNlcjpwYXNz', + }). + to_return( + body: '{"message":"Permanently deleted product"}', + headers: { content_type: "application/json" }, + status: 202, + ) response = @basic_auth.delete "products/1234", force: true @@ -141,10 +161,10 @@ def test_basic_auth_delete_params end def test_oauth_put - FakeWeb.register_uri(:delete, /http:\/\/dev\.test\/wc-api\/v3\/products\/1234\?force=true&oauth_consumer_key=user&oauth_nonce=(.*)&(.*)oauth_signature_method=HMAC-SHA256&oauth_timestamp=(.*)/, + stub_request(:delete, /http:\/\/dev\.test\/wc-api\/v3\/products\/1234\?force=true&oauth_consumer_key=user&oauth_nonce=(.*)&(.*)oauth_signature_method=HMAC-SHA256&oauth_timestamp=(.*)/).and_return( body: '{"message":"Permanently deleted product"}', - content_type: "application/json", - status: ["202", "Accepted"] + headers: { content_type: "application/json" }, + status: 202 ) response = @oauth.delete "products/1234?force=true" From 2bc4e161bb1758fc7dfcc5e77d964558310fbb93 Mon Sep 17 00:00:00 2001 From: Dave Goddard Date: Fri, 12 Jun 2020 21:01:10 +1000 Subject: [PATCH 4/6] rename second test_oauth_put to test_oauth_delete --- test/test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test.rb b/test/test.rb index 0b262af..06c09af 100644 --- a/test/test.rb +++ b/test/test.rb @@ -160,7 +160,7 @@ def test_basic_auth_delete_params assert_equal '{"message":"Permanently deleted product"}', response.to_json end - def test_oauth_put + def test_oauth_delete stub_request(:delete, /http:\/\/dev\.test\/wc-api\/v3\/products\/1234\?force=true&oauth_consumer_key=user&oauth_nonce=(.*)&(.*)oauth_signature_method=HMAC-SHA256&oauth_timestamp=(.*)/).and_return( body: '{"message":"Permanently deleted product"}', headers: { content_type: "application/json" }, From a707a261dfe0e13aa25920e64cda1ea3c975ab98 Mon Sep 17 00:00:00 2001 From: Dave Goddard Date: Fri, 12 Jun 2020 20:59:44 +1000 Subject: [PATCH 5/6] update rake, json, minitest, httparty gems --- Gemfile.lock | 12 ++++++++---- test/test.rb | 6 +++--- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 823951e..3bc12ac 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -6,13 +6,17 @@ GEM crack (0.4.3) safe_yaml (~> 1.0.0) hashdiff (1.0.1) - httparty (0.14.0) + httparty (0.18.1) + mime-types (~> 3.0) multi_xml (>= 0.5.2) - json (2.0.2) - minitest (5.10.1) + json (2.3.0) + mime-types (3.3.1) + mime-types-data (~> 3.2015) + mime-types-data (3.2020.0512) + minitest (5.14.1) multi_xml (0.6.0) public_suffix (4.0.5) - rake (12.0.0) + rake (13.0.1) safe_yaml (1.0.5) webmock (3.8.3) addressable (>= 2.3.6) diff --git a/test/test.rb b/test/test.rb index 06c09af..4c7d5b4 100644 --- a/test/test.rb +++ b/test/test.rb @@ -139,7 +139,7 @@ def test_basic_auth_delete response = @basic_auth.delete "products/1234?force=true" assert_equal 202, response.code - assert_equal '{"message":"Permanently deleted product"}', response.to_json + assert_equal '{"message":"Permanently deleted product"}', response.body end def test_basic_auth_delete_params @@ -157,7 +157,7 @@ def test_basic_auth_delete_params response = @basic_auth.delete "products/1234", force: true assert_equal 202, response.code - assert_equal '{"message":"Permanently deleted product"}', response.to_json + assert_equal '{"message":"Permanently deleted product"}', response.body end def test_oauth_delete @@ -170,7 +170,7 @@ def test_oauth_delete response = @oauth.delete "products/1234?force=true" assert_equal 202, response.code - assert_equal '{"message":"Permanently deleted product"}', response.to_json + assert_equal '{"message":"Permanently deleted product"}', response.body end def test_adding_query_params From bb2c6a1754a058e1cac686362710b59684cb4d64 Mon Sep 17 00:00:00 2001 From: Dave Goddard Date: Sat, 23 May 2020 09:25:43 +1000 Subject: [PATCH 6/6] replace deprecated URI.encode URI.encode has been deprecated in ruby and is displaying warnings in ruby 2.7 --- lib/woocommerce_api.rb | 8 ++++---- lib/woocommerce_api/oauth.rb | 2 +- test/test.rb | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/woocommerce_api.rb b/lib/woocommerce_api.rb index 4ef29b2..d363a31 100644 --- a/lib/woocommerce_api.rb +++ b/lib/woocommerce_api.rb @@ -97,7 +97,7 @@ def add_query_params endpoint, data endpoint += "?" unless endpoint.include? "?" endpoint += "&" unless endpoint.end_with? "?" - endpoint + URI.encode(flatten_hash(data).join("&")) + endpoint + URI.encode_www_form(flatten_hash(data)) end # Internal: Get URL for requests @@ -189,12 +189,12 @@ def flatten_hash hash case value when Hash value.map do |inner_key, inner_value| - "#{key}[#{inner_key}]=#{inner_value}" + ["#{key}[#{inner_key}]", "#{inner_value}"] end when Array - value.map { |inner_value| "#{key}[]=#{inner_value}" } + value.map { |inner_value| ["#{key}[]", "#{inner_value}"] } else - "#{key}=#{value}" + [["#{key}", "#{value}"]] end end end diff --git a/lib/woocommerce_api/oauth.rb b/lib/woocommerce_api/oauth.rb index c5bc44e..3df7c66 100644 --- a/lib/woocommerce_api/oauth.rb +++ b/lib/woocommerce_api/oauth.rb @@ -42,7 +42,7 @@ def get_oauth_url params["oauth_timestamp"] = Time.new.to_i params["oauth_signature"] = CGI::escape(generate_oauth_signature(params, url)) - query_string = URI::encode(params.map{|key, value| "#{key}=#{value}"}.join("&")) + query_string = URI.encode_www_form(params) "#{url}?#{query_string}" end diff --git a/test/test.rb b/test/test.rb index 4c7d5b4..d273805 100644 --- a/test/test.rb +++ b/test/test.rb @@ -175,7 +175,7 @@ def test_oauth_delete def test_adding_query_params url = @oauth.send(:add_query_params, 'foo.com', filter: { sku: '123' }, order: 'created_at') - assert_equal url, URI.encode('foo.com?filter[sku]=123&order=created_at') + assert_equal url, 'foo.com?filter%5Bsku%5D=123&order=created_at' end def test_invalid_signature_method