Skip to content

Update versions #51

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 4 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ gem "json"
group :test do
gem "rake"
gem "minitest"
gem "fakeweb"
gem "webmock"
end
28 changes: 21 additions & 7 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,23 +1,37 @@
GEM
remote: https://rubygems.org/
specs:
fakeweb (1.3.0)
httparty (0.14.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.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)
rake (12.0.0)
public_suffix (4.0.5)
rake (13.0.1)
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
1.12.5
2.1.4
8 changes: 4 additions & 4 deletions lib/woocommerce_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/woocommerce_api/oauth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
98 changes: 59 additions & 39 deletions test/test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require "minitest/autorun"
require "fakeweb"
require 'webmock/minitest'
require "json"
require "woocommerce_api"

Expand All @@ -19,40 +19,44 @@ def setup
end

def test_basic_auth_get
FakeWeb.register_uri(:get, "https://user:[email protected]/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"

assert_equal 200, response.code
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"

assert_equal 200, response.code
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'

assert_equal 200, response.code
end

def test_basic_auth_post
FakeWeb.register_uri(:post, "https://user:[email protected]/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 = {
Expand All @@ -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 = {
Expand All @@ -83,10 +87,16 @@ def test_oauth_post
end

def test_basic_auth_put
FakeWeb.register_uri(:put, "https://user:[email protected]/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: {
Expand All @@ -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 = {
Expand All @@ -115,47 +125,57 @@ def test_oauth_put
end

def test_basic_auth_delete
FakeWeb.register_uri(:delete, "https://user:[email protected]/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"

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
FakeWeb.register_uri(:delete, "https://user:[email protected]/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

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_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=(.*)/,
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"}',
content_type: "application/json",
status: ["202", "Accepted"]
headers: { content_type: "application/json" },
status: 202
)

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
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
Expand Down