diff --git a/keychain/__manifest__.py b/keychain/__manifest__.py index 657433280..0e039de42 100644 --- a/keychain/__manifest__.py +++ b/keychain/__manifest__.py @@ -4,7 +4,7 @@ { "name": "Keychain", "summary": "Store accounts and credentials", - "version": "10.0.2.0.0", + "version": "10.0.2.0.1", "category": "Uncategorized", "website": "https://akretion.com/", "author": "Akretion, Odoo Community Association (OCA)", diff --git a/keychain/models/keychain.py b/keychain/models/keychain.py index 77e82bd20..e0f78717e 100644 --- a/keychain/models/keychain.py +++ b/keychain/models/keychain.py @@ -116,12 +116,59 @@ class KeychainAccount(models.Model): @implemented_by_keychain def _validate_data(self, data): + """Ensure data is valid according to the namespace. + + How to use: + - Create a method prefixed with your namespace + - Put your validation logic inside + - Return true if data is valid for your usage + + This method will be called on write(). + If false is returned an user error will be raised. + + Example: + def _hereismynamspace_validate_data(): + return len(data.get('some_param', '') > 6) + + @params data dict + @returns boolean + """ pass + def _default_validate_data(self, data): + """Default validation. + + By default says data is always valid. + See _validata_data() for more information. + """ + return True + @implemented_by_keychain def _init_data(self): + """Initialize data field. + + How to use: + - Create a method prefixed with your namespace + - Return a dict with the keys and may be default + values your expect. + + This method will be called on write(). + + Example: + def _hereismynamspace_init_data(): + return { 'some_param': 'default_value' } + + @returns dict + """ pass + def _default_init_data(self): + """Default initialization. + + See _init_data() for more information. + """ + return {} + @staticmethod def _retrieve_env(): """Return the current environments. diff --git a/keychain/tests/test_keychain.py b/keychain/tests/test_keychain.py index 8b72bb29c..71c99afa9 100644 --- a/keychain/tests/test_keychain.py +++ b/keychain/tests/test_keychain.py @@ -79,14 +79,14 @@ class TestKeychain(TransactionCase): config['keychain_key'] = Fernet.generate_key() try: account._get_password() - self.assertTrue(False, 'It should not work with another key') + self.fail('It should not work with another key') except UserError as err: self.assertTrue(True, 'It should raise a UserError') self.assertTrue( 'has been encrypted with a diff' in str(err), 'It should display the right msg') else: - self.assertTrue(False, 'It should raise a UserError') + self.fail('It should raise a UserError') def test_no_key(self): """It should raise an exception when no key is set.""" @@ -96,7 +96,7 @@ class TestKeychain(TransactionCase): with self.assertRaises(UserError) as err: account.clear_password = 'aiuepr' account._inverse_set_password() - self.assertTrue(False, 'It should not work without key') + self.fail('It should not work without key') self.assertTrue( 'Use a key similar to' in str(err.exception), 'It should display the right msg') @@ -109,7 +109,7 @@ class TestKeychain(TransactionCase): with self.assertRaises(UserError): account.clear_password = 'aiuepr' account._inverse_set_password() - self.assertTrue(False, 'It should not work missing formated key') + self.fail('It should not work missing formated key') self.assertTrue(True, 'It shoud raise a ValueError') @@ -193,9 +193,7 @@ class TestKeychain(TransactionCase): for json in wrong_jsons: with self.assertRaises(ValidationError) as err: account.write({"data": json}) - self.assertTrue( - False, - 'Should not validate baddly formatted json') + self.fail('Should not validate baddly formatted json') self.assertTrue( 'Data should be a valid JSON' in str(err.exception), 'It should raise a ValidationError') @@ -220,4 +218,25 @@ class TestKeychain(TransactionCase): account.write({"data": json}) self.assertTrue(True, 'Should validate json') except: - self.assertTrue(False, 'It should validate a good json') + self.fail('It should validate a good json') + + def test_default_init_and_valid(self): + """.""" + self.keychain._fields['namespace'].selection.append( + ('keychain_test_default', 'test') + ) + account = self.keychain.create({ + "name": "test", + "namespace": "keychain_test_default", + "login": "test", + "technical_name": "keychain.test" + }) + try: + account.write({"login": "test default"}) + except ValidationError: + self.fail('It should validate any json in default') + + self.assertEqual( + account.data, account._serialize_data( + account._default_init_data()), + 'Data should be default value')