diff options
Diffstat (limited to 'yaksh/test_views.py')
-rw-r--r-- | yaksh/test_views.py | 491 |
1 files changed, 335 insertions, 156 deletions
diff --git a/yaksh/test_views.py b/yaksh/test_views.py index 3520c61..569d4d7 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.core.urlresolvers import reverse +from django.urls import reverse from django.test import TestCase from django.test import Client from django.http import Http404 @@ -20,6 +20,7 @@ from django.core import mail from django.conf import settings from django.core.files.uploadedfile import SimpleUploadedFile from django.core.files import File +from django.contrib.messages import get_messages from yaksh.models import ( @@ -420,29 +421,14 @@ class TestStudentDashboard(TestCase): response = self.client.get(reverse('yaksh:quizlist_user'), follow=True ) + courses_in_context = { + 'data': self.course, + 'completion_percentage': None, + } self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, "yaksh/quizzes_user.html") self.assertEqual(response.context['title'], 'All Courses') - self.assertEqual(response.context['courses'][0], self.course) - - def test_student_dashboard_enrolled_courses_get(self): - """ - Check student dashboard for all courses in which student is - enrolled - """ - self.client.login( - username=self.student.username, - password=self.student_plaintext_pass - ) - self.course.students.add(self.student) - response = self.client.get(reverse('yaksh:quizlist_user', - kwargs={'enrolled': "enrolled"}), - follow=True - ) - self.assertEqual(response.status_code, 200) - self.assertTemplateUsed(response, "yaksh/quizzes_user.html") - self.assertEqual(response.context['title'], 'Enrolled Courses') - self.assertEqual(response.context['courses'][0], self.course) + self.assertEqual(response.context['courses'][0], courses_in_context) def test_student_dashboard_hidden_courses_post(self): """ @@ -456,10 +442,14 @@ class TestStudentDashboard(TestCase): response = self.client.post(reverse('yaksh:quizlist_user'), data={'course_code': 'hide'} ) + courses_in_context = { + 'data': self.hidden_course, + 'completion_percentage': None, + } self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, "yaksh/quizzes_user.html") - self.assertEqual(response.context['title'], 'Search') - self.assertEqual(response.context['courses'][0], self.hidden_course) + self.assertEqual(response.context['title'], 'Search Results') + self.assertEqual(response.context['courses'][0], courses_in_context) class TestMonitor(TestCase): @@ -598,7 +588,7 @@ class TestMonitor(TestCase): ) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, "yaksh/monitor.html") - self.assertEqual(response.context['course_details'][0], self.course) + self.assertEqual(response.context['objects'][0], self.course) self.assertEqual(response.context['msg'], "Monitor") def test_monitor_display_quiz_results(self): @@ -784,7 +774,7 @@ class TestGradeUser(TestCase): ) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, "yaksh/grade_user.html") - self.assertEqual(response.context['course_details'][0], self.course) + self.assertEqual(response.context['objects'][0], self.course) def test_grade_user_get_quiz_users(self): """ @@ -948,13 +938,14 @@ class TestDownloadAssignment(TestCase): # create assignment file assignment_file1 = SimpleUploadedFile("file1.txt", b"Test") assignment_file2 = SimpleUploadedFile("file2.txt", b"Test") - SimpleUploadedFile("file3.txt", b"Test") self.assignment1 = AssignmentUpload.objects.create( user=self.student1, assignmentQuestion=self.question, + course=self.course, assignmentFile=assignment_file1, question_paper=self.question_paper ) self.assignment2 = AssignmentUpload.objects.create( user=self.student2, assignmentQuestion=self.question, + course=self.course, assignmentFile=assignment_file2, question_paper=self.question_paper ) @@ -970,23 +961,25 @@ class TestDownloadAssignment(TestCase): self.learning_module.delete() self.learning_unit.delete() self.mod_group.delete() - dir_name = self.quiz.description.replace(" ", "_") + dir_name = self.course.name.replace(" ", "_") file_path = os.sep.join((settings.MEDIA_ROOT, dir_name)) if os.path.exists(file_path): shutil.rmtree(file_path) def test_download_assignment_denies_student(self): """ - Check download assignment denies student + Check download assignment denies student not enrolled in a course """ self.client.login( username=self.student1.username, password=self.student1_plaintext_pass ) - response = self.client.get(reverse('yaksh:download_quiz_assignment', - kwargs={'quiz_id': self.quiz.id}), - follow=True - ) + response = self.client.get( + reverse('yaksh:download_quiz_assignment', + kwargs={'quiz_id': self.quiz.id, + "course_id": self.course.id}), + follow=True + ) self.assertEqual(response.status_code, 404) def test_download_assignment_per_quiz(self): @@ -997,11 +990,13 @@ class TestDownloadAssignment(TestCase): username=self.user.username, password=self.user_plaintext_pass ) - response = self.client.get(reverse('yaksh:download_quiz_assignment', - kwargs={'quiz_id': self.quiz.id}), - follow=True - ) - file_name = "{0}_Assignment_files.zip".format(self.quiz.description) + response = self.client.get( + reverse('yaksh:download_quiz_assignment', + kwargs={'quiz_id': self.quiz.id, + 'course_id': self.course.id}), + follow=True + ) + file_name = "{0}_Assignment_files.zip".format(self.course.name) file_name = file_name.replace(" ", "_") self.assertEqual(response.status_code, 200) self.assertEqual(response.get('Content-Disposition'), @@ -1026,7 +1021,8 @@ class TestDownloadAssignment(TestCase): reverse('yaksh:download_user_assignment', kwargs={'quiz_id': self.quiz.id, 'question_id': self.question.id, - 'user_id': self.student2.id + 'user_id': self.student2.id, + 'course_id': self.course.id }), follow=True ) @@ -1191,9 +1187,6 @@ class TestAddQuiz(TestCase): self.assertEqual(updated_quiz.description, 'updated demo quiz') self.assertEqual(updated_quiz.pass_criteria, 40) - self.assertEqual(response.status_code, 302) - self.assertRedirects(response, '/exam/manage/courses/all_quizzes/') - def test_add_quiz_post_new_quiz(self): """ POST request to add quiz should add new quiz if no quiz exists @@ -1236,9 +1229,6 @@ class TestAddQuiz(TestCase): self.assertEqual(new_quiz.description, 'new demo quiz') self.assertEqual(new_quiz.pass_criteria, 50) - self.assertEqual(response.status_code, 302) - self.assertRedirects(response, '/exam/manage/courses/all_quizzes/') - def test_add_exercise_denies_anonymous(self): """ If not logged in redirect to login page @@ -1301,8 +1291,6 @@ class TestAddQuiz(TestCase): self.assertEqual(updated_exercise.description, 'updated demo exercise') self.assertEqual(updated_exercise.pass_criteria, 0) self.assertTrue(updated_exercise.is_exercise) - self.assertEqual(response.status_code, 302) - self.assertRedirects(response, '/exam/manage/courses/all_quizzes/') def test_add_exercise_post_new_exercise(self): """ @@ -1328,8 +1316,6 @@ class TestAddQuiz(TestCase): self.assertEqual(new_exercise.description, 'Demo Exercise') self.assertEqual(new_exercise.pass_criteria, 0) self.assertTrue(new_exercise.is_exercise) - self.assertEqual(response.status_code, 302) - self.assertRedirects(response, '/exam/manage/courses/all_quizzes/') def test_show_all_quizzes(self): self.client.login( @@ -1341,9 +1327,8 @@ class TestAddQuiz(TestCase): follow=True ) self.assertEqual(response.status_code, 200) - self.assertEqual(response.context['type'], "quiz") self.assertEqual(response.context['quizzes'][0], self.quiz) - self.assertTemplateUsed(response, "yaksh/courses.html") + self.assertTemplateUsed(response, "yaksh/quizzes.html") class TestAddAsModerator(TestCase): @@ -1702,7 +1687,8 @@ class TestRemoveTeacher(TestCase): institute='IIT', department='Chemical', position='Moderator', - timezone='UTC' + timezone='UTC', + is_moderator=True ) # Create Student @@ -1818,16 +1804,10 @@ class TestRemoveTeacher(TestCase): ), data={'remove': teacher_id_list} ) - - self.assertEqual(response.status_code, 302) - redirect_destination = '/exam/manage/courses' - self.assertRedirects( - response, redirect_destination, status_code=302, - target_status_code=301 + self.assertEqual(response.status_code, 200) + self.assertFalse( + self.course.teachers.filter(id__in=teacher_id_list).exists() ) - for t_id in teacher_id_list: - teacher = User.objects.get(id=t_id) - self.assertNotIn(teacher, self.course.teachers.all()) class TestCourses(TestCase): @@ -1919,7 +1899,7 @@ class TestCourses(TestCase): # Create a learning module to add to course self.learning_module = LearningModule.objects.create( order=0, name="test module", description="module", - check_prerequisite=False, creator=self.teacher) + check_prerequisite=True, creator=self.teacher) self.user1_course = Course.objects.create( name="Python Course", @@ -2074,7 +2054,8 @@ class TestCourses(TestCase): ) # Student is not allowed if not enrolled in the course err_msg = "You are not enrolled for this course!" - self.assertEqual(response.context['msg'], err_msg) + messages = [m.message for m in get_messages(response.wsgi_request)] + self.assertEqual(messages[0], err_msg) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, "yaksh/quizzes_user.html") @@ -2090,7 +2071,8 @@ class TestCourses(TestCase): ) err_msg = "{0} is either expired or not active".format( self.user1_course.name) - self.assertEqual(response.context['msg'], err_msg) + messages = [m.message for m in get_messages(response.wsgi_request)] + self.assertEqual(messages[0], err_msg) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, "yaksh/quizzes_user.html") @@ -2140,8 +2122,8 @@ class TestCourses(TestCase): ) err_msg = "You do not have permissions" self.assertEqual(response.status_code, 200) - self.assertTemplateUsed(response, "yaksh/complete.html") - self.assertIn(err_msg, response.context['message']) + messages = [m.message for m in get_messages(response.wsgi_request)] + self.assertIn(err_msg, messages[0]) # Test clone/duplicate courses and create copies of modules and units @@ -2260,6 +2242,154 @@ class TestCourses(TestCase): self.user1_course.learning_module.remove(self.learning_module1) +class TestSearchFilters(TestCase): + def setUp(self): + self.client = Client() + + # Create moderator group + self.mod_group = Group.objects.create(name="moderator") + + #Create user1 with profile + self.user1_plaintext_pass = "demo1" + self.user1 = User.objects.create_user( + username='demo_user1', + password=self.user1_plaintext_pass, + first_name='user1_first_name', + last_name='user1_last_name', + email='demo1@test.com' + ) + Profile.objects.create( + user=self.user1, + roll_number=10, + institute="IIT", + department="Chemical", + position="moderator", + timezone="UTC", + is_moderator=True + ) + + # Add user1 to moderator group + self.mod_group.user_set.add(self.user1) + + # Create courses for user1 + self.user1_course1 = Course.objects.create( + name="Demo Course", + enrollment="Enroll Request", creator=self.user1) + self.user1_course2 = Course.objects.create( + name="Test Course", + enrollment="Enroll Request", creator=self.user1) + + # Create learning modules for user1 + self.learning_module1 = LearningModule.objects.create( + order=0, name="Demo Module", description="Demo Module", + check_prerequisite=False, creator=self.user1) + self.learning_module2 = LearningModule.objects.create( + order=0, name="Test Module", description="Test Module", + check_prerequisite=False, creator=self.user1) + + # Create quizzes for user1 + self.quiz1 = Quiz.objects.create( + time_between_attempts=0, description='Demo Quiz', + creator=self.user1) + self.question_paper1 = QuestionPaper.objects.create( + quiz=self.quiz1, total_marks=1.0) + + self.quiz2 = Quiz.objects.create( + time_between_attempts=0, description='Test Quiz', + creator=self.user1) + self.question_paper2 = QuestionPaper.objects.create( + quiz=self.quiz2, total_marks=1.0) + + # Create lessons for user1 + self.lesson1 = Lesson.objects.create( + name="Demo Lesson", description="Demo Lession", + creator=self.user1) + self.lesson2 = Lesson.objects.create( + name="Test Lesson", description="Test Lesson", + creator=self.user1) + + # Create units for lesson and quiz + self.lesson_unit1 = LearningUnit.objects.create( + order=1, type="lesson", lesson=self.lesson1) + self.lesson_unit2 = LearningUnit.objects.create( + order=1, type="lesson", lesson=self.lesson2) + self.quiz_unit1 = LearningUnit.objects.create( + order=2, type="quiz", quiz=self.quiz1) + self.quiz_unit2 = LearningUnit.objects.create( + order=2, type="quiz", quiz=self.quiz2) + + # Add units to module + self.learning_module1.learning_unit.add(self.lesson_unit1) + self.learning_module1.learning_unit.add(self.quiz_unit1) + self.learning_module2.learning_unit.add(self.lesson_unit2) + self.learning_module2.learning_unit.add(self.quiz_unit2) + + def tearDown(self): + self.client.logout() + self.user1.delete() + self.mod_group.delete() + + def test_courses_search_filter(self): + """ Test to check if courses are obtained with tags and status """ + self.client.login( + username=self.user1.username, + password=self.user1_plaintext_pass + ) + response = self.client.post( + reverse('yaksh:courses'), + data={'course_tags': 'demo', 'course_status': 'active'} + ) + self.assertEqual(response.status_code, 200) + self.assertTemplateUsed(response, 'yaksh/courses.html') + self.assertIsNotNone(response.context['form']) + self.assertIn(self.user1_course1, response.context['courses']) + + def test_quizzes_search_filter(self): + """ Test to check if quizzes are obtained with tags and status """ + self.client.login( + username=self.user1.username, + password=self.user1_plaintext_pass + ) + response = self.client.post( + reverse('yaksh:show_all_quizzes'), + data={'quiz_tags': 'demo', 'quiz_status': 'active'} + ) + self.assertEqual(response.status_code, 200) + self.assertTemplateUsed(response, 'yaksh/quizzes.html') + self.assertIsNotNone(response.context['form']) + self.assertIn(self.quiz1, response.context['quizzes']) + + def test_lessons_search_filter(self): + """ Test to check if lessons are obtained with tags and status """ + self.client.login( + username=self.user1.username, + password=self.user1_plaintext_pass + ) + response = self.client.post( + reverse('yaksh:show_all_lessons'), + data={'lesson_tags': 'demo', 'lesson_status': 'active'} + ) + self.assertEqual(response.status_code, 200) + self.assertTemplateUsed(response, 'yaksh/lessons.html') + self.assertIsNotNone(response.context['form']) + self.assertIn(self.lesson1, response.context['lessons']) + + def test_learning_modules_search_filter(self): + """ Test to check if learning modules are obtained with tags and status """ + self.client.login( + username=self.user1.username, + password=self.user1_plaintext_pass + ) + response = self.client.post( + reverse('yaksh:show_all_modules'), + data={'module_tags': 'demo', 'module_status': 'active'} + ) + self.assertEqual(response.status_code, 200) + self.assertTemplateUsed(response, 'yaksh/modules.html') + self.assertIsNotNone(response.context['form']) + self.assertIn(self.learning_module1, response.context['modules']) + + class TestAddCourse(TestCase): def setUp(self): self.client = Client() @@ -2514,6 +2644,10 @@ class TestCourseDetail(TestCase): name="Python Course", enrollment="Enroll Request", creator=self.user1 ) + self.user1_othercourse = Course.objects.create( + name="Python Course II", + enrollment="Enroll Request", creator=self.user1 + ) self.learning_module = LearningModule.objects.create( name="test module", description="test description module", html_data="test html description module", creator=self.user1, @@ -2532,6 +2666,7 @@ class TestCourseDetail(TestCase): self.user2.delete() self.student.delete() self.user1_course.delete() + self.user1_othercourse.delete() self.mod_group.delete() def test_upload_users_with_correct_csv(self): @@ -2554,9 +2689,71 @@ class TestCourseDetail(TestCase): # Then uploaded_user = User.objects.filter(email="abc@xyz.com") self.assertEqual(uploaded_user.count(), 1) - self.assertEqual(response.status_code, 200) - self.assertIn('upload_details', response.context) - self.assertTemplateUsed(response, 'yaksh/course_detail.html') + self.assertEqual(response.status_code, 302) + messages = [m.message for m in get_messages(response.wsgi_request)] + self.assertIn("abc@xyz.com", messages[0]) + self.assertIn(uploaded_user.first(), self.user1_course.students.all()) + + def test_upload_existing_user(self): + # Given + self.client.login( + username=self.user1.username, password=self.user1_plaintext_pass) + csv_file_path = os.path.join(FIXTURES_DIR_PATH, 'existing_user.csv') + csv_file = open(csv_file_path, 'rb') + upload_file = SimpleUploadedFile(csv_file_path, csv_file.read()) + csv_file.close() + + # When + response = self.client.post( + reverse('yaksh:upload_users', + kwargs={'course_id': self.user1_course.id}), + data={'csv_file': upload_file}) + + # Then + self.assertEqual(response.status_code, 302) + messages = [m.message for m in get_messages(response.wsgi_request)] + self.assertIn("demo_user2", messages[0]) + self.assertIn(self.user2, self.user1_course.students.all()) + + def test_upload_same_user_multiple_course(self): + # Given + self.client.login( + username=self.user1.username, password=self.user1_plaintext_pass) + csv_file_path = os.path.join(FIXTURES_DIR_PATH, 'users_correct.csv') + csv_file = open(csv_file_path, 'rb') + upload_file1 = SimpleUploadedFile(csv_file_path, csv_file.read()) + csv_file.seek(0) + upload_file2 = SimpleUploadedFile(csv_file_path, csv_file.read()) + csv_file.close() + + # When + response1 = self.client.post( + reverse('yaksh:upload_users', + kwargs={'course_id': self.user1_course.id}), + data={'csv_file': upload_file1}) + + response2 = self.client.post( + reverse('yaksh:upload_users', + kwargs={'course_id': self.user1_othercourse.id}), + data={'csv_file': upload_file2}) + + # Then + uploaded_users = User.objects.filter(email='abc@xyz.com') + self.assertEqual(response1.status_code, 302) + messages1 = [m.message for m in get_messages(response1.wsgi_request)] + self.assertIn('abc@xyz.com', messages1[0]) + self.assertEqual(response2.status_code, 302) + messages2 = [m.message for m in get_messages(response2.wsgi_request)] + self.assertIn('abc@xyz.com', messages2[0]) + self.assertIn('abc@xyz.com', messages2[1]) + self.assertTrue( + self.user1_course.students.filter( + id=uploaded_users.first().id).exists() + ) + self.assertTrue( + self.user1_othercourse.students.filter( + id=uploaded_users.first().id).exists() + ) def test_upload_users_add_update_reject(self): # Given @@ -2577,14 +2774,15 @@ class TestCourseDetail(TestCase): csv_file.close() # Then - uploaded_user = User.objects.filter(username="test") + uploaded_user = User.objects.filter(username="test2") user = uploaded_user[0] self.assertEqual(uploaded_user.count(), 1) self.assertEqual(user.first_name, "test2") self.assertIn(user, self.user1_course.get_rejected()) - self.assertEqual(response.status_code, 200) - self.assertIn('upload_details', response.context) - self.assertTemplateUsed(response, 'yaksh/course_detail.html') + self.assertEqual(response.status_code, 302) + messages = [m.message for m in get_messages(response.wsgi_request)] + self.assertIn('test2', messages[2]) + self.assertIn('User rejected', messages[2]) def test_upload_users_with_wrong_csv(self): # Given @@ -2605,11 +2803,9 @@ class TestCourseDetail(TestCase): csv_file.close() # Then - self.assertEqual(response.status_code, 200) - self.assertNotIn('upload_details', response.context) - self.assertIn('message', response.context) - self.assertEqual(response.context['message'], message) - self.assertTemplateUsed(response, 'yaksh/course_detail.html') + self.assertEqual(response.status_code, 302) + messages = [m.message for m in get_messages(response.wsgi_request)] + self.assertEqual('The file uploaded is not a CSV file.', messages[0]) def test_upload_users_csv_with_missing_headers(self): # Given @@ -2631,11 +2827,11 @@ class TestCourseDetail(TestCase): csv_file.close() # Then - self.assertEqual(response.status_code, 200) - self.assertNotIn('upload_details', response.context) - self.assertIn('message', response.context) - self.assertEqual(response.context['message'], message) - self.assertTemplateUsed(response, 'yaksh/course_detail.html') + self.assertEqual(response.status_code, 302) + messages = [m.message for m in get_messages(response.wsgi_request)] + self.assertIn( + 'The CSV file does not contain the required headers', messages[0] + ) def test_upload_users_csv_with_no_values(self): # Given @@ -2656,12 +2852,9 @@ class TestCourseDetail(TestCase): csv_file.close() # Then - self.assertEqual(response.status_code, 200) - self.assertIn('upload_details', response.context) - self.assertNotIn('message', response.context) - self.assertIn("No rows in the CSV file", - response.context['upload_details']) - self.assertTemplateUsed(response, 'yaksh/course_detail.html') + self.assertEqual(response.status_code, 302) + messages = [m.message for m in get_messages(response.wsgi_request)] + self.assertIn("No rows in the CSV file", messages[0]) def test_upload_users_csv_with_missing_values(self): ''' @@ -2697,10 +2890,9 @@ class TestCourseDetail(TestCase): # Then uploaded_user = User.objects.filter(email="dummy@xyz.com") self.assertEqual(uploaded_user.count(), 1) - self.assertEqual(response.status_code, 200) - self.assertIn('upload_details', response.context) - self.assertNotIn('message', response.context) - self.assertTemplateUsed(response, 'yaksh/course_detail.html') + self.assertEqual(response.status_code, 302) + messages = [m.message for m in get_messages(response.wsgi_request)] + self.assertIn("Missing Values", messages[0]) def test_course_detail_denies_anonymous(self): """ @@ -2783,7 +2975,7 @@ class TestCourseDetail(TestCase): 'user_id': self.student.id}) ) enrolled_student = self.user1_course.students.all() - self.assertEqual(response.status_code, 200) + self.assertEqual(response.status_code, 302) self.assertSequenceEqual([self.student], enrolled_student) def test_student_course_enroll_post(self): @@ -2800,7 +2992,7 @@ class TestCourseDetail(TestCase): data={'check': self.student1.id} ) enrolled_student = self.user1_course.students.all() - self.assertEqual(response.status_code, 200) + self.assertEqual(response.status_code, 302) self.assertSequenceEqual([self.student1], enrolled_student) def test_student_course_reject_get(self): @@ -2817,7 +3009,7 @@ class TestCourseDetail(TestCase): 'user_id': self.student.id}) ) enrolled_student = self.user1_course.rejected.all() - self.assertEqual(response.status_code, 200) + self.assertEqual(response.status_code, 302) self.assertSequenceEqual([self.student], enrolled_student) def test_student_course_reject_post(self): @@ -2834,7 +3026,7 @@ class TestCourseDetail(TestCase): data={'check': self.student1.id} ) enrolled_student = self.user1_course.rejected.all() - self.assertEqual(response.status_code, 200) + self.assertEqual(response.status_code, 302) self.assertSequenceEqual([self.student1], enrolled_student) def test_toggle_course_status_get(self): @@ -2912,7 +3104,7 @@ class TestCourseDetail(TestCase): ) self.assertEqual(get_response.status_code, 200) self.assertEqual(get_response.context['course'], self.user1_course) - self.assertEqual(get_response.context['state'], 'mail') + self.assertTrue(get_response.context['is_mail']) def test_download_users_template(self): """ Test to check download users template """ @@ -2958,7 +3150,7 @@ class TestCourseDetail(TestCase): response = self.client.get(reverse('yaksh:course_status', kwargs={'course_id': self.user1_course.id})) self.assertEqual(response.status_code, 200) - self.assertEqual(response.context['state'], "course_status") + self.assertTrue(response.context['is_progress']) self.assertEqual(response.context['course'], self.user1_course) student_details = response.context['student_details'][0] student, grade, percent, current_unit = student_details @@ -2973,7 +3165,7 @@ class TestCourseDetail(TestCase): response = self.client.get(reverse('yaksh:course_status', kwargs={'course_id': self.user1_course.id})) self.assertEqual(response.status_code, 200) - self.assertEqual(response.context['state'], "course_status") + self.assertTrue(response.context['is_progress']) self.assertEqual(response.context['course'], self.user1_course) student_details = response.context['student_details'][0] student, grade, percent, current_unit = student_details @@ -3696,7 +3888,7 @@ class TestPasswordReset(TestCase): # Then self.assertEqual(response.context['email'], self.user1.email) self.assertEqual(response.status_code, 302) - self.assertRedirects(response, '/exam/reset/password_reset/mail_sent/') + self.assertRedirects(response, '/exam/reset/password_reset/done/') def test_password_change_post(self): """ @@ -3921,37 +4113,8 @@ class TestModeratorDashboard(TestCase): response = self.client.get(reverse('yaksh:manage'), follow=True) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, "yaksh/moderator_dashboard.html") - self.assertEqual(response.context['trial_paper'][0], - self.trial_answerpaper) self.assertEqual(response.context['courses'][0], self.course) - def test_moderator_dashboard_delete_trial_papers(self): - """ - Check moderator dashboard to delete trial papers - """ - self.client.login( - username=self.user.username, - password=self.user_plaintext_pass - ) - self.course.is_trial = True - self.course.save() - response = self.client.post( - reverse('yaksh:manage'), - data={'delete_paper': [self.trial_answerpaper.id]} - ) - - self.assertEqual(response.status_code, 200) - self.assertTemplateUsed(response, "yaksh/moderator_dashboard.html") - updated_answerpaper = AnswerPaper.objects.filter(user=self.user) - updated_quiz = Quiz.objects.filter( - description=self.trial_question_paper.quiz.description - ) - updated_course = Course.objects.filter( - name=self.trial_course.name) - self.assertSequenceEqual(updated_answerpaper, []) - self.assertSequenceEqual(updated_quiz, []) - self.assertSequenceEqual(updated_course, []) - class TestUserLogin(TestCase): def setUp(self): @@ -4196,6 +4359,24 @@ class TestDownloadCsv(TestCase): self.assertEqual(response.get('Content-Disposition'), 'attachment; filename="{0}"'.format(file_name)) + def test_download_course_progress_csv(self): + """ + Check for csv result of a course progress + """ + self.client.login( + username=self.user.username, + password=self.user_plaintext_pass + ) + response = self.client.get( + reverse('yaksh:download_course_progress', + kwargs={'course_id': self.course.id}), + follow=True + ) + 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)) + def test_download_quiz_csv(self): """ Check for csv result of a quiz @@ -4369,7 +4550,9 @@ class TestShowQuestions(TestCase): ) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'yaksh/showquestions.html') - self.assertIn("download", response.context['msg']) + messages = [m.message for m in get_messages(response.wsgi_request)] + err_msg = "Please select atleast one question to download" + self.assertIn(err_msg, messages[0]) def test_upload_zip_questions(self): """ @@ -4411,7 +4594,8 @@ class TestShowQuestions(TestCase): ) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'yaksh/showquestions.html') - self.assertIn("ZIP file", response.context['message']) + messages = [m.message for m in get_messages(response.wsgi_request)] + self.assertIn("Please Upload a ZIP file", messages[0]) def test_upload_yaml_questions(self): """ @@ -5011,19 +5195,25 @@ class TestQuestionPaper(TestCase): ) self.assertEqual(response.status_code, 404) - def test_preview_qustionpaper_without_quiz_owner(self): + def test_preview_questionpaper_without_quiz_owner(self): self.client.login( username=self.teacher.username, password=self.teacher_plaintext_pass ) - # Should raise an HTTP 404 response + # Should pass successfully response = self.client.get( reverse('yaksh:preview_questionpaper', kwargs={"questionpaper_id": self.question_paper.id} ) ) - self.assertEqual(response.status_code, 404) + self.assertEqual(response.status_code, 200) + self.assertTemplateUsed(response, 'yaksh/preview_questionpaper.html') + self.assertEqual( + response.context['questions'], + self.questions_list + ) + self.assertEqual(response.context['paper'], self.question_paper) def test_mcq_attempt_right_after_wrong(self): """ Case:- Check if answerpaper and answer marks are updated after @@ -5307,7 +5497,7 @@ class TestQuestionPaper(TestCase): # Design question paper for a quiz response = self.client.post( - reverse('yaksh:design_questionpaper', + reverse('yaksh:designquestionpaper', kwargs={"quiz_id": self.quiz_without_qp.id}), data={"marks": "1.0", "question_type": "code"}) self.assertEqual(response.status_code, 200) @@ -5625,8 +5815,7 @@ class TestLearningModule(TestCase): "description": "my test1", "Save": "Save"}) - self.assertEqual(response.status_code, 302) - self.assertRedirects(response, self.expected_url) + self.assertEqual(response.status_code, 200) learning_module = LearningModule.objects.get(name="test module1") self.assertEqual(learning_module.description, "my test1") self.assertEqual(learning_module.creator, self.user) @@ -5649,8 +5838,7 @@ class TestLearningModule(TestCase): "description": "my test2", "Save": "Save"}) - self.assertEqual(response.status_code, 302) - self.assertRedirects(response, self.expected_url) + self.assertEqual(response.status_code, 200) learning_module = LearningModule.objects.get(name="test module2") self.assertEqual(learning_module.description, "my test2") self.assertEqual(learning_module.creator, self.user) @@ -5666,9 +5854,7 @@ class TestLearningModule(TestCase): ) response = self.client.get(reverse('yaksh:show_all_modules')) self.assertEqual(response.status_code, 200) - self.assertTemplateUsed(response, 'yaksh/courses.html') - self.assertEqual(response.context['type'], "learning_module") - self.assertEqual(response.context['learning_modules'][0], + self.assertEqual(response.context['modules'][0], self.learning_module) def test_teacher_can_edit_module(self): @@ -5685,8 +5871,7 @@ class TestLearningModule(TestCase): "description": "teacher module 2", "Save": "Save"}) - self.assertEqual(response.status_code, 302) - self.assertRedirects(response, "/exam/manage/courses/") + self.assertEqual(response.status_code, 200) learning_module = LearningModule.objects.get(name="teacher module 2") self.assertEqual(learning_module.description, "teacher module 2") self.assertEqual(learning_module.creator, self.user) @@ -5702,7 +5887,7 @@ class TestLearningModule(TestCase): kwargs={"module_id": self.learning_module1.id, "course_id": self.course.id}), data={"Add": "Add", - "choosen_list": ",".join([str(self.quiz.id)+":"+"quiz"]) + "chosen_list": ",".join([str(self.quiz.id)+":"+"quiz"]) }) # Test add learning unit @@ -5969,8 +6154,6 @@ class TestLessons(TestCase): self.learning_module.id, self.learning_module2.id]) self.course.teachers.add(self.teacher.id) - self.expected_url = "/exam/manage/courses/" - def tearDown(self): self.user.delete() self.student.delete() @@ -6018,18 +6201,16 @@ class TestLessons(TestCase): ) # Teacher edits existing lesson and adds file - self.assertEqual(response.status_code, 302) - self.assertRedirects(response, self.expected_url) + self.assertEqual(response.status_code, 200) updated_lesson = Lesson.objects.get(name="updated lesson") self.assertEqual(updated_lesson.description, "updated description") self.assertEqual(updated_lesson.creator, self.user) self.assertEqual(updated_lesson.html_data, Markdown().convert("updated description")) - self.assertEqual(os.path.basename(updated_lesson.video_file.name), - "test.mp4") + self.assertIn("test", os.path.basename(updated_lesson.video_file.name)) lesson_files = LessonFile.objects.filter( lesson=self.lesson).first() - self.assertIn("test.txt", lesson_files.file.name) + self.assertIn("test", lesson_files.file.name) lesson_file_path = lesson_files.file.path # Teacher removes the lesson file response = self.client.post( @@ -6039,8 +6220,7 @@ class TestLessons(TestCase): data={"delete_files": [str(lesson_files.id)], "Delete": "Delete"} ) - self.assertEqual(response.status_code, 302) - self.assertRedirects(response, self.expected_url) + self.assertEqual(response.status_code, 200) lesson_file_exists = LessonFile.objects.filter( lesson=self.lesson).exists() self.assertFalse(lesson_file_exists) @@ -6115,8 +6295,7 @@ class TestLessons(TestCase): ) response = self.client.get(reverse('yaksh:show_all_lessons')) self.assertEqual(response.status_code, 200) - self.assertTemplateUsed(response, "yaksh/courses.html") - self.assertEqual(response.context["type"], "lesson") + self.assertTemplateUsed(response, "yaksh/lessons.html") self.assertEqual(response.context["lessons"][0], self.lesson) def test_preview_lesson_description(self): |