bgneal@998: """Unit tests for the news app.""" bgneal@998: bgneal@998: import datetime bgneal@998: bgneal@998: from django.contrib.auth.models import User bgneal@998: from django.core.urlresolvers import reverse bgneal@998: from django.test import TestCase bgneal@998: bgneal@998: from news.models import PendingStory bgneal@998: bgneal@998: bgneal@998: class NewsTestCase(TestCase): bgneal@998: bgneal@998: def setUp(self): bgneal@998: self.username = 'test_user' bgneal@998: self.pw = 'password' bgneal@998: self.user = User.objects.create_user(self.username, '', self.pw) bgneal@998: self.user.save() bgneal@998: self.assertTrue(self.client.login(username=self.username, password=self.pw)) bgneal@998: bgneal@998: def tearDown(self): bgneal@998: pass bgneal@998: bgneal@998: bgneal@998: class SubmitViewTestCase(NewsTestCase): bgneal@998: bgneal@998: fixtures = ['news_categories.json'] bgneal@998: bgneal@998: def setUp(self): bgneal@998: super(SubmitViewTestCase, self).setUp() bgneal@998: self.view_url = reverse('news-submit') bgneal@998: bgneal@998: def test_get(self): bgneal@998: response = self.client.get(self.view_url) bgneal@998: self.assertEqual(response.status_code, 200) bgneal@998: bgneal@998: def test_post(self): bgneal@998: args = { bgneal@998: 'title': 'My news story', bgneal@998: 'category': '1', bgneal@998: 'short_markup': "Here is the story text.", bgneal@998: } bgneal@998: now = datetime.datetime.now() bgneal@998: response = self.client.post(self.view_url, args, follow=True) bgneal@998: self.assertEqual(response.status_code, 200) bgneal@998: self.assertEqual(response.redirect_chain, bgneal@998: [('http://testserver' + reverse('news-submit_thanks'), 302)]) bgneal@998: bgneal@998: stories = list(PendingStory.objects.all()) bgneal@998: self.assertEqual(len(stories), 1) bgneal@998: story = stories[0] bgneal@998: self.assertEqual(story.submitter, self.user) bgneal@998: self.assertEqual(story.title, args['title']) bgneal@998: self.assertEqual(story.short_markup, args['short_markup']) bgneal@998: self.assertTrue(now - story.date_submitted < datetime.timedelta(seconds=2)) bgneal@998: self.assertTrue(now - story.update_date < datetime.timedelta(seconds=2)) bgneal@998: self.assertEqual(story.category.pk, 1) bgneal@998: self.assertEqual(story.long_markup, '') bgneal@998: self.assertEqual(story.long_text, '') bgneal@998: self.assertEqual(story.short_text, '
{}
'.format(args['short_markup'])) bgneal@998: bgneal@998: def test_post_with_bad_image(self): bgneal@998: args = { bgneal@998: 'title': 'My news story', bgneal@998: 'category': '1', bgneal@998: 'short_markup': "Image: ![image](http://example.com/a.jpg)\n", bgneal@998: } bgneal@998: response = self.client.post(self.view_url, args, follow=True) bgneal@998: self.assertEqual(response.status_code, 200) bgneal@998: self.assertTrue('Image must be accessed via https' in response.content) bgneal@998: self.assertEqual(response.redirect_chain, []) bgneal@998: bgneal@998: stories = list(PendingStory.objects.all()) bgneal@998: self.assertEqual(len(stories), 0)