def test_non_base64_key(self): """A non-base64 key should also raise a ValueError.""" with self.assertRaises(ValueError) as exc: config._validate_fernet_key('not base 64') self.assertEqual( str(exc.exception), 'Fernet key must be 32 url-safe base64-encoded bytes.')
def test_changeme(self): """A value of CHANGE should raise a ValueError.""" with self.assertRaises(ValueError) as exc: config._validate_fernet_key('CHANGEME') self.assertEqual( str(exc.exception), 'This setting must be changed from its default value.')
def test_wrong_length_key(self): """An key with wrong length should raise a ValueError.""" with self.assertRaises(ValueError) as exc: config._validate_fernet_key( 'VGhpcyBpcyBhIHRlc3Qgb2YgdGhlIHN5c3RlbS4K') self.assertEqual( str(exc.exception), 'Fernet key must be 32 url-safe base64-encoded bytes.')
def test_valid_key(self): """A valid key should be a string, even if we pass it a unicode""" key = six.text_type('gFqE6rcBXVLssjLjffsQsAa-nlm5Bg06MTKrVT9hsMA=') result = config._validate_fernet_key(key) self.assertEqual(result, key) self.assertTrue(type(result), str)
def test_valid_key(self): """A valid key should be a string, even if we pass it a unicode""" key = b'gFqE6rcBXVLssjLjffsQsAa-nlm5Bg06MTKrVT9hsMA=' result = config._validate_fernet_key(key) self.assertEqual(result, key) if six.PY2: self.assertIs(type(result), str) else: self.assertIs(type(result), bytes)
def test_valid_key_text_type(self): """Assert that we can pass a six.text_type and get the right type back.""" key = six.text_type('gFqE6rcBXVLssjLjffsQsAa-nlm5Bg06MTKrVT9hsMA=') result = config._validate_fernet_key(key) if six.PY3: # In Python 3, this will become a byte array and the equality test later will fail. # Let's encode key to assert the right thing happens there. key = key.encode('utf-8') self.assertEqual(result, key) if six.PY2: self.assertIs(type(result), str) else: self.assertIs(type(result), bytes)