Python 3.6 compatibility: bytes vs strings stuff

These changes mostly revolve around the change in ObjectId constructor
when running on Python 3.6. Where on 2.7 the constructor would accept
12- and 24-byte strings, now only 12-byte bytes and 24-character strings
are accepted. Good thing, but required some changes in our code.

Other changes include hashing of strings, which isn't supported, so they
are converted to bytes first, and sometimes converted back afterwards.
This commit is contained in:
2017-03-03 14:14:36 +01:00
parent 6fb58a3f26
commit a9e40ccf10
12 changed files with 62 additions and 46 deletions

View File

@@ -75,3 +75,14 @@ class LocalAuthTest(AbstractPillarTest):
'password': 'koro'})
self.assertEqual(403, resp.status_code, resp.data)
def test_hash_password(self):
from pillar.api.local_auth import hash_password
salt = b'$2b$12$cHdK4M8/yJ7SWp2Q.PYW0O'
self.assertEqual(hash_password('© 2017 je moeder™', salt),
'$2b$12$cHdK4M8/yJ7SWp2Q.PYW0OAU1gE3DIVdeehq0XIzOMM0Vp3ldPMb6')
self.assertIsInstance(hash_password('Резиновая уточка', salt), str)
# The password should be encodable as ASCII.
hash_password('Резиновая уточка', salt).encode('ascii')

View File

@@ -95,7 +95,7 @@ class NodeMoverTest(unittest.TestCase):
],
}
}
prid = ObjectId('project_dest')
prid = ObjectId(b'project_dest')
new_project = {
'_id': prid
}
@@ -124,7 +124,7 @@ class NodeMoverTest(unittest.TestCase):
],
}
}
prid = ObjectId('project_dest')
prid = ObjectId(b'project_dest')
new_project = {
'_id': prid
}

View File

@@ -146,7 +146,7 @@ class ProjectCreationTest(AbstractProjectTest):
self.assertEqual({'Prøject B'}, {p['name'] for p in proj_list['_items']})
# No access to anything for user C, should result in empty list.
self._create_user_with_token(roles={'subscriber'}, token='token-c', user_id=12 * 'c')
self._create_user_with_token(roles={'subscriber'}, token='token-c', user_id=24 * 'c')
resp = self.client.get('/api/projects',
headers={'Authorization': self.make_header('token-c')})
self.assertEqual(200, resp.status_code)
@@ -277,7 +277,7 @@ class ProjectEditTest(AbstractProjectTest):
project_url = '/api/projects/%s' % project_id
# Create test user.
self._create_user_with_token(['admin'], 'admin-token', user_id='cafef00dbeef')
self._create_user_with_token(['admin'], 'admin-token', user_id='cafef00dbeefcafef00dbeef')
# Admin user should be able to PUT.
put_project = remove_private_keys(project)
@@ -324,7 +324,7 @@ class ProjectEditTest(AbstractProjectTest):
# Create admin user that doesn't own the project, to check that
# non-owner admins can delete projects too.
self._create_user_with_token(['admin'], 'admin-token', user_id='cafef00dbeef')
self._create_user_with_token(['admin'], 'admin-token', user_id='cafef00dbeefcafef00dbeef')
# Admin user should be able to DELETE.
resp = self.client.delete(project_url,
@@ -361,7 +361,8 @@ class ProjectEditTest(AbstractProjectTest):
project_url = '/api/projects/%s' % project_id
# Create test user.
self._create_user_with_token(['subscriber'], 'mortal-token', user_id='cafef00dbeef')
self._create_user_with_token(['subscriber'], 'mortal-token',
user_id='cafef00dbeefcafef00dbeef')
# Other user should NOT be able to DELETE.
resp = self.client.delete(project_url,
@@ -451,7 +452,7 @@ class ProjectNodeAccess(AbstractProjectTest):
put_project = remove_private_keys(self.project)
# Create admin user.
self._create_user_with_token(['admin'], 'admin-token', user_id='cafef00dbeef')
self._create_user_with_token(['admin'], 'admin-token', user_id='cafef00dbeefcafef00dbeef')
# Make the project public
put_project['permissions']['world'] = ['GET'] # make public

View File

@@ -15,7 +15,7 @@ class Str2idTest(AbstractPillarTest):
self.assertEqual(ObjectId(str_id), str2id(str_id))
happy(24 * 'a')
happy(12 * 'a')
happy(12 * b'a')
happy('577e23ad98377323f74c368c')
def test_unhappy(self):
@@ -25,10 +25,11 @@ class Str2idTest(AbstractPillarTest):
self.assertRaises(BadRequest, str2id, str_id)
unhappy(13 * 'a')
unhappy(13 * b'a')
unhappy('577e23ad 8377323f74c368c')
unhappy('김치') # Kimchi
unhappy('')
unhappy('')
unhappy(b'')
unhappy(None)