diff options
Diffstat (limited to 'pydis_site/apps/content/tests')
-rw-r--r-- | pydis_site/apps/content/tests/__init__.py | 0 | ||||
-rw-r--r-- | pydis_site/apps/content/tests/helpers.py | 91 | ||||
-rw-r--r-- | pydis_site/apps/content/tests/test_utils.py | 93 | ||||
-rw-r--r-- | pydis_site/apps/content/tests/test_views.py | 184 |
4 files changed, 368 insertions, 0 deletions
diff --git a/pydis_site/apps/content/tests/__init__.py b/pydis_site/apps/content/tests/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/pydis_site/apps/content/tests/__init__.py diff --git a/pydis_site/apps/content/tests/helpers.py b/pydis_site/apps/content/tests/helpers.py new file mode 100644 index 00000000..29140375 --- /dev/null +++ b/pydis_site/apps/content/tests/helpers.py @@ -0,0 +1,91 @@ +from pyfakefs.fake_filesystem_unittest import TestCase + +# Valid markdown content with YAML metadata +MARKDOWN_WITH_METADATA = """ +--- +title: TestTitle +description: TestDescription +relevant_links: + Python Discord: https://pythondiscord.com + Discord: https://discord.com +toc: 0 +--- +# This is a header. +""" + +MARKDOWN_WITHOUT_METADATA = """#This is a header.""" + +# Valid YAML in a _info.yml file +CATEGORY_INFO = """ +title: Category Name +description: Description +""" + +# The HTML generated from the above markdown data +PARSED_HTML = ( + '<h1 id="this-is-a-header">This is a header.' + '<a class="headerlink" href="#this-is-a-header" title="Permanent link">¶</a></h1>' +) + +# The YAML metadata parsed from the above markdown data +PARSED_METADATA = { + "title": "TestTitle", "description": "TestDescription", + "relevant_links": { + "Python Discord": "https://pythondiscord.com", + "Discord": "https://discord.com" + }, + "toc": 0 +} + +# The YAML data parsed from the above _info.yml file +PARSED_CATEGORY_INFO = {"title": "Category Name", "description": "Description"} + + +class MockPagesTestCase(TestCase): + """ + TestCase with a fake filesystem for testing. + + Structure: + ├── _info.yml + ├── root.md + ├── root_without_metadata.md + ├── not_a_page.md + ├── tmp.md + ├── tmp + | ├── _info.yml + | └── category + | ├── _info.yml + | └── subcategory_without_info + └── category + ├── _info.yml + ├── with_metadata.md + └── subcategory + ├── with_metadata.md + └── without_metadata.md + """ + + def setUp(self): + """Create the fake filesystem.""" + self.setUpPyfakefs() + + self.fs.create_file("_info.yml", contents=CATEGORY_INFO) + self.fs.create_file("root.md", contents=MARKDOWN_WITH_METADATA) + self.fs.create_file("root_without_metadata.md", contents=MARKDOWN_WITHOUT_METADATA) + self.fs.create_file("not_a_page.md/_info.yml", contents=CATEGORY_INFO) + self.fs.create_file("category/_info.yml", contents=CATEGORY_INFO) + self.fs.create_file("category/with_metadata.md", contents=MARKDOWN_WITH_METADATA) + self.fs.create_file("category/subcategory/_info.yml", contents=CATEGORY_INFO) + self.fs.create_file( + "category/subcategory/with_metadata.md", contents=MARKDOWN_WITH_METADATA + ) + self.fs.create_file( + "category/subcategory/without_metadata.md", contents=MARKDOWN_WITHOUT_METADATA + ) + + # There is always a `tmp` directory in the filesystem, so make it a category + # for testing purposes. + # See: https://jmcgeheeiv.github.io/pyfakefs/release/usage.html#os-temporary-directories + self.fs.create_file("tmp/_info.yml", contents=CATEGORY_INFO) + self.fs.create_file("tmp.md", contents=MARKDOWN_WITH_METADATA) + self.fs.create_file("tmp/category/_info.yml", contents=CATEGORY_INFO) + self.fs.create_dir("tmp/category/subcategory_without_info") diff --git a/pydis_site/apps/content/tests/test_utils.py b/pydis_site/apps/content/tests/test_utils.py new file mode 100644 index 00000000..6612e44c --- /dev/null +++ b/pydis_site/apps/content/tests/test_utils.py @@ -0,0 +1,93 @@ +from pathlib import Path + +from django.http import Http404 + +from pydis_site.apps.content import utils +from pydis_site.apps.content.tests.helpers import ( + MockPagesTestCase, PARSED_CATEGORY_INFO, PARSED_HTML, PARSED_METADATA +) + + +class GetCategoryTests(MockPagesTestCase): + """Tests for the get_category function.""" + + def test_get_valid_category(self): + result = utils.get_category(Path("category")) + + self.assertEqual(result, {"title": "Category Name", "description": "Description"}) + + def test_get_nonexistent_category(self): + with self.assertRaises(Http404): + utils.get_category(Path("invalid")) + + def test_get_category_with_path_to_file(self): + # Valid categories are directories, not files + with self.assertRaises(Http404): + utils.get_category(Path("root.md")) + + def test_get_category_without_info_yml(self): + # Categories should provide an _info.yml file + with self.assertRaises(FileNotFoundError): + utils.get_category(Path("tmp/category/subcategory_without_info")) + + +class GetCategoriesTests(MockPagesTestCase): + """Tests for the get_categories function.""" + + def test_get_root_categories(self): + result = utils.get_categories(Path(".")) + + info = PARSED_CATEGORY_INFO + self.assertEqual(result, {"category": info, "tmp": info, "not_a_page.md": info}) + + def test_get_categories_with_subcategories(self): + result = utils.get_categories(Path("category")) + + self.assertEqual(result, {"subcategory": PARSED_CATEGORY_INFO}) + + def test_get_categories_without_subcategories(self): + result = utils.get_categories(Path("category/subcategory")) + + self.assertEqual(result, {}) + + +class GetCategoryPagesTests(MockPagesTestCase): + """Tests for the get_category_pages function.""" + + def test_get_pages_in_root_category_successfully(self): + """The method should successfully retrieve page metadata.""" + root_category_pages = utils.get_category_pages(Path(".")) + self.assertEqual( + root_category_pages, {"root": PARSED_METADATA, "root_without_metadata": {}} + ) + + def test_get_pages_in_subcategories_successfully(self): + """The method should successfully retrieve page metadata.""" + category_pages = utils.get_category_pages(Path("category")) + + # Page metadata is properly retrieved + self.assertEqual(category_pages, {"with_metadata": PARSED_METADATA}) + + +class GetPageTests(MockPagesTestCase): + """Tests for the get_page function.""" + + def test_get_page(self): + # TOC is a special case because the markdown converter outputs the TOC as HTML + updated_metadata = {**PARSED_METADATA, "toc": '<div class="toc">\n<ul></ul>\n</div>\n'} + cases = [ + ("Root page with metadata", "root.md", PARSED_HTML, updated_metadata), + ("Root page without metadata", "root_without_metadata.md", PARSED_HTML, {}), + ("Page with metadata", "category/with_metadata.md", PARSED_HTML, updated_metadata), + ("Page without metadata", "category/subcategory/without_metadata.md", PARSED_HTML, {}), + ] + + for msg, page_path, expected_html, expected_metadata in cases: + with self.subTest(msg=msg): + html, metadata = utils.get_page(Path(page_path)) + self.assertEqual(html, expected_html) + self.assertEqual(metadata, expected_metadata) + + def test_get_nonexistent_page_returns_404(self): + with self.assertRaises(Http404): + utils.get_page(Path("invalid")) diff --git a/pydis_site/apps/content/tests/test_views.py b/pydis_site/apps/content/tests/test_views.py new file mode 100644 index 00000000..74d38f78 --- /dev/null +++ b/pydis_site/apps/content/tests/test_views.py @@ -0,0 +1,184 @@ +from pathlib import Path +from unittest import TestCase + +from django.http import Http404 +from django.test import RequestFactory, SimpleTestCase, override_settings +from pyfakefs import fake_filesystem_unittest + +from pydis_site.apps.content.tests.helpers import ( + MockPagesTestCase, PARSED_CATEGORY_INFO, PARSED_HTML, PARSED_METADATA +) +from pydis_site.apps.content.views import PageOrCategoryView + + +# Set the module constant within Patcher to use the fake filesystem +# https://jmcgeheeiv.github.io/pyfakefs/master/usage.html#modules-to-reload +with fake_filesystem_unittest.Patcher() as _: + BASE_PATH = Path(".") + + +def patch_dispatch_attributes(view: PageOrCategoryView, location: str) -> None: + """ + Set the attributes set in the `dispatch` method manually. + + This is necessary because it is never automatically called during tests. + """ + view.location = Path(location) + + # URL location on the filesystem + view.full_location = view.location + + # Possible places to find page content information + view.category_path = view.full_location + view.page_path = view.full_location.with_suffix(".md") + + +@override_settings(CONTENT_PAGES_PATH=BASE_PATH) +class PageOrCategoryViewTests(MockPagesTestCase, SimpleTestCase, TestCase): + """Tests for the PageOrCategoryView class.""" + + def setUp(self): + """Set test helpers, then set up fake filesystem.""" + self.factory = RequestFactory() + self.view = PageOrCategoryView.as_view() + self.ViewClass = PageOrCategoryView() + super().setUp() + + # Integration tests + def test_valid_page_or_category_returns_200(self): + cases = [ + ("Page at root", "root"), + ("Category page", "category"), + ("Page in category", "category/with_metadata"), + ("Subcategory page", "category/subcategory"), + ("Page in subcategory", "category/subcategory/with_metadata"), + ] + for msg, path in cases: + with self.subTest(msg=msg, path=path): + request = self.factory.get(f"/{path}") + response = self.view(request, location=path) + self.assertEqual(response.status_code, 200) + + def test_nonexistent_page_returns_404(self): + with self.assertRaises(Http404): + request = self.factory.get("/invalid") + self.view(request, location="invalid") + + # Unit tests + def test_get_template_names_returns_correct_templates(self): + category_template = "content/listing.html" + page_template = "content/page.html" + cases = [ + ("root", page_template), + ("root_without_metadata", page_template), + ("category/with_metadata", page_template), + ("category/subcategory/with_metadata", page_template), + ("category", category_template), + ("category/subcategory", category_template), + ] + + for path, expected_template in cases: + with self.subTest(path=path, expected_template=expected_template): + patch_dispatch_attributes(self.ViewClass, path) + self.assertEqual(self.ViewClass.get_template_names(), [expected_template]) + + def test_get_template_names_with_nonexistent_paths_returns_404(self): + for path in ("invalid", "another_invalid", "nonexistent"): + with self.subTest(path=path): + patch_dispatch_attributes(self.ViewClass, path) + with self.assertRaises(Http404): + self.ViewClass.get_template_names() + + def test_get_template_names_returns_page_template_for_category_with_page(self): + """Make sure the proper page is returned for category locations with pages.""" + patch_dispatch_attributes(self.ViewClass, "tmp") + self.assertEqual(self.ViewClass.get_template_names(), ["content/page.html"]) + + def test_get_context_data_with_valid_page(self): + """The method should return required fields in the template context.""" + request = self.factory.get("/root") + self.ViewClass.setup(request) + self.ViewClass.dispatch(request, location="root") + + cases = [ + ("Context includes HTML page content", "page", PARSED_HTML), + ("Context includes page title", "page_title", PARSED_METADATA["title"]), + ( + "Context includes page description", + "page_description", + PARSED_METADATA["description"] + ), + ( + "Context includes relevant link names and URLs", + "relevant_links", + PARSED_METADATA["relevant_links"] + ), + ] + context = self.ViewClass.get_context_data() + for msg, key, expected_value in cases: + with self.subTest(msg=msg): + self.assertEqual(context[key], expected_value) + + def test_get_context_data_with_valid_category(self): + """The method should return required fields in the template context.""" + request = self.factory.get("/category") + self.ViewClass.setup(request) + self.ViewClass.dispatch(request, location="category") + + cases = [ + ( + "Context includes subcategory names and their information", + "categories", + {"subcategory": PARSED_CATEGORY_INFO} + ), + ( + "Context includes page names and their metadata", + "pages", + {"with_metadata": PARSED_METADATA} + ), + ( + "Context includes page description", + "page_description", + PARSED_CATEGORY_INFO["description"] + ), + ("Context includes page title", "page_title", PARSED_CATEGORY_INFO["title"]), + ] + + context = self.ViewClass.get_context_data() + for msg, key, expected_value in cases: + with self.subTest(msg=msg): + self.assertEqual(context[key], expected_value) + + def test_get_context_data_for_category_with_page(self): + """Make sure the proper page is returned for category locations with pages.""" + request = self.factory.get("/category") + self.ViewClass.setup(request) + self.ViewClass.dispatch(request, location="tmp") + + context = self.ViewClass.get_context_data() + expected_page_context = { + "page": PARSED_HTML, + "page_title": PARSED_METADATA["title"], + "page_description": PARSED_METADATA["description"], + "relevant_links": PARSED_METADATA["relevant_links"], + "subarticles": [{"path": "category", "name": "Category Name"}] + } + for key, expected_value in expected_page_context.items(): + with self.subTest(): + self.assertEqual(context[key], expected_value) + + def test_get_context_data_breadcrumbs(self): + """The method should return correct breadcrumbs.""" + request = self.factory.get("/category/subcategory/with_metadata") + self.ViewClass.setup(request) + self.ViewClass.dispatch(request, location="category/subcategory/with_metadata") + + context = self.ViewClass.get_context_data() + self.assertEquals( + context["breadcrumb_items"], + [ + {"name": PARSED_CATEGORY_INFO["title"], "path": "."}, + {"name": PARSED_CATEGORY_INFO["title"], "path": "category"}, + {"name": PARSED_CATEGORY_INFO["title"], "path": "category/subcategory"}, + ] + ) |