1
Fork 0
This repository has been archived on 2023-03-26. You can view files and clone it, but cannot push or open issues or pull requests.
theorangeone.net-legacy/project/blog/tests.py

68 lines
2.5 KiB
Python
Raw Normal View History

2016-02-20 21:37:03 +00:00
from django.test import TestCase
import requests_mock, json
from . import utils
from django.core.urlresolvers import reverse
@requests_mock.mock()
2016-03-15 22:56:50 +00:00
class WordPressAPITestCase(TestCase):
2016-02-20 21:37:03 +00:00
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>",
2016-02-20 23:06:49 +00:00
"slug": "test-post",
"date": "2000-01-01T18:05:00+00:00"
2016-02-20 21:37:03 +00:00
}
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'])