Exemplo n.º 1
0
 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()
Exemplo n.º 2
0
 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)
Exemplo n.º 3
0
    def test_retrieve_access_creds(self):
        _, creds_store_path = mkstemp("wp-api-python-test-store-access-creds.json")
        with open(creds_store_path, 'w+') as creds_store_file:
            creds_store_file.write('{"access_token": "XXXXXXXXXXXX", "access_token_secret": "YYYYYYYYYYYY"}')

        api = API(
            url="http://woo.test",
            consumer_key=self.consumer_key,
            consumer_secret=self.consumer_secret,
            oauth1a_3leg=True,
            wp_user='******',
            wp_pass='******',
            callback='http://127.0.0.1/oauth1_callback',
            creds_store=creds_store_path
        )

        api.auth.retrieve_access_creds()

        self.assertEqual(
            api.auth.access_token,
            'XXXXXXXXXXXX'
        )

        self.assertEqual(
            api.auth.access_token_secret,
            'YYYYYYYYYYYY'
        )
Exemplo n.º 4
0
 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)
Exemplo n.º 5
0
 def test_endpoint_url(self):
     api = API(**self.api_params)
     endpoint_url = api.requester.endpoint_url(self.endpoint)
     endpoint_url = api.auth.get_auth_url(endpoint_url, 'GET')
     self.assertEqual(
         endpoint_url,
         UrlUtils.join_components(
             [self.base_url, self.api_name, self.api_ver, self.endpoint]))
Exemplo n.º 6
0
    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)
Exemplo n.º 7
0
 def setUp(self):
     super(WPAPITestCasesBasic, self).setUp()
     self.api_params.update({
         'user_auth': True,
         'basic_auth': True,
         'query_string_auth': False,
     })
     self.wpapi = API(**self.api_params)
Exemplo n.º 8
0
 def setUp(self):
     self.consumer_key = "ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
     self.consumer_secret = "cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
     self.api = API(url="http://woo.test",
                    consumer_key=self.consumer_key,
                    consumer_secret=self.consumer_secret,
                    oauth1a_3leg=True,
                    wp_user='******',
                    wp_pass='******',
                    callback='http://127.0.0.1/oauth1_callback')
Exemplo n.º 9
0
    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)
Exemplo n.º 10
0
 def test_query_string_endpoint_url(self):
     query_string_api_params = dict(**self.api_params)
     query_string_api_params.update(dict(query_string_auth=True))
     api = API(**query_string_api_params)
     endpoint_url = api.requester.endpoint_url(self.endpoint)
     endpoint_url = api.auth.get_auth_url(endpoint_url, 'GET')
     expected_endpoint_url = '%s?consumer_key=%s&consumer_secret=%s' % (
         self.endpoint, self.consumer_key, self.consumer_secret)
     expected_endpoint_url = UrlUtils.join_components([
         self.base_url, self.api_name, self.api_ver, expected_endpoint_url
     ])
     self.assertEqual(endpoint_url, expected_endpoint_url)
     endpoint_url = api.requester.endpoint_url(self.endpoint)
     endpoint_url = api.auth.get_auth_url(endpoint_url, 'GET')
Exemplo n.º 11
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)
Exemplo n.º 12
0
 def test_APIPutWithSimpleQuery(self):
     wcapi = API(**self.apiParams)
     nonce = str(random.random())
     response = wcapi.put('products/633?filter%5Blimit%5D=5',
                          {"product": {
                              "title": str(nonce)
                          }})
     request_params = UrlUtils.get_query_dict_singular(response.request.url)
     # print "\ntest_APIPutWithSimpleQuery"
     # print "request url", response.request.url
     # print "response", UrlUtils.beautify_response(response)
     response_obj = response.json()
     # print "response obj", response_obj
     self.assertEqual(response_obj['product']['title'], str(nonce))
     self.assertEqual(request_params['filter[limit]'], str(5))
Exemplo n.º 13
0
    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}})
Exemplo n.º 14
0
    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})
Exemplo n.º 15
0
    def test_store_access_creds(self):
        _, creds_store_path = mkstemp(
            "wp-api-python-test-store-access-creds.json")
        api = API(url="http://woo.test",
                  consumer_key=self.consumer_key,
                  consumer_secret=self.consumer_secret,
                  oauth1a_3leg=True,
                  wp_user='******',
                  wp_pass='******',
                  callback='http://127.0.0.1/oauth1_callback',
                  access_token='XXXXXXXXXXXX',
                  access_token_secret='YYYYYYYYYYYY',
                  creds_store=creds_store_path)
        api.auth.store_access_creds()

        with open(creds_store_path) as creds_store_file:
            self.assertEqual(creds_store_file.read(),
                             ('{"access_token": "XXXXXXXXXXXX", '
                              '"access_token_secret": "YYYYYYYYYYYY"}'))
Exemplo n.º 16
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)
Exemplo n.º 17
0
    def setUp(self):
        self.base_url = "http://localhost:8888/wordpress/"
        self.api_name = 'wc-api'
        self.api_ver = 'v3'
        self.endpoint = 'products/99'
        self.signature_method = "HMAC-SHA1"
        self.consumer_key = "ck_681c2be361e415519dce4b65ee981682cda78bc6"
        self.consumer_secret = "cs_b11f652c39a0afd3752fc7bb0c56d60d58da5877"

        self.wcapi = API(url=self.base_url,
                         consumer_key=self.consumer_key,
                         consumer_secret=self.consumer_secret,
                         api=self.api_name,
                         version=self.api_ver,
                         signature_method=self.signature_method)

        self.rfc1_api_url = 'https://photos.example.net/'
        self.rfc1_consumer_key = 'dpf43f3p2l4k3l03'
        self.rfc1_consumer_secret = 'kd94hf93k423kf44'
        self.rfc1_oauth_token = 'hh5s93j4hdidpola'
        self.rfc1_signature_method = 'HMAC-SHA1'
        self.rfc1_callback = 'http://printer.example.com/ready'
        self.rfc1_api = API(url=self.rfc1_api_url,
                            consumer_key=self.rfc1_consumer_key,
                            consumer_secret=self.rfc1_consumer_secret,
                            api='',
                            version='',
                            callback=self.rfc1_callback,
                            wp_user='',
                            wp_pass='',
                            oauth1a_3leg=True)
        self.rfc1_request_method = 'POST'
        self.rfc1_request_target_url = 'https://photos.example.net/initiate'
        self.rfc1_request_timestamp = '137131200'
        self.rfc1_request_nonce = 'wIjqoS'
        self.rfc1_request_params = [
            ('oauth_consumer_key', self.rfc1_consumer_key),
            ('oauth_signature_method', self.rfc1_signature_method),
            ('oauth_timestamp', self.rfc1_request_timestamp),
            ('oauth_nonce', self.rfc1_request_nonce),
            ('oauth_callback', self.rfc1_callback),
        ]
        self.rfc1_request_signature = b'74KNZJeDHnMBp0EMJ9ZHt/XKycU='

        self.twitter_api_url = "https://api.twitter.com/"
        self.twitter_consumer_secret = \
            "kAcSOqF21Fu85e7zjz7ZN2U4ZRhfV3WpwPAoE3Z7kBw"
        self.twitter_consumer_key = "xvz1evFS4wEEPTGEFPHBog"
        self.twitter_signature_method = "HMAC-SHA1"
        self.twitter_api = API(
            url=self.twitter_api_url,
            consumer_key=self.twitter_consumer_key,
            consumer_secret=self.twitter_consumer_secret,
            api='',
            version='1',
            signature_method=self.twitter_signature_method,
        )

        self.twitter_method = "POST"
        self.twitter_target_url = (
            "https://api.twitter.com/1/statuses/update.json?"
            "include_entities=true")
        self.twitter_params_raw = [
            ("status", "Hello Ladies + Gentlemen, a signed OAuth request!"),
            ("include_entities", "true"),
            ("oauth_consumer_key", self.twitter_consumer_key),
            ("oauth_nonce", "kYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg"),
            ("oauth_signature_method", self.twitter_signature_method),
            ("oauth_timestamp", "1318622958"),
            ("oauth_token",
             "370773112-GmHxMAgYyLbNEtIKZeRNFsMKPR9EyMZeS9weJAEb"),
            ("oauth_version", "1.0"),
        ]
        self.twitter_param_string = (
            r"include_entities=true&"
            r"oauth_consumer_key=xvz1evFS4wEEPTGEFPHBog&"
            r"oauth_nonce=kYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg&"
            r"oauth_signature_method=HMAC-SHA1&"
            r"oauth_timestamp=1318622958&"
            r"oauth_token=370773112-GmHxMAgYyLbNEtIKZeRNFsMKPR9EyMZeS9weJAEb&"
            r"oauth_version=1.0&"
            r"status=Hello%20Ladies%20%2B%20Gentlemen%2C%20a%20"
            r"signed%20OAuth%20request%21")
        self.twitter_signature_base_string = (
            r"POST&"
            r"https%3A%2F%2Fapi.twitter.com%2F1%2Fstatuses%2Fupdate.json&"
            r"include_entities%3Dtrue%26"
            r"oauth_consumer_key%3Dxvz1evFS4wEEPTGEFPHBog%26"
            r"oauth_nonce%3DkYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg%26"
            r"oauth_signature_method%3DHMAC-SHA1%26"
            r"oauth_timestamp%3D1318622958%26"
            r"oauth_token%3D370773112-"
            r"GmHxMAgYyLbNEtIKZeRNFsMKPR9EyMZeS9weJAEb%26"
            r"oauth_version%3D1.0%26"
            r"status%3DHello%2520Ladies%2520%252B%2520Gentlemen%252C%2520"
            r"a%2520signed%2520OAuth%2520request%2521")
        self.twitter_token_secret = 'LswwdoUaIvS8ltyTt5jkRh4J50vUPVVHtR2YPi5kE'
        self.twitter_signing_key = (
            'kAcSOqF21Fu85e7zjz7ZN2U4ZRhfV3WpwPAoE3Z7kBw&'
            'LswwdoUaIvS8ltyTt5jkRh4J50vUPVVHtR2YPi5kE')
        self.twitter_oauth_signature = b'tnnArxj06cWHq44gCs1OSKk/jLY='

        self.lexev_consumer_key = 'your_app_key'
        self.lexev_consumer_secret = 'your_app_secret'
        self.lexev_callback = 'http://127.0.0.1/oauth1_callback'
        self.lexev_signature_method = 'HMAC-SHA1'
        self.lexev_version = '1.0'
        self.lexev_api = API(url='https://bitbucket.org/',
                             api='api',
                             version='1.0',
                             consumer_key=self.lexev_consumer_key,
                             consumer_secret=self.lexev_consumer_secret,
                             signature_method=self.lexev_signature_method,
                             callback=self.lexev_callback,
                             wp_user='',
                             wp_pass='',
                             oauth1a_3leg=True)
        self.lexev_request_method = 'POST'
        self.lexev_request_url = \
            'https://bitbucket.org/api/1.0/oauth/request_token'
        self.lexev_request_nonce = '27718007815082439851427366369'
        self.lexev_request_timestamp = '1427366369'
        self.lexev_request_params = [
            ('oauth_callback', self.lexev_callback),
            ('oauth_consumer_key', self.lexev_consumer_key),
            ('oauth_nonce', self.lexev_request_nonce),
            ('oauth_signature_method', self.lexev_signature_method),
            ('oauth_timestamp', self.lexev_request_timestamp),
            ('oauth_version', self.lexev_version),
        ]
        self.lexev_request_signature = b"iPdHNIu4NGOjuXZ+YCdPWaRwvJY="
        self.lexev_resource_url = (
            'https://api.bitbucket.org/1.0/repositories/st4lk/'
            'django-articles-transmeta/branches')
