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