Skip to content

Commit

Permalink
Ajout des tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DevHugo committed Mar 20, 2015
1 parent 91aa901 commit 4cb73ae
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 0 deletions.
126 changes: 126 additions & 0 deletions zds/article/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import tempfile
import zipfile
import datetime
from django.contrib import messages

try:
import ujson as json_reader
Expand Down Expand Up @@ -756,6 +757,131 @@ def test_change_update(self):
follow=False)
self.assertEqual(result.status_code, 404)

def test_warn_typo(self):
"""
Add a non-regression test about warning the author(s) of a typo in an article
"""

typo_text = u'T\'as fait une faute, t\'es nul'

# login with author
self.assertEqual(
self.client.login(
username=self.user_author.username,
password='hostel77'),
True)

# check if author get error when warning typo on its own tutorial
result = self.client.post(
reverse('zds.article.views.warn_typo'),
{
'explication': u'ceci est un test',
'article_pk': self.article.pk
},
follow=True)
self.assertEqual(result.status_code, 200)

msgs = result.context['messages']
last = None
for msg in msgs:
last = msg
self.assertEqual(last.level, messages.ERROR)

# login with normal user
self.client.logout()

self.assertEqual(
self.client.login(
username=self.user.username,
password='hostel77'),
True)

# check if user can warn typo in tutorial
result = self.client.post(
reverse('zds.article.views.warn_typo'),
{
'explication': typo_text,
'article_pk': self.article.pk
},
follow=True)
self.assertEqual(result.status_code, 200)

msgs = result.context['messages']
last = None
for msg in msgs:
last = msg
self.assertEqual(last.level, messages.SUCCESS)

# check PM :
sent_pm = PrivateTopic.objects.filter(author=self.user.pk).last()
self.assertIn(self.user_author, sent_pm.participants.all()) # author is in participants
self.assertIn(typo_text, sent_pm.last_message.text) # typo is in message
self.assertIn(self.article.get_absolute_url_online(), sent_pm.last_message.text) # public url is in message

# Check if we send a bad pk key
result = self.client.post(
reverse('zds.article.views.warn_typo'),
{
'explication': typo_text,
'article_pk': 'Nope'
},
follow=False)
self.assertEqual(result.status_code, 404)

# Check if we send a wrong pk key
result = self.client.post(
reverse('zds.article.views.warn_typo'),
{
'explication': typo_text,
'article_pk': 1111
},
follow=False)
self.assertEqual(result.status_code, 404)

# Check if we send no explanation
result = self.client.post(
reverse('zds.article.views.warn_typo'),
{
'explication': '',
'article_pk': self.article.pk
},
follow=True)
self.assertEqual(result.status_code, 200)

msgs = result.context['messages']
last = None
for msg in msgs:
last = msg
self.assertEqual(last.level, messages.ERROR)

# Check if we send an explanation with only space
result = self.client.post(
reverse('zds.article.views.warn_typo'),
{
'explication': ' ',
'article_pk': self.article.pk
},
follow=True)
self.assertEqual(result.status_code, 200)

msgs = result.context['messages']
last = None
for msg in msgs:
last = msg
self.assertEqual(last.level, messages.ERROR)

# Check if a guest can not warn the author
self.client.logout()

result = self.client.post(
reverse('zds.article.views.warn_typo'),
{
'explication': typo_text,
'article_pk': self.article.pk
},
follow=False)
self.assertEqual(result.status_code, 302)

def tearDown(self):
if os.path.isdir(settings.ZDS_APP['article']['repo_path']):
shutil.rmtree(settings.ZDS_APP['article']['repo_path'])
Expand Down
4 changes: 4 additions & 0 deletions zds/article/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ def warn_typo(request):

article = get_object_or_404(Article, pk=article_pk)

# Check if the article is published
if article.sha_public is None:
raise Http404

# Fetch explanation
if 'explication' not in request.POST or not request.POST['explication'].strip():
messages.error(request, _(u'Votre proposition de correction est vide'))
Expand Down

0 comments on commit 4cb73ae

Please sign in to comment.