changeset 998:e2c3d7ecfa30

Added tests for submit news.
author Brian Neal <bgneal@gmail.com>
date Fri, 20 Nov 2015 23:07:37 -0600
parents 19b86e684cc2
children 8386a8ebcbc7
files news/migrations/0004_auto_20151120_2228.py news/models.py news/tests/__init__.py news/tests/test_views.py news/urls.py news/views.py
diffstat 5 files changed, 107 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/news/migrations/0004_auto_20151120_2228.py	Fri Nov 20 23:07:37 2015 -0600
@@ -0,0 +1,26 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import models, migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('news', '0003_auto_20151117_2032'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='pendingstory',
+            name='long_text',
+            field=models.TextField(default=b'', blank=True),
+            preserve_default=True,
+        ),
+        migrations.AlterField(
+            model_name='story',
+            name='long_text',
+            field=models.TextField(default=b'', blank=True),
+            preserve_default=True,
+        ),
+    ]
--- a/news/models.py	Tue Nov 17 21:01:20 2015 -0600
+++ b/news/models.py	Fri Nov 20 23:07:37 2015 -0600
@@ -37,7 +37,7 @@
     submitter = models.ForeignKey(User)
     category = models.ForeignKey(Category)
     short_text = models.TextField()
-    long_text = models.TextField(blank=True)
+    long_text = models.TextField(default='', blank=True)
     date_submitted = models.DateTimeField(db_index=True)
     allow_comments = models.BooleanField(default=True)
     tags = TagField()
@@ -60,12 +60,12 @@
         else:
             self.update_date = datetime.datetime.now()
 
-        self.short_text = kwargs.pop('short_text', None)
-        if self.short_text is None and self.short_markup:
+        self.short_text = kwargs.pop('short_text', '')
+        if not self.short_text and self.short_markup:
             self.short_text = site_markup(self.short_markup)
 
-        self.long_text = kwargs.pop('long_text', None)
-        if self.long_text is None and self.long_markup:
+        self.long_text = kwargs.pop('long_text', '')
+        if not self.long_text and self.long_markup:
             self.long_text = site_markup(self.long_markup)
 
         super(StoryBase, self).save(*args, **kwargs)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/news/tests/test_views.py	Fri Nov 20 23:07:37 2015 -0600
@@ -0,0 +1,74 @@
+"""Unit tests for the news app."""
+
+import datetime
+
+from django.contrib.auth.models import User
+from django.core.urlresolvers import reverse
+from django.test import TestCase
+
+from news.models import PendingStory
+
+
+class NewsTestCase(TestCase):
+
+    def setUp(self):
+        self.username = 'test_user'
+        self.pw = 'password'
+        self.user = User.objects.create_user(self.username, '', self.pw)
+        self.user.save()
+        self.assertTrue(self.client.login(username=self.username, password=self.pw))
+
+    def tearDown(self):
+        pass
+
+
+class SubmitViewTestCase(NewsTestCase):
+
+    fixtures = ['news_categories.json']
+
+    def setUp(self):
+        super(SubmitViewTestCase, self).setUp()
+        self.view_url = reverse('news-submit')
+
+    def test_get(self):
+        response = self.client.get(self.view_url)
+        self.assertEqual(response.status_code, 200)
+
+    def test_post(self):
+        args = {
+            'title': 'My news story',
+            'category': '1',
+            'short_markup': "Here is the story text.",
+        }
+        now = datetime.datetime.now()
+        response = self.client.post(self.view_url, args, follow=True)
+        self.assertEqual(response.status_code, 200)
+        self.assertEqual(response.redirect_chain,
+                         [('http://testserver' + reverse('news-submit_thanks'), 302)])
+
+        stories = list(PendingStory.objects.all())
+        self.assertEqual(len(stories), 1)
+        story = stories[0]
+        self.assertEqual(story.submitter, self.user)
+        self.assertEqual(story.title, args['title'])
+        self.assertEqual(story.short_markup, args['short_markup'])
+        self.assertTrue(now - story.date_submitted < datetime.timedelta(seconds=2))
+        self.assertTrue(now - story.update_date < datetime.timedelta(seconds=2))
+        self.assertEqual(story.category.pk, 1)
+        self.assertEqual(story.long_markup, '')
+        self.assertEqual(story.long_text, '')
+        self.assertEqual(story.short_text, '<p>{}</p>'.format(args['short_markup']))
+
+    def test_post_with_bad_image(self):
+        args = {
+            'title': 'My news story',
+            'category': '1',
+            'short_markup': "Image: ![image](http://example.com/a.jpg)\n",
+        }
+        response = self.client.post(self.view_url, args, follow=True)
+        self.assertEqual(response.status_code, 200)
+        self.assertTrue('Image must be accessed via https' in response.content)
+        self.assertEqual(response.redirect_chain, [])
+
+        stories = list(PendingStory.objects.all())
+        self.assertEqual(len(stories), 0)
--- a/news/urls.py	Tue Nov 17 21:01:20 2015 -0600
+++ b/news/urls.py	Fri Nov 20 23:07:37 2015 -0600
@@ -5,7 +5,7 @@
     url(r'^date/$', 'index', name='news-index_page'),
     url(r'^archive/$', 'archive_index', name='news-archive_index'),
     url(r'^archive/(?P<year>\d{4})/(?P<month>\d\d?)/$',
-        'archive', 
+        'archive',
         name='news-archive_page'),
     url(r'^categories/$', 'category_index', name='news-category_index'),
     url(r'^category/(?P<slug>[\w\d-]+)/$', 'category', name='news-category'),
--- a/news/views.py	Tue Nov 17 21:01:20 2015 -0600
+++ b/news/views.py	Fri Nov 20 23:07:37 2015 -0600
@@ -143,7 +143,7 @@
         add_form = AddNewsForm(request.POST)
         if add_form.is_valid():
             add_form.save(request.user)
-            return HttpResponseRedirect(reverse('news.views.submit_thanks'))
+            return HttpResponseRedirect(reverse('news-submit_thanks'))
     else:
         add_form = AddNewsForm()