diff options
Diffstat (limited to 'yaksh/test_views.py')
-rw-r--r-- | yaksh/test_views.py | 153 |
1 files changed, 151 insertions, 2 deletions
diff --git a/yaksh/test_views.py b/yaksh/test_views.py index 24cf7d8..06a4fa3 100644 --- a/yaksh/test_views.py +++ b/yaksh/test_views.py @@ -25,7 +25,7 @@ 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 + LessonFile, CourseStatus, dict_to_yaml ) from yaksh.decorators import user_has_profile @@ -1743,6 +1743,10 @@ class TestCourses(TestCase): self.lesson = Lesson.objects.create( name="demo lesson", description="test description", creator=self.user1) + lesson_file = SimpleUploadedFile("file1.mp4", b"Test") + django_file = File(lesson_file) + self.lesson.video_file.save(lesson_file.name, django_file, + save=True) self.lesson_unit = LearningUnit.objects.create( order=1, type="lesson", lesson=self.lesson) @@ -2002,6 +2006,63 @@ class TestCourses(TestCase): os.remove(file_path) shutil.rmtree(os.path.dirname(file_path)) + def test_download_course_offline(self): + """ Test to download course with lessons offline""" + + # Student fails to download course if not enrolled in that course + self.client.login( + username=self.student.username, + password=self.student_plaintext_pass + ) + response = self.client.get( + reverse('yaksh:download_course', + kwargs={"course_id": self.user1_course.id}), + follow=True + ) + self.assertEqual(response.status_code, 404) + + # Teacher/Moderator should be able to download course + self.client.login( + username=self.teacher.username, + password=self.teacher_plaintext_pass + ) + + # Should not allow to download if the course doesn't have lessons + self.user1_course.learning_module.add(self.learning_module) + response = self.client.get( + reverse('yaksh:download_course', + kwargs={"course_id": self.user1_course.id}), + follow=True + ) + self.user1_course.learning_module.remove(self.learning_module) + self.assertEqual(response.status_code, 404) + lesson_file = SimpleUploadedFile("file1.txt", b"Test") + django_file = File(lesson_file) + lesson_file_obj = LessonFile() + lesson_file_obj.lesson = self.lesson + lesson_file_obj.file.save(lesson_file.name, django_file, save=True) + self.user1_course.learning_module.add(self.learning_module1) + response = self.client.get( + reverse('yaksh:download_course', + kwargs={"course_id": self.user1_course.id}), + follow=True + ) + course_name = self.user1_course.name.replace(" ", "_") + self.assertEqual(response.status_code, 200) + zip_file = string_io(response.content) + zipped_file = zipfile.ZipFile(zip_file, 'r') + self.assertIsNone(zipped_file.testzip()) + files_in_zip = zipped_file.namelist() + module_path = os.path.join(course_name, "demo_module", + "demo_module.html") + lesson_path = os.path.join(course_name, "demo_module", "demo_lesson", + "demo_lesson.html") + self.assertIn(module_path, files_in_zip) + self.assertIn(lesson_path, files_in_zip) + zip_file.close() + zipped_file.close() + self.user1_course.learning_module.remove(self.learning_module1) + class TestAddCourse(TestCase): def setUp(self): @@ -3988,6 +4049,36 @@ class TestShowQuestions(TestCase): points=1.0, language="python", type="mcq", user=self.user, active=True ) + test_case_upload_data = [{"test_case": "assert fact(3)==6", + "test_case_type": "standardtestcase", + "test_case_args": "", + "weight": 1.0 + }] + question_data_1 = {"snippet": "def fact()", "active": True, + "points": 1.0, + "description": "factorial of a no", + "language": "Python", "type": "Code", + "testcase": test_case_upload_data, + "summary": "Yaml Demo 2", + "tags": ['yaml_demo'] + } + + question_data_2 = {"snippet": "def fact()", "active": True, + "points": 1.0, + "description": "factorial of a no", + "language": "Python", "type": "Code", + "testcase": test_case_upload_data, + "summary": "Yaml Demo 3", + "tags": ['yaml_demo'] + } + yaml_question_1 = dict_to_yaml(question_data_1) + yaml_question_2 = dict_to_yaml(question_data_2) + self.yaml_file_1 = SimpleUploadedFile("test1.yaml", + yaml_question_1.encode("utf-8") + ) + self.yaml_file_2 = SimpleUploadedFile("test2.yaml", + yaml_question_2.encode("utf-8") + ) def test_show_questions_denies_student(self): """ @@ -4050,7 +4141,7 @@ class TestShowQuestions(TestCase): self.assertTemplateUsed(response, 'yaksh/showquestions.html') self.assertIn("download", response.context['msg']) - def test_upload_questions(self): + def test_upload_zip_questions(self): """ Check for uploading questions zip file """ @@ -4090,6 +4181,59 @@ class TestShowQuestions(TestCase): self.assertTemplateUsed(response, 'yaksh/showquestions.html') self.assertIn("ZIP file", response.context['message']) + def test_upload_yaml_questions(self): + """ + Check for uploading questions yaml file + """ + self.client.login( + username=self.user.username, + password=self.user_plaintext_pass + ) + + response = self.client.post( + reverse('yaksh:show_questions'), + data={'file': self.yaml_file_1, + 'upload': 'upload'} + ) + uploaded_ques = Question.objects.filter( + active=True, summary="Yaml Demo 2", + user=self.user) + self.assertEqual(response.status_code, 200) + self.assertTemplateUsed(response, 'yaksh/showquestions.html') + self.assertEqual(uploaded_ques.count(), 1) + uploaded_ques.delete() + + def test_upload_multiple_yaml_zip_questions(self): + """ + Check for uploading questions zip file with + multiple yaml files + """ + self.client.login( + username=self.user.username, + password=self.user_plaintext_pass + ) + zipfile_name = string_io() + zip_file = zipfile.ZipFile(zipfile_name, "w") + zip_file.writestr("test1.yaml", self.yaml_file_1.read()) + zip_file.writestr("test2.yaml", self.yaml_file_2.read()) + zip_file.close() + zipfile_name.seek(0) + questions_file = SimpleUploadedFile("questions.zip", + zipfile_name.read(), + content_type="application/zip" + ) + response = self.client.post( + reverse('yaksh:show_questions'), + data={'file': questions_file, + 'upload': 'upload'} + ) + uploaded_ques = Question.objects.filter( + active=True, summary="Yaml Demo 2", + user=self.user).count() + self.assertEqual(response.status_code, 200) + self.assertTemplateUsed(response, 'yaksh/showquestions.html') + self.assertEqual(uploaded_ques, 1) + def test_attempt_questions(self): """ Check for testing questions @@ -5591,6 +5735,7 @@ class TestLessons(TestCase): password=self.teacher_plaintext_pass ) dummy_file = SimpleUploadedFile("test.txt", b"test") + video_file = SimpleUploadedFile("test.mp4", b"test") response = self.client.post( reverse('yaksh:edit_lesson', kwargs={"lesson_id": self.lesson.id, @@ -5598,6 +5743,7 @@ class TestLessons(TestCase): data={"name": "updated lesson", "description": "updated description", "Lesson_files": dummy_file, + "video_file": video_file, "Save": "Save"} ) @@ -5609,6 +5755,8 @@ class TestLessons(TestCase): 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") lesson_files = LessonFile.objects.filter( lesson=self.lesson).first() self.assertIn("test.txt", lesson_files.file.name) @@ -5627,6 +5775,7 @@ class TestLessons(TestCase): lesson=self.lesson).exists() self.assertFalse(lesson_file_exists) self.assertFalse(os.path.exists(lesson_file_path)) + updated_lesson.remove_file() def test_show_lesson(self): """ Student should be able to view lessons """ |