Example #1
0
    def test_APIPostWithUnicodeQuery(self):
        wcapi = API(**self.api_params)
        nonce = "%f\u00ae" % random.random()

        data = {
            "name": nonce,
            "type": "simple",
        }

        response = wcapi.post('products', data)
        response_obj = response.json()
        product_id = response_obj.get('id')
        self.assertEqual(response_obj.get('name'), nonce)
        wcapi.delete('products/%s' % product_id)
Example #2
0
    def test_APIPostWithBytesQuery(self):
        wcapi = API(**self.api_params)
        nonce = b"%f\xff" % random.random()

        data = {
            "name": nonce,
            "type": "simple",
        }

        response = wcapi.post('products', data)
        response_obj = response.json()
        product_id = response_obj.get('id')

        expected = StrUtils.to_text(nonce, encoding='ascii', errors='replace')

        self.assertEqual(
            response_obj.get('name'),
            expected,
        )
        wcapi.delete('products/%s' % product_id)
Example #3
0
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)