diff --git a/project/blog/tests.py b/project/blog/tests.py new file mode 100644 index 0000000..5dab360 --- /dev/null +++ b/project/blog/tests.py @@ -0,0 +1,66 @@ +from django.test import TestCase +import requests_mock, json +from . import utils +from django.core.urlresolvers import reverse + + +@requests_mock.mock() +class WordPressTestCase(TestCase): + def setUp(self): + self.test_blog_data = { + "title": "Test Blog Post", + "ID": 1, + "content": "

Test blog post content

", + "slug": "test-post" + } + self.invalid_blog_data = { + "title": "Invalid blog post", + "content": "

", + "slug": "invalid" + } + + def test_gets_correct_data(self, m): + payload = json.dumps(self.test_blog_data) + m.get(utils.build_url(self.test_blog_data['slug']), text=payload) + blog_data = utils.get_post(self.test_blog_data['slug']) + self.assertEqual(blog_data, self.test_blog_data) + + def test_invalid_response(self, m): + payload = json.dumps(self.invalid_blog_data) + m.get(utils.build_url(self.invalid_blog_data['slug']), text=payload) + blog_data = utils.get_post(self.invalid_blog_data['slug']) + self.assertFalse(blog_data) + + def test_invalid_status(self, m): + payload = json.dumps(self.test_blog_data) + m.get(utils.build_url(self.test_blog_data['slug']), text=payload, status_code=500) + blog_data = utils.get_post(self.test_blog_data['slug']) + self.assertFalse(blog_data) + + def test_no_slug(self, m): + blog_data = utils.get_post('') + self.assertFalse(blog_data) + + +@requests_mock.mock() +class BlogViewTestCase(TestCase): + def setUp(self): + self.test_blog_data = { + "title": "Test Blog Post", + "ID": 1, + "content": "

Test blog post content

", + "slug": "test-post" + } + + def test_accessable(self, m): + payload = json.dumps(self.test_blog_data) + m.get(utils.build_url(self.test_blog_data['slug']), text=payload) + response = self.client.get(reverse('blog:blog-post', args=[self.test_blog_data['slug']])) + self.assertEqual(response.status_code, 200) + + def test_correct_content(self, m): + payload = json.dumps(self.test_blog_data) + m.get(utils.build_url(self.test_blog_data['slug']), text=payload) + response = self.client.get(reverse('blog:blog-post', args=[self.test_blog_data['slug']])) + self.assertContains(response, self.test_blog_data['content']) + self.assertEqual(response.context['html_title'], self.test_blog_data['title']) diff --git a/project/blog/urls.py b/project/blog/urls.py index eec053e..5bd7321 100644 --- a/project/blog/urls.py +++ b/project/blog/urls.py @@ -2,5 +2,5 @@ from django.conf.urls import url from . import views urlpatterns = [ - url(r'^(?P.+)/$', views.BlogView.as_view(), name='blog-view'), + url(r'^(?P.+)/$', views.BlogView.as_view(), name='blog-post'), ] diff --git a/project/blog/utils.py b/project/blog/utils.py index 81eefc5..e6987ce 100644 --- a/project/blog/utils.py +++ b/project/blog/utils.py @@ -3,11 +3,17 @@ from django.conf import settings API_PATH = "https://public-api.wordpress.com/rest/v1.1/sites/{0}/posts/slug:{1}" + +def build_url(slug): + if not slug: + return + return API_PATH.format(settings.WORDPRESS_URL, slug) + + def get_post(slug): if not slug: return - url = API_PATH.format(settings.WORDPRESS_URL, slug) - response = requests.get(url) + response = requests.get(build_url(slug)) if response.status_code != 200: return diff --git a/project/blog/views.py b/project/blog/views.py index 02bd386..2578942 100644 --- a/project/blog/views.py +++ b/project/blog/views.py @@ -4,7 +4,7 @@ from django.http import Http404 class BlogView(CustomTemplate): - template_name="blog/posts.html" + template_name = "blog/posts.html" def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) diff --git a/project/settings.py b/project/settings.py index d60e379..7035932 100644 --- a/project/settings.py +++ b/project/settings.py @@ -30,6 +30,7 @@ INSTALLED_APPS = ( 'project.pages', 'project.common', + 'project.blog' ) MIDDLEWARE_CLASSES = ( diff --git a/requirements.txt b/requirements.txt index 2dc3ab1..00ddf1e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -11,5 +11,6 @@ flake8==2.5.0 markdown2==2.3.0 max-django-mail-templated==1.2 requests==2.9.1 +requests-mock==0.7.0 whitenoise==2.0.6 waitress==0.8.10