[ADD] base_external_system: Implement interface/adapter (#993)

* [ADD] base_external_system: Implement interface/adapter for external systems

* base_external_system: Fix OS model, add inherits, add validate

* base_external_system: Usability and private key pass

* base_external_system: Use contextmanager in adapter client

* base_external_system: Move contextmanager to interface

* base_external_system: Include contextmanager on adapter and system

* base_external_system: Unify client

* Use password widget for password field

* Add tests & security

* Fix lint

* Add plaintext note
This commit is contained in:
Dave Lasley
2017-10-20 13:30:44 -07:00
committed by Pedro M. Baeza
parent e259028623
commit 21df00169e
16 changed files with 661 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
# -*- coding: utf-8 -*-
from . import test_external_system
from . import test_external_system_adapter
from . import test_external_system_os

View File

@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
# Copyright 2017 LasLabs Inc.
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
from contextlib import contextmanager
from mock import MagicMock
from odoo.tests.common import TransactionCase
class Common(TransactionCase):
@contextmanager
def _mock_method(self, method_name, method_obj=None):
if method_obj is None:
method_obj = self.record
magic = MagicMock()
method_obj._patch_method(method_name, magic)
try:
yield magic
finally:
method_obj._revert_method(method_name)

View File

@@ -0,0 +1,54 @@
# -*- coding: utf-8 -*-
# Copyright 2017 LasLabs Inc.
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
from odoo.exceptions import UserError, ValidationError
from .common import Common
class TestExternalSystem(Common):
def setUp(self):
super(TestExternalSystem, self).setUp()
self.record = self.env.ref('base_external_system.external_system_os')
def test_get_system_types(self):
"""It should return at least the test record's interface."""
self.assertIn(
(self.record._name, self.record._description),
self.env['external.system']._get_system_types(),
)
def test_check_fingerprint_blank(self):
"""It should not allow blank fingerprints when checking enabled."""
with self.assertRaises(ValidationError):
self.record.write({
'ignore_fingerprint': False,
'fingerprint': False,
})
def test_check_fingerprint_allowed(self):
"""It should not raise a validation error if there is a fingerprint."""
self.record.write({
'ignore_fingerprint': False,
'fingerprint': 'Data',
})
self.assertTrue(True)
def test_client(self):
"""It should yield the open interface client."""
with self._mock_method('client', self.record) as magic:
with self.record.system_id.client() as client:
self.assertEqual(client, magic().__enter__())
def test_create_creates_and_assigns_interface(self):
"""It should create and assign the interface on record create."""
self.assertEqual(
self.record.interface._name, 'external.system.os',
)
def test_action_test_connection(self):
"""It should proxy to the interface connection tester."""
with self.assertRaises(UserError):
self.record.system_id.action_test_connection()

View File

@@ -0,0 +1,45 @@
# -*- coding: utf-8 -*-
# Copyright 2017 LasLabs Inc.
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
from odoo.exceptions import UserError
from .common import Common
class TestExternalSystemAdapter(Common):
def setUp(self):
super(TestExternalSystemAdapter, self).setUp()
self.system = self.env.ref('base_external_system.external_system_os')
self.record = self.env['external.system.adapter'].new({
'system_id': self.system.id,
})
def test_client_yields_client(self):
"""It should yield the client."""
with self._mock_method('external_get_client') as magic:
with self.record.client() as client:
self.assertEqual(client, magic())
def test_client_destroys_client(self):
"""It should destroy the client after use."""
with self._mock_method('external_destroy_client') as magic:
with self.record.client() as client:
self.assertFalse(magic.call_count)
magic.assert_called_once_with(client)
def test_external_get_client_ensure_one(self):
"""It should assert singletons."""
with self.assertRaises(ValueError):
self.env['external.system.adapter'].external_get_client()
def test_external_destroy_client_ensure_one(self):
"""It should assert singletons."""
with self.assertRaises(ValueError):
self.env['external.system.adapter'].external_destroy_client(None)
def test_external_test_connection(self):
"""It should raise a UserError."""
with self.assertRaises(UserError):
self.record.external_test_connection()

View File

@@ -0,0 +1,40 @@
# -*- coding: utf-8 -*-
# Copyright 2017 LasLabs Inc.
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
import os
from .common import Common
class TestExternalSystemOs(Common):
@classmethod
def setUpClass(cls):
"""Remember the working dir, just in case."""
super(TestExternalSystemOs, cls).setUpClass()
cls.working_dir = os.getcwd()
@classmethod
def tearDownClass(cls):
"""Set the working dir back to origin, just in case."""
super(TestExternalSystemOs, cls).tearDownClass()
os.chdir(cls.working_dir)
def setUp(self):
super(TestExternalSystemOs, self).setUp()
self.record = self.env.ref('base_external_system.external_system_os')
def test_external_get_client_returns_os(self):
"""It should return the Pyhton OS module."""
self.assertEqual(self.record.external_get_client(), os)
def test_external_get_client_changes_directories(self):
"""It should change to the proper directory."""
self.record.external_get_client()
self.assertEqual(os.getcwd(), self.record.remote_path)
def test_external_destroy_client_changes_directory(self):
"""It should change back to the previous working directory."""
self.record.external_destroy_client(None)
self.assertEqual(os.getcwd(), self.working_dir)