From 053e3d96a309e3d46515ddfb6c788b37d75aa4f5 Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Sat, 7 Mar 2020 10:08:23 -0600 Subject: [PATCH] attempt to load resolved journal files one step closer to actual functionality, attempt to load the resolved markdown file in the instance directory, or 404 if it doesn't exist --- incorporealcms/journal.py | 20 ++++++++++++++++++-- tests/conftest.py | 20 ++++++++++++++++++++ tests/instance/config.py | 21 +++++++++++++++++++++ tests/instance/journal/index.md | 3 +++ tests/test_journal.py | 10 ++++++++++ 5 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 tests/conftest.py create mode 100644 tests/instance/config.py create mode 100644 tests/instance/journal/index.md diff --git a/incorporealcms/journal.py b/incorporealcms/journal.py index 9777026..1ebb2c9 100644 --- a/incorporealcms/journal.py +++ b/incorporealcms/journal.py @@ -1,5 +1,9 @@ """Journal functionality.""" -from flask import Blueprint +import logging + +from flask import Blueprint, abort, current_app as app + +logger = logging.getLogger(__name__) bp = Blueprint('journal', __name__, url_prefix='/') @@ -7,7 +11,19 @@ bp = Blueprint('journal', __name__, url_prefix='/') @bp.route('/', defaults={'path': 'index'}) @bp.route('/') def display_journal_entry(path): - return path + """Get the file contents of the requested path and render the file.""" + resolved_path = journal_file_resolver(path) + logger.info("received request for path '%s', resolved to '%s'", path, resolved_path) + try: + with app.open_instance_resource(resolved_path, 'r') as entry_file: + logger.debug("file '%s' found", resolved_path) + except FileNotFoundError: + logger.warning("requested path '%s' (resolved path '%s') not found!", path, resolved_path) + abort(404) + else: + return "OK" + + return resolved_path def journal_file_resolver(path): diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..b24d937 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,20 @@ +"""Create the test app and other fixtures.""" +import os + +import pytest + +from incorporealcms import create_app + +HERE = os.path.dirname(os.path.abspath(__file__)) + + +@pytest.fixture +def app(): + app = create_app(instance_path=os.path.join(HERE, 'instance')) + + yield app + + +@pytest.fixture +def client(app): + return app.test_client() diff --git a/tests/instance/config.py b/tests/instance/config.py new file mode 100644 index 0000000..f46a131 --- /dev/null +++ b/tests/instance/config.py @@ -0,0 +1,21 @@ +LOGGING = { + 'version': 1, + 'formatters': { + 'default': { + 'format': '[%(asctime)s %(levelname)-7s %(name)s] %(message)s', + }, + }, + 'handlers': { + 'console': { + 'level': 'DEBUG', + 'class': 'logging.StreamHandler', + 'formatter': 'default', + }, + }, + 'loggers': { + '': { + 'level': 'DEBUG', + 'handlers': ['console'], + }, + }, +} diff --git a/tests/instance/journal/index.md b/tests/instance/journal/index.md new file mode 100644 index 0000000..f02eb3f --- /dev/null +++ b/tests/instance/journal/index.md @@ -0,0 +1,3 @@ +# test index + +this is some test content diff --git a/tests/test_journal.py b/tests/test_journal.py index 9b02d58..a261802 100644 --- a/tests/test_journal.py +++ b/tests/test_journal.py @@ -12,3 +12,13 @@ def test_journal_file_resolver_subdir_to_index(): def test_journal_file_resolver_other_requests_fine(): assert journal_file_resolver('foo/baz') == 'journal/foo/baz.md' + + +def test_journal_file_that_exists(client): + response = client.get('/') + assert response.status_code == 200 + + +def test_journal_file_that_doesnt_exist(client): + response = client.get('/ohuesthaoeusth') + assert response.status_code == 404