diff options
author | 2020-10-06 21:48:12 +0300 | |
---|---|---|
committer | 2020-10-06 21:48:12 +0300 | |
commit | 968f3504f49c5695ef88686c89be1fbe0974484c (patch) | |
tree | 2ede1f915acfcbb811ef99dc3486b5bd43cf390b | |
parent | Rename last guides test to articles tests (diff) |
Cover fetching article GitHub information with tests
-rw-r--r-- | pydis_site/apps/content/tests/test_utils.py | 44 | ||||
-rw-r--r-- | pydis_site/apps/content/tests/test_views.py | 4 | ||||
-rw-r--r-- | pydis_site/apps/content/utils.py | 2 |
3 files changed, 47 insertions, 3 deletions
diff --git a/pydis_site/apps/content/tests/test_utils.py b/pydis_site/apps/content/tests/test_utils.py index 9c7c4f31..a00c293f 100644 --- a/pydis_site/apps/content/tests/test_utils.py +++ b/pydis_site/apps/content/tests/test_utils.py @@ -1,6 +1,8 @@ +from datetime import datetime from pathlib import Path -from unittest.mock import patch +from unittest.mock import patch, MagicMock +from dateutil.parser import isoparse from django.conf import settings from django.http import Http404 from django.test import TestCase @@ -134,3 +136,43 @@ class TestGetArticle(TestCase): with patch("pydis_site.apps.content.utils._get_base_path", return_value=BASE_PATH): with self.assertRaises(Http404): utils.get_article("some-guide", "invalid") + + +class GetGitHubInformationTests(TestCase): + @patch("pydis_site.apps.content.utils.requests.get") + @patch("pydis_site.apps.content.utils.COMMITS_URL", "foobar") + def test_call_get_github_information_requests_get(self, requests_get_mock): + """Check does this call requests.get function with proper URL.""" + utils.get_github_information("foo", None) + requests_get_mock.assert_called_once_with("foobar") + + @patch("pydis_site.apps.content.utils.requests.get") + def test_github_status_code_200_response(self, requests_get_mock): + """Check does this return provided modified date and contributors.""" + requests_get_mock.return_value = MagicMock(status_code=200) + requests_get_mock.return_value.json.return_value = [{ + "commit": { + "committer": { + "date": datetime(2020, 10, 1).isoformat(), + "name": "foobar", + } + }, + "committer": { + "html_url": "abc1234" + } + }] + result = utils.get_github_information("foo", None) + self.assertEqual(result, { + "last_modified": datetime(2020, 10, 1).strftime("%dth %B %Y"), + "contributors": {"foobar": "abc1234"} + }) + + @patch("pydis_site.apps.content.utils.requests.get") + def test_github_other_status_code_response(self, requests_get_mock): + """Check does this return provided modified date and contributors.""" + requests_get_mock.return_value = MagicMock(status_code=404) + result = utils.get_github_information("foo", None) + self.assertEqual(result, { + "last_modified": "N/A", + "contributors": {} + }) diff --git a/pydis_site/apps/content/tests/test_views.py b/pydis_site/apps/content/tests/test_views.py index c5da066f..67177c93 100644 --- a/pydis_site/apps/content/tests/test_views.py +++ b/pydis_site/apps/content/tests/test_views.py @@ -23,7 +23,8 @@ class TestArticlesIndexView(TestCase): class TestArticleView(TestCase): @patch("pydis_site.apps.content.views.article.get_article") @patch("pydis_site.apps.content.views.article.get_category") - def test_article_return_code_200(self, get_category_mock, get_article_mock): + @patch("pydis_site.apps.content.views.article.get_github_information") + def test_article_return_code_200(self, gh_info_mock, get_category_mock, get_article_mock): get_article_mock.return_value = {"guide": "test", "metadata": {}} url = reverse("articles:article", args=["test-guide"]) @@ -31,6 +32,7 @@ class TestArticleView(TestCase): self.assertEqual(response.status_code, 200) get_category_mock.assert_not_called() get_article_mock.assert_called_once_with("test-guide", None) + gh_info_mock.assert_called_once() @patch("pydis_site.apps.content.views.article.get_article") @patch("pydis_site.apps.content.views.article.get_category") diff --git a/pydis_site/apps/content/utils.py b/pydis_site/apps/content/utils.py index 0c2a027d..e8f1af70 100644 --- a/pydis_site/apps/content/utils.py +++ b/pydis_site/apps/content/utils.py @@ -113,5 +113,5 @@ def get_github_information( else: return { "last_modified": "N/A", - "contributors": [] + "contributors": {} } |