11# -*- coding: utf-8 -*-
22
33
4- from io import StringIO
4+ from io import BytesIO
55from json import loads , dumps
66from urllib .parse import urlparse
77
1414import datetime
1515import psycopg2
1616from unittest .case import skip
17+ from django_tinsel .exceptions import HttpBadRequestException
1718
1819from django .db import connection
1920from django .contrib .auth .models import AnonymousUser
@@ -96,11 +97,11 @@ def send_json_body(url, body_object, client, method, user=None):
9697 are posting form data, so you need to manually setup the parameters
9798 to override that default functionality.
9899 """
99- body_string = dumps (body_object )
100- body_stream = StringIO ( body_string )
100+ body_binary_string = dumps (body_object ). encode ( )
101+ body_stream = BytesIO ( body_binary_string )
101102 parsed_url = urlparse (url )
102103 client_params = {
103- 'CONTENT_LENGTH' : len (body_string ),
104+ 'CONTENT_LENGTH' : len (body_binary_string ),
104105 'CONTENT_TYPE' : 'application/json' ,
105106 'PATH_INFO' : _get_path (parsed_url ),
106107 'QUERY_STRING' : parsed_url [4 ],
@@ -375,8 +376,8 @@ def test_locations_plots_endpoint_max_plots_param_must_be_a_number(self):
375376 API_PFX , self .instance .url_name ))
376377 self .assertEqual (response .status_code , 400 )
377378 self .assertEqual (response .content ,
378- 'The max_plots parameter must be '
379- 'a number between 1 and 500' )
379+ b 'The max_plots parameter must be '
380+ b 'a number between 1 and 500' )
380381
381382 def test_locations_plots_max_plots_param_cannot_be_greater_than_500 (self ):
382383 response = get_signed (
@@ -385,8 +386,8 @@ def test_locations_plots_max_plots_param_cannot_be_greater_than_500(self):
385386 API_PFX , self .instance .url_name ))
386387 self .assertEqual (response .status_code , 400 )
387388 self .assertEqual (response .content ,
388- 'The max_plots parameter must be '
389- 'a number between 1 and 500' )
389+ b 'The max_plots parameter must be '
390+ b 'a number between 1 and 500' )
390391 response = get_signed (
391392 self .client ,
392393 "%s/instance/%s/locations/0,0/plots?max_plots=500" %
@@ -402,8 +403,8 @@ def test_locations_plots_endpoint_max_plots_param_cannot_be_less_than_1(
402403
403404 self .assertEqual (response .status_code , 400 )
404405 self .assertEqual (response .content ,
405- 'The max_plots parameter must be a '
406- 'number between 1 and 500' )
406+ b 'The max_plots parameter must be a '
407+ b 'number between 1 and 500' )
407408 response = get_signed (
408409 self .client ,
409410 "%s/instance/%s/locations/0,0/plots?max_plots=1" %
@@ -419,7 +420,7 @@ def test_locations_plots_endpoint_distance_param_must_be_a_number(self):
419420
420421 self .assertEqual (response .status_code , 400 )
421422 self .assertEqual (response .content ,
422- 'The distance parameter must be a number' )
423+ b 'The distance parameter must be a number' )
423424
424425 response = get_signed (
425426 self .client ,
@@ -472,7 +473,7 @@ def test_create_plot_with_tree(self):
472473 data , self .client , self .user )
473474
474475 self .assertEqual (200 , response .status_code ,
475- "Create failed:" + response .content )
476+ "Create failed:" + response .content . decode () )
476477
477478 # Assert that a plot was added
478479 self .assertEqual (plot_count + 1 , Plot .objects .count ())
@@ -509,7 +510,7 @@ def test_create_plot_with_invalid_tree_returns_400(self):
509510 self .assertEqual (400 ,
510511 response .status_code ,
511512 "Expected creating a million foot "
512- "tall tree to return 400:" + response .content )
513+ "tall tree to return 400:" + response .content . decode () )
513514
514515 body_dict = loads (response .content )
515516 self .assertTrue ('fieldErrors' in body_dict ,
@@ -548,7 +549,7 @@ def test_create_plot_with_geometry(self):
548549 data , self .client , self .user )
549550
550551 self .assertEqual (200 , response .status_code ,
551- "Create failed:" + response .content )
552+ "Create failed:" + response .content . decode () )
552553
553554 # Assert that a plot was added
554555 self .assertEqual (plot_count + 1 , Plot .objects .count ())
@@ -1334,8 +1335,8 @@ def setUp(self):
13341335 'sort_key' : 'Date' }
13351336 ]
13361337 self .instance .save ()
1337- self . instance . logo . save (Instance .test_png_name ,
1338- File ( open ( Instance .test_png_path , 'r' )) )
1338+ with open (Instance .test_png_path , 'rb' ) as f :
1339+ self . instance . logo . save ( Instance .test_png_name , f )
13391340
13401341 def test_returns_config_colors (self ):
13411342 request = sign_request_as_user (make_request (), self .user )
@@ -1568,7 +1569,7 @@ def _test_post_photo(self, path):
15681569 self .instance .url_name ,
15691570 plot_id )
15701571
1571- with open (path ) as img :
1572+ with open (path , 'rb' ) as img :
15721573 req = self .factory .post (
15731574 url , {'name' : 'afile' , 'file' : img })
15741575
@@ -1621,7 +1622,7 @@ def testUploadPhoto(self):
16211622 url = reverse ('update_user_photo' , kwargs = {'version' : 3 ,
16221623 'user_id' : peon .pk })
16231624
1624- with open (TreePhotoTest .test_jpeg_path ) as img :
1625+ with open (TreePhotoTest .test_jpeg_path , 'rb' ) as img :
16251626 req = self .factory .post (
16261627 url , {'name' : 'afile' , 'file' : img })
16271628
@@ -1648,7 +1649,7 @@ def testCanOnlyUploadAsSelf(self):
16481649 grunt = make_user (username = 'grunt' , password = 'pw' )
16491650 grunt .save ()
16501651
1651- with open (TreePhotoTest .test_jpeg_path ) as img :
1652+ with open (TreePhotoTest .test_jpeg_path , 'rb' ) as img :
16521653 req = self .factory .post (
16531654 url , {'name' : 'afile' , 'file' : img })
16541655
@@ -1883,7 +1884,7 @@ def testTimestampVoidsSignature(self):
18831884 acred = APIAccessCredential .create ()
18841885 url = ('http://testserver.com/test/blah?'
18851886 'timestamp=%%s&'
1886- 'k1=4&k2=a&access_key=%s' % acred .access_key )
1887+ 'k1=4&k2=a&access_key=%s' % acred .access_key . decode () )
18871888
18881889 curtime = datetime .datetime .now ()
18891890 invalid = curtime - datetime .timedelta (minutes = 100 )
@@ -1902,7 +1903,7 @@ def testPOSTBodyChangesSig(self):
19021903 url = "%s/i/plots/1/tree/photo" % API_PFX
19031904
19041905 def get_sig (path ):
1905- with open (path ) as img :
1906+ with open (path , 'rb' ) as img :
19061907 req = self .factory .post (
19071908 url , {'name' : 'afile' , 'file' : img })
19081909
@@ -1949,14 +1950,14 @@ def testMalformedTimestamp(self):
19491950
19501951 url = ('http://testserver.com/test/blah?'
19511952 'timestamp=%%s&'
1952- 'k1=4&k2=a&access_key=%s' % acred .access_key )
1953+ 'k1=4&k2=a&access_key=%s' % acred .access_key . decode () )
19531954
19541955 req = self .sign_and_send (url % ('%sFAIL' % timestamp ),
19551956 acred .secret_key )
19561957
19571958 self .assertEqual (req .status_code , 400 )
19581959
1959- req = self .sign_and_send (url % timestamp , acred .secret_key )
1960+ req = self .sign_and_send (url % timestamp , acred .secret_key . decode () )
19601961
19611962 self .assertRequestWasSuccess (req )
19621963
@@ -1972,7 +1973,7 @@ def testMissingAccessKey(self):
19721973
19731974 self .assertEqual (req .status_code , 400 )
19741975
1975- req = self .sign_and_send ('%s&access_key=%s' % (url , acred .access_key ),
1976+ req = self .sign_and_send ('%s&access_key=%s' % (url , acred .access_key . decode () ),
19761977 acred .secret_key )
19771978
19781979 self .assertRequestWasSuccess (req )
@@ -1987,9 +1988,8 @@ def testAuthenticatesAsUser(self):
19871988 req = self .sign_and_send ('http://testserver.com/test/blah?'
19881989 'timestamp=%s&'
19891990 'k1=4&k2=a&access_key=%s' %
1990- (timestamp , acred .access_key ),
1991- acred .secret_key )
1992-
1991+ (timestamp , acred .access_key .decode ()),
1992+ acred .secret_key .decode ())
19931993 self .assertEqual (req .user .pk , peon .pk )
19941994
19951995
@@ -2003,7 +2003,7 @@ def test_401(self):
20032003 self .assertEqual (ret .status_code , 401 )
20042004
20052005 def test_ok (self ):
2006- auth = base64 .b64encode ("jim:password" )
2006+ auth = base64 .b64encode (b "jim:password" )
20072007 withauth = {"HTTP_AUTHORIZATION" : "Basic %s" % auth }
20082008
20092009 ret = get_signed (self .client , "%s/user" % API_PFX , ** withauth )
@@ -2015,14 +2015,14 @@ def test_malformed_auth(self):
20152015 ret = get_signed (self .client , "%s/user" % API_PFX , ** withauth )
20162016 self .assertEqual (ret .status_code , 401 )
20172017
2018- auth = base64 .b64encode ("foobar" )
2018+ auth = base64 .b64encode (b "foobar" )
20192019 withauth = {"HTTP_AUTHORIZATION" : "Basic %s" % auth }
20202020
20212021 ret = get_signed (self .client , "%s/user" % API_PFX , ** withauth )
20222022 self .assertEqual (ret .status_code , 401 )
20232023
20242024 def test_bad_cred (self ):
2025- auth = base64 .b64encode ("jim:passwordz" )
2025+ auth = base64 .b64encode (b "jim:passwordz" )
20262026 withauth = {"HTTP_AUTHORIZATION" : "Basic %s" % auth }
20272027
20282028 ret = get_signed (self .client , "%s/user" % API_PFX , ** withauth )
@@ -2034,7 +2034,7 @@ def test_user_has_rep(self):
20342034 ijim .reputation = 1001
20352035 ijim .save ()
20362036
2037- auth = base64 .b64encode ("jim:password" )
2037+ auth = base64 .b64encode (b "jim:password" )
20382038 withauth = dict (list (self .sign .items ()) +
20392039 [("HTTP_AUTHORIZATION" , "Basic %s" % auth )])
20402040
0 commit comments