Browse Source

[FIX] keychain: Avoid crash upon warning (#1225)

pull/1231/head
Alexis de Lattre 7 years ago
committed by Pedro M. Baeza
parent
commit
4b28387685
  1. 10
      keychain/models/keychain.py
  2. 16
      keychain/tests/test_keychain.py

10
keychain/models/keychain.py

@ -7,7 +7,7 @@ import logging
import json import json
from odoo import models, fields, api from odoo import models, fields, api
from odoo.exceptions import ValidationError
from odoo.exceptions import ValidationError, UserError
from odoo.tools.config import config from odoo.tools.config import config
from odoo.tools.translate import _ from odoo.tools.translate import _
@ -66,8 +66,8 @@ class KeychainAccount(models.Model):
"""Password in clear text.""" """Password in clear text."""
try: try:
return self._decode_password(self.password) return self._decode_password(self.password)
except Warning as warn:
raise Warning(_(
except UserError as warn:
raise UserError(_(
"%s \n" "%s \n"
"Account: %s %s %s " % ( "Account: %s %s %s " % (
warn, warn,
@ -161,7 +161,7 @@ class KeychainAccount(models.Model):
try: try:
return unicode(cipher.decrypt(str(data)), 'UTF-8') return unicode(cipher.decrypt(str(data)), 'UTF-8')
except InvalidToken: except InvalidToken:
raise Warning(_(
raise UserError(_(
"Password has been encrypted with a different " "Password has been encrypted with a different "
"key. Unless you can recover the previous key, " "key. Unless you can recover the previous key, "
"this password is unreadable." "this password is unreadable."
@ -195,7 +195,7 @@ class KeychainAccount(models.Model):
envs = cls._retrieve_env() # ex: ('dev', False) envs = cls._retrieve_env() # ex: ('dev', False)
keys = _get_keys(envs) keys = _get_keys(envs)
if len(keys) == 0: if len(keys) == 0:
raise Warning(_(
raise UserError(_(
"No 'keychain_key_%s' entries found in config file. " "No 'keychain_key_%s' entries found in config file. "
"Use a key similar to: %s" % (envs[0], Fernet.generate_key()) "Use a key similar to: %s" % (envs[0], Fernet.generate_key())
)) ))

16
keychain/tests/test_keychain.py

@ -4,7 +4,7 @@
from odoo.tests.common import TransactionCase from odoo.tests.common import TransactionCase
from odoo.tools.config import config from odoo.tools.config import config
from odoo.exceptions import ValidationError
from odoo.exceptions import ValidationError, UserError
import logging import logging
@ -80,20 +80,20 @@ class TestKeychain(TransactionCase):
try: try:
account._get_password() account._get_password()
self.assertTrue(False, 'It should not work with another key') self.assertTrue(False, 'It should not work with another key')
except Warning as err:
self.assertTrue(True, 'It should raise a Warning')
except UserError as err:
self.assertTrue(True, 'It should raise a UserError')
self.assertTrue( self.assertTrue(
'has been encrypted with a diff' in str(err), 'has been encrypted with a diff' in str(err),
'It should display the right msg') 'It should display the right msg')
else: else:
self.assertTrue(False, 'It should raise a Warning')
self.assertTrue(False, 'It should raise a UserError')
def test_no_key(self): def test_no_key(self):
"""It should raise an exception when no key is set.""" """It should raise an exception when no key is set."""
account = self._create_account() account = self._create_account()
del config.options['keychain_key'] del config.options['keychain_key']
with self.assertRaises(Warning) as err:
with self.assertRaises(UserError) as err:
account.clear_password = 'aiuepr' account.clear_password = 'aiuepr'
account._inverse_set_password() account._inverse_set_password()
self.assertTrue(False, 'It should not work without key') self.assertTrue(False, 'It should not work without key')
@ -106,7 +106,7 @@ class TestKeychain(TransactionCase):
account = self._create_account() account = self._create_account()
config['keychain_key'] = "" config['keychain_key'] = ""
with self.assertRaises(Warning):
with self.assertRaises(UserError):
account.clear_password = 'aiuepr' account.clear_password = 'aiuepr'
account._inverse_set_password() account._inverse_set_password()
self.assertTrue(False, 'It should not work missing formated key') self.assertTrue(False, 'It should not work missing formated key')
@ -138,7 +138,7 @@ class TestKeychain(TransactionCase):
'abc', 'Should work with dev') 'abc', 'Should work with dev')
config['running_env'] = 'prod' config['running_env'] = 'prod'
with self.assertRaises(Warning):
with self.assertRaises(UserError):
self.assertEqual( self.assertEqual(
account._get_password(), account._get_password(),
'abc', 'Should not work with prod key') 'abc', 'Should not work with prod key')
@ -176,7 +176,7 @@ class TestKeychain(TransactionCase):
account.clear_password = 'abc' account.clear_password = 'abc'
account._inverse_set_password() account._inverse_set_password()
with self.assertRaises(Warning):
with self.assertRaises(UserError):
self.assertEqual( self.assertEqual(
account._get_password(), account._get_password(),
'abc', 'Should not work with dev') 'abc', 'Should not work with dev')

Loading…
Cancel
Save