Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Pascal MOLIN
lektor-admin-extra
Commits
03434cb3
Commit
03434cb3
authored
Jan 07, 2021
by
Pascal
Browse files
test buttons
parent
211ca714
Changes
3
Hide whitespace changes
Inline
Side-by-side
lektor_admin_extra.py
View file @
03434cb3
...
...
@@ -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
button
s
(
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
)
test/conftest.py
View file @
03434cb3
...
...
@@ -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
...
...
test/test_lektor_admin_extra.py
View file @
03434cb3
...
...
@@ -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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment