-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathtest_api.py
161 lines (135 loc) · 5.2 KB
/
test_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
""" API Tests """
import unittest
import woocommerce
from woocommerce import oauth
from httmock import all_requests, HTTMock
class WooCommerceTestCase(unittest.TestCase):
"""Test case for the client methods."""
def setUp(self):
self.consumer_key = "ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
self.consumer_secret = "cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
self.api = woocommerce.API(
url="http://woo.test",
consumer_key=self.consumer_key,
consumer_secret=self.consumer_secret
)
def test_version(self):
""" Test default version """
api = woocommerce.API(
url="https://woo.test",
consumer_key=self.consumer_key,
consumer_secret=self.consumer_secret
)
self.assertEqual(api.version, "wc/v3")
def test_non_ssl(self):
""" Test non-ssl """
api = woocommerce.API(
url="http://woo.test",
consumer_key=self.consumer_key,
consumer_secret=self.consumer_secret
)
self.assertFalse(api.is_ssl)
def test_with_ssl(self):
""" Test ssl """
api = woocommerce.API(
url="https://woo.test",
consumer_key=self.consumer_key,
consumer_secret=self.consumer_secret
)
self.assertTrue(api.is_ssl, True)
def test_with_timeout(self):
""" Test timeout """
api = woocommerce.API(
url="https://woo.test",
consumer_key=self.consumer_key,
consumer_secret=self.consumer_secret,
timeout=10,
)
self.assertEqual(api.timeout, 10)
@all_requests
def woo_test_mock(*args, **kwargs):
""" URL Mock """
return {'status_code': 200,
'content': 'OK'}
with HTTMock(woo_test_mock):
# call requests
status = api.get("products").status_code
self.assertEqual(status, 200)
def test_get(self):
""" Test GET requests """
@all_requests
def woo_test_mock(*args, **kwargs):
""" URL Mock """
return {'status_code': 200,
'content': 'OK'}
with HTTMock(woo_test_mock):
# call requests
status = self.api.get("products").status_code
self.assertEqual(status, 200)
def test_get_with_parameters(self):
""" Test GET requests w/ url params """
@all_requests
def woo_test_mock(*args, **kwargs):
return {'status_code': 200,
'content': 'OK'}
with HTTMock(woo_test_mock):
# call requests
status = self.api.get("products", params={"per_page": 10, "page": 1, "offset": 0}).status_code
self.assertEqual(status, 200)
def test_get_with_requests_kwargs(self):
""" Test GET requests w/ optional requests-module kwargs """
@all_requests
def woo_test_mock(*args, **kwargs):
return {'status_code': 200,
'content': 'OK'}
with HTTMock(woo_test_mock):
# call requests
status = self.api.get("products", allow_redirects=True).status_code
self.assertEqual(status, 200)
def test_post(self):
""" Test POST requests """
@all_requests
def woo_test_mock(*args, **kwargs):
""" URL Mock """
return {'status_code': 201,
'content': 'OK'}
with HTTMock(woo_test_mock):
# call requests
status = self.api.post("products", {}).status_code
self.assertEqual(status, 201)
def test_put(self):
""" Test PUT requests """
@all_requests
def woo_test_mock(*args, **kwargs):
""" URL Mock """
return {'status_code': 200,
'content': 'OK'}
with HTTMock(woo_test_mock):
# call requests
status = self.api.put("products", {}).status_code
self.assertEqual(status, 200)
def test_delete(self):
""" Test DELETE requests """
@all_requests
def woo_test_mock(*args, **kwargs):
""" URL Mock """
return {'status_code': 200,
'content': 'OK'}
with HTTMock(woo_test_mock):
# call requests
status = self.api.delete("products").status_code
self.assertEqual(status, 200)
def test_oauth_sorted_params(self):
""" Test order of parameters for OAuth signature """
def check_sorted(keys, expected):
params = oauth.OrderedDict()
for key in keys:
params[key] = ''
ordered = list(oauth.OAuth.sorted_params(params).keys())
self.assertEqual(ordered, expected)
check_sorted(['a', 'b'], ['a', 'b'])
check_sorted(['b', 'a'], ['a', 'b'])
check_sorted(['a', 'b[a]', 'b[b]', 'b[c]', 'c'], ['a', 'b[a]', 'b[b]', 'b[c]', 'c'])
check_sorted(['a', 'b[c]', 'b[a]', 'b[b]', 'c'], ['a', 'b[c]', 'b[a]', 'b[b]', 'c'])
check_sorted(['d', 'b[c]', 'b[a]', 'b[b]', 'c'], ['b[c]', 'b[a]', 'b[b]', 'c', 'd'])
check_sorted(['a1', 'b[c]', 'b[a]', 'b[b]', 'a2'], ['a1', 'a2', 'b[c]', 'b[a]', 'b[b]'])