summaryrefslogtreecommitdiff
path: root/yaksh/test_views.py
diff options
context:
space:
mode:
Diffstat (limited to 'yaksh/test_views.py')
-rw-r--r--yaksh/test_views.py290
1 files changed, 269 insertions, 21 deletions
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):