aboutsummaryrefslogtreecommitdiffstats
path: root/pydis_site/apps/content/tests
diff options
context:
space:
mode:
authorGravatar ks129 <[email protected]>2020-10-04 18:55:33 +0300
committerGravatar ks129 <[email protected]>2020-10-04 18:55:33 +0300
commitaff3a89c3cec04eda096e8f27115e36108ee6286 (patch)
tree299fb51b60916e6b552fa1ac7e0e05dbef512343 /pydis_site/apps/content/tests
parentAdd more information to how to write a guide guide (diff)
Change guides system to content system
As this system will be used for more than just guides, I had to do some refactoring to match this system with plans. Basically now there isn't guides, but articles instead.
Diffstat (limited to 'pydis_site/apps/content/tests')
-rw-r--r--pydis_site/apps/content/tests/__init__.py0
-rw-r--r--pydis_site/apps/content/tests/test_content/category/_info.yml2
-rw-r--r--pydis_site/apps/content/tests/test_content/category/test3.md5
-rw-r--r--pydis_site/apps/content/tests/test_content/test.md11
-rw-r--r--pydis_site/apps/content/tests/test_content/test2.md5
-rw-r--r--pydis_site/apps/content/tests/test_utils.py122
-rw-r--r--pydis_site/apps/content/tests/test_views.py104
7 files changed, 249 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/test_content/category/_info.yml b/pydis_site/apps/content/tests/test_content/category/_info.yml
new file mode 100644
index 00000000..8311509d
--- /dev/null
+++ b/pydis_site/apps/content/tests/test_content/category/_info.yml
@@ -0,0 +1,2 @@
+name: My Category
+description: My Description
diff --git a/pydis_site/apps/content/tests/test_content/category/test3.md b/pydis_site/apps/content/tests/test_content/category/test3.md
new file mode 100644
index 00000000..bdde6188
--- /dev/null
+++ b/pydis_site/apps/content/tests/test_content/category/test3.md
@@ -0,0 +1,5 @@
+Title: Test 3
+ShortDescription: Testing 3
+Contributors: user3
+
+This is too test content, but in category.
diff --git a/pydis_site/apps/content/tests/test_content/test.md b/pydis_site/apps/content/tests/test_content/test.md
new file mode 100644
index 00000000..7a917899
--- /dev/null
+++ b/pydis_site/apps/content/tests/test_content/test.md
@@ -0,0 +1,11 @@
+Title: Test
+ShortDescription: Testing
+Contributors: user
+RelevantLinks: https://pythondiscord.com/pages/resources/guides/asking-good-questions/
+ https://pythondiscord.com/pages/resources/guides/help-channels/
+ https://pythondiscord.com/pages/code-of-conduct/
+RelevantLinkValues: Asking Good Questions
+ Help Channel Guide
+ Code of Conduct
+
+This is test content.
diff --git a/pydis_site/apps/content/tests/test_content/test2.md b/pydis_site/apps/content/tests/test_content/test2.md
new file mode 100644
index 00000000..f0852356
--- /dev/null
+++ b/pydis_site/apps/content/tests/test_content/test2.md
@@ -0,0 +1,5 @@
+Title: Test 2
+ShortDescription: Testing 2
+Contributors: user2
+
+This is too test content. \ No newline at end of file
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..82e1ac5f
--- /dev/null
+++ b/pydis_site/apps/content/tests/test_utils.py
@@ -0,0 +1,122 @@
+import os
+from unittest.mock import patch
+
+from django.conf import settings
+from django.http import Http404
+from django.test import TestCase
+from markdown import Markdown
+
+from pydis_site.apps.content import utils
+
+BASE_PATH = os.path.join(settings.BASE_DIR, "pydis_site", "apps", "content", "tests", "test_content")
+
+
+class TestGetBasePath(TestCase):
+ def test_get_base_path(self):
+ """Test does function return content base path."""
+ self.assertEqual(
+ utils._get_base_path(),
+ os.path.join(settings.BASE_DIR, "pydis_site", "apps", "content", "resources", "content")
+ )
+
+
+class TestGetCategory(TestCase):
+ def test_get_category_successfully(self):
+ """Check does this get right data from category data file."""
+ with patch("pydis_site.apps.content.utils._get_base_path", return_value=BASE_PATH):
+ result = utils.get_category("category")
+
+ self.assertEqual(result, {"name": "My Category", "description": "My Description"})
+
+ def test_get_category_not_exists(self):
+ """Check does this raise 404 error when category don't exists."""
+ with patch("pydis_site.apps.content.utils._get_base_path", return_value=BASE_PATH):
+ with self.assertRaises(Http404):
+ utils.get_category("invalid")
+
+ def test_get_category_not_directory(self):
+ """Check does this raise 404 error when category isn't directory."""
+ with patch("pydis_site.apps.content.utils._get_base_path", return_value=BASE_PATH):
+ with self.assertRaises(Http404):
+ utils.get_category("test.md")
+
+
+class TestGetCategories(TestCase):
+ def test_get_categories(self):
+ """Check does this return test content categories."""
+ with patch("pydis_site.apps.content.utils._get_base_path", return_value=BASE_PATH):
+ result = utils.get_categories()
+
+ self.assertEqual(result, {"category": {"name": "My Category", "description": "My Description"}})
+
+
+class TestGetArticles(TestCase):
+ def test_get_all_root_articles(self):
+ """Check does this return all root level testing content."""
+ with patch("pydis_site.apps.content.utils._get_base_path", return_value=BASE_PATH):
+ result = utils.get_articles()
+
+ for case in ["test", "test2"]:
+ with self.subTest(guide=case):
+ md = Markdown(extensions=['meta'])
+ with open(os.path.join(BASE_PATH, f"{case}.md")) as f:
+ md.convert(f.read())
+
+ self.assertIn(case, result)
+ self.assertEqual(md.Meta, result[case])
+
+ def test_get_all_category_articles(self):
+ """Check does this return all category testing content."""
+ with patch("pydis_site.apps.content.utils._get_base_path", return_value=BASE_PATH):
+ result = utils.get_articles("category")
+
+ md = Markdown(extensions=['meta'])
+ with open(os.path.join(BASE_PATH, "category", "test3.md")) as f:
+ md.convert(f.read())
+
+ self.assertIn("test3", result)
+ self.assertEqual(md.Meta, result["test3"])
+
+
+class TestGetArticle(TestCase):
+ def test_get_root_article_success(self):
+ """Check does this return article HTML and metadata when root article exist."""
+ with patch("pydis_site.apps.content.utils._get_base_path", return_value=BASE_PATH):
+ result = utils.get_article("test", None)
+
+ md = Markdown(extensions=['meta', 'attr_list', 'fenced_code'])
+
+ with open(os.path.join(BASE_PATH, "test.md")) as f:
+ html = md.convert(f.read())
+
+ self.assertEqual(result, {"article": html, "metadata": md.Meta})
+
+ def test_get_root_article_dont_exist(self):
+ """Check does this raise Http404 when root article don't exist."""
+ with patch("pydis_site.apps.content.utils._get_base_path", return_value=BASE_PATH):
+ with self.assertRaises(Http404):
+ utils.get_article("invalid", None)
+
+ def test_get_category_article_success(self):
+ """Check does this return article HTML and metadata when category guide exist."""
+ with patch("pydis_site.apps.content.utils._get_base_path", return_value=BASE_PATH):
+ result = utils.get_article("test3", "category")
+
+ md = Markdown(extensions=['meta', 'attr_list', 'fenced_code'])
+
+ with open(os.path.join(BASE_PATH, "category", "test3.md")) as f:
+ html = md.convert(f.read())
+
+ self.assertEqual(result, {"article": html, "metadata": md.Meta})
+
+ def test_get_category_article_dont_exist(self):
+ """Check does this raise Http404 when category article don't exist."""
+ with patch("pydis_site.apps.content.utils._get_base_path", return_value=BASE_PATH):
+ with self.assertRaises(Http404):
+ utils.get_article("invalid", "category")
+
+ def test_get_category_article_category_dont_exist(self):
+ """Check does this raise Http404 when category don't exist."""
+ with patch("pydis_site.apps.content.utils._get_base_path", return_value=BASE_PATH):
+ with self.assertRaises(Http404):
+ utils.get_article("some-guide", "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..98054534
--- /dev/null
+++ b/pydis_site/apps/content/tests/test_views.py
@@ -0,0 +1,104 @@
+from unittest.mock import patch
+
+from django.http import Http404
+from django.test import TestCase
+from django_hosts.resolvers import reverse
+
+
+class TestGuidesIndexView(TestCase):
+ @patch("pydis_site.apps.content.views.articles.get_articles")
+ @patch("pydis_site.apps.content.views.articles.get_categories")
+ def test_articles_index_return_200(self, get_categories_mock, get_articles_mock):
+ """Check that content index return HTTP code 200."""
+ get_categories_mock.return_value = {}
+ get_articles_mock.return_value = {}
+
+ url = reverse('content:content')
+ response = self.client.get(url)
+ self.assertEqual(response.status_code, 200)
+ get_articles_mock.assert_called_once()
+ get_categories_mock.assert_called_once()
+
+
+class TestGuideView(TestCase):
+ @patch("pydis_site.apps.content.views.article.os.path.getmtime")
+ @patch("pydis_site.apps.content.views.article.get_article")
+ @patch("pydis_site.apps.content.views.article.get_category")
+ def test_guide_return_code_200(self, get_category_mock, get_article_mock, get_time_mock):
+ get_article_mock.return_value = {"guide": "test", "metadata": {}}
+
+ url = reverse("content:article", args=["test-guide"])
+ response = self.client.get(url)
+ self.assertEqual(response.status_code, 200)
+ get_category_mock.assert_not_called()
+ get_article_mock.assert_called_once_with("test-guide", None)
+
+ @patch("pydis_site.apps.content.views.article.os.path.getmtime")
+ @patch("pydis_site.apps.content.views.article.get_article")
+ @patch("pydis_site.apps.content.views.article.get_category")
+ def test_guide_return_404(self, get_category_mock, get_article_mock, get_time_mock):
+ """Check that return code is 404 when invalid article provided."""
+ get_article_mock.side_effect = Http404("Article not found.")
+
+ url = reverse("content:article", args=["invalid-guide"])
+ response = self.client.get(url)
+ self.assertEqual(response.status_code, 404)
+ get_article_mock.assert_called_once_with("invalid-guide", None)
+ get_category_mock.assert_not_called()
+
+
+class TestCategoryView(TestCase):
+ @patch("pydis_site.apps.content.views.category.get_category")
+ @patch("pydis_site.apps.content.views.category.get_articles")
+ def test_valid_category_code_200(self, get_articles_mock, get_category_mock):
+ """Check that return code is 200 when visiting valid category."""
+ get_category_mock.return_value = {"name": "test", "description": "test"}
+ get_articles_mock.return_value = {}
+
+ url = reverse("content:category", args=["category"])
+ response = self.client.get(url)
+
+ self.assertEqual(response.status_code, 200)
+ get_articles_mock.assert_called_once_with("category")
+ get_category_mock.assert_called_once_with("category")
+
+ @patch("pydis_site.apps.content.views.category.get_category")
+ @patch("pydis_site.apps.content.views.category.get_articles")
+ def test_invalid_category_code_404(self, get_articles_mock, get_category_mock):
+ """Check that return code is 404 when trying to visit invalid category."""
+ get_category_mock.side_effect = Http404("Category not found.")
+
+ url = reverse("content:category", args=["invalid-category"])
+ response = self.client.get(url)
+
+ self.assertEqual(response.status_code, 404)
+ get_category_mock.assert_called_once_with("invalid-category")
+ get_articles_mock.assert_not_called()
+
+
+class TestCategoryGuidesView(TestCase):
+ @patch("pydis_site.apps.content.views.article.os.path.getmtime")
+ @patch("pydis_site.apps.content.views.article.get_article")
+ @patch("pydis_site.apps.content.views.article.get_category")
+ def test_valid_category_article_code_200(self, get_category_mock, get_article_mock, get_time_mock):
+ """Check that return code is 200 when visiting valid category article."""
+ get_article_mock.return_value = {"guide": "test", "metadata": {}}
+
+ url = reverse("content:category_article", args=["category", "test3"])
+ response = self.client.get(url)
+ self.assertEqual(response.status_code, 200)
+ get_article_mock.assert_called_once_with("test3", "category")
+ get_category_mock.assert_called_once_with("category")
+
+ @patch("pydis_site.apps.content.views.article.os.path.getmtime")
+ @patch("pydis_site.apps.content.views.article.get_article")
+ @patch("pydis_site.apps.content.views.article.get_category")
+ def test_invalid_category_article_code_404(self, get_category_mock, get_article_mock, get_time_mock):
+ """Check that return code is 200 when trying to visit invalid category article."""
+ get_article_mock.side_effect = Http404("Article not found.")
+
+ url = reverse("content:category_article", args=["category", "invalid"])
+ response = self.client.get(url)
+ self.assertEqual(response.status_code, 404)
+ get_article_mock.assert_called_once_with("invalid", "category")
+ get_category_mock.assert_not_called()