Skip to content

Commit 302438b

Browse files
committed
better test setup
1 parent 6b776fa commit 302438b

File tree

5 files changed

+176
-167
lines changed

5 files changed

+176
-167
lines changed

test/test.rb

Lines changed: 0 additions & 167 deletions
This file was deleted.

test/test_helper.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
require "minitest/autorun"
2+
require "fakeweb"
3+
require "json"
4+
require "woocommerce_api"
5+
6+
module WoocommerceApi; end
7+
8+
pattern = File.join(__dir__, 'woocommerce_api', '*')
9+
files = Dir[pattern]
10+
files.each { |file| require file }
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
class WoocommerceApi::HttpBasicAuthTest < Minitest::Test
2+
def setup
3+
@basic_auth = WooCommerce::API.new(
4+
"https://dev.test/",
5+
"user",
6+
"pass"
7+
)
8+
end
9+
10+
def test_basic_auth_get
11+
FakeWeb.register_uri(:get, "https://user:[email protected]/wc-api/v3/customers",
12+
body: '{"customers":[]}',
13+
content_type: "application/json"
14+
)
15+
response = @basic_auth.get "customers"
16+
17+
assert_equal 200, response.code
18+
end
19+
20+
def test_basic_auth_post
21+
FakeWeb.register_uri(:post, "https://user:[email protected]/wc-api/v3/products",
22+
body: '{"products":[]}',
23+
content_type: "application/json",
24+
status: ["201", "Created"]
25+
)
26+
27+
data = {
28+
product: {
29+
title: "Testing product"
30+
}
31+
}
32+
response = @basic_auth.post "products", data
33+
34+
assert_equal 201, response.code
35+
end
36+
37+
def test_basic_auth_put
38+
FakeWeb.register_uri(:put, "https://user:[email protected]/wc-api/v3/products/1234",
39+
body: '{"customers":[]}',
40+
content_type: "application/json"
41+
)
42+
43+
data = {
44+
product: {
45+
title: "Updating product title"
46+
}
47+
}
48+
response = @basic_auth.put "products/1234", data
49+
50+
assert_equal 200, response.code
51+
end
52+
53+
def test_basic_auth_delete
54+
FakeWeb.register_uri(:delete, "https://user:[email protected]/wc-api/v3/products/1234?force=true",
55+
body: '{"message":"Permanently deleted product"}',
56+
content_type: "application/json",
57+
status: ["202", "Accepted"]
58+
)
59+
60+
response = @basic_auth.delete "products/1234?force=true"
61+
62+
assert_equal 202, response.code
63+
assert_equal '{"message":"Permanently deleted product"}', response.to_s
64+
end
65+
66+
def test_basic_auth_delete_params
67+
FakeWeb.register_uri(:delete, "https://user:[email protected]/wc-api/v3/products/1234?force=true",
68+
body: '{"message":"Permanently deleted product"}',
69+
content_type: "application/json",
70+
status: ["202", "Accepted"]
71+
)
72+
73+
response = @basic_auth.delete "products/1234", force: true
74+
75+
assert_equal 202, response.code
76+
assert_equal '{"message":"Permanently deleted product"}', response.to_s
77+
end
78+
end

test/woocommerce_api/oauth_test.rb

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# class WoocommerceApi::OauthTest < Minitest::Test
2+
# def setup
3+
# @oauth = WooCommerce::API.new(
4+
# "http://dev.test/",
5+
# "user",
6+
# "pass"
7+
# )
8+
# end
9+
10+
# def test_oauth_get
11+
# FakeWeb.register_uri(:get, /http:\/\/dev\.test\/wc-api\/v3\/customers\?oauth_consumer_key=user&oauth_nonce=(.*)&(.*)oauth_signature_method=HMAC-SHA256&oauth_timestamp=(.*)/,
12+
# body: '{"customers":[]}',
13+
# content_type: "application/json"
14+
# )
15+
# response = @oauth.get "customers"
16+
17+
# assert_equal 200, response.code
18+
# end
19+
20+
# def test_oauth_get_puts_data_in_alpha_order
21+
# 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/,
22+
# body: '{"customers":[]}',
23+
# content_type: "application/json"
24+
# )
25+
# response = @oauth.get "customers", abc: '123', oauth_d: '456', xyz: '789'
26+
27+
# assert_equal 200, response.code
28+
# end
29+
30+
# def test_oauth_post
31+
# FakeWeb.register_uri(:post, /http:\/\/dev\.test\/wc-api\/v3\/products\?oauth_consumer_key=user&oauth_nonce=(.*)&(.*)oauth_signature_method=HMAC-SHA256&oauth_timestamp=(.*)/,
32+
# body: '{"products":[]}',
33+
# content_type: "application/json",
34+
# status: ["201", "Created"]
35+
# )
36+
37+
# data = {
38+
# product: {
39+
# title: "Testing product"
40+
# }
41+
# }
42+
# response = @oauth.post "products", data
43+
44+
# assert_equal 201, response.code
45+
# end
46+
47+
# def test_oauth_put
48+
# FakeWeb.register_uri(:put, /http:\/\/dev\.test\/wc-api\/v3\/products\?oauth_consumer_key=user&oauth_nonce=(.*)&(.*)oauth_signature_method=HMAC-SHA256&oauth_timestamp=(.*)/,
49+
# body: '{"products":[]}',
50+
# content_type: "application/json"
51+
# )
52+
53+
# data = {
54+
# product: {
55+
# title: "Updating product title"
56+
# }
57+
# }
58+
# response = @oauth.put "products", data
59+
60+
# assert_equal 200, response.code
61+
# end
62+
63+
# def test_oauth_put
64+
# 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=(.*)/,
65+
# body: '{"message":"Permanently deleted product"}',
66+
# content_type: "application/json",
67+
# status: ["202", "Accepted"]
68+
# )
69+
70+
# response = @oauth.delete "products/1234?force=true"
71+
72+
# assert_equal 202, response.code
73+
# assert_equal '{"message":"Permanently deleted product"}', response.to_json
74+
# end
75+
76+
# def test_adding_query_params
77+
# url = @oauth.send(:add_query_params, 'foo.com', filter: { sku: '123' }, order: 'created_at')
78+
# assert_equal url, CGI::escape('foo.com?filter[sku]=123&order=created_at')
79+
# end
80+
# end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class WoocommerceApi::SignatureTest < Minitest::Test
2+
def test_invalid_signature_method
3+
assert_raises WooCommerce::OAuth::InvalidSignatureMethodError do
4+
client = WooCommerce::API.new("http://dev.test/", "user", "pass", signature_method: 'GARBAGE')
5+
client.get 'products'
6+
end
7+
end
8+
end

0 commit comments

Comments
 (0)