# HG changeset patch
# User Denis Laxalde <denis.laxalde@logilab.fr>
# Date 1535975303 -7200
#      Mon Sep 03 13:48:23 2018 +0200
# Node ID 1f9689d9080dca062d98f798995f6c198ed8b2ce
# Parent  635090b28a664d215c4d04fcb9d6c15dfc3d20b3
Add a "testing" module with a mixin class handling s3 setup/teardown

So as to ease testing in downstream projects.

diff --git a/cubicweb_s3storage/testing.py b/cubicweb_s3storage/testing.py
new file mode 100644
--- /dev/null
+++ b/cubicweb_s3storage/testing.py
@@ -0,0 +1,22 @@
+import boto3
+from moto import mock_s3
+
+from cubicweb_s3storage.storages import S3Storage
+
+
+class S3StorageTestMixin(object):
+
+    bucket = 'test-bucket'
+
+    def setUp(self):
+        self.s3_mock = mock_s3()
+        self.s3_mock.start()
+        resource = boto3.resource('s3', region_name='somewhere')
+        self.s3_bucket = resource.create_bucket(Bucket=self.bucket)
+        self.s3_storage = S3Storage(self.bucket)
+        super(S3StorageTestMixin, self).setUp()
+
+    def tearDown(self):
+        super(S3StorageTestMixin, self).tearDown()
+        del self.s3_storage
+        self.s3_mock.stop()
diff --git a/test/test_s3storage.py b/test/test_s3storage.py
--- a/test/test_s3storage.py
+++ b/test/test_s3storage.py
@@ -10,6 +10,7 @@
 from cubicweb import Binary
 from cubicweb.server.migractions import ServerMigrationHelper
 from cubicweb_s3storage.storages import S3Storage
+from cubicweb_s3storage import testing
 
 
 def create_file(cnx, content=b'the-data'):
@@ -18,25 +19,16 @@
                              data_name=u'foo.pdf')
 
 
-class S3StorageTC(CubicWebTC):
-    bucket = 'test-bucket'
+class S3StorageTC(testing.S3StorageTestMixin, CubicWebTC):
 
     def setUp(self):
         super(S3StorageTC, self).setUp()
-        mock = mock_s3()
-        mock.start()
-        resource = boto3.resource('s3', region_name='somewhere')
-        self.s3_bucket = resource.create_bucket(Bucket=self.bucket)
-        s3_storage = S3Storage(self.bucket)
-        storages.set_attribute_storage(self.repo, 'File', 'data', s3_storage)
-        self.s3_storage = s3_storage
-        self.s3_mock = mock
+        storages.set_attribute_storage(
+            self.repo, 'File', 'data', self.s3_storage)
 
     def tearDown(self):
         super(S3StorageTC, self).tearDown()
         storages.unset_attribute_storage(self.repo, 'File', 'data')
-        del self.s3_storage
-        self.s3_mock.stop()
 
     def test_s3key_gen(self):
         with self.admin_access.client_cnx() as cnx: