summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--yaksh/templates/yaksh/course_forum.html2
-rw-r--r--yaksh/templates/yaksh/post_comments.html4
-rw-r--r--yaksh/test_models.py44
-rw-r--r--yaksh/test_views.py290
4 files changed, 316 insertions, 24 deletions
diff --git a/yaksh/templates/yaksh/course_forum.html b/yaksh/templates/yaksh/course_forum.html
index ff83c1a..17dc287 100644
--- a/yaksh/templates/yaksh/course_forum.html
+++ b/yaksh/templates/yaksh/course_forum.html
@@ -73,7 +73,7 @@
</td>
<td>
{% if user.profile.is_moderator %}
- <small><a href="{% url "yaksh:hide_post" course.id post.uid %}" class="pull-right">Delete</i></a></small>
+ <small><a href="{% url "yaksh:hide_post" course.id post.uid %}" class="pull-right btn btn-danger">Delete</i></a></small>
{% endif %}
</td>
</tr>
diff --git a/yaksh/templates/yaksh/post_comments.html b/yaksh/templates/yaksh/post_comments.html
index ee54c74..97d6eec 100644
--- a/yaksh/templates/yaksh/post_comments.html
+++ b/yaksh/templates/yaksh/post_comments.html
@@ -17,7 +17,7 @@
<small>
<strong>{{post.creator.username}}</strong>
{{post.created_at|naturaltime}}
- {% if user.profile.is_moderator %}<a href="{% url "yaksh:hide_post" post.course.id post.uid %}" class="pull-right">Delete</a>{% endif %}
+ {% if user.profile.is_moderator %}<a href="{% url "yaksh:hide_post" post.course.id post.uid %}" class="pull-right btn btn-danger">Delete</a>{% endif %}
</small>
</div>
@@ -44,7 +44,7 @@
<div>{{comment.description}}</div>
<small class="pull-right">
by: <strong>{{comment.creator.username}} </strong>. {{comment.created_at|naturaltime}}
- {% if user.profile.is_moderator %}<a href="{% url "yaksh:hide_comment" post.course.id comment.uid %}">Delete</a>{% endif %}
+ {% if user.profile.is_moderator %}<a href="{% url "yaksh:hide_comment" post.course.id comment.uid %}" class="btn btn-danger">Delete</a>{% endif %}
</small>
</div>
{% endfor %}
diff --git a/yaksh/test_models.py b/yaksh/test_models.py
index a57a61b..4e6b1ae 100644
--- a/yaksh/test_models.py
+++ b/yaksh/test_models.py
@@ -2320,3 +2320,47 @@ class PostModelTestCases(unittest.TestCase):
self.user3.delete()
self.course.delete()
self.post1.delete()
+
+
+class CommentModelTestCases(unittest.TestCase):
+ def setUp(self):
+ self.user1 = User.objects.create(
+ username='bart',
+ password='bart',
+ email='bart@test.com'
+ )
+ Profile.objects.create(
+ user=self.user1,
+ roll_number=1,
+ institute='IIT',
+ department='Chemical',
+ position='Student'
+ )
+ self.course = Course.objects.create(
+ name='Python Course',
+ enrollment='Enroll Request',
+ creator=self.user1
+ )
+ self.post1 = Post.objects.create(
+ title='Post 1',
+ course=self.course,
+ creator=self.user1,
+ description='Post 1 description'
+ )
+ self.comment1 = Comment.objects.create(
+ post_field=self.post1,
+ creator=self.user1,
+ description='Post 1 comment 1'
+ )
+
+ def test__str__(self):
+ self.assertEquals(
+ str(self.comment1.post_field.title),
+ self.comment1.post_field.title
+ )
+
+ def tearDown(self):
+ self.user1.delete()
+ self.course.delete()
+ self.post1.delete()
+ self.comment1.delete() \ No newline at end of file
diff --git a/yaksh/test_views.py b/yaksh/test_views.py
index 3b63b41..39d5865 100644
--- a/yaksh/test_views.py
+++ b/yaksh/test_views.py
@@ -11,7 +11,7 @@ import shutil
from markdown import Markdown
from django.contrib.auth.models import Group
from django.contrib.auth import authenticate
-from django.urls import reverse
+from django.urls import reverse, resolve
from django.test import TestCase
from django.test import Client
from django.http import Http404
@@ -29,7 +29,8 @@ from yaksh.models import (
FloatTestCase, FIXTURES_DIR_PATH, LearningModule, LearningUnit, Lesson,
LessonFile, CourseStatus, dict_to_yaml, Post, Comment
)
-from yaksh.views import add_as_moderator
+from yaksh.views import add_as_moderator, course_forum, post_comments
+from yaksh.forms import PostForm, CommentForm
from yaksh.decorators import user_has_profile
@@ -6337,10 +6338,23 @@ class TestPost(TestCase):
enrollment="Enroll Request", creator=self.user
)
+ def test_csrf(self):
+ self.client.login(
+ username=self.student.username,
+ password=self.student_plaintext_pass
+ )
+ self.course.students.add(self.student)
+ url = reverse('yaksh:course_forum', kwargs={
+ 'course_id': self.course.id
+ })
+ response = self.client.get(url)
+ self.assertContains(response, 'csrfmiddlewaretoken')
+
def test_view_course_forum_denies_anonymous_user(self):
- response = self.client.get(reverse('yaksh:course_forum', kwargs={
+ url = reverse('yaksh:course_forum', kwargs= {
'course_id': self.course.id
- }), follow=True)
+ })
+ response = self.client.get(url, follow=True)
self.assertEqual(response.status_code, 200)
redirection_url = '/exam/login/?next=/exam/forum/{0}/'.format(
str(self.course.id)
@@ -6353,30 +6367,116 @@ class TestPost(TestCase):
password=self.student_plaintext_pass
)
self.course.students.add(self.student)
- response = self.client.get(reverse('yaksh:course_forum', kwargs={
+ url = reverse('yaksh:course_forum', kwargs={
'course_id': self.course.id
- }), follow=True)
+ })
+ response = self.client.get(url, follow=True)
self.assertEquals(response.status_code, 200)
self.assertTemplateUsed(response, 'yaksh/course_forum.html')
- def test_create_post(self):
+ def test_view_course_forum_not_found_status_code(self):
self.client.login(
username=self.student.username,
password=self.student_plaintext_pass
)
self.course.students.add(self.student)
- response = self.client.post(reverse('yaksh:course_forum', kwargs={
+ url = reverse('yaksh:course_forum', kwargs={
+ 'course_id': 99
+ })
+ response = self.client.get(url)
+ self.assertEquals(response.status_code, 404)
+
+ def test_course_forum_url_resolves_course_forum_view(self):
+ view = resolve('/exam/forum/1/')
+ self.assertEqual(view.func, course_forum)
+
+ def test_course_forum_contains_link_to_post_comments_page(self):
+ # create a post in setup
+ self.client.login(
+ username=self.student.username,
+ password=self.student_plaintext_pass
+ )
+ self.course.students.add(self.student)
+ url = reverse('yaksh:course_forum', kwargs={
'course_id': self.course.id
- }), data={
+ })
+ post = Post.objects.create(
+ title='post 1',
+ description='post 1 description',
+ course=self.course,
+ creator=self.student
+ )
+ response = self.client.get(url)
+ post_comments_url = reverse('yaksh:post_comments', kwargs={
+ 'course_id': self.course.id,
+ 'uuid': post.uid
+ })
+ self.assertContains(response, 'href="{0}'.format(post_comments_url))
+
+
+ def test_new_post_valid_post_data(self):
+ self.client.login(
+ username=self.student.username,
+ password=self.student_plaintext_pass
+ )
+ self.course.students.add(self.student)
+ url = reverse('yaksh:course_forum', kwargs={
+ 'course_id': self.course.id
+ })
+ data = {
"title": 'Post 1',
"description": 'Post 1 description',
- })
- self.assertEquals(response.status_code, 302)
+ }
+ response = self.client.post(url, data)
+ # This shouldn't be 302. Check where does it redirects.
result = Post.objects.filter(title='Post 1',
creator=self.student,
course=self.course)
self.assertTrue(result.exists())
+ def test_new_post_invalid_post_data(self):
+ self.client.login(
+ username=self.student.username,
+ password=self.student_plaintext_pass
+ )
+ self.course.students.add(self.student)
+ url = reverse('yaksh:course_forum', kwargs={
+ 'course_id': self.course.id
+ })
+ data = {}
+ response = self.client.post(url, data)
+ self.assertEquals(response.status_code, 200)
+
+ def test_new_post_invalid_post_data_empty_fields(self):
+ self.client.login(
+ username=self.student.username,
+ password=self.student_plaintext_pass
+ )
+ self.course.students.add(self.student)
+ url = reverse('yaksh:course_forum', kwargs={
+ 'course_id': self.course.id
+ })
+ data = {
+ "title": '',
+ "description": '',
+ }
+ response = self.client.post(url, data)
+ self.assertEquals(response.status_code, 200)
+ self.assertFalse(Post.objects.exists())
+
+ def test_contains_form(self):
+ self.client.login(
+ username=self.student.username,
+ password=self.student_plaintext_pass
+ )
+ self.course.students.add(self.student)
+ url = reverse('yaksh:course_forum', kwargs={
+ 'course_id': self.course.id
+ })
+ response = self.client.get(url)
+ form = response.context.get('form')
+ self.assertIsInstance(form, PostForm)
+
def test_open_created_post_denies_anonymous_user(self):
post = Post.objects.create(
title='post 1',
@@ -6384,16 +6484,36 @@ class TestPost(TestCase):
course=self.course,
creator=self.student
)
- response = self.client.get(reverse('yaksh:post_comments', kwargs={
+ url = reverse('yaksh:post_comments', kwargs={
'course_id': self.course.id,
'uuid': post.uid
- }), follow=True)
+ })
+ response = self.client.get(url, follow=True)
self.assertEqual(response.status_code, 200)
redirection_url = '/exam/login/?next=/exam/forum/{0}/post/{1}/'.format(
str(self.course.id), str(post.uid)
)
self.assertRedirects(response, redirection_url)
+ def test_new_post_invalid_post_data(self):
+ """
+ Invalid post data should not redirect
+ The expected behavior is to show form again with validation errors
+ """
+ self.client.login(
+ username=self.student.username,
+ password=self.student_plaintext_pass
+ )
+ self.course.students.add(self.student)
+ url = reverse('yaksh:course_forum', kwargs={
+ 'course_id': self.course.id
+ })
+ data = {}
+ response = self.client.post(url, data)
+ form = response.context.get('form')
+ self.assertEquals(response.status_code, 200)
+ self.assertTrue(form.errors)
+
def test_hide_post(self):
self.client.login(
username=self.student.username,
@@ -6406,10 +6526,11 @@ class TestPost(TestCase):
course=self.course,
creator=self.student
)
- response = self.client.get(reverse('yaksh:hide_post', kwargs={
+ url = reverse('yaksh:hide_post', kwargs={
'course_id': self.course.id,
'uuid': post.uid
- }), follow=True)
+ })
+ response = self.client.get(url, follow=True)
self.assertEqual(response.status_code, 200)
def tearDown(self):
@@ -6473,24 +6594,150 @@ class TestPostComment(TestCase):
creator=self.student
)
- def test_create_post_comment(self):
+ def test_csrf(self):
self.client.login(
username=self.student.username,
password=self.student_plaintext_pass
)
self.course.students.add(self.student)
- response = self.client.post(reverse('yaksh:post_comments', kwargs={
+ url = reverse('yaksh:post_comments', kwargs={
'course_id': self.course.id,
'uuid': self.post.uid
- }), {
+ })
+ response = self.client.get(url)
+ self.assertContains(response, 'csrfmiddlewaretoken')
+
+ def test_post_comments_view_success_status_code(self):
+ self.client.login(
+ username=self.student.username,
+ password=self.student_plaintext_pass
+ )
+ self.course.students.add(self.student)
+ url = reverse('yaksh:post_comments', kwargs={
+ 'course_id': self.course.id,
+ 'uuid': self.post.uid
+ })
+ response = self.client.get(url)
+ self.assertEquals(response.status_code, 200)
+
+ def test_post_comments_view_not_found_status_code(self):
+ self.client.login(
+ username=self.student.username,
+ password=self.student_plaintext_pass
+ )
+ self.course.students.add(self.student)
+ url = reverse('yaksh:post_comments', kwargs={
+ 'course_id': 99,
+ 'uuid': '90da38ad-06fa-451b-9e82-5035e839da90'
+ })
+ response = self.client.get(url)
+ self.assertEquals(response.status_code, 404)
+
+ def test_post_comments_url_resolves_post_comments_view(self):
+ view = resolve(
+ '/exam/forum/1/post/90da38ad-06fa-451b-9e82-5035e839da89/'
+ )
+ self.assertEquals(view.func, post_comments)
+
+ def test_post_comments_view_contains_link_back_to_course_forum_view(self):
+ self.client.login(
+ username=self.student.username,
+ password=self.student_plaintext_pass
+ )
+ self.course.students.add(self.student)
+ comment_url = reverse('yaksh:post_comments', kwargs={
+ 'course_id': self.course.id,
+ 'uuid': self.post.uid
+ })
+ course_forum_url = reverse('yaksh:course_forum', kwargs={
+ 'course_id': self.course.id
+ })
+ response = self.client.get(comment_url)
+ self.assertContains(response, 'href="{0}"'.format(course_forum_url))
+
+ def test_post_comments_valid_post_data(self):
+ self.client.login(
+ username=self.student.username,
+ password=self.student_plaintext_pass
+ )
+ self.course.students.add(self.student)
+ url = reverse('yaksh:post_comments', kwargs={
+ 'course_id': self.course.id,
+ 'uuid': self.post.uid
+ })
+ data = {
'post_field': self.post,
'description': 'post 1 comment',
'creator': self.user,
- })
+ }
+ response = self.client.post(url, data)
self.assertEquals(response.status_code, 302)
result = Comment.objects.filter(post_field__uid=self.post.uid)
self.assertTrue(result.exists())
+ def test_post_comments_invalid_post_data(self):
+ self.client.login(
+ username=self.student.username,
+ password=self.student_plaintext_pass
+ )
+ self.course.students.add(self.student)
+ url = reverse('yaksh:post_comments', kwargs={
+ 'course_id': self.course.id,
+ 'uuid': self.post.uid
+ })
+ data = {}
+ response = self.client.post(url, data)
+ self.assertEquals(response.status_code, 200)
+
+ def test_post_comments_post_data_empty_fields(self):
+ self.client.login(
+ username=self.student.username,
+ password=self.student_plaintext_pass
+ )
+ self.course.students.add(self.student)
+ url = reverse('yaksh:post_comments', kwargs={
+ 'course_id': self.course.id,
+ 'uuid': self.post.uid
+ })
+ data = {
+ 'post_field': '',
+ 'description': '',
+ 'creator': '',
+ }
+ response = self.client.post(url, data)
+ self.assertEquals(response.status_code, 200)
+ self.assertFalse(Comment.objects.exists())
+
+ def test_contains_form(self):
+ self.client.login(
+ username=self.student.username,
+ password=self.student_plaintext_pass
+ )
+ self.course.students.add(self.student)
+ url = reverse('yaksh:post_comments', kwargs={
+ 'course_id': self.course.id,
+ 'uuid': self.post.uid
+ })
+ response = self.client.get(url)
+ form = response.context.get('form')
+ self.assertIsInstance(form, CommentForm)
+
+ def post_comment_invalid_post_data(self):
+ self.client.login(
+ username=self.student.username,
+ password=self.student_plaintext_pass
+ )
+ self.course.students.add(self.student)
+ url = reverse('yaksh:post_comments', kwargs={
+ 'course_id': self.course.id,
+ 'uuid': self.post.uid
+ })
+ data = {}
+ response = self.client.post(url, data)
+ form = response.context.get('form')
+ self.assertEquals(response.status_code, 200)
+ self.assertTrue(form.errors)
+
def test_hide_post_comment(self):
self.client.login(
username=self.student.username,
@@ -6502,10 +6749,11 @@ class TestPostComment(TestCase):
description='post 1 comment',
creator=self.user
)
- response = self.client.get(reverse('yaksh:hide_comment', kwargs={
+ url = reverse('yaksh:hide_comment', kwargs={
'course_id': self.course.id,
'uuid': comment.uid
- }))
+ })
+ response = self.client.get(url)
self.assertEquals(response.status_code, 302)
def tearDown(self):