From 4dcc1c91c2c44390f10507f68ba6f72528b56efd Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Sat, 20 Feb 2021 19:09:59 -0600 Subject: [PATCH] add method to from resource path to request path --- incorporealcms/pages.py | 10 ++++++++++ tests/test_pages.py | 40 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/incorporealcms/pages.py b/incorporealcms/pages.py index 7148848..c870ca1 100644 --- a/incorporealcms/pages.py +++ b/incorporealcms/pages.py @@ -2,6 +2,7 @@ import datetime import logging import os +import re from flask import Blueprint, Markup, abort from flask import current_app as app @@ -110,6 +111,15 @@ def request_path_to_instance_resource_path(path): return absolute_resource.replace(f'{app.instance_path}{os.path.sep}', '') +def instance_resource_path_to_request_path(path): + """Reverse a (presumed to exist) disk path to the canonical path that would show up in a Flask route. + + This does not include the leading /, so aside from the root index case, this should be + bidirectional. + """ + return re.sub(r'^pages/', '', re.sub(r'.md$', '', re.sub(r'index.md$', '', path))) + + def resolve_page_file(path): """Manipulate the request path to find appropriate page file. diff --git a/tests/test_pages.py b/tests/test_pages.py index eaa8084..d3cd73a 100644 --- a/tests/test_pages.py +++ b/tests/test_pages.py @@ -2,8 +2,8 @@ import pytest from werkzeug.http import dump_cookie -from incorporealcms.pages import (generate_parent_navs, render, request_path_to_instance_resource_path, - resolve_page_file) +from incorporealcms.pages import (generate_parent_navs, instance_resource_path_to_request_path, render, + request_path_to_instance_resource_path, resolve_page_file) def test_resolve_page_file_dir_to_index(): @@ -146,3 +146,39 @@ def test_request_path_to_instance_resource_path_absolute_file_errors(app): 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'