# HG changeset patch # User Brian Neal # Date 1448082457 21600 # Node ID e2c3d7ecfa306bde187aaec235351d85a8c546e9 # Parent 19b86e684cc2f3da1c49f6b7a01233598a5cbe25 Added tests for submit news. diff -r 19b86e684cc2 -r e2c3d7ecfa30 news/migrations/0004_auto_20151120_2228.py --- /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, + ), + ] diff -r 19b86e684cc2 -r e2c3d7ecfa30 news/models.py --- 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) diff -r 19b86e684cc2 -r e2c3d7ecfa30 news/tests/test_views.py --- /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, '

{}

'.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) diff -r 19b86e684cc2 -r e2c3d7ecfa30 news/urls.py --- 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\d{4})/(?P\d\d?)/$', - 'archive', + 'archive', name='news-archive_page'), url(r'^categories/$', 'category_index', name='news-category_index'), url(r'^category/(?P[\w\d-]+)/$', 'category', name='news-category'), diff -r 19b86e684cc2 -r e2c3d7ecfa30 news/views.py --- 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()