165 lines
6.9 KiB
Python
165 lines
6.9 KiB
Python
"""Unit test helper methods."""
|
|
import pytest
|
|
from werkzeug.http import dump_cookie
|
|
|
|
from incorporealcms.pages import (generate_parent_navs, instance_resource_path_to_request_path, render,
|
|
request_path_to_instance_resource_path)
|
|
|
|
|
|
def test_generate_page_navs_index(app):
|
|
"""Test that the index page has navs to the root (itself)."""
|
|
with app.app_context():
|
|
assert generate_parent_navs('pages/index.md') == [('incorporeal.org', '/')]
|
|
|
|
|
|
def test_generate_page_navs_subdir_index(app):
|
|
"""Test that dir pages have navs to the root and themselves."""
|
|
with app.app_context():
|
|
assert generate_parent_navs('pages/subdir/index.md') == [('incorporeal.org', '/'), ('/subdir/', '/subdir/')]
|
|
|
|
|
|
def test_generate_page_navs_subdir_real_page(app):
|
|
"""Test that real pages have navs to the root, their parent, and themselves."""
|
|
with app.app_context():
|
|
assert generate_parent_navs('pages/subdir/page.md') == [('incorporeal.org', '/'), ('/subdir/', '/subdir/'),
|
|
('Page', '/subdir/page')]
|
|
|
|
|
|
def test_generate_page_navs_subdir_with_title_parsing_real_page(app):
|
|
"""Test that title metadata is used in the nav text."""
|
|
with app.app_context():
|
|
assert generate_parent_navs('pages/subdir-with-title/page.md') == [
|
|
('incorporeal.org', '/'),
|
|
('SUB!', '/subdir-with-title/'),
|
|
('/subdir-with-title/page', '/subdir-with-title/page')
|
|
]
|
|
|
|
|
|
def test_render_with_user_dark_theme(app):
|
|
"""Test that a request with the dark theme selected renders the dark theme."""
|
|
cookie = dump_cookie("user-style", 'dark')
|
|
with app.test_request_context(headers={'COOKIE': cookie}):
|
|
assert b'dark.css' in render('base.html').data
|
|
assert b'light.css' not in render('base.html').data
|
|
|
|
|
|
def test_render_with_user_light_theme(app):
|
|
"""Test that a request with the light theme selected renders the light theme."""
|
|
with app.test_request_context():
|
|
assert b'light.css' in render('base.html').data
|
|
assert b'dark.css' not in render('base.html').data
|
|
|
|
|
|
def test_render_with_no_user_theme(app):
|
|
"""Test that a request with no theme set renders the light theme."""
|
|
with app.test_request_context():
|
|
assert b'light.css' in render('base.html').data
|
|
assert b'dark.css' not in render('base.html').data
|
|
|
|
|
|
def test_request_path_to_instance_resource_path(app):
|
|
"""Test a normal URL request is transformed into the file path."""
|
|
with app.test_request_context():
|
|
assert request_path_to_instance_resource_path('index') == 'pages/index.md'
|
|
|
|
|
|
def test_request_path_to_instance_resource_path_direct_file(app):
|
|
"""Test a normal URL request is transformed into the file path."""
|
|
with app.test_request_context():
|
|
assert request_path_to_instance_resource_path('no-title') == 'pages/no-title.md'
|
|
|
|
|
|
def test_request_path_to_instance_resource_path_in_subdir(app):
|
|
"""Test a normal URL request is transformed into the file path."""
|
|
with app.test_request_context():
|
|
assert request_path_to_instance_resource_path('subdir/page') == 'pages/subdir/page.md'
|
|
|
|
|
|
def test_request_path_to_instance_resource_path_subdir_index(app):
|
|
"""Test a normal URL request is transformed into the file path."""
|
|
with app.test_request_context():
|
|
assert request_path_to_instance_resource_path('subdir/') == 'pages/subdir/index.md'
|
|
|
|
|
|
def test_request_path_to_instance_resource_path_relatives_walked(app):
|
|
"""Test a normal URL request is transformed into the file path."""
|
|
with app.test_request_context():
|
|
assert (request_path_to_instance_resource_path('subdir/more-subdir/../../more-metadata') ==
|
|
'pages/more-metadata.md')
|
|
|
|
|
|
def test_request_path_to_instance_resource_path_relatives_walked_indexes_work_too(app):
|
|
"""Test a normal URL request is transformed into the file path."""
|
|
with app.test_request_context():
|
|
assert request_path_to_instance_resource_path('subdir/more-subdir/../../') == 'pages/index.md'
|
|
|
|
|
|
def test_request_path_to_instance_resource_path_relatives_walked_into_subdirs_also_fine(app):
|
|
"""Test a normal URL request is transformed into the file path."""
|
|
with app.test_request_context():
|
|
assert request_path_to_instance_resource_path('subdir/more-subdir/../../subdir/page') == 'pages/subdir/page.md'
|
|
|
|
|
|
def test_request_path_to_instance_resource_path_permission_error_on_ref_above_pages(app):
|
|
"""Test that attempts to get above the base dir ("/../../foo") fail."""
|
|
with app.test_request_context():
|
|
with pytest.raises(PermissionError):
|
|
assert request_path_to_instance_resource_path('../unreachable')
|
|
|
|
|
|
def test_request_path_to_instance_resource_path_isadirectory_on_file_like_req_for_dir(app):
|
|
"""Test that a request for e.g. '/foo' when foo is a dir indicate to redirect."""
|
|
with app.test_request_context():
|
|
with pytest.raises(IsADirectoryError):
|
|
assert request_path_to_instance_resource_path('subdir')
|
|
|
|
|
|
def test_request_path_to_instance_resource_path_nonexistant_file_errors(app):
|
|
"""Test that a request for something not on disk errors."""
|
|
with app.test_request_context():
|
|
with pytest.raises(FileNotFoundError):
|
|
assert request_path_to_instance_resource_path('nthanpthpnh')
|
|
|
|
|
|
def test_request_path_to_instance_resource_path_absolute_file_errors(app):
|
|
"""Test that a request for something not on disk errors."""
|
|
with app.test_request_context():
|
|
with pytest.raises(PermissionError):
|
|
assert request_path_to_instance_resource_path('/etc/hosts')
|
|
|
|
|
|
def test_instance_resource_path_to_request_path_on_index(app):
|
|
"""Test index.md -> /."""
|
|
with app.test_request_context():
|
|
assert instance_resource_path_to_request_path('index.md') == ''
|
|
|
|
|
|
def test_instance_resource_path_to_request_path_on_page(app):
|
|
"""Test no-title.md -> no-title."""
|
|
with app.test_request_context():
|
|
assert instance_resource_path_to_request_path('no-title.md') == 'no-title'
|
|
|
|
|
|
def test_instance_resource_path_to_request_path_on_subdir(app):
|
|
"""Test subdir/index.md -> subdir/."""
|
|
with app.test_request_context():
|
|
assert instance_resource_path_to_request_path('subdir/index.md') == 'subdir/'
|
|
|
|
|
|
def test_instance_resource_path_to_request_path_on_subdir_and_page(app):
|
|
"""Test subdir/page.md -> subdir/page."""
|
|
with app.test_request_context():
|
|
assert instance_resource_path_to_request_path('subdir/page.md') == 'subdir/page'
|
|
|
|
|
|
def test_request_resource_request_root(app):
|
|
"""Test that a request can resolve to a resource and back to a request."""
|
|
with app.test_request_context():
|
|
instance_resource_path_to_request_path(request_path_to_instance_resource_path('index')) == ''
|
|
|
|
|
|
def test_request_resource_request_page(app):
|
|
"""Test that a request can resolve to a resource and back to a request."""
|
|
with app.test_request_context():
|
|
instance_resource_path_to_request_path(request_path_to_instance_resource_path('no-title')) == 'no-title'
|