Exemplo n.º 18
0
    def setUp(self):
        self.base_url = "http://*****:*****@', ""),
        #     ('a2', 'r b'),
        #     ('oauth_consumer_key', '9djdj82h48djs9d2'),
        #     ('oauth_token', 'kkk9d7dh3k39sjv7'),
        #     ('oauth_signature_method', 'HMAC-SHA1'),
        #     ('oauth_timestamp', 137131201),
        #     ('oauth_nonce', '7d8f3e4a'),
        #     ('c2', ''),
        #     ('a3', '2 q')
        # ]
        # self.rfc3_params_encoded = [
        #     ('b5', r"%3D%253D"),
        #     ('a3', "a"),
        #     ('c%40', ""),
        #     ('a2', r"r%20b"),
        #     ('oauth_consumer_key', '9djdj82h48djs9d2'),
        #     ('oauth_token', 'kkk9d7dh3k39sjv7'),
        #     ('oauth_signature_method', 'HMAC-SHA1'),
        #     ('oauth_timestamp', '137131201'),
        #     ('oauth_nonce', '7d8f3e4a'),
        #     ('c2', ''),
        #     ('a3', r"2%20q")
        # ]
        # self.rfc3_params_sorted = [
        #     ('a2', r"r%20b"),
        #     # ('a3', r"2%20q"), # disallow multiple
        #     ('a3', "a"),
        #     ('b5', r"%3D%253D"),
        #     ('c%40', ""),
        #     ('c2', ''),
        #     ('oauth_consumer_key', '9djdj82h48djs9d2'),
        #     ('oauth_nonce', '7d8f3e4a'),
        #     ('oauth_signature_method', 'HMAC-SHA1'),
        #     ('oauth_timestamp', '137131201'),
        #     ('oauth_token', 'kkk9d7dh3k39sjv7'),
        # ]
        # self.rfc3_param_string = r"a2=r%20b&a3=2%20q&a3=a&b5=%3D%253D&c%40=&c2=&oauth_consumer_key=9djdj82h48djs9d2&oauth_nonce=7d8f3e4a&oauth_signature_method=HMAC-SHA1&oauth_timestamp=137131201&oauth_token=kkk9d7dh3k39sjv7"
        # self.rfc3_base_string = r"GET&http%3A%2F%2Fexample.com%2Frequest&a2%3Dr%2520b%26a3%3D2%2520q%26a3%3Da%26b5%3D%253D%25253D%26c%2540%3D%26c2%3D%26oauth_consumer_key%3D9djdj82h48djs9d2%26oauth_nonce%3D7d8f3e4a%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D137131201%26oauth_token%3Dkkk9d7dh3k39sjv7"

        # test data taken from : https://dev.twitter.com/oauth/overview/creating-signatures

        self.twitter_api_url = "https://api.twitter.com/"
        self.twitter_consumer_secret = "kAcSOqF21Fu85e7zjz7ZN2U4ZRhfV3WpwPAoE3Z7kBw"
        self.twitter_consumer_key = "xvz1evFS4wEEPTGEFPHBog"
        self.twitter_signature_method = "HMAC-SHA1"
        self.twitter_api = API(
            url=self.twitter_api_url,
            consumer_key=self.twitter_consumer_key,
            consumer_secret=self.twitter_consumer_secret,
            api='',
            version='1',
            signature_method=self.twitter_signature_method,
        )

        self.twitter_method = "POST"
        self.twitter_target_url = "https://api.twitter.com/1/statuses/update.json?include_entities=true"
        self.twitter_params_raw = [
            ("status", "Hello Ladies + Gentlemen, a signed OAuth request!"),
            ("include_entities", "true"),
            ("oauth_consumer_key", self.twitter_consumer_key),
            ("oauth_nonce", "kYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg"),
            ("oauth_signature_method", self.twitter_signature_method),
            ("oauth_timestamp", "1318622958"),
            ("oauth_token", "370773112-GmHxMAgYyLbNEtIKZeRNFsMKPR9EyMZeS9weJAEb"),
            ("oauth_version", "1.0"),
        ]
        self.twitter_param_string = r"include_entities=true&oauth_consumer_key=xvz1evFS4wEEPTGEFPHBog&oauth_nonce=kYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1318622958&oauth_token=370773112-GmHxMAgYyLbNEtIKZeRNFsMKPR9EyMZeS9weJAEb&oauth_version=1.0&status=Hello%20Ladies%20%2B%20Gentlemen%2C%20a%20signed%20OAuth%20request%21"
        self.twitter_signature_base_string = r"POST&https%3A%2F%2Fapi.twitter.com%2F1%2Fstatuses%2Fupdate.json&include_entities%3Dtrue%26oauth_consumer_key%3Dxvz1evFS4wEEPTGEFPHBog%26oauth_nonce%3DkYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1318622958%26oauth_token%3D370773112-GmHxMAgYyLbNEtIKZeRNFsMKPR9EyMZeS9weJAEb%26oauth_version%3D1.0%26status%3DHello%2520Ladies%2520%252B%2520Gentlemen%252C%2520a%2520signed%2520OAuth%2520request%2521"
        self.twitter_token_secret = 'LswwdoUaIvS8ltyTt5jkRh4J50vUPVVHtR2YPi5kE'
        self.twitter_signing_key = 'kAcSOqF21Fu85e7zjz7ZN2U4ZRhfV3WpwPAoE3Z7kBw&LswwdoUaIvS8ltyTt5jkRh4J50vUPVVHtR2YPi5kE'
        self.twitter_oauth_signature = b'tnnArxj06cWHq44gCs1OSKk/jLY='

        self.lexev_consumer_key='your_app_key'
        self.lexev_consumer_secret='your_app_secret'
        self.lexev_callback='http://127.0.0.1/oauth1_callback'
        self.lexev_signature_method='HMAC-SHA1'
        self.lexev_version='1.0'
        self.lexev_api = API(
            url='https://bitbucket.org/',
            api='api',
            version='1.0',
            consumer_key=self.lexev_consumer_key,
            consumer_secret=self.lexev_consumer_secret,
            signature_method=self.lexev_signature_method,
            callback=self.lexev_callback,
            wp_user='',
            wp_pass='',
            oauth1a_3leg=True
        )
        self.lexev_request_method='POST'
        self.lexev_request_url='https://bitbucket.org/api/1.0/oauth/request_token'
        self.lexev_request_nonce='27718007815082439851427366369'
        self.lexev_request_timestamp='1427366369'
        self.lexev_request_params=[
               ('oauth_callback',self.lexev_callback),
               ('oauth_consumer_key',self.lexev_consumer_key),
               ('oauth_nonce',self.lexev_request_nonce),
               ('oauth_signature_method',self.lexev_signature_method),
               ('oauth_timestamp',self.lexev_request_timestamp),
               ('oauth_version',self.lexev_version),
        ]
        self.lexev_request_signature=b"iPdHNIu4NGOjuXZ+YCdPWaRwvJY="
        self.lexev_resource_url='https://api.bitbucket.org/1.0/repositories/st4lk/django-articles-transmeta/branches'
Exemplo n.º 19
0
 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')
Exemplo n.º 20
0
 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'])
Exemplo n.º 21
0
 def setUp(self):
     Auth.force_timestamp = CURRENT_TIMESTAMP
     Auth.force_nonce = SHITTY_NONCE
     self.wpapi = API(**self.api_params)