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