Added tests for blog page
This commit is contained in:
parent
662e48804e
commit
30214dd834
6 changed files with 78 additions and 4 deletions
66
project/blog/tests.py
Normal file
66
project/blog/tests.py
Normal file
|
@ -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": "<p>Test blog post content</p>",
|
||||
"slug": "test-post"
|
||||
}
|
||||
self.invalid_blog_data = {
|
||||
"title": "Invalid blog post",
|
||||
"content": "<p></p>",
|
||||
"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": "<p>Test blog post content</p>",
|
||||
"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'])
|
|
@ -2,5 +2,5 @@ from django.conf.urls import url
|
|||
from . import views
|
||||
|
||||
urlpatterns = [
|
||||
url(r'^(?P<slug>.+)/$', views.BlogView.as_view(), name='blog-view'),
|
||||
url(r'^(?P<slug>.+)/$', views.BlogView.as_view(), name='blog-post'),
|
||||
]
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -30,6 +30,7 @@ INSTALLED_APPS = (
|
|||
|
||||
'project.pages',
|
||||
'project.common',
|
||||
'project.blog'
|
||||
)
|
||||
|
||||
MIDDLEWARE_CLASSES = (
|
||||
|
|
|
@ -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
|
||||
|
|
Reference in a new issue