"""Unit test helper methods.""" 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) def test_resolve_page_file_dir_to_index(): """Test that a request to a directory path results in the dir's index.md.""" assert resolve_page_file('foo/') == 'pages/foo/index.md' def test_resolve_page_file_subdir_to_index(): """Test that a request to a dir's subdir path results in the subdir's index.md.""" assert resolve_page_file('foo/bar/') == 'pages/foo/bar/index.md' def test_resolve_page_file_other_requests_fine(): """Test that a request to non-dir path results in a Markdown file.""" assert resolve_page_file('foo/baz') == 'pages/foo/baz.md' 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('/') == [('incorporeal.org', '/')] def test_generate_page_navs_alternate_index(app): """Test that the index page (as a page, not a path) also has navs only to the root (by path).""" with app.app_context(): assert generate_parent_navs('index') == [('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('subdir/') == [('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('subdir/page') == [('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('subdir-with-title/page') == [ ('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')