summaryrefslogtreecommitdiff
path: root/yaksh/test_views.py
diff options
context:
space:
mode:
authoradityacp2020-04-27 15:04:31 +0530
committeradityacp2020-04-27 15:31:37 +0530
commitb8636965de8a86e68fd542754678e6826c7e5eac (patch)
tree638930dd42140d760e443c0d35874de6e27b3ef9 /yaksh/test_views.py
parente3f73db877aaf2f6406bef39aee590530bcb503b (diff)
parent53a0c4ad3e733f3960000527f83565f2fd8fc412 (diff)
downloadonline_test-b8636965de8a86e68fd542754678e6826c7e5eac.tar.gz
online_test-b8636965de8a86e68fd542754678e6826c7e5eac.tar.bz2
online_test-b8636965de8a86e68fd542754678e6826c7e5eac.zip
Resolve conflicts in travis, requirements and views
Diffstat (limited to 'yaksh/test_views.py')
-rw-r--r--yaksh/test_views.py537
1 files changed, 506 insertions, 31 deletions
diff --git a/yaksh/test_views.py b/yaksh/test_views.py
index ecfe872..70b090a 100644
--- a/yaksh/test_views.py
+++ b/yaksh/test_views.py
@@ -12,7 +12,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
@@ -30,9 +30,10 @@ from yaksh.models import (
User, Profile, Question, Quiz, QuestionPaper, AnswerPaper, Answer, Course,
AssignmentUpload, McqTestCase, IntegerTestCase, StringTestCase,
FloatTestCase, FIXTURES_DIR_PATH, LearningModule, LearningUnit, Lesson,
- LessonFile, CourseStatus, dict_to_yaml
+ 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
from online_test.celery import app
@@ -1126,11 +1127,10 @@ class TestAddQuiz(TestCase):
If not logged in redirect to login page
"""
response = self.client.get(
- reverse('yaksh:add_quiz',
- kwargs={'course_id': self.course.id,
- 'module_id': self.module.id}),
- follow=True
- )
+ reverse('yaksh:add_quiz', kwargs={
+ 'course_id': self.course.id,
+ 'module_id': self.module.id
+ }), follow=True)
redirect_destination = (
'/exam/login/?next=/exam/manage/addquiz/{0}/{1}/'.format(
self.course.id, self.module.id
@@ -1147,11 +1147,10 @@ class TestAddQuiz(TestCase):
password=self.student_plaintext_pass
)
response = self.client.get(
- reverse('yaksh:add_quiz',
- kwargs={'course_id': self.course.id,
- 'module_id': self.module.id}),
- follow=True
- )
+ reverse('yaksh:add_quiz', kwargs={
+ 'course_id': self.course.id,
+ 'module_id': self.module.id
+ }), follow=True)
self.assertEqual(response.status_code, 404)
def test_add_quiz_get(self):
@@ -1163,11 +1162,10 @@ class TestAddQuiz(TestCase):
password=self.user_plaintext_pass
)
response = self.client.get(
- reverse('yaksh:add_quiz',
- kwargs={'course_id': self.course.id,
- 'module_id': self.module.id}
- )
- )
+ reverse('yaksh:add_quiz', kwargs={
+ 'course_id': self.course.id,
+ 'module_id': self.module.id
+ }))
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'yaksh/add_quiz.html')
self.assertIsNotNone(response.context['form'])
@@ -1182,10 +1180,11 @@ class TestAddQuiz(TestCase):
)
tzone = pytz.timezone('UTC')
response = self.client.post(
- reverse('yaksh:edit_quiz',
- kwargs={'course_id': self.course.id,
- 'module_id': self.module.id,
- 'quiz_id': self.quiz.id}),
+ reverse('yaksh:edit_quiz', kwargs={
+ 'course_id': self.course.id,
+ 'module_id': self.module.id,
+ 'quiz_id': self.quiz.id
+ }),
data={
'start_date_time': '2016-01-10 09:00:15',
'end_date_time': '2016-01-15 09:00:15',
@@ -1319,10 +1318,11 @@ class TestAddQuiz(TestCase):
password=self.user_plaintext_pass
)
response = self.client.post(
- reverse('yaksh:edit_exercise',
- kwargs={'course_id': self.course.id,
- 'module_id': self.module.id,
- 'quiz_id': self.exercise.id}),
+ reverse('yaksh:edit_exercise', kwargs={
+ 'course_id': self.course.id,
+ 'module_id': self.module.id,
+ 'quiz_id': self.exercise.id
+ }),
data={
'description': 'updated demo exercise',
'active': True
@@ -1347,9 +1347,10 @@ class TestAddQuiz(TestCase):
password=self.user_plaintext_pass
)
response = self.client.post(
- reverse('yaksh:add_exercise',
- kwargs={'course_id': self.course.id,
- 'module_id': self.module.id}),
+ reverse('yaksh:add_exercise', kwargs={
+ 'course_id': self.course.id,
+ 'module_id': self.module.id
+ }),
data={
'description': "Demo Exercise",
'active': True
@@ -2284,7 +2285,7 @@ class TestSearchFilters(TestCase):
# Create moderator group
self.mod_group = Group.objects.create(name="moderator")
- #Create user1 with profile
+ # Create user1 with profile
self.user1_plaintext_pass = "demo1"
self.user1 = User.objects.create_user(
username='demo_user1',
@@ -4336,7 +4337,9 @@ class TestDownloadCsv(TestCase):
kwargs={'course_id': self.course.id}),
follow=True
)
- file_name = "{0}.csv".format(self.course.name.lower().replace(" ", "_"))
+ file_name = "{0}.csv".format(
+ self.course.name.lower().replace(" ", "_")
+ )
self.assertEqual(response.status_code, 200)
self.assertEqual(response.get('Content-Disposition'),
'attachment; filename="{0}"'.format(file_name))
@@ -6314,3 +6317,475 @@ class TestLessons(TestCase):
)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()['data'], '<p>test description</p>')
+
+
+class TestPost(TestCase):
+ def setUp(self):
+ self.client = Client()
+ self.mod_group = Group.objects.create(name='moderator')
+
+ self.student_plaintext_pass = 'student'
+ self.student = User.objects.create_user(
+ username='student',
+ password=self.student_plaintext_pass,
+ first_name='first_name',
+ last_name='last_name',
+ email='student@test.com'
+ )
+
+ Profile.objects.create(
+ user=self.student,
+ roll_number=10,
+ institute='IIT',
+ department='Chemical',
+ position='student',
+ timezone='UTC'
+ )
+
+ # moderator
+ self.user_plaintext_pass = 'demo'
+ self.user = User.objects.create_user(
+ username='demo_user',
+ password=self.user_plaintext_pass,
+ first_name='first_name',
+ last_name='last_name',
+ email='demo@test.com'
+ )
+
+ Profile.objects.create(
+ user=self.user,
+ roll_number=10,
+ institute='IIT',
+ department='Chemical',
+ position='Moderator',
+ timezone='UTC'
+ )
+
+ self.course = Course.objects.create(
+ name="Python Course",
+ 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):
+ url = reverse('yaksh:course_forum', kwargs= {
+ 'course_id': self.course.id
+ })
+ 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)
+ )
+ self.assertRedirects(response, redirection_url)
+
+ def test_view_course_forum(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, follow=True)
+ self.assertEquals(response.status_code, 200)
+ self.assertTemplateUsed(response, 'yaksh/course_forum.html')
+
+ 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)
+ 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
+ })
+ 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',
+ }
+ 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',
+ description='post 1 description',
+ course=self.course,
+ creator=self.student
+ )
+ url = reverse('yaksh:post_comments', kwargs={
+ 'course_id': self.course.id,
+ 'uuid': post.uid
+ })
+ 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.user.username,
+ password=self.user_plaintext_pass
+ )
+ self.course.students.add(self.user)
+ post = Post.objects.create(
+ title='post 1',
+ description='post 1 description',
+ course=self.course,
+ creator=self.user
+ )
+ url = reverse('yaksh:hide_post', kwargs={
+ 'course_id': self.course.id,
+ 'uuid': post.uid
+ })
+ response = self.client.get(url, follow=True)
+ self.assertEqual(response.status_code, 200)
+
+ def tearDown(self):
+ self.client.logout()
+ self.user.delete()
+ self.course.delete()
+ self.mod_group.delete()
+
+
+class TestPostComment(TestCase):
+ def setUp(self):
+ self.client = Client()
+ self.mod_group = Group.objects.create(name='moderator')
+
+ self.student_plaintext_pass = 'student'
+ self.student = User.objects.create_user(
+ username='student',
+ password=self.student_plaintext_pass,
+ first_name='first_name',
+ last_name='last_name',
+ email='student@test.com'
+ )
+
+ Profile.objects.create(
+ user=self.student,
+ roll_number=10,
+ institute='IIT',
+ department='Chemical',
+ position='student',
+ timezone='UTC'
+ )
+
+ # moderator
+ self.user_plaintext_pass = 'demo'
+ self.user = User.objects.create_user(
+ username='demo_user',
+ password=self.user_plaintext_pass,
+ first_name='first_name',
+ last_name='last_name',
+ email='demo@test.com'
+ )
+
+ Profile.objects.create(
+ user=self.user,
+ roll_number=10,
+ institute='IIT',
+ department='Chemical',
+ position='Moderator',
+ timezone='UTC'
+ )
+
+ self.course = Course.objects.create(
+ name="Python Course",
+ enrollment="Enroll Request", creator=self.user
+ )
+
+ self.post = Post.objects.create(
+ title='post 1',
+ description='post 1 description',
+ course=self.course,
+ creator=self.student
+ )
+
+ 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: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.user.username,
+ password=self.user_plaintext_pass
+ )
+ self.course.students.add(self.user)
+ comment = Comment.objects.create(
+ post_field=self.post,
+ description='post 1 comment',
+ creator=self.user
+ )
+ 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):
+ self.client.logout()
+ self.user.delete()
+ self.course.delete()
+ self.mod_group.delete()