From d79646d10895b2a831f8c230d1af9efa80b9d087 Mon Sep 17 00:00:00 2001 From: Thierry Ducrest Date: Fri, 27 Oct 2017 10:45:23 +0200 Subject: [PATCH 1/2] Add mocking in Swift tests --- attachment_swift/tests/__init__.py | 2 +- attachment_swift/tests/test_mock_swift_api.py | 74 +++++++++++++++++++ .../{tests.py => test_with_swift_store.py} | 7 +- 3 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 attachment_swift/tests/test_mock_swift_api.py rename attachment_swift/tests/{tests.py => test_with_swift_store.py} (86%) diff --git a/attachment_swift/tests/__init__.py b/attachment_swift/tests/__init__.py index d8d07b2..ae2c5d6 100644 --- a/attachment_swift/tests/__init__.py +++ b/attachment_swift/tests/__init__.py @@ -1,3 +1,3 @@ # -*- coding: utf-8 -*- -from . import tests +from . import test_mock_swift_api diff --git a/attachment_swift/tests/test_mock_swift_api.py b/attachment_swift/tests/test_mock_swift_api.py new file mode 100644 index 0000000..d06c8cb --- /dev/null +++ b/attachment_swift/tests/test_mock_swift_api.py @@ -0,0 +1,74 @@ +# -*- coding: utf-8 -*- +# Copyright 2017 Camptocamp SA +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) + +import os + +from mock import patch +from odoo.addons.base.tests.test_ir_attachment import TestIrAttachment +from ..swift_uri import SwiftUri + + +class TestAttachmentSwift(TestIrAttachment): + + def setup(self): + super(TestAttachmentSwift, self).setUp() + self.env['ir.config_parameter'].set_param('ir_attachment.location', + 'swift') + + @patch('swiftclient.client') + def test_connection(self, mock_swift_client): + """ Test the connection to the store""" + os.environ['SWIFT_AUTH_URL'] = 'auth_url' + os.environ['SWIFT_ACCOUNT'] = 'account' + os.environ['SWIFT_PASSWORD'] = 'password' + os.environ['SWIFT_TENANT_NAME'] = 'tenant_name' + attachment = self.Attachment + attachment._get_swift_connection() + mock_swift_client.Connection.assert_called_once_with( + authurl=os.environ.get('SWIFT_AUTH_URL'), + user=os.environ.get('SWIFT_ACCOUNT'), + key=os.environ.get('SWIFT_PASSWORD'), + tenant_name=os.environ.get('SWIFT_TENANT_NAME'), + auth_version='2.0' + ) + + def test_store_file_on_swift(self): + """ + Test writing a file + """ + (self.env['ir.config_parameter']. + set_param('ir_attachment.location', 'swift')) + os.environ['SWIFT_AUTH_URL'] = 'auth_url' + os.environ['SWIFT_ACCOUNT'] = 'account' + os.environ['SWIFT_PASSWORD'] = 'password' + os.environ['SWIFT_TENANT_NAME'] = 'tenant_name' + container = os.environ.get('SWIFT_WRITE_CONTAINER') + attachment = self.Attachment + bin_data = self.blob1_b64.decode('base64') + with patch('swiftclient.client.Connection') as MockConnection: + conn = MockConnection.return_value + attachment.create({'name': 'a5', 'datas': self.blob1_b64}) + conn.put_object.assert_called_with( + container, + attachment._compute_checksum(bin_data), + bin_data) + + def test_delete_file_on_swift(self): + """ + Test deleting a file + """ + (self.env['ir.config_parameter']. + set_param('ir_attachment.location', 'swift')) + os.environ['SWIFT_AUTH_URL'] = 'auth_url' + os.environ['SWIFT_ACCOUNT'] = 'account' + os.environ['SWIFT_PASSWORD'] = 'password' + os.environ['SWIFT_TENANT_NAME'] = 'tenant_name' + attachment = self.Attachment + container = os.environ.get('SWIFT_WRITE_CONTAINER') + with patch('swiftclient.client.Connection') as MockConnection: + conn = MockConnection.return_value + a5 = attachment.create({'name': 'a5', 'datas': self.blob1_b64}) + uri = SwiftUri(a5.store_fname) + a5.unlink() + conn.delete_object.assert_called_with(container, uri.item()) diff --git a/attachment_swift/tests/tests.py b/attachment_swift/tests/test_with_swift_store.py similarity index 86% rename from attachment_swift/tests/tests.py rename to attachment_swift/tests/test_with_swift_store.py index e557554..61c44fd 100644 --- a/attachment_swift/tests/tests.py +++ b/attachment_swift/tests/test_with_swift_store.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Copyright 2016 Camptocamp SA +# Copyright 2017 Camptocamp SA # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) from odoo.addons.base.tests.test_ir_attachment import TestIrAttachment @@ -8,6 +8,9 @@ from swiftclient.exceptions import ClientException class TestAttachmentSwift(TestIrAttachment): + """ + Those tests are made to be run against a real Swift store (local or remote) + """ def setup(self): super(TestAttachmentSwift, self).setUp() @@ -20,6 +23,7 @@ class TestAttachmentSwift(TestIrAttachment): self.assertNotEquals(conn, False) def test_store_file_on_swift(self): + """ Test writing a file and then reading it """ (self.env['ir.config_parameter']. set_param('ir_attachment.location', 'swift')) a5 = self.Attachment.create({'name': 'a5', 'datas': self.blob1_b64}) @@ -27,6 +31,7 @@ class TestAttachmentSwift(TestIrAttachment): self.assertEquals(a5.datas, a5bis.datas) def test_delete_file_on_swift(self): + """ Create a file and then test the deletion """ (self.env['ir.config_parameter']. set_param('ir_attachment.location', 'swift')) a5 = self.Attachment.create({'name': 'a5', 'datas': self.blob1_b64}) From 79993862732828dd3f5b147dbc23860bf6d7a5cf Mon Sep 17 00:00:00 2001 From: Guewen Baconnier Date: Fri, 10 Nov 2017 08:52:53 +0100 Subject: [PATCH 2/2] Fix tests When there is no write container setup, no write/delete actions are done. --- attachment_swift/tests/test_mock_swift_api.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/attachment_swift/tests/test_mock_swift_api.py b/attachment_swift/tests/test_mock_swift_api.py index d06c8cb..9bea70d 100644 --- a/attachment_swift/tests/test_mock_swift_api.py +++ b/attachment_swift/tests/test_mock_swift_api.py @@ -43,6 +43,7 @@ class TestAttachmentSwift(TestIrAttachment): os.environ['SWIFT_ACCOUNT'] = 'account' os.environ['SWIFT_PASSWORD'] = 'password' os.environ['SWIFT_TENANT_NAME'] = 'tenant_name' + os.environ['SWIFT_WRITE_CONTAINER'] = 'my_container' container = os.environ.get('SWIFT_WRITE_CONTAINER') attachment = self.Attachment bin_data = self.blob1_b64.decode('base64') @@ -64,6 +65,8 @@ class TestAttachmentSwift(TestIrAttachment): os.environ['SWIFT_ACCOUNT'] = 'account' os.environ['SWIFT_PASSWORD'] = 'password' os.environ['SWIFT_TENANT_NAME'] = 'tenant_name' + os.environ['SWIFT_WRITE_CONTAINER'] = 'my_container' + attachment = self.Attachment container = os.environ.get('SWIFT_WRITE_CONTAINER') with patch('swiftclient.client.Connection') as MockConnection: