Improve string encoding for password retrieval

- Prevent the password from displaying as b'...' in the app;
 - Use Flask's `get_data(as_test=True)` to read the data, in the tests;
 - Add test to ensure `get_password` is not returning bytes.
This commit is contained in:
Samuel Dion-Girardeau 2016-08-11 21:50:37 -04:00
parent c0ec6dbec2
commit a46fc40aa3
2 changed files with 8 additions and 1 deletions

View file

@ -31,6 +31,8 @@ def set_password(password, ttl):
def get_password(key): def get_password(key):
password = redis_client.get(key) password = redis_client.get(key)
if password is not None:
password = password.decode('utf-8')
redis_client.delete(key) redis_client.delete(key)
return password return password

View file

@ -23,6 +23,11 @@ class SnapPassTestCase(TestCase):
# Assert that we can't look this up a second time. # Assert that we can't look this up a second time.
self.assertEqual(None, snappass.get_password(key)) self.assertEqual(None, snappass.get_password(key))
def test_password_is_decoded(self):
password = "correct horse battery staple"
key = snappass.set_password(password, 30)
self.assertFalse(isinstance(snappass.get_password(key), bytes))
def test_clean_input(self): def test_clean_input(self):
# Test Bad Data # Test Bad Data
with snappass.app.test_request_context( with snappass.app.test_request_context(
@ -54,7 +59,7 @@ class SnapPassRoutesTestCase(TestCase):
password = "I like novelty kitten statues!" password = "I like novelty kitten statues!"
key = snappass.set_password(password, 30) key = snappass.set_password(password, 30)
rv = self.app.get('/{}'.format(key)) rv = self.app.get('/{}'.format(key))
self.assertIn(password, rv.data) self.assertIn(password, rv.get_data(as_text=True))
if __name__ == '__main__': if __name__ == '__main__':