Commit 2bfb6b9c authored by Clemens Wolff's avatar Clemens Wolff

Make file-admin test easier to extend

Currently the unit tests for file-admin are tied to the local
implementation. However, all other implementations of the file-admin
(e..g S3) should maintain the same invariants as the local one. As such,
this change makes it easier to copy the local file-admin tests for other
storage backends.
parent 6f570e33
import os.path as op import os.path as op
import unittest
from nose.tools import eq_, ok_ from nose.tools import eq_, ok_
...@@ -14,21 +15,30 @@ except ImportError: ...@@ -14,21 +15,30 @@ except ImportError:
from io import StringIO from io import StringIO
def create_view(): class Base:
app, admin = setup() class FileAdminTests(unittest.TestCase):
_test_files_root = op.join(op.dirname(__file__), 'files')
class MyFileAdmin(fileadmin.FileAdmin): def fileadmin_class(self):
editable_extensions = ('txt',) raise NotImplementedError
path = op.join(op.dirname(__file__), 'files') def fileadmin_args(self):
view = MyFileAdmin(path, '/files/', name='Files') raise NotImplementedError
admin.add_view(view)
def test_file_admin(self):
fileadmin_class = self.fileadmin_class()
fileadmin_args, fileadmin_kwargs = self.fileadmin_args()
return app, admin, view app, admin = setup()
class MyFileAdmin(fileadmin_class):
editable_extensions = ('txt',)
view_kwargs = dict(fileadmin_kwargs)
view_kwargs.setdefault('name', 'Files')
view = MyFileAdmin(*fileadmin_args, **view_kwargs)
def test_file_admin(): admin.add_view(view)
app, admin, view = create_view()
client = app.test_client() client = app.test_client()
...@@ -42,9 +52,8 @@ def test_file_admin(): ...@@ -42,9 +52,8 @@ def test_file_admin():
eq_(rv.status_code, 200) eq_(rv.status_code, 200)
ok_('dummy.txt' in rv.data.decode('utf-8')) ok_('dummy.txt' in rv.data.decode('utf-8'))
rv = client.post('/admin/myfileadmin/edit/?path=dummy.txt', data=dict( rv = client.post('/admin/myfileadmin/edit/?path=dummy.txt',
content='new_string' data=dict(content='new_string'))
))
eq_(rv.status_code, 302) eq_(rv.status_code, 302)
rv = client.get('/admin/myfileadmin/edit/?path=dummy.txt') rv = client.get('/admin/myfileadmin/edit/?path=dummy.txt')
...@@ -57,10 +66,9 @@ def test_file_admin(): ...@@ -57,10 +66,9 @@ def test_file_admin():
eq_(rv.status_code, 200) eq_(rv.status_code, 200)
ok_('dummy.txt' in rv.data.decode('utf-8')) ok_('dummy.txt' in rv.data.decode('utf-8'))
rv = client.post('/admin/myfileadmin/rename/?path=dummy.txt', data=dict( rv = client.post('/admin/myfileadmin/rename/?path=dummy.txt',
name='dummy_renamed.txt', data=dict(name='dummy_renamed.txt',
path='dummy.txt' path='dummy.txt'))
))
eq_(rv.status_code, 302) eq_(rv.status_code, 302)
rv = client.get('/admin/myfileadmin/') rv = client.get('/admin/myfileadmin/')
...@@ -72,9 +80,8 @@ def test_file_admin(): ...@@ -72,9 +80,8 @@ def test_file_admin():
rv = client.get('/admin/myfileadmin/upload/') rv = client.get('/admin/myfileadmin/upload/')
eq_(rv.status_code, 200) eq_(rv.status_code, 200)
rv = client.post('/admin/myfileadmin/upload/', data=dict( rv = client.post('/admin/myfileadmin/upload/',
upload=(StringIO(""), 'dummy.txt'), data=dict(upload=(StringIO(""), 'dummy.txt')))
))
eq_(rv.status_code, 302) eq_(rv.status_code, 302)
rv = client.get('/admin/myfileadmin/') rv = client.get('/admin/myfileadmin/')
...@@ -83,9 +90,8 @@ def test_file_admin(): ...@@ -83,9 +90,8 @@ def test_file_admin():
ok_('path=dummy_renamed.txt' in rv.data.decode('utf-8')) ok_('path=dummy_renamed.txt' in rv.data.decode('utf-8'))
# delete # delete
rv = client.post('/admin/myfileadmin/delete/', data=dict( rv = client.post('/admin/myfileadmin/delete/',
path='dummy_renamed.txt' data=dict(path='dummy_renamed.txt'))
))
eq_(rv.status_code, 302) eq_(rv.status_code, 302)
rv = client.get('/admin/myfileadmin/') rv = client.get('/admin/myfileadmin/')
...@@ -97,9 +103,8 @@ def test_file_admin(): ...@@ -97,9 +103,8 @@ def test_file_admin():
rv = client.get('/admin/myfileadmin/mkdir/') rv = client.get('/admin/myfileadmin/mkdir/')
eq_(rv.status_code, 200) eq_(rv.status_code, 200)
rv = client.post('/admin/myfileadmin/mkdir/', data=dict( rv = client.post('/admin/myfileadmin/mkdir/',
name='dummy_dir' data=dict(name='dummy_dir'))
))
eq_(rv.status_code, 302) eq_(rv.status_code, 302)
rv = client.get('/admin/myfileadmin/') rv = client.get('/admin/myfileadmin/')
...@@ -112,10 +117,9 @@ def test_file_admin(): ...@@ -112,10 +117,9 @@ def test_file_admin():
eq_(rv.status_code, 200) eq_(rv.status_code, 200)
ok_('dummy_dir' in rv.data.decode('utf-8')) ok_('dummy_dir' in rv.data.decode('utf-8'))
rv = client.post('/admin/myfileadmin/rename/?path=dummy_dir', data=dict( rv = client.post('/admin/myfileadmin/rename/?path=dummy_dir',
name='dummy_renamed_dir', data=dict(name='dummy_renamed_dir',
path='dummy_dir' path='dummy_dir'))
))
eq_(rv.status_code, 302) eq_(rv.status_code, 302)
rv = client.get('/admin/myfileadmin/') rv = client.get('/admin/myfileadmin/')
...@@ -124,9 +128,8 @@ def test_file_admin(): ...@@ -124,9 +128,8 @@ def test_file_admin():
ok_('path=dummy_dir' not in rv.data.decode('utf-8')) ok_('path=dummy_dir' not in rv.data.decode('utf-8'))
# delete - directory # delete - directory
rv = client.post('/admin/myfileadmin/delete/', data=dict( rv = client.post('/admin/myfileadmin/delete/',
path='dummy_renamed_dir' data=dict(path='dummy_renamed_dir'))
))
eq_(rv.status_code, 302) eq_(rv.status_code, 302)
rv = client.get('/admin/myfileadmin/') rv = client.get('/admin/myfileadmin/')
...@@ -134,30 +137,37 @@ def test_file_admin(): ...@@ -134,30 +137,37 @@ def test_file_admin():
ok_('path=dummy_renamed_dir' not in rv.data.decode('utf-8')) ok_('path=dummy_renamed_dir' not in rv.data.decode('utf-8'))
ok_('path=dummy.txt' in rv.data.decode('utf-8')) ok_('path=dummy.txt' in rv.data.decode('utf-8'))
def test_modal_edit(self):
def test_modal_edit():
# bootstrap 2 - test edit_modal # bootstrap 2 - test edit_modal
app_bs2 = Flask(__name__) app_bs2 = Flask(__name__)
admin_bs2 = Admin(app_bs2, template_mode="bootstrap2") admin_bs2 = Admin(app_bs2, template_mode="bootstrap2")
class EditModalOn(fileadmin.FileAdmin): fileadmin_class = self.fileadmin_class()
fileadmin_args, fileadmin_kwargs = self.fileadmin_args()
class EditModalOn(fileadmin_class):
edit_modal = True edit_modal = True
editable_extensions = ('txt',) editable_extensions = ('txt',)
class EditModalOff(fileadmin.FileAdmin): class EditModalOff(fileadmin_class):
edit_modal = False edit_modal = False
editable_extensions = ('txt',) editable_extensions = ('txt',)
path = op.join(op.dirname(__file__), 'files') on_view_kwargs = dict(fileadmin_kwargs)
edit_modal_on = EditModalOn(path, '/files/', endpoint='edit_modal_on') on_view_kwargs.setdefault('endpoint', 'edit_modal_on')
edit_modal_off = EditModalOff(path, '/files/', endpoint='edit_modal_off') edit_modal_on = EditModalOn(*fileadmin_args, **on_view_kwargs)
off_view_kwargs = dict(fileadmin_kwargs)
off_view_kwargs.setdefault('endpoint', 'edit_modal_off')
edit_modal_off = EditModalOff(*fileadmin_args, **off_view_kwargs)
admin_bs2.add_view(edit_modal_on) admin_bs2.add_view(edit_modal_on)
admin_bs2.add_view(edit_modal_off) admin_bs2.add_view(edit_modal_off)
client_bs2 = app_bs2.test_client() client_bs2 = app_bs2.test_client()
# bootstrap 2 - ensure modal window is added when edit_modal is enabled # bootstrap 2 - ensure modal window is added when edit_modal is
# enabled
rv = client_bs2.get('/admin/edit_modal_on/') rv = client_bs2.get('/admin/edit_modal_on/')
eq_(rv.status_code, 200) eq_(rv.status_code, 200)
data = rv.data.decode('utf-8') data = rv.data.decode('utf-8')
...@@ -178,7 +188,8 @@ def test_modal_edit(): ...@@ -178,7 +188,8 @@ def test_modal_edit():
client_bs3 = app_bs3.test_client() client_bs3 = app_bs3.test_client()
# bootstrap 3 - ensure modal window is added when edit_modal is enabled # bootstrap 3 - ensure modal window is added when edit_modal is
# enabled
rv = client_bs3.get('/admin/edit_modal_on/') rv = client_bs3.get('/admin/edit_modal_on/')
eq_(rv.status_code, 200) eq_(rv.status_code, 200)
data = rv.data.decode('utf-8') data = rv.data.decode('utf-8')
...@@ -189,3 +200,11 @@ def test_modal_edit(): ...@@ -189,3 +200,11 @@ def test_modal_edit():
eq_(rv.status_code, 200) eq_(rv.status_code, 200)
data = rv.data.decode('utf-8') data = rv.data.decode('utf-8')
ok_('fa_modal_window' not in data) ok_('fa_modal_window' not in data)
class LocalFileAdminTests(Base.FileAdminTests):
def fileadmin_class(self):
return fileadmin.FileAdmin
def fileadmin_args(self):
return (self._test_files_root, '/files'), {}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment