Skip to content

Commit

Permalink
topic.private tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nitely committed May 15, 2016
1 parent e5431f2 commit 0921689
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 12 deletions.
86 changes: 82 additions & 4 deletions spirit/topic/private/tests.py
Expand Up @@ -2,8 +2,9 @@

from __future__ import unicode_literals
import datetime
import hashlib

from django.test import TestCase
from django.test import TestCase, override_settings
from django.core.cache import cache
from django.core.urlresolvers import reverse
from django.template import Template, Context
Expand All @@ -15,8 +16,11 @@
from ...core.tests import utils
from ...category.models import Category
from .models import TopicPrivate
from .forms import TopicForPrivateForm, TopicPrivateInviteForm,\
TopicPrivateManyForm, TopicPrivateJoinForm
from .forms import (
TopicForPrivateForm,
TopicPrivateInviteForm,
TopicPrivateManyForm,
TopicPrivateJoinForm)
from .tags import render_invite_form
from ..models import Topic
from ...comment.bookmark.models import CommentBookmark
Expand Down Expand Up @@ -72,6 +76,42 @@ def test_private_publish_user(self):
response = self.client.get(reverse('spirit:topic:private:publish', kwargs={'user_id': self.user2.pk, }))
self.assertEqual(response.context['tpform'].initial['users'], [self.user2.username, ])

@override_settings(ST_DOUBLE_POST_THRESHOLD_MINUTES=10)
def test_private_publish_double_post(self):
"""
Should prevent double posts
"""
utils.login(self)
category_private = Category.objects.get(
pk=settings.ST_TOPIC_PRIVATE_CATEGORY_PK)
topic_title = 'title foobar'

# First post
self.client.post(
reverse('spirit:topic:private:publish'),
{'comment': 'foo', 'title': topic_title, 'users': [self.user2.username]})
self.assertEqual(len(Topic.objects.all()), 1)

# Double post
cache.clear() # Clear rate limit
response = self.client.post(
reverse('spirit:topic:private:publish'),
{'comment': 'new foo', 'title': topic_title, 'users': [self.user2.username]})
self.assertEqual(len(Topic.objects.all()), 1) # Prevented!

self.assertRedirects(
response,
expected_url=category_private.get_absolute_url(),
status_code=302,
target_status_code=200)

# New post
cache.clear() # Clear rate limit
self.client.post(
reverse('spirit:topic:private:publish'),
{'comment': 'foo', 'title': 'new topic', 'users': [self.user2.username]})
self.assertEqual(len(Topic.objects.all()), 2)

def test_private_detail(self):
"""
private topic detail
Expand Down Expand Up @@ -405,6 +445,44 @@ def test_private_publish(self):
form = TopicForPrivateForm(data=form_data)
self.assertEqual(form.is_valid(), True)

def test_private_publish_category(self):
"""
Should return the private category
"""
category_private = Category.objects.get(
pk=settings.ST_TOPIC_PRIVATE_CATEGORY_PK)
form = TopicForPrivateForm()
self.assertEqual(form.category, category_private)
self.assertEqual(form.category, category_private) # Cached

def test_private_publish_get_topic_hash(self):
"""
Should return the topic hash
"""
category_private = Category.objects.get(
pk=settings.ST_TOPIC_PRIVATE_CATEGORY_PK)
title = 'title foobar'
form = TopicForPrivateForm(data={'title': title})
self.assertTrue(form.is_valid())
self.assertEqual(
form.get_topic_hash(),
hashlib.md5(
'{}category-{}'
.format(title, category_private.pk)
.encode('utf-8')).hexdigest())

def test_private_publish_get_topic_hash_from_field(self):
"""
Should return the topic hash from form field
"""
topic_hash = '1' * 32
form = TopicForPrivateForm(
data={
'title': 'foobar',
'topic_hash': topic_hash})
self.assertTrue(form.is_valid())
self.assertEqual(form.get_topic_hash(), topic_hash)

def test_private_create_many(self):
"""
create many private topics accesses
Expand Down Expand Up @@ -496,4 +574,4 @@ def test_topic_private_notify_access(self):
notification = TopicNotification.objects.get(user=private.user, topic=private.topic)
self.assertTrue(notification.is_active)
self.assertFalse(notification.is_read)
self.assertEqual(notification.comment, comment)
self.assertEqual(notification.comment, comment)
7 changes: 4 additions & 3 deletions spirit/topic/private/views.py
Expand Up @@ -43,8 +43,9 @@ def publish(request, user_id=None):
if (not request.is_limited and
all([tform.is_valid(), cform.is_valid(), tpform.is_valid()])): # TODO: test!
if not user.st.update_post_hash(tform.get_topic_hash()):
return redirect(request.POST.get('next', None) or
tform.category.get_absolute_url())
return redirect(
request.POST.get('next', None) or
tform.category.get_absolute_url())

# wrap in transaction.atomic?
topic = tform.save()
Expand All @@ -61,7 +62,7 @@ def publish(request, user_id=None):
cform = CommentForm()
initial = None

if user_id:
if user_id: # todo: move to form
user_to = get_object_or_404(User, pk=user_id)
initial = {'users': [user_to.username]}

Expand Down
7 changes: 2 additions & 5 deletions spirit/topic/tests.py
Expand Up @@ -128,7 +128,7 @@ def test_topic_publish_double_post(self):
response = self.client.post(
reverse('spirit:topic:publish'),
{'comment': 'foo', 'title': topic_title, 'category': category.pk})
self.assertEqual(len(Comment.objects.all()), 1) # Prevented!
self.assertEqual(len(Topic.objects.all()), 1) # Prevented!

self.assertRedirects(
response,
Expand All @@ -141,7 +141,7 @@ def test_topic_publish_double_post(self):
self.client.post(
reverse('spirit:topic:publish'),
{'comment': 'foo', 'title': 'new topic', 'category': category.pk})
self.assertEqual(len(Comment.objects.all()), 2)
self.assertEqual(len(Topic.objects.all()), 2)

@override_settings(ST_DOUBLE_POST_THRESHOLD_MINUTES=10)
def test_topic_publish_same_post_into_another_topic(self):
Expand Down Expand Up @@ -434,7 +434,6 @@ def test_topic_get_category(self):
"""
category = utils.create_category()
form_data = {
'comment': 'foo',
'title': 'foobar',
'category': category.pk}
form = TopicForm(self.user, data=form_data)
Expand All @@ -448,7 +447,6 @@ def test_topic_get_topic_hash(self):
category = utils.create_category()
title = 'title foobar'
form_data = {
'comment': 'foo',
'title': title,
'category': category.pk}
form = TopicForm(self.user, data=form_data)
Expand All @@ -467,7 +465,6 @@ def test_topic_get_topic_hash_from_field(self):
category = utils.create_category()
topic_hash = '1' * 32
form_data = {
'comment': 'foo',
'title': 'foobar',
'category': category.pk,
'topic_hash': topic_hash}
Expand Down

0 comments on commit 0921689

Please sign in to comment.