1
1
""" API Tests """
2
2
import unittest
3
+ from httmock import all_requests , HTTMock , urlmatch
4
+ from collections import OrderedDict
5
+
3
6
import wordpress
4
7
from wordpress import oauth
5
- from httmock import all_requests , HTTMock
8
+ from wordpress import __default_api_version__ , __default_api__
9
+ from wordpress .helpers import UrlUtils , SeqUtils , StrUtils
10
+ from wordpress .transport import API_Requests_Wrapper
11
+ from wordpress .api import API
12
+ from wordpress .oauth import OAuth
6
13
7
14
8
15
class WordpressTestCase (unittest .TestCase ):
@@ -17,6 +24,16 @@ def setUp(self):
17
24
consumer_secret = self .consumer_secret
18
25
)
19
26
27
+ def test_api (self ):
28
+ """ Test default API """
29
+ api = wordpress .API (
30
+ url = "https://woo.test" ,
31
+ consumer_key = self .consumer_key ,
32
+ consumer_secret = self .consumer_secret
33
+ )
34
+
35
+ self .assertEqual (api .namespace , __default_api__ )
36
+
20
37
def test_version (self ):
21
38
""" Test default version """
22
39
api = wordpress .API (
@@ -25,7 +42,7 @@ def test_version(self):
25
42
consumer_secret = self .consumer_secret
26
43
)
27
44
28
- self .assertEqual (api .version , "v3" )
45
+ self .assertEqual (api .version , __default_api_version__ )
29
46
30
47
def test_non_ssl (self ):
31
48
""" Test non-ssl """
@@ -134,3 +151,249 @@ def check_sorted(keys, expected):
134
151
check_sorted (['a' , 'b[c]' , 'b[a]' , 'b[b]' , 'c' ], ['a' , 'b[c]' , 'b[a]' , 'b[b]' , 'c' ])
135
152
check_sorted (['d' , 'b[c]' , 'b[a]' , 'b[b]' , 'c' ], ['b[c]' , 'b[a]' , 'b[b]' , 'c' , 'd' ])
136
153
check_sorted (['a1' , 'b[c]' , 'b[a]' , 'b[b]' , 'a2' ], ['a1' , 'a2' , 'b[c]' , 'b[a]' , 'b[b]' ])
154
+
155
+ class HelperTestcase (unittest .TestCase ):
156
+ def test_url_is_ssl (self ):
157
+ self .assertTrue (UrlUtils .is_ssl ("https://woo.test:8888" ))
158
+ self .assertFalse (UrlUtils .is_ssl ("http://woo.test:8888" ))
159
+
160
+ def test_url_substitute_query (self ):
161
+ self .assertEqual (
162
+ UrlUtils .substitute_query ("https://woo.test:8888/sdf?param=value" , "newparam=newvalue" ),
163
+ "https://woo.test:8888/sdf?newparam=newvalue"
164
+ )
165
+ self .assertEqual (
166
+ UrlUtils .substitute_query ("https://woo.test:8888/sdf?param=value" ),
167
+ "https://woo.test:8888/sdf"
168
+ )
169
+ self .assertEqual (
170
+ UrlUtils .substitute_query (
171
+ "https://woo.test:8888/sdf?param=value" ,
172
+ "newparam=newvalue&othernewparam=othernewvalue"
173
+ ),
174
+ "https://woo.test:8888/sdf?newparam=newvalue&othernewparam=othernewvalue"
175
+ )
176
+ self .assertEqual (
177
+ UrlUtils .substitute_query (
178
+ "https://woo.test:8888/sdf?param=value" ,
179
+ "newparam=newvalue&othernewparam=othernewvalue"
180
+ ),
181
+ "https://woo.test:8888/sdf?newparam=newvalue&othernewparam=othernewvalue"
182
+ )
183
+
184
+ def test_url_join_components (self ):
185
+ self .assertEqual (
186
+ 'https://woo.test:8888/wp-json' ,
187
+ UrlUtils .join_components (['https://woo.test:8888/' , '' , 'wp-json' ])
188
+ )
189
+ self .assertEqual (
190
+ 'https://woo.test:8888/wp-json/wp/v2' ,
191
+ UrlUtils .join_components (['https://woo.test:8888/' , 'wp-json' , 'wp/v2' ])
192
+ )
193
+
194
+ def test_url_get_php_value (self ):
195
+ self .assertEqual (
196
+ '1' ,
197
+ UrlUtils .get_value_like_as_php (True )
198
+ )
199
+ self .assertEqual (
200
+ '' ,
201
+ UrlUtils .get_value_like_as_php (False )
202
+ )
203
+ self .assertEqual (
204
+ 'asd' ,
205
+ UrlUtils .get_value_like_as_php ('asd' )
206
+ )
207
+ self .assertEqual (
208
+ '1' ,
209
+ UrlUtils .get_value_like_as_php (1 )
210
+ )
211
+ self .assertEqual (
212
+ '1' ,
213
+ UrlUtils .get_value_like_as_php (1.0 )
214
+ )
215
+ self .assertEqual (
216
+ '1.1' ,
217
+ UrlUtils .get_value_like_as_php (1.1 )
218
+ )
219
+
220
+
221
+ def test_seq_filter_true (self ):
222
+ self .assertEquals (
223
+ ['a' , 'b' , 'c' , 'd' ],
224
+ SeqUtils .filter_true ([None , 'a' , False , 'b' , 'c' ,'d' ])
225
+ )
226
+
227
+ def test_str_remove_tail (self ):
228
+ self .assertEqual (
229
+ 'sdf' ,
230
+ StrUtils .remove_tail ('sdf/' ,'/' )
231
+ )
232
+
233
+ class TransportTestcases (unittest .TestCase ):
234
+ def setUp (self ):
235
+ self .requester = API_Requests_Wrapper (
236
+ url = 'https://woo.test:8888/' ,
237
+ api = 'wp-json' ,
238
+ api_version = 'wp/v2'
239
+ )
240
+
241
+ def test_api_url (self ):
242
+ self .assertEqual (
243
+ 'https://woo.test:8888/wp-json' ,
244
+ self .requester .api_url
245
+ )
246
+
247
+ def test_endpoint_url (self ):
248
+ self .assertEqual (
249
+ 'https://woo.test:8888/wp-json/wp/v2/posts' ,
250
+ self .requester .endpoint_url ('posts' )
251
+ )
252
+
253
+ def test_request (self ):
254
+
255
+ @all_requests
256
+ def woo_test_mock (* args , ** kwargs ):
257
+ """ URL Mock """
258
+ return {'status_code' : 200 ,
259
+ 'content' : 'OK' }
260
+
261
+ with HTTMock (woo_test_mock ):
262
+ # call requests
263
+ response = self .requester .request ("GET" , "https://woo.test:8888/wp-json/wp/v2/posts" )
264
+ self .assertEqual (response .status_code , 200 )
265
+ self .assertEqual (response .request .url , 'https://woo.test:8888/wp-json/wp/v2/posts' )
266
+
267
+ class OAuthTestcases (unittest .TestCase ):
268
+ def setUp (self ):
269
+ self .consumer_key = "ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
270
+ self .consumer_secret = "cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
271
+ self .api = wordpress .API (
272
+ url = "http://woo.test" ,
273
+ consumer_key = self .consumer_key ,
274
+ consumer_secret = self .consumer_secret
275
+ )
276
+
277
+ # def test_get_sign(self):
278
+ # message = "POST&http%3A%2F%2Flocalhost%3A8888%2Fwordpress%2Foauth1%2Frequest&oauth_callback%3Dlocalhost%253A8888%252Fwordpress%26oauth_consumer_key%3DLCLwTOfxoXGh%26oauth_nonce%3D85285179173071287531477036693%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1477036693%26oauth_version%3D1.0"
279
+ # signature_method = 'HMAC-SHA1'
280
+ # sig_key = 'k7zLzO3mF75Xj65uThpAnNvQHpghp4X1h5N20O8hCbz2kfJq&'
281
+ # sig = OAuth.get_sign(message, signature_method, sig_key)
282
+ # expected_sig = '8T93S/PDOrEd+N9cm84EDvsPGJ4='
283
+ # self.assertEqual(sig, expected_sig)
284
+
285
+ def test_normalize_params (self ):
286
+ params = dict ([('oauth_callback' , 'localhost:8888/wordpress' ), ('oauth_consumer_key' , 'LCLwTOfxoXGh' ), ('oauth_nonce' , '45474014077032100721477037582' ), ('oauth_signature_method' , 'HMAC-SHA1' ), ('oauth_timestamp' , 1477037582 ), ('oauth_version' , '1.0' )])
287
+ expected_normalized_params = "oauth_callback=localhost%3A8888%2Fwordpress&oauth_consumer_key=LCLwTOfxoXGh&oauth_nonce=45474014077032100721477037582&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1477037582&oauth_version=1.0"
288
+ normalized_params = OAuth .normalize_params (params )
289
+ self .assertEqual (expected_normalized_params , normalized_params )
290
+
291
+ def generate_oauth_signature (self ):
292
+ base_url = "http://localhost:8888/wordpress/"
293
+ api_name = 'wc-api'
294
+ api_ver = 'v3'
295
+ endpoint = 'products/99'
296
+ signature_method = "HAMC-SHA1"
297
+ consumer_key = "ck_681c2be361e415519dce4b65ee981682cda78bc6"
298
+ consumer_secret = "cs_b11f652c39a0afd3752fc7bb0c56d60d58da5877"
299
+
300
+ wcapi = API (
301
+ url = base_url ,
302
+ consumer_key = consumer_key ,
303
+ consumer_secret = consumer_secret ,
304
+ api = api_name ,
305
+ version = api_ver ,
306
+ signature_method = signature_method
307
+ )
308
+
309
+ endpoint_url = UrlUtils .join_components ([base_url , api_name , api_ver , endpoint ])
310
+
311
+ params = OrderedDict ()
312
+ params ["oauth_consumer_key" ] = consumer_key
313
+ params ["oauth_timestamp" ] = "1477041328"
314
+ params ["oauth_nonce" ] = "166182658461433445531477041328"
315
+ params ["oauth_signature_method" ] = signature_method
316
+ params ["oauth_version" ] = "1.0"
317
+ params ["oauth_callback" ] = 'localhost:8888/wordpress'
318
+
319
+ sig = wcapi .oauth .generate_oauth_signature ("POST" , params , endpoint_url )
320
+ expected_sig = "517qNKeq/vrLZGj2UH7+q8ILWAg="
321
+ self .assertEqual (sig , expected_sig )
322
+
323
+ class OAuth3LegTestcases (unittest .TestCase ):
324
+ def setUp (self ):
325
+ self .consumer_key = "ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
326
+ self .consumer_secret = "cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
327
+ self .api = API (
328
+ url = "http://woo.test" ,
329
+ consumer_key = self .consumer_key ,
330
+ consumer_secret = self .consumer_secret ,
331
+ oauth1a_3leg = True ,
332
+ wp_user = 'test_user' ,
333
+ wp_pass = 'test_pass' ,
334
+ callback = 'http://127.0.0.1/oauth1_callback'
335
+ )
336
+
337
+ @urlmatch (path = r'.*wp-json.*' )
338
+ def woo_api_mock (* args , ** kwargs ):
339
+ """ URL M
F438
ock """
340
+ return {
341
+ 'status_code' : 200 ,
342
+ 'content' : """
343
+ {
344
+ "name": "Wordpress",
345
+ "description": "Just another WordPress site",
346
+ "url": "http://localhost:8888/wordpress",
347
+ "home": "http://localhost:8888/wordpress",
348
+ "namespaces": [
349
+ "wp/v2",
350
+ "oembed/1.0",
351
+ "wc/v1"
352
+ ],
353
+ "authentication": {
354
+ "oauth1": {
355
+ "request": "http://localhost:8888/wordpress/oauth1/request",
356
+ "authorize": "http://localhost:8888/wordpress/oauth1/authorize",
357
+ "access": "http://localhost:8888/wordpress/oauth1/access",
358
+ "version": "0.1"
359
+ }
360
+ }
361
+ }
362
+ """
363
+ }
364
+
365
+ @urlmatch (path = r'.*oauth.*' )
366
+ def woo_authentication_mock (* args , ** kwargs ):
367
+ """ URL Mock """
368
+ return {
369
+ 'status_code' :200 ,
370
+ 'content' :"""oauth_token=XXXXXXXXXXXX&oauth_token_secret=YYYYYYYYYYYY"""
371
+ }
372
+
373
+ def test_auth_discovery (self ):
374
+
375
+ with HTTMock (self .woo_api_mock ):
376
+ # call requests
377
+ authentication = self .api .oauth .authentication
378
+ self .assertEquals (
379
+ authentication ,
380
+ {
381
+ "oauth1" : {
382
+ "request" : "http://localhost:8888/wordpress/oauth1/request" ,
383
+ "authorize" : "http://localhost:8888/wordpress/oauth1/authorize" ,
384
+ "access" : "http://localhost:8888/wordpress/oauth1/access" ,
385
+ "version" : "0.1"
386
+ }
387
+ }
388
+ )
389
+
390
+ def test_request_access_token (self ):
391
+
392
+ with HTTMock (self .woo_api_mock ):
393
+ authentication = self .api .oauth .authentication
394
+ self .assertTrue (authentication )
395
+
396
+ with HTTMock (self .woo_authentication_mock ):
397
+ access_token , access_token_secret = self .api .oauth .request_access_token ()
398
+ self .assertEquals (access_token , ['XXXXXXXXXXXX' ])
399
+ self .assertEquals (access_token_secret , ['YYYYYYYYYYYY' ])
0 commit comments