comparison news/tests/test_views.py @ 998:e2c3d7ecfa30

Added tests for submit news.
author Brian Neal <bgneal@gmail.com>
date Fri, 20 Nov 2015 23:07:37 -0600
parents
children
comparison
equal deleted inserted replaced
997:19b86e684cc2 998:e2c3d7ecfa30
1 """Unit tests for the news app."""
2
3 import datetime
4
5 from django.contrib.auth.models import User
6 from django.core.urlresolvers import reverse
7 from django.test import TestCase
8
9 from news.models import PendingStory
10
11
12 class NewsTestCase(TestCase):
13
14 def setUp(self):
15 self.username = 'test_user'
16 self.pw = 'password'
17 self.user = User.objects.create_user(self.username, '', self.pw)
18 self.user.save()
19 self.assertTrue(self.client.login(username=self.username, password=self.pw))
20
21 def tearDown(self):
22 pass
23
24
25 class SubmitViewTestCase(NewsTestCase):
26
27 fixtures = ['news_categories.json']
28
29 def setUp(self):
30 super(SubmitViewTestCase, self).setUp()
31 self.view_url = reverse('news-submit')
32
33 def test_get(self):
34 response = self.client.get(self.view_url)
35 self.assertEqual(response.status_code, 200)
36
37 def test_post(self):
38 args = {
39 'title': 'My news story',
40 'category': '1',
41 'short_markup': "Here is the story text.",
42 }
43 now = datetime.datetime.now()
44 response = self.client.post(self.view_url, args, follow=True)
45 self.assertEqual(response.status_code, 200)
46 self.assertEqual(response.redirect_chain,
47 [('http://testserver' + reverse('news-submit_thanks'), 302)])
48
49 stories = list(PendingStory.objects.all())
50 self.assertEqual(len(stories), 1)
51 story = stories[0]
52 self.assertEqual(story.submitter, self.user)
53 self.assertEqual(story.title, args['title'])
54 self.assertEqual(story.short_markup, args['short_markup'])
55 self.assertTrue(now - story.date_submitted < datetime.timedelta(seconds=2))
56 self.assertTrue(now - story.update_date < datetime.timedelta(seconds=2))
57 self.assertEqual(story.category.pk, 1)
58 self.assertEqual(story.long_markup, '')
59 self.assertEqual(story.long_text, '')
60 self.assertEqual(story.short_text, '<p>{}</p>'.format(args['short_markup']))
61
62 def test_post_with_bad_image(self):
63 args = {
64 'title': 'My news story',
65 'category': '1',
66 'short_markup': "Image: ![image](http://example.com/a.jpg)\n",
67 }
68 response = self.client.post(self.view_url, args, follow=True)
69 self.assertEqual(response.status_code, 200)
70 self.assertTrue('Image must be accessed via https' in response.content)
71 self.assertEqual(response.redirect_chain, [])
72
73 stories = list(PendingStory.objects.all())
74 self.assertEqual(len(stories), 0)