Browse Source

[FIX] split keychain backend model and test into 2 files

pull/1165/head
Mourad Elhadj Mimoune 7 years ago
committed by andreparames
parent
commit
d29388342e
  1. 5
      keychain/README.rst
  2. 1
      keychain/models/__init__.py
  3. 68
      keychain/models/keychain.py
  4. 74
      keychain/models/keychain_backend.py
  5. 1
      keychain/tests/__init__.py
  6. 46
      keychain/tests/test_keychain.py
  7. 82
      keychain/tests/test_keychain_backend.py

5
keychain/README.rst

@ -209,6 +209,9 @@ help us smashing it by providing a detailed and welcomed feedback.
Credits Credits
======= =======
* `Akretion <https://akretion.com>`_
Contributors Contributors
------------ ------------
@ -219,7 +222,7 @@ Funders
The development of this module has been financially supported by: The development of this module has been financially supported by:
* `Akretion <https://akretion.com>`_
* Akretion
Maintainer Maintainer
---------- ----------

1
keychain/models/__init__.py

@ -1 +1,2 @@
from . import keychain from . import keychain
from . import keychain_backend

68
keychain/models/keychain.py

@ -198,71 +198,3 @@ class KeychainAccount(models.Model):
"Use a key similar to: %s" % (envs[0], Fernet.generate_key()) "Use a key similar to: %s" % (envs[0], Fernet.generate_key())
)) ))
return MultiFernet(keys) return MultiFernet(keys)
class KeychainBackend(models.AbstractModel):
_name = 'keychain.backend'
_backend_name = None
name = fields.Char(required=True)
password = fields.Char(
compute="_compute_password",
inverse="_inverse_password",
required=True)
data = fields.Serialized(
compute="_compute_keychain",
inverse="_inverse_keychain")
def _get_technical_name(self):
return '%s,%s' % (self._name, self.id)
def _get_existing_keychain(self):
self.ensure_one()
return self.env['keychain.account'].retrieve([
('namespace', '=', self._backend_name),
('technical_name', '=', self._get_technical_name())
])
def _prepare_keychain(self):
env = config.get('running_env')
return {
'name': "%s %s" % (self.name, env),
'technical_name': self._get_technical_name(),
'namespace': self._backend_name,
'environment': env,
}
def _get_keychain_account(self):
self.ensure_one()
account = self._get_existing_keychain()
if not account:
vals = self._prepare_keychain()
account = self.env['keychain.account'].create(vals)
return account
def _inverse_password(self):
for record in self:
account = self._get_keychain_account()
if record.password and record.password != '******':
account.clear_password = record.password
def _compute_password(self):
for record in self:
account = record._get_existing_keychain()
if account and account.password:
record.password = "******"
else:
record.password = ""
def _inverse_keychain(self):
for record in self:
account = record._get_keychain_account()
account.data = account._serialize_data(record.data)
def _compute_keychain(self):
for record in self:
account = record._get_existing_keychain()
if account:
record.data = account.get_data()
else:
record.data = {}

74
keychain/models/keychain_backend.py

@ -0,0 +1,74 @@
# -*- coding: utf-8 -*-
# © 2016 Akretion Sebastien Beau
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo import models, fields
from odoo.tools.config import config
class KeychainBackend(models.AbstractModel):
_name = 'keychain.backend'
_backend_name = None
name = fields.Char(required=True)
password = fields.Char(
compute="_compute_password",
inverse="_inverse_password",
required=True)
data = fields.Serialized(
compute="_compute_keychain",
inverse="_inverse_keychain")
def _get_technical_name(self):
return '%s,%s' % (self._name, self.id)
def _get_existing_keychain(self):
self.ensure_one()
return self.env['keychain.account'].retrieve([
('namespace', '=', self._backend_name),
('technical_name', '=', self._get_technical_name())
])
def _prepare_keychain(self):
env = config.get('running_env')
return {
'name': "%s %s" % (self.name, env),
'technical_name': self._get_technical_name(),
'namespace': self._backend_name,
'environment': env,
}
def _get_keychain_account(self):
self.ensure_one()
account = self._get_existing_keychain()
if not account:
vals = self._prepare_keychain()
account = self.env['keychain.account'].create(vals)
return account
def _inverse_password(self):
for record in self:
account = self._get_keychain_account()
if record.password and record.password != '******':
account.clear_password = record.password
def _compute_password(self):
for record in self:
account = record._get_existing_keychain()
if account and account.password:
record.password = "******"
else:
record.password = ""
def _inverse_keychain(self):
for record in self:
account = record._get_keychain_account()
account.data = account._serialize_data(record.data)
def _compute_keychain(self):
for record in self:
account = record._get_existing_keychain()
if account:
record.data = account.get_data()
else:
record.data = {}

1
keychain/tests/__init__.py

@ -1 +1,2 @@
from . import test_keychain from . import test_keychain
from . import test_keychain_backend

46
keychain/tests/test_keychain.py

@ -20,7 +20,6 @@ class TestKeychain(TransactionCase):
super(TestKeychain, self).setUp() super(TestKeychain, self).setUp()
self.keychain = self.env['keychain.account'] self.keychain = self.env['keychain.account']
self.keychain_backend = self.env['keychain.backend']
config['keychain_key'] = Fernet.generate_key() config['keychain_key'] = Fernet.generate_key()
self.old_running_env = config.get('running_env', '') self.old_running_env = config.get('running_env', '')
@ -37,20 +36,12 @@ class TestKeychain(TransactionCase):
return 'c' in data return 'c' in data
keychain_clss = self.keychain.__class__ keychain_clss = self.keychain.__class__
keychain_clss._test_backend_init_data = _init_data
keychain_clss._keychain_test_init_data = _init_data keychain_clss._keychain_test_init_data = _init_data
keychain_clss._keychain_test_validate_data = _validate_data keychain_clss._keychain_test_validate_data = _validate_data
keychain_clss._test_backend_validate_data = _validate_data
keychain_backend_clss = self.keychain_backend.__class__
keychain_backend_clss._backend_name = 'test_backend'
self.keychain._fields['namespace'].selection.append( self.keychain._fields['namespace'].selection.append(
('keychain_test', 'test') ('keychain_test', 'test')
) )
self.keychain._fields['namespace'].selection.append(
('test_backend', 'test backend')
)
def tearDown(self): def tearDown(self):
config['running_env'] = self.old_running_env config['running_env'] = self.old_running_env
@ -227,40 +218,3 @@ class TestKeychain(TransactionCase):
self.assertTrue(True, 'Should validate json') self.assertTrue(True, 'Should validate json')
except: except:
self.assertTrue(False, 'It should validate a good json') self.assertTrue(False, 'It should validate a good json')
def test_keychain_bakend(self):
"""It should work with valid data."""
config['keychain_key_dev'] = Fernet.generate_key()
config['running_env'] = 'dev'
vals = {
'name': 'backend_test',
'password': 'test',
'data': '{"a": "o", "c": "b"}'
}
# we use new because keychain.backend is an abstract model
backend = self.keychain_backend.new(vals)
backend._inverse_keychain()
account = backend._get_existing_keychain()
self.assertEqual(
account.data, '{"a": "o", "c": "b"}',
'Account data is not correct')
backend._inverse_password()
self.assertTrue(account, 'Account was not created')
self.assertEqual(
account.clear_password, u'test',
'Account clear password is not correct')
self.assertEqual(backend.password, u'test')
backend._compute_password()
self.assertEqual(
backend.password, u'******', 'Backend password was not computed')
self.assertEqual(
account.name, u'backend_test dev', 'Account name is not correct')
self.assertEqual(
account.namespace, u'test_backend',
'Account namespace is not correct')
self.assertEqual(
account.environment, u'dev', 'Account environment is not correct')
self.assertEqual(
account.technical_name, '%s,%s' % (backend._name, backend.id),
'Account technical_name is not correct')

82
keychain/tests/test_keychain_backend.py

@ -0,0 +1,82 @@
# -*- coding: utf-8 -*-
# © 2016 Akretion Mourad EL HADJ MIMOUNE
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo.tests.common import TransactionCase
from odoo.tools.config import config
import logging
_logger = logging.getLogger(__name__)
try:
from cryptography.fernet import Fernet
except ImportError as err:
_logger.debug(err)
class TestKeychain(TransactionCase):
def setUp(self):
super(TestKeychain, self).setUp()
self.keychain = self.env['keychain.account']
self.keychain_backend = self.env['keychain.backend']
def _init_data(self):
return {
"c": True,
"a": "b",
"d": "",
}
def _validate_data(self, data):
return 'c' in data
keychain_clss = self.keychain.__class__
keychain_clss._test_backend_init_data = _init_data
keychain_clss._test_backend_validate_data = _validate_data
keychain_backend_clss = self.keychain_backend.__class__
keychain_backend_clss._backend_name = 'test_backend'
self.keychain._fields['namespace'].selection.append(
('test_backend', 'test backend')
)
def test_keychain_bakend(self):
"""It should work with valid data."""
config['keychain_key_dev'] = Fernet.generate_key()
config['running_env'] = 'dev'
vals = {
'name': 'backend_test',
'password': 'test',
'data': '{"a": "o", "c": "b"}'
}
# we use new because keychain.backend is an abstract model
backend = self.keychain_backend.new(vals)
backend._inverse_keychain()
account = backend._get_existing_keychain()
self.assertEqual(
account.data, '{"a": "o", "c": "b"}',
'Account data is not correct')
backend._inverse_password()
self.assertTrue(account, 'Account was not created')
self.assertEqual(
account.clear_password, u'test',
'Account clear password is not correct')
self.assertEqual(backend.password, u'test')
backend._compute_password()
self.assertEqual(
backend.password, u'******', 'Backend password was not computed')
self.assertEqual(
account.name, u'backend_test dev', 'Account name is not correct')
self.assertEqual(
account.namespace, u'test_backend',
'Account namespace is not correct')
self.assertEqual(
account.environment, u'dev', 'Account environment is not correct')
self.assertEqual(
account.technical_name, '%s,%s' % (backend._name, backend.id),
'Account technical_name is not correct')
Loading…
Cancel
Save