def test_APIGetWithComplexQuery(self): wcapi = API(**self.api_params) response = wcapi.get('products?page=2&filter%5Blimit%5D=2') self.assertIn(response.status_code, [200,201]) response_obj = response.json() self.assertIn('products', response_obj) self.assertEqual(len(response_obj['products']), 2) response = wcapi.get('products?oauth_consumer_key=ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&oauth_nonce=037470f3b08c9232b0888f52cb9d4685b44d8fd1&oauth_signature=wrKfuIjbwi%2BTHynAlTP4AssoPS0%3D&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1481606275&filter%5Blimit%5D=3') self.assertIn(response.status_code, [200,201]) response_obj = response.json() self.assertIn('products', response_obj) self.assertEqual(len(response_obj['products']), 3)
def test_APIGet(self): wcapi = API(**self.api_params) per_page = 10 response = wcapi.get('products?per_page=%d' % per_page) self.assertIn(response.status_code, [200,201]) response_obj = response.json() self.assertEqual(len(response_obj), per_page)
class WPAPITestCasesBase(unittest.TestCase): def setUp(self): Auth.force_timestamp = CURRENT_TIMESTAMP Auth.force_nonce = SHITTY_NONCE self.creds_store = '~/wc-api-creds-test.json' self.api_params = { 'url': 'http://derwent-mac.ddns.me:18080/wptest/', 'api': 'wp-json', 'version': 'wp/v2', 'consumer_key': 'tYG1tAoqjBEM', 'consumer_secret': 's91fvylVrqChwzzDbEJHEWyySYtAmlIsqqYdjka1KyVDdAyB', 'callback': 'http://127.0.0.1/oauth1_callback', 'wp_user': '******', 'wp_pass': '******', 'oauth1a_3leg': True, } # @debug_on() def test_APIGet(self): self.wpapi = API(**self.api_params) response = self.wpapi.get('users/me') self.assertIn(response.status_code, [200, 201]) response_obj = response.json() self.assertEqual(response_obj['name'], self.api_params['wp_user'])
def test_APIGet(self): wcapi = API(**self.api_params) response = wcapi.get('products') # print UrlUtils.beautify_response(response) self.assertIn(response.status_code, [200,201]) response_obj = response.json() self.assertIn('products', response_obj) self.assertEqual(len(response_obj['products']), 10)
def test_APIGetWithSimpleQuery(self): wpapi = API(**self.apiParams) response = wpapi.get('media?page=2') # print UrlUtils.beautify_response(response) self.assertIn(response.status_code, [200, 201]) response_obj = response.json() self.assertEqual(len(response_obj), 10)
def test_APIPutWithSimpleQuery(self): wcapi = API(**self.api_params) response = wcapi.get('products') first_product = (response.json())['products'][0] original_title = first_product['title'] product_id = first_product['id'] nonce = str(random.random()) response = wcapi.put('products/%s?filter%%5Blimit%%5D=5' % (product_id), {"product":{"title":str(nonce)}}) request_params = UrlUtils.get_query_dict_singular(response.request.url) response_obj = response.json() self.assertEqual(response_obj['product']['title'], str(nonce)) self.assertEqual(request_params['filter[limit]'], str(5)) wcapi.put('products/%s' % (product_id), {"product":{"title":original_title}})
class WPAPITestCases3leg(WPAPITestCasesBase): def setUp(self): super(WPAPITestCases3leg, self).setUp() self.api_params.update({ 'creds_store': self.creds_store, }) self.wpapi = API(**self.api_params) self.wpapi.auth.clear_stored_creds() def test_APIGetWithSimpleQuery(self): response = self.wpapi.get('media?page=2&per_page=2') # print UrlUtils.beautify_response(response) self.assertIn(response.status_code, [200,201]) response_obj = response.json() self.assertEqual(len(response_obj), 2)
def test_APIPutWithSimpleQuery(self): wcapi = API(**self.api_params) response = wcapi.get('products') first_product = (response.json())[0] # from pprint import pformat # print "first product %s" % pformat(response.json()) original_title = first_product['name'] product_id = first_product['id'] nonce = str(random.random()) response = wcapi.put('products/%s?page=2&per_page=5' % (product_id), {"name":str(nonce)}) request_params = UrlUtils.get_query_dict_singular(response.request.url) response_obj = response.json() self.assertEqual(response_obj['name'], str(nonce)) self.assertEqual(request_params['per_page'], '5') wcapi.put('products/%s' % (product_id), {"name":original_title})
class WPAPITestCasesBase(unittest.TestCase): api_params = { 'url': 'http://localhost:8083/', 'api': 'wp-json', 'version': 'wp/v2', 'consumer_key': 'tYG1tAoqjBEM', 'consumer_secret': 's91fvylVrqChwzzDbEJHEWyySYtAmlIsqqYdjka1KyVDdAyB', 'callback': 'http://127.0.0.1/oauth1_callback', 'wp_user': '******', 'wp_pass': '******', 'oauth1a_3leg': True, } def setUp(self): Auth.force_timestamp = CURRENT_TIMESTAMP Auth.force_nonce = SHITTY_NONCE self.wpapi = API(**self.api_params) # @debug_on() def test_APIGet(self): response = self.wpapi.get('users/me') self.assertIn(response.status_code, [200, 201]) response_obj = response.json() self.assertEqual(response_obj['name'], self.api_params['wp_user']) def test_APIGetWithSimpleQuery(self): response = self.wpapi.get('pages?page=2&per_page=2') self.assertIn(response.status_code, [200, 201]) response_obj = response.json() self.assertEqual(len(response_obj), 2) def test_APIPostData(self): nonce = "%f\u00ae" % random.random() content = "api test post" data = { "title": nonce, "content": content, "excerpt": content } response = self.wpapi.post('posts', data) response_obj = response.json() post_id = response_obj.get('id') self.assertEqual(response_obj.get('title').get('raw'), nonce) self.wpapi.delete('posts/%s' % post_id) def test_APIPostBadData(self): """ No excerpt so should fail to be created. """ nonce = "%f\u00ae" % random.random() data = { 'a': nonce } with self.assertRaises(UserWarning): self.wpapi.post('posts', data) def test_APIPostBadDataHandleBadStatus(self): """ Test handling explicitly a bad status code for a request. """ nonce = "%f\u00ae" % random.random() data = { 'a': nonce } response = self.wpapi.post('posts', data, handle_status_codes=[400]) self.assertEqual(response.status_code, 400) # If we don't specify a correct status code to handle we should # still expect an exception with self.assertRaises(UserWarning): self.wpapi.post('posts', data, handle_status_codes=[404]) def test_APIPostMedia(self): img_path = 'tests/data/test.jpg' with open(img_path, 'rb') as test_file: img_data = test_file.read() img_name = os.path.basename(img_path) res = self.wpapi.post( 'media', data=img_data, headers={ 'Content-Type': 'image/jpg', 'Content-Disposition' : 'attachment; filename=%s'% img_name } ) self.assertEqual(res.status_code, 201) res_obj = res.json() created_id = res_obj.get('id') self.assertTrue(created_id) uploaded_res = requests.get(res_obj.get('source_url')) # check for bug where image bytestream was quoted self.assertNotEqual(StrUtils.to_binary(uploaded_res.text[0]), b'"') self.wpapi.delete('media/%s?force=True' % created_id) def test_APIPostComplexContent(self): data = { 'content': "this content has <a href='#'>links</a>" } res = self.wpapi.post('posts', data) self.assertEqual(res.status_code, 201) res_obj = res.json() res_id= res_obj.get('id') self.assertTrue(res_id) print(res_obj) res_content = res_obj.get('content').get('raw') self.assertEqual(data.get('content'), res_content)
def test_APIGet(self): wpapi = API(**self.apiParams) response = wpapi.get('users') self.assertIn(response.status_code, [200, 201]) response_obj = response.json() self.assertEqual(response_obj[0]['name'], 'woocommerce')