10000 Successfully discovers and acquires access token and passes all tests · jamesbrink/wp-api-python@ad62608 · GitHub
[go: up one dir, main page]

Skip to content

Commit ad62608

Browse files
author
derwentx
committed
Successfully discovers and acquires access token and passes all tests
1 parent 9a6e580 commit ad62608

File tree

7 files changed

+607
-137
lines changed

7 files changed

+607
-137
lines changed

README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Wordpress API - Python Client
22
===============================
33

44
A Python wrapper for the Wordpress REST API that also works on the WooCommerce REST API v1-3 and WooCommerce WP-API v1.
5-
Forked from the Wordpress API written by Claudio Sanches @ WooThemes and modified to work with Wordpress: https://github.com/woocommerce/wc-api-python
5+
Forked from the excellent Wordpress API written by Claudio Sanches @ WooThemes and modified to work with Wordpress: https://github.com/woocommerce/wc-api-python
66

77
I created this fork because I prefer the way that the wc-api-python client interfaces with
88
the Wordpress API compared to the existing python client, https://pypi.python.org/pypi/wordpress_json
@@ -116,7 +116,7 @@ Options
116116
+-----------------------+-------------+----------+-------------------------------------------------------------------------------------------------------+
117117
| ``api`` | ``string`` | no | Determines which api to use, defaults to ``wp-json``, can be arbitrary: ``wc-api``, ``oembed`` |
118118
+-----------------------+-------------+----------+-------------------------------------------------------------------------------------------------------+
119-
| ``version`` | ``string`` | no | API version, default is ``wp/v2``, can be ``wp/v2`` if using ``wp-api`` |
119+
| ``version`` | ``string`` | no | API version, default is ``wp/v2``, can be ``v3`` or ``wc/v1`` if using ``wc-api`` |
120120
+-----------------------+-------------+----------+-------------------------------------------------------------------------------------------------------+
121121
| ``timeout`` | ``integer`` | no | Connection timeout, default is ``5`` |
122122
+-----------------------+-------------+----------+-------------------------------------------------------------------------------------------------------+

tests.py

Lines changed: 265 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
""" API Tests """
22
import unittest
3+
from httmock import all_requests, HTTMock, urlmatch
4+
from collections import OrderedDict
5+
36
import wordpress
47
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
613

714

815
class WordpressTestCase(unittest.TestCase):
@@ -17,6 +24,16 @@ def setUp(self):
1724
consumer_secret=self.consumer_secret
1825
)
1926

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+
2037
def test_version(self):
2138
""" Test default version """
2239
api = wordpress.API(
@@ -25,7 +42,7 @@ def test_version(self):
2542
consumer_secret=self.consumer_secret
2643
)
2744

28-
self.assertEqual(api.version, "v3")
45+
self.assertEqual(api.version, __default_api_version__)
2946

3047
def test_non_ssl(self):
3148
""" Test non-ssl """
@@ -134,3 +151,249 @@ def check_sorted(keys, expected):
134151
check_sorted(['a', 'b[c]', 'b[a]', 'b[b]', 'c'], ['a', 'b[c]', 'b[a]', 'b[b]', 'c'])
135152
check_sorted(['d', 'b[c]', 'b[a]', 'b[b]', 'c'], ['b[c]', 'b[a]', 'b[b]', 'c', 'd'])
136153
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'])

wordpress/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,7 @@
1414
__author__ = "Claudio Sanches @ WooThemes"
1515
__license__ = "MIT"
1616

17+
__default_api_version__ = "wp/v2"
18+
__default_api__ = "wp-json"
19+
1720
from wordpress.api import API

0 commit comments

Comments
 (0)
0