4
4
import pdb
5
5
import functools
6
6
import traceback
7
- from httmock import all_requests , HTTMock , urlmatch
8
7
from collections import OrderedDict
8
+ from tempfile import mkstemp
9
9
10
+ from httmock import all_requests , HTTMock , urlmatch
10
11
import wordpress
11
12
from wordpress import auth
12
13
from wordpress import __default_api_version__ , __default_api__
@@ -746,25 +747,75 @@ def test_get_request_token(self):
746
747
self .assertTrue (authentication )
747
748
748
749
with HTTMock (self .woo_authentication_mock ):
749
- access_token , access_token_secret = self .api .auth .get_request_token ()
750
- self .assertEquals (access_token , 'XXXXXXXXXXXX' )
751
- self .assertEquals (access_token_secret , 'YYYYYYYYYYYY' )
750
+ request_token , request_token_secret = self .api .auth .get_request_token ()
751
+ self .assertEquals (request_token , 'XXXXXXXXXXXX' )
752
+ self .assertEquals (request_token_secret , 'YYYYYYYYYYYY' )
753
+
754
+ def test_store_access_creds (self ):
755
+ _ , creds_store_path = mkstemp ("wp-api-python-test-store-access-creds.json" )
756
+ api = API (
757
+ url = "http://woo.test" ,
758
+ consumer_key = self .consumer_key ,
759
+ consumer_secret = self .consumer_secret ,
760
+ oauth1a_3leg = True ,
761
+ wp_user = 'test_user' ,
762
+ wp_pass = 'test_pass' ,
763
+ callback = 'http://127.0.0.1/oauth1_callback' ,
764
+ access_token = 'XXXXXXXXXXXX' ,
765
+ access_token_secret = 'YYYYYYYYYYYY' ,
766
+ creds_store = creds_store_path
767
+ )
768
+ api .auth .store_access_creds ()
769
+
770
+ with open (creds_store_path ) as creds_store_file :
771
+ self .assertEqual (
772
+ creds_store_file .read (),
773
+ '{"access_token": "XXXXXXXXXXXX", "access_token_secret": "YYYYYYYYYYYY"}'
774
+ )
775
+
776
+ def test_retrieve_access_creds (self ):
777
+ _ , creds_store_path = mkstemp ("wp-api-python-test-store-access-creds.json" )
778
+ with open (creds_store_path , 'w+' ) as creds_store_file :
779
+ creds_store_file .write ('{"access_token": "XXXXXXXXXXXX", "access_token_secret": "YYYYYYYYYYYY"}' )
780
+
781
+ api = API (
782
+ url = "http://woo.test" ,
783
+ consumer_key = self .consumer_key ,
784
+ consumer_secret = self .consumer_secret ,
785
+ oauth1a_3leg = True ,
786
+ wp_user = 'test_user' ,
787
+ wp_pass = 'test_pass' ,
788
+ callback = 'http://127.0.0.1/oauth1_callback' ,
789
+ creds_store = creds_store_path
790
+ )
791
+
792
+ api .auth .retrieve_access_creds ()
793
+
794
+ self .assertEqual (
795
+ api .auth .access_token ,
796
+ 'XXXXXXXXXXXX'
797
+ )
798
+
799
+ self .assertEqual (
800
+ api .auth .access_token_secret ,
801
+ 'YYYYYYYYYYYY'
802
+ )
752
803
753
804
# @unittest.skipIf(platform.uname()[1] != "Ich.lan", "should only work on my machine")
754
805
@unittest .skip ("Should only work on my machine" )
755
806
class WCApiTestCases (unittest .TestCase ):
807
+ """ Tests for WC API V3 """
756
808
def setUp (self ):
757
- self .apiParams = {
809
+ self .api_params = {
758
810
'url' :'http://ich.local:8888/woocommerce/' ,
759
811
'api' :'wc-api' ,
760
812
'version' :'v3' ,
761
813
'consumer_key' :'ck_0297450a41484f27184d1a8a3275f9bab5b69143' ,
762
814
'consumer_secret' :'cs_68ef2cf6a708e1c6b30bfb2a38dc948b16bf46c0' ,
763
815
}
764
816
765
- @debug_on ()
766
817
def test_APIGet (self ):
767
- wcapi = API (** self .apiParams )
818
+ wcapi = API (** self .api_params )
768
819
response = wcapi .get ('products' )
769
820
# print UrlUtils.beautify_response(response)
770
821
self .assertIn (response .status_code , [200 ,201 ])
@@ -774,9 +825,8 @@ def test_APIGet(self):
774
825
self .assertEqual (len (response_obj ['products' ]), 10 )
775
826
# print "test_APIGet", response_obj
776
827
777
- @debug_on ()
778
828
def test_APIGetWithSimpleQuery (self ):
779
- wcapi = API (** self .apiParams )
829
+ wcapi = API (** self .api_params )
780
830
response = wcapi .get ('products?page=2' )
781
831
# print UrlUtils.beautify_response(response)
782
832
self .assertIn (response .status_code , [200 ,201 ])
@@ -786,9 +836,8 @@ def test_APIGetWithSimpleQuery(self):
786
836
self .assertEqual (len (response_obj ['products' ]), 10 )
787
837
# print "test_ApiGenWithSimpleQuery", response_obj
788
838
789
- @debug_on ()
790
839
def test_APIGetWithComplexQuery (self ):
791
- wcapi = API (** self .apiParams )
840
+ wcapi = API (** self .api_params )
792
841
response = wcapi .get ('products?page=2&filter%5Blimit%5D=2' )
793
842
self .assertIn (response .status_code , [200 ,201 ])
794
843
response_obj = response .json ()
@@ -802,50 +851,100 @@ def test_APIGetWithComplexQuery(self):
802
851
self .assertEqual (len (response_obj ['products' ]), 3 )
803
852
804
853
def test_APIPutWithSimpleQuery (self ):
805
- wcapi = API (** self .apiParams )
854
+ wcapi = API (** self .api_params )
855
+ response = wcapi .get ('products' )
856
+ first_product = (response .json ())['products' ][0 ]
857
+ original_title = first_product ['title' ]
858
+ product_id = first_product ['id' ]
859
+
806
860
nonce = str (random .random ())
807
- response = wcapi .put ('products/633 ?filter%5Blimit%5D=5' , {"product" :{"title" :str (nonce )}})
861
+ response = wcapi .put ('products/%s ?filter%% 5Blimit%% 5D=5' % ( product_id ) , {"product" :{"title" :str (nonce )}})
808
862
request_params = UrlUtils .get_query_dict_singular (response .request .url )
809
- # print "\ntest_APIPutWithSimpleQuery"
810
- # print "request url", response.request.url
811
- # print "response", UrlUtils.beautify_response(response)
812
863
response_obj = response .json ()
813
- # print "response obj", response_obj
814
864
self .assertEqual (response_obj ['product' ]['title' ], str (nonce ))
815
865
self .assertEqual (request_params ['filter[limit]' ], str (5 ))
816
866
867
+ wcapi .put ('products/%s' % (product_id ), {"product" :{"title" :original_title }})
868
+
869
+ @unittest .skip ("Should only work on my machine" )
870
+ class WCApiTestCasesNew (unittest .TestCase ):
871
+ """ Tests for New WC API """
872
+ def setUp (self ):
873
+ self .api_params = {
874
+ 'url' :'http://ich.local:8888/woocommerce/' ,
875
+ 'api' :'wp-json' ,
876
+ 'version' :'wc/v2' ,
877
+ 'consumer_key' :'ck_0297450a41484f27184d1a8a3275f9bab5b69143' ,
878
+ 'consumer_secret' :'cs_68ef2cf6a708e1c6b30bfb2a38dc948b16bf46c0' ,
879
+ 'callback' :'http://127.0.0.1/oauth1_callback' ,
880
+ }
881
+
882
+ def test_APIGet (self ):
883
+ wcapi = API (** self .api_params )
884
+ per_page = 10
885
+ response = wcapi .get ('products?per_page=%d' % per_page )
886
+ self .assertIn (response .status_code , [200 ,201 ])
887
+ response_obj = response .json ()
888
+ self .assertEqual (len (response_obj ), per_page )
889
+
890
+
891
+ def test_APIPutWithSimpleQuery (self ):
892
+ wcapi = API (** self .api_params )
893
+ response = wcapi .get ('products' )
894
+ first_product = (response .json ())[0 ]
895
+ # from pprint import pformat
896
+ # print "first product %s" % pformat(response.json())
897
+ original_title = first_product ['name' ]
898
+ product_id = first_product ['id' ]
899
+
900
+ nonce = str (random .random ())
901
+ response = wcapi .put ('products/%s?filter%%5Blimit%%5D=5' % (product_id ), {"name" :str (nonce )})
902
+ request_params = UrlUtils .get_query_dict_singular (response .request .url )
903
+ response_obj = response .json ()
904
+ self .assertEqual (response_obj ['name' ], str (nonce ))
905
+ self .assertEqual (request_params ['filter[limit]' ], str (5 ))
906
+
907
+ wcapi .put ('products/%s' % (product_id ), {"name" :original_title })
908
+
909
+
910
+ # def test_APIPut(self):
911
+
912
+
817
913
# @unittest.skipIf(platform.uname()[1] != "Ich.lan", "should only work on my machine")
818
914
@unittest .skip ("Should only work on my machine" )
819
915
class WPAPITestCases (unittest .TestCase ):
820
916
def setUp (self ):
821
- self .apiParams = {
917
+ self .creds_store = '~/wc-api-creds.json'
918
+ self .api_params = {
822
919
'url' :'http://ich.local:8888/woocommerce/' ,
823
920
'api' :'wp-json' ,
824
- 'version' :'wp/v2 ' ,
825
- 'consumer_key' :'kGUDYhYPNTTq ' ,
826
- 'consumer_secret' :'44fhpRsd0yo5deHaUSTZUtHgamrKwARzV8JUgTbGu61qrI0i ' ,
921
+ 'version' :'wp/v1 ' ,
922
+ 'consumer_key' :'ox0p2NZSOja8 ' ,
923
+ 'consumer_secret' :'6Ye77tGlYgxjCexn1m7zGs0GLYmmoGXeHM82jgmw3kqffNLe ' ,
827
924
'callback' :'http://127.0.0.1/oauth1_callback' ,
828
925
'wp_user' :'woocommerce' ,
829
926
'wp_pass' :'woocommerce' ,
830
- 'oauth1a_3leg' :True
927
+ 'oauth1a_3leg' :True ,
928
+ 'creds_store' : self .creds_store
831
929
}
832
930
833
931
@debug_on ()
834
932
def test_APIGet (self ):
835
- wpapi = API (** self .apiParams )
933
+ wpapi = API (** self .api_params )
934
+ wpapi .auth .clear_stored_creds ()
836
935
response = wpapi .get ('users' )
837
936
self .assertIn (response .status_code , [200 ,201 ])
838
937
response_obj = response .json ()
839
938
self .assertEqual (response_obj [0 ]['name' ], 'woocommerce' )
840
939
841
940
def test_APIGetWithSimpleQuery (self ):
842
- wpapi = API (** self .apiParams )
843
- response = wpapi .get ('media?page=2' )
941
+ wpapi = API (** self .api_params )
942
+ response = wpapi .get ('media?page=2&per_page=2 ' )
844
943
# print UrlUtils.beautify_response(response)
845
944
self .assertIn (response .status_code , [200 ,201 ])
846
945
847
946
response_obj = response .json ()
848
- self .assertEqual (len (response_obj ), 10 )
947
+ self .assertEqual (len (response_obj ), 2 )
849
948
# print "test_ApiGenWithSimpleQuery", response_obj
850
949
851
950
0 commit comments