Commit 03434cb3 authored by Pascal's avatar Pascal
Browse files

test buttons

parent 211ca714
......@@ -41,8 +41,8 @@ def add_content(contents, extra_routes=[]):
class AdminExtraPlugin(Plugin):
name = 'admin-extra'
description = u'Add buttons on lektor admin pages.'
states = ['all', 'dash' ]
right_buttons = [ ]
right_buttons = { 'serve' : [], 'dash': [] }
bp = utilsbp
def on_setup_env(self, *args, **extra):
......@@ -57,7 +57,7 @@ class AdminExtraPlugin(Plugin):
def after_request_serve(response):
if response.mimetype == "text/html":
response.direct_passthrough = False
response.set_data(add_content(response.get_data(),self.buttons()))
response.set_data(add_content(response.get_data(),self.buttons('serve')))
return response
@dash.bp.after_request
......@@ -65,13 +65,18 @@ class AdminExtraPlugin(Plugin):
def after_request_dash(response):
if response.mimetype == "text/html":
response.direct_passthrough = False
response.set_data(add_content(response.get_data(),self.buttons()))
response.set_data(add_content(response.get_data(),self.buttons('dash')))
return response
def add_button(self, route, title, html_entity, ignore = None):
self.right_buttons.append( ((route, title, html_entity), ignore) )
def buttons(self, bp, **kwargs):
return [ b for b,f in self.right_buttons[bp] if f is None or f(**kwargs) ]
def buttons(self, **kwargs):
return [ b for b,f in self.right_buttons if f is None or f(**kwargs) ]
def add_button(self, route, title, html_entity, bp=['serve','dash'], ignore = None):
for b in bp:
self.right_buttons[b].append( ((route, title, html_entity), ignore) )
def add_serve_button(self, *args, **kwargs):
self.add_button(*args, bp=['serve'], **kwargs)
def add_dash_button(self, *args, **kwargs):
self.add_button(*args, bp=['dash'], **kwargs)
......@@ -12,6 +12,7 @@ from configparser import ConfigParser
def pytest_addoption(parser):
parser.addini('project', 'path of a the lektor test project')
parser.addini('packages', 'plugins path to add', 'pathlist')
parser.addini('port', 'http server port', default='5787')
@pytest.fixture(scope='session')
......@@ -23,7 +24,12 @@ def port(request):
return int(request.config.getini('port'))
@pytest.fixture(scope='session')
def lektorproject(project_path):
def packages(request):
return request.config.getini('packages')
@pytest.fixture(scope='session')
def lektorproject(project_path, packages):
try:
output_path = tempfile.mkdtemp()
print(output_path)
......@@ -33,8 +39,11 @@ def lektorproject(project_path):
# add local modifications
if os.path.isdir('test/site'):
shutil.copytree('test/site', output_path, dirs_exist_ok=True)
# add current plugin
shutil.copytree('.', os.path.join(output_path,'packages/lektor-admin-utils'), ignore = shutil.ignore_patterns('test'), dirs_exist_ok=True)
# add packages and current plugin
testdir = shutil.ignore_patterns('test')
for p in packages:
shutil.copytree(p, os.path.join(output_path,'packages/'), ignore = testdir, dirs_exist_ok=True)
shutil.copytree('.', os.path.join(output_path,'packages/lektor-admin-utils'), ignore = testdir, dirs_exist_ok=True)
except (OSError, IOError) as e:
pytest.exit('FATAL: could not copy test site directory. %s', e) # error
......
......@@ -8,6 +8,58 @@ def test_project(lektorproject):
print(dirnames, files)
assert os.path.exists(os.path.join(lektorproject,'packages/lektor-admin-utils'))
def test_webclient_index(webclient):
rv = webclient.get('/')
print(rv.data)
assert b'<body>' in rv.data
def test_webclient_buttons(webclient):
rv = webclient.get('/')
assert b'auth-button-div' in rv.data
# no test for plugin.bp.route, impossible to
# add route dynamically
def test_webclient_add_button(env, webclient):
from lektor.pluginsystem import get_plugin
plugin = get_plugin('admin-extra', env)
plugin.add_button('/', 'zorglub', 'H')
rv = webclient.get('/')
assert b'zorglub' in rv.data
rv = webclient.get('/blog')
assert b'zorglub' in rv.data
rv = webclient.get('/admin/edit')
assert b'zorglub' in rv.data
def test_webclient_add_serve_button(env, webclient):
from lektor.pluginsystem import get_plugin
plugin = get_plugin('admin-extra', env)
plugin.add_serve_button('/nirvana', 'albatros', 'A')
rv = webclient.get('/')
print(rv.data)
assert b'albatros' in rv.data
rv = webclient.get('/admin/edit')
print(rv.data)
assert b'albatros' not in rv.data
def test_webclient_add_dash_button(env, webclient):
from lektor.pluginsystem import get_plugin
plugin = get_plugin('admin-extra', env)
plugin.add_dash_button('/devnull', 'foobar', 'F')
rv = webclient.get('/')
print(rv.data)
assert b'foobar' not in rv.data
rv = webclient.get('/admin/edit')
print(rv.data)
assert b'foobar' in rv.data
def test_server_started(anonymous):
rv = anonymous.get('/', timeout=0.1)
assert rv.status_code == 200
......@@ -24,8 +76,4 @@ def test_help_page(anonymous):
rv = anonymous.get('/admin-pages/help', timeout=0.1)
assert '<body>' in rv.text
def test_webclient_index(webclient):
rv = webclient.get('/')
print(rv.data)
assert b'<body>' in rv.data
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