From 907c689f1f581341dae3676ec01119d44d975ba2 Mon Sep 17 00:00:00 2001 From: Dave Lasley Date: Mon, 22 Aug 2016 18:09:25 -0700 Subject: [PATCH] [MIG] auto_backup: Migrate to v9 * Add self.ensure_ones * Add test coverage --- auto_backup/README.rst | 12 +++--- auto_backup/__openerp__.py | 5 +-- auto_backup/models/db_backup.py | 4 +- auto_backup/tests/test_auto_backup.py | 53 ++++++++++++++++++++++----- auto_backup/view/db_backup_view.xml | 7 ++-- 5 files changed, 59 insertions(+), 22 deletions(-) diff --git a/auto_backup/README.rst b/auto_backup/README.rst index 7579fce45..7767a2f38 100644 --- a/auto_backup/README.rst +++ b/auto_backup/README.rst @@ -70,15 +70,16 @@ manually execute the selected processes. .. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas :alt: Try me on Runbot - :target: https://runbot.odoo-community.org/runbot/149/8.0 + :target: https://runbot.odoo-community.org/runbot/149/9.0 Bug Tracker =========== -Bugs are tracked on `GitHub Issues `_. -In case of trouble, please check there if your issue has already been reported. -If you spotted it first, help us smashing it by providing a detailed and welcomed feedback -`here `_. + +Bugs are tracked on `GitHub Issues +`_. In case of trouble, please +check there if your issue has already been reported. If you spotted it first, +help us smashing it by providing a detailed and welcomed feedback. Credits ======= @@ -89,6 +90,7 @@ Contributors * Yenthe Van Ginneken * Alessio Gerace * Jairo Llopis +* Dave Lasley Maintainer ---------- diff --git a/auto_backup/__openerp__.py b/auto_backup/__openerp__.py index 69979550a..ab07274f2 100644 --- a/auto_backup/__openerp__.py +++ b/auto_backup/__openerp__.py @@ -7,7 +7,7 @@ { "name": "Database Auto-Backup", "summary": "Backups database", - "version": "8.0.1.0.1", + "version": "9.0.1.0.0", "author": ( "VanRoey.be - Yenthe Van Ginneken, Agile Business Group," " Grupo ESOC Ingeniería de Servicios," @@ -16,8 +16,7 @@ 'license': "AGPL-3", "website": "http://www.vanroey.be/applications/bedrijfsbeheer/odoo", "category": "Tools", - "depends": ['email_template'], - "demo": [], + "depends": ['mail'], "data": [ "data/backup_data.yml", "security/ir.model.access.csv", diff --git a/auto_backup/models/db_backup.py b/auto_backup/models/db_backup.py index 28653d95d..a723b3a1e 100644 --- a/auto_backup/models/db_backup.py +++ b/auto_backup/models/db_backup.py @@ -16,7 +16,7 @@ import logging _logger = logging.getLogger(__name__) try: import pysftp -except ImportError: +except ImportError: # pragma: no cover _logger.debug('Cannot import pysftp') @@ -235,6 +235,7 @@ class DbBackup(models.Model): @contextmanager def cleanup_log(self): """Log a possible cleanup failure.""" + self.ensure_one() try: _logger.info("Starting cleanup process after database backup: %s", self.name) @@ -263,6 +264,7 @@ class DbBackup(models.Model): @api.multi def sftp_connection(self): """Return a new SFTP connection with found parameters.""" + self.ensure_one() params = { "host": self.sftp_host, "username": self.sftp_user, diff --git a/auto_backup/tests/test_auto_backup.py b/auto_backup/tests/test_auto_backup.py index dac490aba..8cac9a117 100644 --- a/auto_backup/tests/test_auto_backup.py +++ b/auto_backup/tests/test_auto_backup.py @@ -2,27 +2,62 @@ # © 2015 Agile Business Group # © 2015 Alessio Gerace # © 2016 Grupo ESOC Ingeniería de Servicios, S.L.U. - Jairo Llopis +# Copyright 2016 LasLabs Inc. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). import os from datetime import datetime from openerp.tests import common +from openerp import exceptions, tools class TestsAutoBackup(common.TransactionCase): def setUp(self): super(TestsAutoBackup, self).setUp() - self.abk = self.env["db.backup"].create( - { - 'name': u'Têst backup', - } - ) + + def new_record(self, method='sftp'): + vals = { + 'name': u'Têst backup', + 'method': method, + } + if method == 'sftp': + vals.update({ + 'sftp_host': 'test_host', + 'sftp_port': '222', + 'sftp_user': 'tuser', + }) + self.vals = vals + self.env["db.backup"].create(vals) def test_local(self): """A local database is backed up.""" - filename = self.abk.filename(datetime.now()) - self.abk.action_backup() - generated_backup = [f for f in os.listdir(self.abk.folder) + rec_id = self.new_record('local') + filename = rec_id.filename(datetime.now()) + rec_id.action_backup() + generated_backup = [f for f in os.listdir(rec_id.folder) if f >= filename] - self.assertEqual(len(generated_backup), 1) + self.assertEqual(1, len(generated_backup)) + + def test_compute_name_sftp(self): + """ It should create proper SFTP URI """ + rec_id = self.new_record() + self.assertEqual( + 'sftp://%(user)@%(host):%(port)%(folder)' % { + 'user': self.vals['sftp_user'], + 'host': self.vals['sftp_host'], + 'port': self.vals['sftp_port'], + 'folder': self.vals['folder'], + }, + rec_id.name, + ) + + def test_check_folder(self): + """ It should not allow recursive backups """ + rec_id = self.new_record() + with self.assertRaises(exceptions.ValidationError): + rec_id.write({ + 'folder': '%s/another/path' % tools.config.filestore( + self.env.cr.dbname + ), + }) diff --git a/auto_backup/view/db_backup_view.xml b/auto_backup/view/db_backup_view.xml index d1f11e442..00d6012f5 100644 --- a/auto_backup/view/db_backup_view.xml +++ b/auto_backup/view/db_backup_view.xml @@ -1,6 +1,5 @@ - - + Automated Backups @@ -104,5 +103,5 @@ db.backup client_action_multi - - + +