from datetime import datetime import pytz import os import json try: from StringIO import StringIO as string_io except ImportError: from io import BytesIO as string_io import zipfile 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.test import TestCase from django.test import Client from django.http import Http404 from django.utils import timezone from django.core import mail from django.conf import settings from django.core.files.uploadedfile import SimpleUploadedFile from django.core.files import File 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 ) from yaksh.views import add_as_moderator from yaksh.decorators import user_has_profile class TestUserRegistration(TestCase): def setUp(self): self.client = Client() self.mod_group = Group.objects.create(name='moderator') def tearDown(self): self.registered_user.delete() self.mod_group.delete() def test_register_user_post(self): self.client.post( reverse('yaksh:register'), data={'username': 'register_user', 'email': 'register_user@mail.com', 'password': 'reg_user', 'confirm_password': 'reg_user', 'first_name': 'user1_f_name', 'last_name': 'user1_l_name', 'roll_number': '1', 'institute': 'demo_institute', 'department': 'demo_dept', 'position': 'student', 'timezone': pytz.utc.zone } ) self.registered_user = User.objects.get(username='register_user') self.assertEqual(self.registered_user.email, 'register_user@mail.com') self.assertEqual(self.registered_user.first_name, 'user1_f_name') self.assertEqual(self.registered_user.last_name, 'user1_l_name') self.assertEqual(self.registered_user.profile.roll_number, '1') self.assertEqual(self.registered_user.profile.institute, 'demo_institute') self.assertEqual(self.registered_user.profile.department, 'demo_dept') self.assertEqual(self.registered_user.profile.position, 'student') self.assertEqual(self.registered_user.profile.timezone, 'UTC') class TestProfile(TestCase): def setUp(self): self.client = Client() self.mod_group = Group.objects.create(name='moderator') # Create User without profile self.user1_plaintext_pass = 'demo1' self.user1 = User.objects.create_user( username='demo_user1', password=self.user1_plaintext_pass, email='demo1@test.com' ) # Create User with profile self.user2_plaintext_pass = 'demo2' self.user2 = User.objects.create_user( username='demo_user2', password=self.user2_plaintext_pass, first_name='first_name', last_name='last_name', email='demo2@test.com' ) Profile.objects.create( user=self.user2, roll_number=10, institute='IIT', department='Chemical', position='Student', timezone='UTC' ) def tearDown(self): self.client.logout() self.user1.delete() self.user2.delete() self.mod_group.delete() def test_user_has_profile_for_user_without_profile(self): """ If no profile exists for user passed as argument return False """ has_profile_status = user_has_profile(self.user1) self.assertFalse(has_profile_status) def test_user_has_profile_for_user_with_profile(self): """ If profile exists for user passed as argument return True """ has_profile_status = user_has_profile(self.user2) self.assertTrue(has_profile_status) def test_view_profile_denies_anonymous(self): """ If not logged in redirect to login page """ response = self.client.get(reverse('yaksh:view_profile'), follow=True) redirect_destination = '/exam/login/?next=/exam/viewprofile/' self.assertRedirects(response, redirect_destination) def test_view_profile_get_for_user_without_profile(self): """ If no profile exists a blank profile form will be displayed """ self.client.login( username=self.user1.username, password=self.user1_plaintext_pass ) response = self.client.get(reverse('yaksh:view_profile')) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'yaksh/editprofile.html') def test_view_profile_get_for_user_with_profile(self): """ If profile exists a viewprofile.html template will be rendered """ self.client.login( username=self.user2.username, password=self.user2_plaintext_pass ) response = self.client.get(reverse('yaksh:view_profile')) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'yaksh/view_profile.html') def test_email_verification_for_user_post(self): """ POST request to verify email """ self.client.login( username=self.user2.username, password=self.user2_plaintext_pass ) post_response = self.client.post( reverse('yaksh:new_activation'), data={'email': self.user2.email} ) subject = mail.outbox[0].subject.replace(" ", "_") activation_key = mail.outbox[0].body.split("\n")[2].split("/")[-1] get_response = self.client.get( reverse('yaksh:activate', kwargs={'key': activation_key}), follow=True ) updated_profile_user = User.objects.get(id=self.user2.id) updated_profile = Profile.objects.get(user=updated_profile_user) self.assertEqual(post_response.status_code, 200) self.assertEqual(subject, "Yaksh_Email_Verification") self.assertEqual(get_response.status_code, 200) self.assertEqual(updated_profile.is_email_verified, True) self.assertTemplateUsed(get_response, 'yaksh/activation_status.html') post_response = self.client.post( reverse('yaksh:new_activation'), data={'email': 'user@mail.com'} ) self.assertEqual(post_response.status_code, 200) self.assertFalse(post_response.context['success']) self.assertTemplateUsed(get_response, 'yaksh/activation_status.html') def test_edit_profile_post(self): """ POST request to edit_profile view should update the user's profile """ self.client.login( username=self.user2.username, password=self.user2_plaintext_pass ) response = self.client.post( reverse('yaksh:edit_profile'), data={ 'user': self.user2, 'first_name': 'new_first_name', 'last_name': 'new_last_name', 'roll_number': 20, 'institute': 'new_institute', 'department': 'Aerospace', 'position': 'new_position', 'timezone': 'UTC' } ) updated_profile_user = User.objects.get(id=self.user2.id) updated_profile = Profile.objects.get(user=updated_profile_user) self.assertEqual(updated_profile_user.first_name, 'new_first_name') self.assertEqual(updated_profile_user.last_name, 'new_last_name') self.assertEqual(updated_profile.roll_number, '20') self.assertEqual(updated_profile.institute, 'new_institute') self.assertEqual(updated_profile.department, 'Aerospace') self.assertEqual(updated_profile.position, 'new_position') self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'yaksh/profile_updated.html') def test_edit_profile_post_for_user_without_profile(self): """ POST request to edit_profile view should update the user's profile """ self.client.login( username=self.user1.username, password=self.user1_plaintext_pass ) response = self.client.post( reverse('yaksh:edit_profile'), data={ 'user': self.user1, 'first_name': 'new_first_name', 'last_name': 'new_last_name', 'roll_number': 21, 'institute': 'new_institute', 'department': 'Aerospace', 'position': 'new_position', 'timezone': 'UTC' } ) updated_profile_user = User.objects.get(id=self.user1.id) updated_profile = Profile.objects.get(user=updated_profile_user) self.assertEqual(updated_profile_user.first_name, 'new_first_name') self.assertEqual(updated_profile_user.last_name, 'new_last_name') self.assertEqual(updated_profile.roll_number, '21') self.assertEqual(updated_profile.institute, 'new_institute') self.assertEqual(updated_profile.department, 'Aerospace') self.assertEqual(updated_profile.position, 'new_position') self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'yaksh/profile_updated.html') def test_edit_profile_get(self): """ GET request to edit profile should display profile form """ self.client.login( username=self.user2.username, password=self.user2_plaintext_pass ) response = self.client.get(reverse('yaksh:edit_profile')) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'yaksh/editprofile.html') def test_edit_profile_get_for_user_without_profile(self): """ If no profile exists a blank profile form will be displayed """ self.client.login( username=self.user1.username, password=self.user1_plaintext_pass ) response = self.client.get(reverse('yaksh:edit_profile')) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'yaksh/editprofile.html') def test_edit_profile_get_for_user_with_profile(self): """ If profile exists a editprofile.html template will be rendered """ self.client.login( username=self.user2.username, password=self.user2_plaintext_pass ) response = self.client.get(reverse('yaksh:edit_profile')) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'yaksh/editprofile.html') def test_update_email_for_user_post(self): """ POST request to update email if multiple users with same email are found """ self.client.login( username=self.user2.username, password=self.user2_plaintext_pass ) response = self.client.post( reverse('yaksh:update_email'), data={ 'username': self.user2.username, 'email': "demo_user2@mail.com" } ) updated_user = User.objects.get(id=self.user2.id) self.assertEqual(updated_user.email, "demo_user2@mail.com") self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'yaksh/activation_status.html') class TestStudentDashboard(TestCase): def setUp(self): self.client = Client() self.mod_group = Group.objects.create(name='moderator') # student 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' ) # student without profile self.student_no_profile_plaintext_pass = 'student2' self.student_no_profile = User.objects.create_user( username='student_no_profile', password=self.student_no_profile_plaintext_pass, first_name='first_name', last_name='last_name', email='student_no_profile@test.com' ) # 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.hidden_course = Course.objects.create( name="Hidden Course", enrollment="Enroll Request", creator=self.user, code="hide", hidden=True ) def tearDown(self): self.client.logout() self.user.delete() self.course.delete() self.mod_group.delete() def test_student_dashboard_denies_anonymous_user(self): """ Check student dashboard denies anonymous user """ response = self.client.get(reverse('yaksh:quizlist_user'), follow=True ) self.assertEqual(response.status_code, 200) redirection_url = '/exam/login/?next=/exam/quizzes/' self.assertRedirects(response, redirection_url) def test_student_dashboard_get_for_user_without_profile(self): """ If no profile exists a blank profile form will be displayed """ self.client.login( username=self.student_no_profile.username, password=self.student_no_profile_plaintext_pass ) response = self.client.get(reverse('yaksh:quizlist_user')) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'yaksh/editprofile.html') def test_student_dashboard_get_for_user_with_profile(self): """ If profile exists a editprofile.html template will be rendered """ self.client.login( username=self.student.username, password=self.student_plaintext_pass ) response = self.client.get(reverse('yaksh:quizlist_user')) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'yaksh/quizzes_user.html') def test_student_dashboard_all_courses_get(self): """ Check student dashboard for all non hidden courses """ self.client.login( username=self.student.username, password=self.student_plaintext_pass ) response = self.client.get(reverse('yaksh:quizlist_user'), follow=True ) 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) def test_student_dashboard_hidden_courses_post(self): """ Get courses for student based on the course code """ self.client.login( username=self.student.username, password=self.student_plaintext_pass ) response = self.client.post(reverse('yaksh:quizlist_user'), data={'course_code': 'hide'} ) 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) class TestMonitor(TestCase): def setUp(self): self.client = Client() self.mod_group = Group.objects.create(name='moderator') tzone = pytz.timezone('UTC') # Create Moderator with profile 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', is_moderator=True ) # Create Student self.student_plaintext_pass = 'demo_student' self.student = User.objects.create_user( username='demo_student', password=self.student_plaintext_pass, first_name='student_first_name', last_name='student_last_name', email='demo_student@test.com' ) Profile.objects.create( user=self.student, roll_number=10, institute='IIT', department='Chemical', position='Moderator', timezone='UTC' ) # Add to moderator group self.mod_group.user_set.add(self.user) self.course = Course.objects.create( name="Python Course", enrollment="Open Enrollment", creator=self.user ) self.quiz = Quiz.objects.create( start_date_time=datetime(2014, 10, 9, 10, 8, 15, 0, tzone), end_date_time=datetime(2015, 10, 9, 10, 8, 15, 0, tzone), duration=30, active=True, instructions="Demo Instructions", attempts_allowed=-1, time_between_attempts=0, description='demo quiz', pass_criteria=40, creator=self.user ) self.learning_unit = LearningUnit.objects.create( order=1, type="quiz", quiz=self.quiz) self.learning_module = LearningModule.objects.create( order=1, name="test module", description="test", creator=self.user, check_prerequisite=False) self.learning_module.learning_unit.add(self.learning_unit.id) self.course.learning_module.add(self.learning_module) self.question = Question.objects.create( summary="Test_question", description="Add two numbers", points=1.0, language="python", type="code", user=self.user ) self.question_paper = QuestionPaper.objects.create( quiz=self.quiz, total_marks=1.0, fixed_question_order=str(self.question) ) self.question_paper.fixed_questions.add(self.question) user_answer = "def add(a, b)\n\treturn a+b" self.new_answer = Answer( question=self.question, answer=user_answer, correct=True, error=json.dumps([]) ) self.new_answer.save() self.answerpaper = AnswerPaper.objects.create( user=self.student, question_paper=self.question_paper, attempt_number=1, start_time=datetime(2014, 10, 9, 10, 8, 15, 0, tzone), end_time=datetime(2014, 10, 9, 10, 15, 15, 0, tzone), user_ip="127.0.0.1", status="completed", passed=True, percent=1, marks_obtained=1, course=self.course ) self.answerpaper.answers.add(self.new_answer) self.answerpaper.questions_answered.add(self.question) def tearDown(self): self.client.logout() self.user.delete() self.student.delete() self.quiz.delete() self.course.delete() self.answerpaper.delete() self.question.delete() self.question_paper.delete() self.new_answer.delete() self.learning_module.delete() self.learning_unit.delete() self.mod_group.delete() def test_monitor_denies_student(self): """ Check Monitor denies student """ self.client.login( username=self.student.username, password=self.student_plaintext_pass ) response = self.client.get(reverse('yaksh:monitor'), follow=True ) self.assertEqual(response.status_code, 404) def test_monitor_display_quizzes(self): """ Check all the available quizzes in monitor """ self.client.login( username=self.user.username, password=self.user_plaintext_pass ) response = self.client.get(reverse('yaksh:monitor'), follow=True ) 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['msg'], "Monitor") def test_monitor_display_quiz_results(self): """ Check all the quiz results in monitor """ self.client.login( username=self.user.username, password=self.user_plaintext_pass ) response = self.client.get( reverse('yaksh:monitor', kwargs={'quiz_id': self.quiz.id, 'course_id': self.course.id}), follow=True ) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, "yaksh/monitor.html") self.assertEqual(response.context['msg'], "Quiz Results") self.assertEqual(response.context['papers'][0], self.answerpaper) self.assertEqual(response.context['latest_attempts'][0], self.answerpaper) def test_get_quiz_user_data(self): """ Check for getting user data for a quiz """ self.client.login( username=self.user.username, password=self.user_plaintext_pass ) response = self.client.get( reverse('yaksh:user_data', kwargs={'user_id': self.student.id, 'questionpaper_id': self.question_paper.id, 'course_id': self.course.id}), follow=True ) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'yaksh/user_data.html') self.assertEqual(response.context['data']['papers'][0], self.answerpaper) self.assertEqual(response.context['data']['profile'], self.student.profile) self.assertEqual(response.context['data']['user'], self.student) self.assertEqual(response.context['data']['questionpaperid'], str(self.question_paper.id)) class TestGradeUser(TestCase): def setUp(self): self.client = Client() self.mod_group = Group.objects.create(name='moderator') tzone = pytz.timezone('UTC') # Create Moderator with profile 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', is_moderator=True ) # Create Student self.student_plaintext_pass = 'demo_student' self.student = User.objects.create_user( username='demo_student', password=self.student_plaintext_pass, first_name='student_first_name', last_name='student_last_name', email='demo_student@test.com' ) Profile.objects.create( user=self.student, roll_number=10, institute='IIT', department='Chemical', position='Moderator', timezone='UTC' ) # Add to moderator group self.mod_group.user_set.add(self.user) self.course = Course.objects.create( name="Python Course", enrollment="Open Enrollment", creator=self.user) self.quiz = Quiz.objects.create( start_date_time=datetime(2014, 10, 9, 10, 8, 15, 0, tzone), end_date_time=datetime(2015, 10, 9, 10, 8, 15, 0, tzone), duration=30, active=True, instructions="Demo Instructions", attempts_allowed=-1, time_between_attempts=0, description='demo quiz', pass_criteria=40, creator=self.user ) self.learning_unit = LearningUnit.objects.create( order=1, type="quiz", quiz=self.quiz) self.learning_module = LearningModule.objects.create( order=1, name="test module", description="test", creator=self.user, check_prerequisite=False) self.learning_module.learning_unit.add(self.learning_unit.id) self.course.learning_module.add(self.learning_module) self.question = Question.objects.create( summary="Test_question", description="Add two numbers", points=1.0, language="python", type="code", user=self.user ) self.question_paper = QuestionPaper.objects.create( quiz=self.quiz, total_marks=1.0, fixed_question_order=str(self.question.id) ) self.question_paper.fixed_questions.add(self.question) user_answer = "def add(a, b)\n\treturn a+b" self.new_answer = Answer( question=self.question, answer=user_answer, correct=True, error=json.dumps([]), marks=0.5 ) self.new_answer.save() self.answerpaper = AnswerPaper.objects.create( user=self.student, question_paper=self.question_paper, attempt_number=1, start_time=datetime(2014, 10, 9, 10, 8, 15, 0, tzone), end_time=datetime(2014, 10, 9, 10, 15, 15, 0, tzone), user_ip="127.0.0.1", status="completed", passed=True, marks_obtained=0.5, course=self.course ) self.answerpaper.answers.add(self.new_answer) self.answerpaper.questions_answered.add(self.question) self.answerpaper.questions.add(self.question) def tearDown(self): self.client.logout() self.user.delete() self.student.delete() self.quiz.delete() self.course.delete() self.answerpaper.delete() self.question.delete() self.question_paper.delete() self.new_answer.delete() self.learning_module.delete() self.learning_unit.delete() def test_grade_user_denies_student(self): """ Check Grade User denies student """ self.client.login( username=self.student.username, password=self.student_plaintext_pass ) response = self.client.get(reverse('yaksh:grade_user'), follow=True ) self.assertEqual(response.status_code, 404) def test_grade_user_display_quizzes(self): """ Check all the available quizzes in grade user """ self.client.login( username=self.user.username, password=self.user_plaintext_pass ) response = self.client.get(reverse('yaksh:grade_user'), follow=True ) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, "yaksh/grade_user.html") self.assertEqual(response.context['course_details'][0], self.course) def test_grade_user_get_quiz_users(self): """ Check all the available users in quiz in grade user """ self.client.login( username=self.user.username, password=self.user_plaintext_pass ) response = self.client.get( reverse('yaksh:grade_user', kwargs={"quiz_id": self.quiz.id, 'course_id': self.course.id}), follow=True ) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, "yaksh/grade_user.html") self.assertEqual(response.context['users'][0]['user__first_name'], self.student.first_name) self.assertEqual(response.context['quiz'], self.quiz) self.assertFalse(response.context['has_quiz_assignments']) def test_grade_user_get_quiz_user_data(self): """ Check student attempts and answers """ self.client.login( username=self.user.username, password=self.user_plaintext_pass ) response = self.client.get( reverse('yaksh:grade_user', kwargs={"quiz_id": self.quiz.id, "course_id": self.course.id, "user_id": self.student.id}), follow=True ) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, "yaksh/grade_user.html") self.assertFalse(response.context['has_user_assignments']) self.assertEqual(response.context['quiz_id'], str(self.quiz.id)) self.assertEqual(response.context['user_id'], str(self.student.id)) self.assertEqual(response.context['attempts'][0], self.answerpaper) def test_grade_user_update_user_marks(self): """ Check update marks of student """ self.client.login( username=self.user.username, password=self.user_plaintext_pass ) self.client.get(reverse('yaksh:grade_user', kwargs={"quiz_id": self.quiz.id, "course_id": self.course.id, "user_id": self.student.id}), follow=True ) question_marks = "q{0}_marks".format(self.question.id) response = self.client.post( reverse( 'yaksh:grade_user', kwargs={"quiz_id": self.quiz.id, "user_id": self.student.id, "course_id": self.course.id, "attempt_number": self.answerpaper.attempt_number} ), data={question_marks: 1.0} ) updated_ans_paper = AnswerPaper.objects.get( user=self.student, question_paper=self.question_paper, attempt_number=self.answerpaper.attempt_number ) updated_ans = Answer.objects.get(question=self.question) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, "yaksh/grade_user.html") self.assertEqual(updated_ans.marks, 1.0) self.assertEqual(updated_ans_paper.marks_obtained, 1.0) class TestDownloadAssignment(TestCase): def setUp(self): self.client = Client() self.mod_group = Group.objects.create(name='moderator') tzone = pytz.timezone('UTC') # Create Moderator with profile 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', is_moderator=True ) # Add to moderator group self.mod_group.user_set.add(self.user) # Create Student 1 self.student1_plaintext_pass = 'demo_student1' self.student1 = User.objects.create_user( username='demo_student1', password=self.student1_plaintext_pass, first_name='student1_first_name', last_name='student1_last_name', email='demo_student1@test.com' ) # Create Student 2 self.student2_plaintext_pass = 'demo_student2' self.student2 = User.objects.create_user( username='demo_student2', password=self.student2_plaintext_pass, first_name='student2_first_name', last_name='student2_last_name', email='demo_student2@test.com' ) self.course = Course.objects.create( name="Python Course", enrollment="Enroll Request", creator=self.user ) self.quiz = Quiz.objects.create( start_date_time=datetime(2014, 10, 9, 10, 8, 15, 0, tzone), end_date_time=datetime(2015, 10, 9, 10, 8, 15, 0, tzone), duration=30, active=True, instructions="Demo Instructions", attempts_allowed=-1, time_between_attempts=0, description='demo_quiz', pass_criteria=40 ) self.learning_unit = LearningUnit.objects.create( order=1, type="quiz", quiz=self.quiz) self.learning_module = LearningModule.objects.create( order=1, name="test module", description="test", creator=self.user, check_prerequisite=False) self.learning_module.learning_unit.add(self.learning_unit.id) self.course.learning_module.add(self.learning_module) self.question = Question.objects.create( summary="Test_question", description="Assignment Upload", points=1.0, language="python", type="upload", user=self.user ) self.question_paper = QuestionPaper.objects.create( quiz=self.quiz, total_marks=1.0, fixed_question_order=str(self.question.id) ) self.question_paper.fixed_questions.add(self.question) # create assignment file assignment_file1 = SimpleUploadedFile("file1.txt", b"Test") assignment_file2 = SimpleUploadedFile("file2.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 ) def tearDown(self): self.client.logout() self.user.delete() self.student1.delete() self.student2.delete() self.assignment1.delete() self.assignment2.delete() self.quiz.delete() self.course.delete() self.learning_module.delete() self.learning_unit.delete() self.mod_group.delete() 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 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, "course_id": self.course.id}), follow=True ) self.assertEqual(response.status_code, 404) def test_download_assignment_per_quiz(self): """ Check for download assignments per quiz """ self.client.login( username=self.user.username, password=self.user_plaintext_pass ) 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'), "attachment; filename={0}".format(file_name)) zip_file = string_io(response.content) zipped_file = zipfile.ZipFile(zip_file, 'r') self.assertIsNone(zipped_file.testzip()) self.assertIn('file1.txt', zipped_file.namelist()[0]) self.assertIn('file2.txt', zipped_file.namelist()[1]) zip_file.close() zipped_file.close() def test_download_assignment_per_user(self): """ Check for download assignments per quiz """ self.client.login( username=self.user.username, password=self.user_plaintext_pass ) response = self.client.get( reverse('yaksh:download_user_assignment', kwargs={'quiz_id': self.quiz.id, 'question_id': self.question.id, 'user_id': self.student2.id, 'course_id': self.course.id }), follow=True ) file_name = "{0}.zip".format(self.student2.get_full_name()) file_name = file_name.replace(" ", "_") self.assertEqual(response.status_code, 200) self.assertEqual(response.get('Content-Disposition'), "attachment; filename={0}".format(file_name)) zip_file = string_io(response.content) zipped_file = zipfile.ZipFile(zip_file, 'r') self.assertIsNone(zipped_file.testzip()) self.assertIn('file2.txt', zipped_file.namelist()[0]) zip_file.close() zipped_file.close() class TestAddQuiz(TestCase): def setUp(self): self.client = Client() self.mod_group = Group.objects.create(name='moderator') tzone = pytz.timezone('UTC') # Create Moderator with profile 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', is_moderator=True ) # Create Student self.student_plaintext_pass = 'demo_student' self.student = User.objects.create_user( username='demo_student', password=self.student_plaintext_pass, first_name='student_first_name', last_name='student_last_name', email='demo_student@test.com' ) # Add to moderator group self.mod_group.user_set.add(self.user) self.course = Course.objects.create( name="Python Course", enrollment="Enroll Request", creator=self.user ) self.quiz = Quiz.objects.create( start_date_time=datetime(2014, 10, 9, 10, 8, 15, 0, tzone), end_date_time=datetime(2015, 10, 9, 10, 8, 15, 0, tzone), duration=30, active=True, instructions="Demo Instructions", attempts_allowed=-1, time_between_attempts=0, description='demo quiz', pass_criteria=40, creator=self.user ) self.exercise = Quiz.objects.create( start_date_time=datetime(2014, 10, 9, 10, 8, 15, 0, tzone), end_date_time=datetime(2015, 10, 9, 10, 8, 15, 0, tzone), attempts_allowed=-1, time_between_attempts=0, is_exercise=True, description='demo exercise', creator=self.user ) def tearDown(self): self.client.logout() self.user.delete() self.student.delete() self.quiz.delete() self.exercise.delete() self.course.delete() self.mod_group.delete() def test_add_quiz_denies_anonymous(self): """ If not logged in redirect to login page """ response = self.client.get(reverse('yaksh:add_quiz'), follow=True ) redirect_destination = '/exam/login/?next=/exam/manage/addquiz/' self.assertRedirects(response, redirect_destination) def test_add_quiz_denies_non_moderator(self): """ If not moderator in redirect to login page """ self.client.login( username=self.student.username, password=self.student_plaintext_pass ) response = self.client.get(reverse('yaksh:add_quiz'), follow=True ) self.assertEqual(response.status_code, 404) def test_add_quiz_get(self): """ GET request to add question should display add quiz form """ self.client.login( username=self.user.username, password=self.user_plaintext_pass ) response = self.client.get(reverse('yaksh:add_quiz') ) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'yaksh/add_quiz.html') self.assertIsNotNone(response.context['form']) def test_add_quiz_post_existing_quiz(self): """ POST request to add quiz should edit quiz if quiz exists """ self.client.login( username=self.user.username, password=self.user_plaintext_pass ) tzone = pytz.timezone('UTC') response = self.client.post( reverse('yaksh:edit_quiz', kwargs={'quiz_id': self.quiz.id}), data={ 'start_date_time': '2016-01-10 09:00:15', 'end_date_time': '2016-01-15 09:00:15', 'duration': 30, 'active': False, 'attempts_allowed': 5, 'time_between_attempts': 1, 'description': 'updated demo quiz', 'pass_criteria': 40, 'instructions': "Demo Instructions", 'weightage': 1.0 } ) updated_quiz = Quiz.objects.get(id=self.quiz.id) self.assertEqual( updated_quiz.start_date_time, datetime(2016, 1, 10, 9, 0, 15, 0, tzone) ) self.assertEqual( updated_quiz.end_date_time, datetime(2016, 1, 15, 9, 0, 15, 0, tzone) ) self.assertEqual(updated_quiz.duration, 30) self.assertEqual(updated_quiz.active, False) self.assertEqual(updated_quiz.attempts_allowed, 5) self.assertEqual(updated_quiz.time_between_attempts, 1) 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 """ self.client.login( username=self.user.username, password=self.user_plaintext_pass ) tzone = pytz.timezone('UTC') response = self.client.post( reverse('yaksh:add_quiz'), data={ 'start_date_time': '2016-01-10 09:00:15', 'end_date_time': '2016-01-15 09:00:15', 'duration': 50, 'active': True, 'attempts_allowed': -1, 'time_between_attempts': 2, 'description': 'new demo quiz', 'pass_criteria': 50, 'instructions': "Demo Instructions", 'weightage': 1.0 } ) quiz_list = Quiz.objects.all().order_by('-id') new_quiz = quiz_list[0] self.assertEqual( new_quiz.start_date_time, datetime(2016, 1, 10, 9, 0, 15, 0, tzone) ) self.assertEqual( new_quiz.end_date_time, datetime(2016, 1, 15, 9, 0, 15, 0, tzone) ) self.assertEqual(new_quiz.duration, 50) self.assertEqual(new_quiz.active, True) self.assertEqual(new_quiz.attempts_allowed, -1) self.assertEqual(new_quiz.time_between_attempts, 2) 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 """ response = self.client.get(reverse('yaksh:add_exercise'), follow=True ) redirect_destination = '/exam/login/?next=/exam/manage/add_exercise/' self.assertRedirects(response, redirect_destination) def test_add_exercise_denies_non_moderator(self): """ If not moderator in redirect to login page """ self.client.login( username=self.student.username, password=self.student_plaintext_pass ) response = self.client.get(reverse('yaksh:add_exercise'), follow=True ) self.assertEqual(response.status_code, 404) def test_add_exercise_get(self): """ GET request to add exercise should display add exercise form """ self.client.login( username=self.user.username, password=self.user_plaintext_pass ) response = self.client.get(reverse('yaksh:add_exercise') ) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'yaksh/add_exercise.html') self.assertIsNotNone(response.context['form']) def test_add_exercise_post_existing_exercise(self): """ POST request to edit exercise """ self.client.login( username=self.user.username, password=self.user_plaintext_pass ) response = self.client.post( reverse('yaksh:edit_exercise', kwargs={'quiz_id': self.exercise.id}), data={ 'description': 'updated demo exercise', 'active': True } ) updated_exercise = Quiz.objects.get(id=self.exercise.id) self.assertEqual(updated_exercise.active, True) self.assertEqual(updated_exercise.duration, 1000) self.assertEqual(updated_exercise.attempts_allowed, -1) self.assertEqual(updated_exercise.time_between_attempts, 0) 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): """ POST request to add new exercise """ self.client.login( username=self.user.username, password=self.user_plaintext_pass ) response = self.client.post( reverse('yaksh:add_exercise'), data={ 'description': "Demo Exercise", 'active': True } ) quiz_list = Quiz.objects.all().order_by('-id') new_exercise = quiz_list[0] self.assertEqual(new_exercise.active, True) self.assertEqual(new_exercise.duration, 1000) self.assertEqual(new_exercise.attempts_allowed, -1) self.assertEqual(new_exercise.time_between_attempts, 0) 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( username=self.user.username, password=self.user_plaintext_pass ) response = self.client.get( reverse('yaksh:show_all_quizzes'), 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") class TestAddAsModerator(TestCase): def setUp(self): self.client = Client() self.mod_group = Group.objects.create(name='moderator') # Create Moderator with profile 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', is_moderator=True ) self.course = Course.objects.create( name="Python Course", enrollment="Enroll Request", creator=self.user ) self.mod_group.delete() def tearDown(self): self.client.logout() self.user.delete() def test_add_as_moderator_group_does_not_exist(self): """ If group does not exist return 404 """ self.client.login( username=self.user.username, password=self.user_plaintext_pass ) response = self.client.get( reverse('yaksh:add_teacher', kwargs={'course_id': self.course.id} ), follow=True ) self.assertEqual(response.status_code, 404) with self.assertRaises(Http404): add_as_moderator(self.user, 'moderator') class TestToggleModerator(TestCase): def setUp(self): self.client = Client() self.mod_group = Group.objects.create(name='moderator') # Create Moderator with profile 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', is_moderator=True ) # Create Student self.student_plaintext_pass = 'demo_student' self.student = User.objects.create_user( username='demo_student', password=self.student_plaintext_pass, first_name='student_first_name', last_name='student_last_name', email='demo_student@test.com' ) Profile.objects.create( user=self.student, roll_number=10, institute='IIT', department='Chemical', position='Student', timezone='UTC', ) # Add to moderator group self.mod_group.user_set.add(self.user) self.course = Course.objects.create( name="Python Course", enrollment="Enroll Request", creator=self.user ) def tearDown(self): self.client.logout() self.user.delete() self.student.delete() self.course.delete() self.mod_group.delete() def test_toggle_for_moderator(self): self.client.login( username=self.user.username, password=self.user_plaintext_pass ) response = self.client.get( reverse('yaksh:toggle_moderator') ) self.assertEqual(response.status_code, 302) self.assertEquals(self.user.groups.all().count(), 0) def test_toggle_for_student(self): self.client.login( username=self.student.username, password=self.student_plaintext_pass ) response = self.client.get( reverse('yaksh:toggle_moderator') ) self.assertEqual(response.status_code, 404) class TestAddTeacher(TestCase): def setUp(self): self.client = Client() self.mod_group = Group.objects.create(name='moderator') tzone = pytz.timezone('UTC') # Create User with no profile self.user_no_profile_plaintext_pass = 'demo_no_profile' self.user_no_profile = User.objects.create_user( username='demo_user_no_profile', password=self.user_no_profile_plaintext_pass, first_name='first_name_no_profile', last_name='last_name_no_profile', email='demo_no_profile@test.com' ) # Create Moderator with profile 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', is_moderator=True ) # Create Student self.student_plaintext_pass = 'demo_student' self.student = User.objects.create_user( username='demo_student', password=self.student_plaintext_pass, first_name='student_first_name', last_name='student_last_name', email='demo_student@test.com' ) # Add to moderator group self.mod_group.user_set.add(self.user) self.course = Course.objects.create( name="Python Course", enrollment="Enroll Request", creator=self.user ) self.pre_req_quiz = Quiz.objects.create( start_date_time=datetime(2014, 2, 1, 5, 8, 15, 0, tzone), end_date_time=datetime(2015, 10, 9, 10, 8, 15, 0, tzone), duration=30, active=True, attempts_allowed=-1, time_between_attempts=0, description='pre requisite quiz', pass_criteria=40, creator=self.user ) self.quiz = Quiz.objects.create( start_date_time=datetime(2014, 10, 9, 10, 8, 15, 0, tzone), end_date_time=datetime(2015, 10, 9, 10, 8, 15, 0, tzone), duration=30, active=True, attempts_allowed=-1, time_between_attempts=0, description='demo quiz', pass_criteria=40, creator=self.user ) def tearDown(self): self.client.logout() self.user.delete() self.student.delete() self.quiz.delete() self.pre_req_quiz.delete() self.course.delete() self.mod_group.delete() def test_add_teacher_denies_no_profile(self): """ If not moderator redirect to login page """ self.client.login( username=self.user_no_profile.username, password=self.user_no_profile_plaintext_pass ) response = self.client.get( reverse('yaksh:add_teacher', kwargs={'course_id': self.course.id} ), follow=True ) self.assertEqual(response.status_code, 404) def test_add_teacher_denies_anonymous(self): """ If not logged in redirect to login page """ response = self.client.get( reverse('yaksh:add_teacher', kwargs={'course_id': self.course.id} ), follow=True ) redirect_destination = ( '/exam/login/?next=/exam/manage/addteacher/{0}/'.format( self.course.id) ) self.assertRedirects(response, redirect_destination) def test_add_teacher_denies_non_moderator(self): """ If not moderator redirect to login page """ self.client.login( username=self.student.username, password=self.student_plaintext_pass ) response = self.client.get( reverse('yaksh:add_teacher', kwargs={'course_id': self.course.id} ), follow=True ) self.assertEqual(response.status_code, 404) def test_add_teacher_get(self): """ GET request to add teacher should display list of teachers """ self.client.login( username=self.user.username, password=self.user_plaintext_pass ) response = self.client.get( reverse('yaksh:add_teacher', kwargs={'course_id': self.course.id} ) ) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'yaksh/addteacher.html') self.assertEqual(response.context['course'], self.course) def test_add_teacher_post(self): """ POST request to add teacher should add teachers to a course """ self.client.login( username=self.user.username, password=self.user_plaintext_pass ) teacher_id_list = [] for i in range(5): teacher = User.objects.create_user( username='demo_teacher{}'.format(i), password='demo_teacher_pass{}'.format(i), first_name='teacher_first_name{}'.format(i), last_name='teacher_last_name{}'.format(i), email='demo{}@test.com'.format(i) ) Profile.objects.create( user=teacher, roll_number='T{}'.format(i), institute='IIT', department='Chemical', position='Teacher', timezone='UTC' ) teacher_id_list.append(teacher.id) response = self.client.post( reverse('yaksh:add_teacher', kwargs={'course_id': self.course.id} ), data={'check': teacher_id_list} ) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'yaksh/addteacher.html') self.assertEqual(response.context['status'], True) for t_id in teacher_id_list: teacher_object = User.objects.get(id=t_id) self.assertIn(teacher_object, response.context['teachers_added']) self.assertIn(teacher_object, self.course.teachers.all()) class TestRemoveTeacher(TestCase): def setUp(self): self.client = Client() self.mod_group = Group.objects.create(name='moderator') tzone = pytz.timezone('UTC') # Create Moderator with profile 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' ) # Create Student self.student_plaintext_pass = 'demo_student' self.student = User.objects.create_user( username='demo_student', password=self.student_plaintext_pass, first_name='student_first_name', last_name='student_last_name', email='demo_student@test.com' ) # Add to moderator group self.mod_group.user_set.add(self.user) self.course = Course.objects.create( name="Python Course", enrollment="Enroll Request", creator=self.user) self.pre_req_quiz = Quiz.objects.create( start_date_time=datetime(2014, 2, 1, 5, 8, 15, 0, tzone), end_date_time=datetime(2015, 10, 9, 10, 8, 15, 0, tzone), duration=30, active=True, attempts_allowed=-1, time_between_attempts=0, description='pre requisite quiz', pass_criteria=40, creator=self.user ) self.quiz = Quiz.objects.create( start_date_time=datetime(2014, 10, 9, 10, 8, 15, 0, tzone), end_date_time=datetime(2015, 10, 9, 10, 8, 15, 0, tzone), duration=30, active=True, attempts_allowed=-1, time_between_attempts=0, description='demo quiz', pass_criteria=40, creator=self.user ) def tearDown(self): self.client.logout() self.user.delete() self.student.delete() self.quiz.delete() self.pre_req_quiz.delete() self.course.delete() self.mod_group.delete() def test_remove_teacher_denies_anonymous(self): """ If not logged in redirect to login page """ response = self.client.get( reverse('yaksh:remove_teacher', kwargs={'course_id': self.course.id} ), follow=True ) redirect_destination = ( '/exam/login/?next=/exam/manage/remove_teachers/{0}/'.format( self.course.id) ) self.assertRedirects(response, redirect_destination) def test_remove_teacher_denies_non_moderator(self): """ If not moderator redirect to login page """ self.client.login( username=self.student.username, password=self.student_plaintext_pass ) response = self.client.get( reverse('yaksh:remove_teacher', kwargs={'course_id': self.course.id} ), follow=True ) self.assertEqual(response.status_code, 404) def test_remove_teacher_post(self): """ POST request should remove moderator from course """ teacher_id_list = [] self.client.login( username=self.user.username, password=self.user_plaintext_pass ) for i in range(5): teacher = User.objects.create_user( username='remove_teacher{}'.format(i), password='remove_teacher_pass{}'.format(i), first_name='remove_teacher_first_name{}'.format(i), last_name='remove_teacher_last_name{}'.format(i), email='remove_teacher{}@test.com'.format(i) ) Profile.objects.create( user=teacher, roll_number='RT{}'.format(i), institute='IIT', department='Aeronautical', position='Teacher', timezone='UTC' ) teacher_id_list.append(teacher.id) self.course.teachers.add(teacher) response = self.client.post( reverse('yaksh:remove_teacher', kwargs={'course_id': self.course.id} ), 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 ) for t_id in teacher_id_list: teacher = User.objects.get(id=t_id) self.assertNotIn(teacher, self.course.teachers.all()) class TestCourses(TestCase): def setUp(self): self.client = Client() self.mod_group = Group.objects.create(name='moderator') # Create Moderator 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='demo@test.com' ) Profile.objects.create( user=self.user1, roll_number=10, institute='IIT', department='Chemical', position='Moderator', timezone='UTC', is_moderator=True ) self.user2_plaintext_pass = 'demo2' self.user2 = User.objects.create_user( username='demo_user2', password=self.user2_plaintext_pass, first_name='user2_first_name', last_name='user2_last_name', email='demo2@test.com' ) Profile.objects.create( user=self.user2, roll_number=10, institute='IIT', department='Aeronautical', position='Moderator', timezone='UTC', is_moderator=True ) # Create Student self.student_plaintext_pass = 'demo_student' self.student = User.objects.create_user( username='demo_student', password=self.student_plaintext_pass, first_name='student_first_name', last_name='student_last_name', email='demo_student@test.com' ) Profile.objects.create( user=self.student, roll_number=10, institute='IIT', department='Aeronautical', position='Moderator', timezone='UTC' ) self.teacher_plaintext_pass = 'teacher' self.teacher = User.objects.create_user( username='teacher', password=self.teacher_plaintext_pass, first_name='teacher_first_name', last_name='teacher_last_name', email='demo_teacher@test.com' ) Profile.objects.create( user=self.teacher, roll_number=10, institute='IIT', department='Aeronautical', position='Moderator', timezone='UTC', is_moderator=True ) # Add to moderator group self.mod_group.user_set.add(self.user1) self.mod_group.user_set.add(self.user2) self.mod_group.user_set.add(self.teacher) # 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) self.user1_course = Course.objects.create( name="Python Course", enrollment="Enroll Request", creator=self.user1) # Create Learning Module for Python Course self.learning_module1 = LearningModule.objects.create( order=0, name="demo module", description="module", check_prerequisite=False, creator=self.user1) self.quiz = Quiz.objects.create( time_between_attempts=0, description='demo quiz', creator=self.user1) self.question_paper = QuestionPaper.objects.create( quiz=self.quiz, total_marks=1.0) 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) self.quiz_unit = LearningUnit.objects.create( order=2, type="quiz", quiz=self.quiz) # Add units to module self.learning_module1.learning_unit.add(self.lesson_unit) self.learning_module1.learning_unit.add(self.quiz_unit) # Add teacher to user1 course self.user1_course.teachers.add(self.teacher) self.user2_course = Course.objects.create( name="Java Course", enrollment="Enroll Request", creator=self.user2) self.user2_course.learning_module.add(self.learning_module) def tearDown(self): self.client.logout() self.user1.delete() self.user2.delete() self.student.delete() self.teacher.delete() self.mod_group.delete() def test_courses_denies_anonymous(self): """ If not logged in redirect to login page """ response = self.client.get(reverse('yaksh:courses'), follow=True) redirect_destination = ('/exam/login/?next=/exam/manage/courses/') self.assertRedirects(response, redirect_destination) def test_courses_denies_non_moderator(self): """ If not moderator redirect to login page """ self.client.login( username=self.student.username, password=self.student_plaintext_pass ) response = self.client.get(reverse('yaksh:courses'), follow=True) self.assertEqual(response.status_code, 404) def test_courses_get(self): """ GET request should return courses page """ self.client.login( username=self.user1.username, password=self.user1_plaintext_pass ) response = self.client.get(reverse('yaksh:courses'), follow=True) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'yaksh/courses.html') self.assertIn(self.user1_course, response.context['courses']) self.assertNotIn(self.user2_course, response.context['courses']) def test_teacher_can_design_course(self): """ Check if teacher can design the course i.e add learning modules """ self.client.login( username=self.teacher.username, password=self.teacher_plaintext_pass ) response = self.client.post( reverse('yaksh:design_course', kwargs={"course_id": self.user1_course.id}), data={"Add": "Add", "module_list": [str(self.learning_module.id)] }) # Test add learning module self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'yaksh/design_course_session.html') added_learning_module = self.user1_course.learning_module.all().first() self.assertEqual(self.learning_module, added_learning_module) self.assertEqual(added_learning_module.order, 1) # Test change order of learning module response = self.client.post( reverse('yaksh:design_course', kwargs={"course_id": self.user1_course.id}), data={"Change": "Change", "ordered_list": ",".join( [str(self.learning_module.id)+":"+"0"] )} ) updated_learning_module = LearningModule.objects.get( id=self.learning_module.id) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'yaksh/design_course_session.html') self.assertEqual(updated_learning_module.order, 0) # Test change check prerequisite response = self.client.post( reverse('yaksh:design_course', kwargs={"course_id": self.user1_course.id}), data={"Change_prerequisite": "Change_prerequisite", "check_prereq": [str(self.learning_module.id)] }) updated_learning_module = LearningModule.objects.get( id=self.learning_module.id) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'yaksh/design_course_session.html') self.assertTrue(updated_learning_module.check_prerequisite) # Test to remove learning module from course response = self.client.post( reverse('yaksh:design_course', kwargs={"course_id": self.user1_course.id}), data={"Remove": "Remove", "delete_list": [str(self.learning_module.id)] }) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'yaksh/design_course_session.html') self.assertFalse(self.user1_course.learning_module.all().exists()) def test_get_course_modules(self): """ Test to check if student gets course modules """ self.client.login( username=self.student.username, password=self.student_plaintext_pass ) response = self.client.get( reverse('yaksh:course_modules', kwargs={"course_id": self.user1_course.id}), follow=True ) # 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) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, "yaksh/quizzes_user.html") # enroll student in the course self.user1_course.students.add(self.student) # deactivate the course and check if student is able to view course self.user1_course.active = False self.user1_course.save() response = self.client.get( reverse('yaksh:course_modules', kwargs={"course_id": self.user1_course.id}), follow=True ) err_msg = "{0} is either expired or not active".format( self.user1_course.name) self.assertEqual(response.context['msg'], err_msg) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, "yaksh/quizzes_user.html") # activate the course and check if students gets course modules self.user1_course.active = True self.user1_course.save() self.user1_course.learning_module.add(self.learning_module) response = self.client.get( reverse('yaksh:course_modules', kwargs={"course_id": self.user1_course.id}), follow=True ) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, "yaksh/course_modules.html") self.assertEqual(response.context['course'], self.user1_course) module, percent = response.context['modules'][0] self.assertEqual(module, self.learning_module) self.assertEqual(percent, 0.0) def test_duplicate_course(self): """ Test To clone/duplicate course and link modules""" # Student Login self.client.login( username=self.student.username, password=self.student_plaintext_pass ) response = self.client.get( reverse('yaksh:duplicate_course', kwargs={"course_id": self.user2_course.id}), follow=True ) self.assertEqual(response.status_code, 404) # Teacher Login self.client.login( username=self.teacher.username, password=self.teacher_plaintext_pass ) # Denies teacher not added in the course response = self.client.get( reverse('yaksh:duplicate_course', kwargs={"course_id": self.user2_course.id}), follow=True ) 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']) # Test clone/duplicate courses and create copies of modules and units # Teacher Login # Given # Add files to a lesson 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) # Add module to Python Course self.user1_course.learning_module.add(self.learning_module1) self.client.login( username=self.teacher.username, password=self.teacher_plaintext_pass ) response = self.client.get( reverse('yaksh:duplicate_course', kwargs={"course_id": self.user1_course.id}), follow=True ) # When courses = Course.objects.filter( creator=self.teacher).order_by("id") module = courses.last().get_learning_modules()[0] units = module.get_learning_units() cloned_lesson = units[0].lesson cloned_quiz = units[1].quiz expected_lesson_files = cloned_lesson.get_files() actual_lesson_files = self.lesson.get_files() cloned_qp = cloned_quiz.questionpaper_set.get() self.all_files = LessonFile.objects.filter( lesson_id__in=[self.lesson.id, cloned_lesson.id]) # Then self.assertEqual(response.status_code, 200) self.assertEqual(courses.last().creator, self.teacher) self.assertEqual(courses.last().name, "Copy Of Python Course") self.assertEqual(module.name, "Copy of demo module") self.assertEqual(module.creator, self.teacher) self.assertEqual(module.order, 0) self.assertEqual(len(units), 2) self.assertEqual(cloned_lesson.name, "Copy of demo lesson") self.assertEqual(cloned_lesson.creator, self.teacher) self.assertEqual(cloned_quiz.description, "Copy of demo quiz") self.assertEqual(cloned_quiz.creator, self.teacher) self.assertEqual(cloned_qp.__str__(), "Question Paper for Copy of demo quiz") self.assertEqual(os.path.basename(expected_lesson_files[0].file.name), os.path.basename(actual_lesson_files[0].file.name)) for lesson_file in self.all_files: file_path = lesson_file.file.path if os.path.exists(file_path): 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): self.client = Client() self.mod_group = Group.objects.create(name='moderator') tzone = pytz.timezone('UTC') # Create Moderator with profile 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', is_moderator=True ) # Create a teacher self.teacher_plaintext_pass = 'demo_teacher' self.teacher = User.objects.create_user( username='demo_teacher', password=self.teacher_plaintext_pass, first_name='first_name', last_name='last_name', email='demo@test.com' ) Profile.objects.create( user=self.teacher, roll_number=10, institute='IIT', department='Chemical', position='Moderator', timezone='UTC', is_moderator=True ) # Create Student self.student_plaintext_pass = 'demo_student' self.student = User.objects.create_user( username='demo_student', password=self.student_plaintext_pass, first_name='student_first_name', last_name='student_last_name', email='demo_student@test.com' ) # Add to moderator group self.mod_group.user_set.add(self.user) self.mod_group.user_set.add(self.teacher) self.course = Course.objects.create( name="Python Course", enrollment="Enroll Request", creator=self.user) self.course.teachers.add(self.teacher) self.pre_req_quiz = Quiz.objects.create( start_date_time=datetime(2014, 2, 1, 5, 8, 15, 0, tzone), end_date_time=datetime(2015, 10, 9, 10, 8, 15, 0, tzone), duration=30, active=True, attempts_allowed=-1, time_between_attempts=0, description='pre requisite quiz', pass_criteria=40, creator=self.user ) self.quiz = Quiz.objects.create( start_date_time=datetime(2014, 10, 9, 10, 8, 15, 0, tzone), end_date_time=datetime(2015, 10, 9, 10, 8, 15, 0, tzone), duration=30, active=True, attempts_allowed=-1, time_between_attempts=0, description='demo quiz', pass_criteria=40, creator=self.user ) def tearDown(self): self.client.logout() self.user.delete() self.student.delete() self.quiz.delete() self.pre_req_quiz.delete() self.course.delete() self.mod_group.delete() def test_add_course_denies_anonymous(self): """ If not logged in redirect to login page """ response = self.client.get(reverse('yaksh:add_course'), follow=True ) redirect_destination = ('/exam/login/?next=/exam/manage/add_course/') self.assertRedirects(response, redirect_destination) def test_add_course_denies_non_moderator(self): """ If not moderator in redirect to login page """ self.client.login( username=self.student.username, password=self.student_plaintext_pass ) response = self.client.get(reverse('yaksh:add_course'), follow=True) self.assertEqual(response.status_code, 404) def test_add_course_get(self): """ GET request to add course should display add course form """ self.client.login( username=self.user.username, password=self.user_plaintext_pass ) response = self.client.get(reverse('yaksh:add_course')) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'yaksh/add_course.html') self.assertIsNotNone(response.context['form']) def test_add_course_post_new_course(self): """ POST request to add course should add new courses if no course exists """ self.client.login( username=self.user.username, password=self.user_plaintext_pass ) response = self.client.post( reverse('yaksh:add_course'), data={'name': 'new_demo_course_1', 'active': True, 'enrollment': 'open', 'start_enroll_time': '2016-01-10 09:00:15', 'end_enroll_time': '2016-01-15 09:00:15', } ) new_course = Course.objects.latest('created_on') self.assertEqual(new_course.name, 'new_demo_course_1') self.assertEqual(new_course.enrollment, 'open') self.assertEqual(new_course.active, True) self.assertEqual(response.status_code, 302) self.assertRedirects(response, '/exam/manage/courses', target_status_code=301) def test_add_course_teacher_cannot_be_creator(self): """ Teacher editing the course should not become creator """ self.client.login( username=self.teacher.username, password=self.teacher_plaintext_pass ) response = self.client.post( reverse('yaksh:edit_course', kwargs={"course_id": self.course.id}), data={'name': 'Teacher_course', 'active': True, 'enrollment': 'open', 'start_enroll_time': '2016-01-10 09:00:15', 'end_enroll_time': '2016-01-15 09:00:15', } ) updated_course = Course.objects.get(id=self.course.id) self.assertEqual(updated_course.name, 'Teacher_course') self.assertEqual(updated_course.enrollment, 'open') self.assertEqual(updated_course.active, True) self.assertEqual(response.status_code, 302) self.assertEqual(updated_course.creator, self.user) self.assertRedirects(response, '/exam/manage/courses', target_status_code=301) class TestCourseDetail(TestCase): def setUp(self): self.client = Client() self.mod_group = Group.objects.create(name='moderator') # Create Moderator 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='demo@test.com' ) Profile.objects.create( user=self.user1, roll_number=10, institute='IIT', department='Chemical', position='Moderator', timezone='UTC', is_moderator=True ) self.user2_plaintext_pass = 'demo2' self.user2 = User.objects.create_user( username='demo_user2', password=self.user2_plaintext_pass, first_name='user2_first_name', last_name='user2_last_name', email='demo2@test.com' ) Profile.objects.create( user=self.user2, roll_number=10, institute='IIT', department='Aeronautical', position='Moderator', timezone='UTC', is_moderator=True ) # Create Student self.student_plaintext_pass = 'demo_student' self.student = User.objects.create_user( username='demo_student', password=self.student_plaintext_pass, first_name='student_first_name', last_name='student_last_name', email='demo_student@test.com' ) self.student1_plaintext_pass = 'demo_student1' self.student1 = User.objects.create_user( username='demo_student1', password=self.student1_plaintext_pass, first_name='student_first_name', last_name='student_last_name', email='demo_student1@test.com' ) # Add to moderator group self.mod_group.user_set.add(self.user1) self.mod_group.user_set.add(self.user2) self.user1_course = Course.objects.create( name="Python Course", 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, order=1) self.user1_course.learning_module.add(self.learning_module) self.lesson = Lesson.objects.create( name="test lesson", description="test description", creator=self.user1) self.learning_unit1 = LearningUnit.objects.create( order=1, type="lesson", lesson=self.lesson) self.learning_module.learning_unit.add(self.learning_unit1) def tearDown(self): self.client.logout() self.user1.delete() self.user2.delete() self.student.delete() self.user1_course.delete() self.mod_group.delete() def test_upload_users_with_correct_csv(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_file = SimpleUploadedFile(csv_file_path, csv_file.read()) # When response = self.client.post( reverse('yaksh:upload_users', kwargs={'course_id': self.user1_course.id}), data={'csv_file': upload_file}) csv_file.close() # 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') def test_upload_users_add_update_reject(self): # Given self.client.login( username=self.user1.username, password=self.user1_plaintext_pass ) csv_file_path = os.path.join(FIXTURES_DIR_PATH, "users_add_update_reject.csv") csv_file = open(csv_file_path, 'rb') upload_file = SimpleUploadedFile(csv_file_path, csv_file.read()) # When response = self.client.post( reverse('yaksh:upload_users', kwargs={'course_id': self.user1_course.id}), data={'csv_file': upload_file}) csv_file.close() # Then uploaded_user = User.objects.filter(username="test") 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') def test_upload_users_with_wrong_csv(self): # Given self.client.login( username=self.user1.username, password=self.user1_plaintext_pass ) csv_file_path = os.path.join(FIXTURES_DIR_PATH, "demo_questions.zip") csv_file = open(csv_file_path, 'rb') upload_file = SimpleUploadedFile(csv_file_path, csv_file.read()) message = "The file uploaded is not a CSV file." # When response = self.client.post( reverse('yaksh:upload_users', kwargs={'course_id': self.user1_course.id}), data={'csv_file': upload_file}) 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') def test_upload_users_csv_with_missing_headers(self): # Given self.client.login( username=self.user1.username, password=self.user1_plaintext_pass ) csv_file_path = os.path.join(FIXTURES_DIR_PATH, "users_some_headers_missing.csv") csv_file = open(csv_file_path, 'rb') upload_file = SimpleUploadedFile(csv_file_path, csv_file.read()) message = "The CSV file does not contain the required headers" # When response = self.client.post( reverse('yaksh:upload_users', kwargs={'course_id': self.user1_course.id}), data={'csv_file': upload_file}) 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') def test_upload_users_csv_with_no_values(self): # Given self.client.login( username=self.user1.username, password=self.user1_plaintext_pass ) csv_file_path = os.path.join(FIXTURES_DIR_PATH, "users_with_no_values.csv") csv_file = open(csv_file_path, 'rb') upload_file = SimpleUploadedFile(csv_file_path, csv_file.read()) # When response = self.client.post( reverse('yaksh:upload_users', kwargs={'course_id': self.user1_course.id}), data={'csv_file': upload_file}) 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') def test_upload_users_csv_with_missing_values(self): ''' This test takes csv with 3 row values. 1st row has a missing row. 2nd has a proper row. 3rd has a same row has 2nd Only 2nd user will be added. This test proves that: - Row with missing values is ignored and continued with next row. - Duplicate user is not created. ''' # Given self.client.login( username=self.user1.username, password=self.user1_plaintext_pass ) csv_file_path = os.path.join(FIXTURES_DIR_PATH, "users_some_values_missing.csv") csv_file = open(csv_file_path, 'rb') upload_file = SimpleUploadedFile(csv_file_path, csv_file.read()) # When response = self.client.post( reverse('yaksh:upload_users', kwargs={'course_id': self.user1_course.id}), data={'csv_file': upload_file} ) csv_file.close() # 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') def test_course_detail_denies_anonymous(self): """ If not logged in redirect to login page """ response = self.client.get( reverse('yaksh:course_detail', kwargs={'course_id': self.user1_course.id} ), follow=True ) redirect_destination = ( '/exam/login/?next=/exam/manage/course_detail/{0}/'.format( self.user1_course.id) ) self.assertRedirects(response, redirect_destination) def test_course_detail_denies_non_moderator(self): """ If not moderator redirect to 404 """ self.client.login( username=self.student.username, password=self.student_plaintext_pass ) response = self.client.get( reverse('yaksh:course_detail', kwargs={'course_id': self.user1_course.id} ), follow=True ) self.assertEqual(response.status_code, 404) def test_course_detail_denies_unrelated_moderators(self): """ If not creator of course or related teacher redirect to 404 """ self.client.login( username=self.user2.username, password=self.user2_plaintext_pass ) response = self.client.get( reverse('yaksh:course_detail', kwargs={'course_id': self.user1_course.id} ), follow=True ) self.assertEqual(response.status_code, 404) def test_course_detail_get(self): """ If not creator of course or related teacher redirect to 404 """ self.client.login( username=self.user1.username, password=self.user1_plaintext_pass ) response = self.client.get( reverse('yaksh:course_detail', kwargs={'course_id': self.user1_course.id} ), follow=True ) self.assertEqual(self.user1_course, response.context['course']) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'yaksh/course_detail.html') def test_student_course_enroll_get(self): """ Enroll student in a course using get request """ self.client.login( username=self.user1.username, password=self.user1_plaintext_pass ) response = self.client.get( reverse('yaksh:enroll_user', kwargs={'course_id': self.user1_course.id, 'user_id': self.student.id}) ) enrolled_student = self.user1_course.students.all() self.assertEqual(response.status_code, 200) self.assertSequenceEqual([self.student], enrolled_student) def test_student_course_enroll_post(self): """ Enroll student in a course using post request """ self.client.login( username=self.user1.username, password=self.user1_plaintext_pass ) response = self.client.post( reverse('yaksh:enroll_users', kwargs={'course_id': self.user1_course.id}), data={'check': self.student1.id} ) enrolled_student = self.user1_course.students.all() self.assertEqual(response.status_code, 200) self.assertSequenceEqual([self.student1], enrolled_student) def test_student_course_reject_get(self): """ Reject student in a course using get request """ self.client.login( username=self.user1.username, password=self.user1_plaintext_pass ) response = self.client.get( reverse('yaksh:reject_user', kwargs={'course_id': self.user1_course.id, 'user_id': self.student.id}) ) enrolled_student = self.user1_course.rejected.all() self.assertEqual(response.status_code, 200) self.assertSequenceEqual([self.student], enrolled_student) def test_student_course_reject_post(self): """ Reject student in a course using post request """ self.client.login( username=self.user1.username, password=self.user1_plaintext_pass ) response = self.client.post( reverse('yaksh:reject_users', kwargs={'course_id': self.user1_course.id}), data={'check': self.student1.id} ) enrolled_student = self.user1_course.rejected.all() self.assertEqual(response.status_code, 200) self.assertSequenceEqual([self.student1], enrolled_student) def test_toggle_course_status_get(self): self.client.login( username=self.user1.username, password=self.user1_plaintext_pass ) response = self.client.post( reverse('yaksh:toggle_course_status', kwargs={'course_id': self.user1_course.id}) ) self.assertEqual(response.status_code, 302) course = Course.objects.get(name="Python Course") self.assertFalse(course.active) self.assertRedirects(response, '/exam/manage/courses', target_status_code=301) def test_send_mail_to_course_students(self): """ Check if bulk mail is sent to multiple students enrolled in a course """ self.client.login( username=self.user1.username, password=self.user1_plaintext_pass ) self.student2 = User.objects.create_user( username='demo_student2', password=self.student_plaintext_pass, first_name='student_first_name', last_name='student_last_name', email='demo_student2@test.com' ) self.student3 = User.objects.create_user( username='demo_student3', password=self.student_plaintext_pass, first_name='student_first_name', last_name='student_last_name', email='demo_student3@test.com' ) self.student4 = User.objects.create_user( username='demo_student4', password=self.student_plaintext_pass, first_name='student_first_name', last_name='student_last_name', email='demo_student4@test.com' ) user_ids = [self.student.id, self.student2.id, self.student3.id, self.student4.id] user_emails = [self.student.email, self.student2.email, self.student3.email, self.student4.email] self.user1_course.students.add(*user_ids) attachment = SimpleUploadedFile("file.txt", b"Test") email_data = { 'send_mail': 'send_mail', 'email_attach': [attachment], 'subject': 'test_bulk_mail', 'body': 'Test_Mail', 'check': user_ids } self.client.post(reverse( 'yaksh:send_mail', kwargs={'course_id': self.user1_course.id}), data=email_data ) attachment_file = mail.outbox[0].attachments[0][0] subject = mail.outbox[0].subject body = mail.outbox[0].alternatives[0][0] recipients = mail.outbox[0].bcc self.assertEqual(attachment_file, "file.txt") self.assertEqual(subject, "test_bulk_mail") self.assertEqual(body, "Test_Mail") self.assertSequenceEqual(recipients, user_emails) # Test for get request in send mail get_response = self.client.get(reverse( 'yaksh:send_mail', kwargs={'course_id': self.user1_course.id}) ) self.assertEqual(get_response.status_code, 200) self.assertEqual(get_response.context['course'], self.user1_course) self.assertEqual(get_response.context['state'], 'mail') def test_download_users_template(self): """ Test to check download users template """ self.client.login( username=self.student.username, password=self.student_plaintext_pass ) # Denies student to download users upload template response = self.client.get(reverse('yaksh:download_sample_csv')) self.assertEqual(response.status_code, 404) # Moderator Login self.client.login( username=self.user1.username, password=self.user1_plaintext_pass ) response = self.client.get(reverse('yaksh:download_sample_csv')) self.assertEqual(response.status_code, 200) self.assertEqual(response.get('Content-Disposition'), 'attachment; filename="sample_user_upload"') def test_view_course_status(self): """ Test to view course status """ self.client.login( username=self.student.username, password=self.student_plaintext_pass ) # Denies student to view course status response = self.client.get(reverse('yaksh:course_status', kwargs={'course_id': self.user1_course.id})) self.assertEqual(response.status_code, 404) # Moderator Login self.client.login( username=self.user1.username, password=self.user1_plaintext_pass ) self.user1_course.students.add(self.student) # Check student details when course is not started 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.assertEqual(response.context['course'], self.user1_course) student_details = response.context['student_details'][0] student, grade, percent, current_unit = student_details self.assertEqual(student.username, "demo_student") self.assertEqual(grade, "NA") self.assertEqual(percent, 0.0) self.assertIsNone(current_unit) # Check student details when student starts the course self.course_status = CourseStatus.objects.create( course=self.user1_course, user=self.student) 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.assertEqual(response.context['course'], self.user1_course) student_details = response.context['student_details'][0] student, grade, percent, current_unit = student_details self.assertEqual(student.username, "demo_student") self.assertIsNone(grade) self.assertEqual(percent, 0) self.assertIsNone(current_unit) self.user1_course.students.remove(self.student) def test_course_status_per_user(self): """ Test course status for a particular student""" self.client.login( username=self.student.username, password=self.student_plaintext_pass ) # Denies student to view course status response = self.client.get(reverse('yaksh:get_user_data', kwargs={'course_id': self.user1_course.id, 'student_id': self.student.id})) err_msg = response.json()['user_data'].replace("\n", "").strip() self.assertEqual(response.status_code, 200) self.assertEqual(err_msg, "You are not a moderator") # Other Moderator Login self.client.login( username=self.user2.username, password=self.user2_plaintext_pass ) response = self.client.get(reverse('yaksh:get_user_data', kwargs={'course_id': self.user1_course.id, 'student_id': self.student.id})) err_msg = response.json()['user_data'].strip() actual_err = ('You are neither course creator ' 'nor course teacher for {0}'.format( self.user1_course.name) ) self.assertEqual(response.status_code, 200) self.assertEqual(err_msg, actual_err) # Actual course creator login self.client.login( username=self.user1.username, password=self.user1_plaintext_pass ) response = self.client.get(reverse('yaksh:get_user_data', kwargs={'course_id': self.user1_course.id, 'student_id': self.student.id})) self.assertEqual(response.status_code, 200) data = response.json()['user_data'] self.assertIn("Student_First_Name Student_Last_Name", data) self.assertIn("Overall Course Progress", data) self.assertIn("Per Module Progress", data) class TestEnrollRequest(TestCase): def setUp(self): self.client = Client() self.mod_group = Group.objects.create(name='moderator') # Create Moderator 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='demo@test.com' ) Profile.objects.create( user=self.user1, roll_number=10, institute='IIT', department='Chemical', position='Moderator', timezone='UTC', is_moderator=True ) self.user2_plaintext_pass = 'demo2' self.user2 = User.objects.create_user( username='demo_user2', password=self.user2_plaintext_pass, first_name='user2_first_name', last_name='user2_last_name', email='demo2@test.com' ) Profile.objects.create( user=self.user2, roll_number=10, institute='IIT', department='Aeronautical', position='Moderator', timezone='UTC', is_moderator=True ) # Create Student self.student_plaintext_pass = 'demo_student' self.student = User.objects.create_user( username='demo_student', password=self.student_plaintext_pass, first_name='student_first_name', last_name='student_last_name', email='demo_student@test.com' ) # Add to moderator group self.mod_group.user_set.add(self.user1) self.mod_group.user_set.add(self.user2) self.course = Course.objects.create( name="Python Course", enrollment="Enroll Request", creator=self.user1 ) def tearDown(self): self.client.logout() self.user1.delete() self.user2.delete() self.student.delete() self.course.delete() self.mod_group.delete() def test_enroll_request_denies_anonymous(self): """ If not logged in redirect to login page """ response = self.client.get( reverse('yaksh:enroll_request', kwargs={'course_id': self.course.id} ), follow=True ) redirect_destination = ( '/exam/login/?next=/exam/enroll_request/{}/'.format( self.course.id) ) self.assertRedirects(response, redirect_destination) def test_enroll_request_get_for_student(self): self.client.login( username=self.student.username, password=self.student_plaintext_pass ) response = self.client.get( reverse('yaksh:enroll_request', kwargs={'course_id': self.course.id} ), follow=True ) self.assertRedirects(response, '/exam/quizzes/') def test_enroll_request_get_for_moderator(self): self.client.login( username=self.user2.username, password=self.user2_plaintext_pass ) response = self.client.get( reverse('yaksh:enroll_request', kwargs={'course_id': self.course.id} ), follow=True ) self.assertRedirects(response, '/exam/manage/courses/') class TestViewAnswerPaper(TestCase): def setUp(self): self.client = Client() self.plaintext_pass = 'demo' for i in range(1, 4): User.objects.create_user( username='demo_user{0}'.format(i), password=self.plaintext_pass, first_name='first_name', last_name='last_name', email='demo@test.com' ) self.user1 = User.objects.get(username="demo_user1") self.course = Course.objects.create( name="Python Course", enrollment="Enroll Request", creator=self.user1 ) self.question = Question.objects.create( summary='Dummy', points=1, type='code', user=self.user1 ) self.quiz = Quiz.objects.create(time_between_attempts=0, description='demo quiz') self.user3 = User.objects.get(username="demo_user3") self.question_paper = QuestionPaper.objects.create( quiz=self.quiz, total_marks=1.0 ) self.question_paper.fixed_questions.add(self.question) self.question_paper.save() self.ans_paper = AnswerPaper.objects.create( user=self.user3, attempt_number=1, question_paper=self.question_paper, start_time=timezone.now(), user_ip='101.0.0.1', course=self.course, end_time=timezone.now()+timezone.timedelta(minutes=20) ) def tearDown(self): User.objects.all().delete() Course.objects.all().delete() Question.objects.all().delete() Quiz.objects.all().delete() QuestionPaper.objects.all().delete() AnswerPaper.objects.all().delete() def test_anonymous_user(self): # Given, user not logged in redirect_destination = ( '/exam/login/?next=/exam/view_answerpaper/{0}/{1}'.format( self.question_paper.id, self.course.id) ) # When response = self.client.get( reverse('yaksh:view_answerpaper', kwargs={'questionpaper_id': self.question_paper.id, 'course_id': self.course.id} ), follow=True ) # Then self.assertRedirects(response, redirect_destination) def test_cannot_view(self): # Given, enrolled user tries to view when not permitted by moderator user2 = User.objects.get(username="demo_user2") self.course.students.add(user2) self.course.save() self.quiz.view_answerpaper = False self.quiz.save() self.client.login( username=user2.username, password=self.plaintext_pass ) # When response = self.client.get( reverse('yaksh:view_answerpaper', kwargs={'questionpaper_id': self.question_paper.id, 'course_id': self.course.id} ), follow=True ) # Then self.assertRedirects(response, '/exam/quizzes/') def test_can_view_answerpaper(self): # Given, user enrolled and can view user3 = User.objects.get(username="demo_user3") self.course.students.add(user3) self.course.save() self.quiz.view_answerpaper = True self.quiz.save() self.client.login( username=user3.username, password=self.plaintext_pass ) # When response = self.client.get( reverse('yaksh:view_answerpaper', kwargs={'questionpaper_id': self.question_paper.id, 'course_id': self.course.id} ), follow=True ) # Then self.assertEqual(response.status_code, 200) self.assertTrue('data' in response.context) self.assertTrue('quiz' in response.context) self.assertTemplateUsed(response, 'yaksh/view_answerpaper.html') # When, wrong question paper id response = self.client.get( reverse('yaksh:view_answerpaper', kwargs={'questionpaper_id': 190, 'course_id': self.course.id} ), follow=True ) # Then self.assertEqual(response.status_code, 404) def test_view_when_not_enrolled(self): # Given, user tries to view when not enrolled in the course user2 = User.objects.get(username="demo_user2") self.client.login( username=user2.username, password=self.plaintext_pass ) self.course.students.remove(user2) self.course.save() self.quiz.view_answerpaper = True self.quiz.save() # When response = self.client.get( reverse('yaksh:view_answerpaper', kwargs={'questionpaper_id': self.question_paper.id, 'course_id': self.course.id} ), follow=True ) # Then self.assertRedirects(response, '/exam/quizzes/') class TestSelfEnroll(TestCase): def setUp(self): self.client = Client() self.mod_group = Group.objects.create(name='moderator') # Create Moderator 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='demo@test.com' ) Profile.objects.create( user=self.user1, roll_number=10, institute='IIT', department='Chemical', position='Moderator', timezone='UTC', is_moderator=True ) self.user2_plaintext_pass = 'demo2' self.user2 = User.objects.create_user( username='demo_user2', password=self.user2_plaintext_pass, first_name='user2_first_name', last_name='user2_last_name', email='demo2@test.com' ) Profile.objects.create( user=self.user2, roll_number=10, institute='IIT', department='Aeronautical', position='Moderator', timezone='UTC', is_moderator=True ) # Create Student self.student_plaintext_pass = 'demo_student' self.student = User.objects.create_user( username='demo_student', password=self.student_plaintext_pass, first_name='student_first_name', last_name='student_last_name', email='demo_student@test.com' ) # Add to moderator group self.mod_group.user_set.add(self.user1) self.mod_group.user_set.add(self.user2) self.course = Course.objects.create( name="Python Course", enrollment="Enroll Request", creator=self.user1 ) def tearDown(self): self.client.logout() self.user1.delete() self.user2.delete() self.student.delete() self.course.delete() self.mod_group.delete() def test_self_enroll_denies_anonymous(self): response = self.client.get( reverse('yaksh:self_enroll', kwargs={'course_id': self.course.id} ), follow=True ) redirect_destination = ( '/exam/login/?next=/exam/self_enroll/{}/'.format(self.course.id) ) self.assertRedirects(response, redirect_destination) def test_enroll_request_get_for_student(self): self.client.login( username=self.student.username, password=self.student_plaintext_pass ) response = self.client.get( reverse('yaksh:self_enroll', kwargs={'course_id': self.course.id} ), follow=True ) self.assertRedirects(response, '/exam/quizzes/') def test_enroll_request_get_for_moderator(self): self.client.login( username=self.user2.username, password=self.user2_plaintext_pass ) response = self.client.get( reverse('yaksh:self_enroll', kwargs={'course_id': self.course.id} ), follow=True ) self.assertRedirects(response, '/exam/manage/') class TestGrader(TestCase): def setUp(self): self.client = Client() self.mod_group = Group.objects.create(name='moderator') # Create Moderator 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='demo@test.com' ) Profile.objects.create( user=self.user1, roll_number=10, institute='IIT', department='Chemical', position='Moderator', timezone='UTC', is_moderator=True ) self.user2_plaintext_pass = 'demo2' self.user2 = User.objects.create_user( username='demo_user2', password=self.user2_plaintext_pass, first_name='user2_first_name', last_name='user2_last_name', email='demo2@test.com' ) Profile.objects.create( user=self.user2, roll_number=10, institute='IIT', department='Aeronautical', position='Moderator', timezone='UTC', is_moderator=True ) # Create Student self.student_plaintext_pass = 'demo_student' self.student = User.objects.create_user( username='demo_student', password=self.student_plaintext_pass, first_name='student_first_name', last_name='student_last_name', email='demo_student@test.com' ) # Add to moderator group self.mod_group.user_set.add(self.user1) self.mod_group.user_set.add(self.user2) self.course = Course.objects.create( name="Python Course", enrollment="Enroll Request", creator=self.user1 ) self.question = Question.objects.create( summary='Dummy', points=1, type='code', user=self.user1 ) self.quiz = Quiz.objects.create(time_between_attempts=0, description='demo quiz') self.question_paper = QuestionPaper.objects.create( quiz=self.quiz, total_marks=1.0 ) self.question_paper.fixed_questions.add(self.question) self.question_paper.save() self.answerpaper = AnswerPaper.objects.create( user=self.user2, attempt_number=1, question_paper=self.question_paper, start_time=timezone.now(), user_ip='101.0.0.1', course=self.course, end_time=timezone.now()+timezone.timedelta(minutes=20), ) def tearDown(self): User.objects.all().delete() Course.objects.all().delete() Question.objects.all().delete() Quiz.objects.all().delete() QuestionPaper.objects.all().delete() AnswerPaper.objects.all().delete() self.mod_group.delete() def test_grader_denies_anonymous(self): # Given redirect_destination = ('/exam/login/?next=/exam/manage/grader/') # When response = self.client.get(reverse('yaksh:grader'), follow=True) # Then self.assertRedirects(response, redirect_destination) def test_grader_denies_students(self): # Given self.client.login( username=self.student.username, password=self.student_plaintext_pass ) # When response = self.client.get(reverse('yaksh:grader'), follow=True) # Then self.assertEqual(response.status_code, 404) def test_regrade_denies_anonymous(self): # Given url = "/exam/login/?next=/exam/manage/regrade/answerpaper" redirect_destination = ( url + "/{}/{}/{}/".format( self.course.id, self.question.id, self.answerpaper.id) ) # When response = self.client.get( reverse('yaksh:regrade', kwargs={'course_id': self.course.id, 'question_id': self.question.id, 'answerpaper_id': self.answerpaper.id}), follow=True ) # Then self.assertRedirects(response, redirect_destination) def test_regrade_denies_students(self): # Given self.client.login( username=self.student.username, password=self.student_plaintext_pass ) # When response = self.client.get( reverse('yaksh:regrade', kwargs={'course_id': self.course.id, 'question_id': self.question.id, 'answerpaper_id': self.answerpaper.id}), follow=True ) # Then self.assertEqual(response.status_code, 404) def test_grader_by_moderator(self): # Given self.client.login( username=self.user1.username, password=self.user1_plaintext_pass ) # When response = self.client.get(reverse('yaksh:grader'), follow=True) # Then self.assertEqual(response.status_code, 200) self.assertTrue('courses' in response.context) self.assertTemplateUsed(response, 'yaksh/regrade.html') def test_regrade_by_moderator(self): # Given self.client.login( username=self.user1.username, password=self.user1_plaintext_pass ) # When response = self.client.get( reverse('yaksh:regrade', kwargs={'course_id': self.course.id, 'question_id': self.question.id, 'answerpaper_id': self.answerpaper.id}), follow=True) # Then self.assertEqual(response.status_code, 200) self.assertTrue('courses' in response.context) self.assertTrue('details' in response.context) self.assertTemplateUsed(response, 'yaksh/regrade.html') # When response = self.client.get( reverse('yaksh:regrade', kwargs={'course_id': self.course.id, 'answerpaper_id': self.answerpaper.id}), follow=True) # Then self.assertEqual(response.status_code, 200) self.assertTrue('courses' in response.context) self.assertTrue('details' in response.context) self.assertTemplateUsed(response, 'yaksh/regrade.html') # When response = self.client.get( reverse('yaksh:regrade', kwargs={'course_id': self.course.id, 'question_id': self.question.id, 'questionpaper_id': self.question_paper.id}), follow=True) # Then self.assertEqual(response.status_code, 200) self.assertTrue('courses' in response.context) self.assertTrue('details' in response.context) self.assertTemplateUsed(response, 'yaksh/regrade.html') def test_regrade_denies_moderator_not_in_course(self): # Given self.client.login( username=self.user2.username, password=self.user2_plaintext_pass ) self.mod_group.user_set.remove(self.user2) # When response = self.client.get( reverse('yaksh:regrade', kwargs={'course_id': self.course.id, 'question_id': self.question.id, 'answerpaper_id': self.answerpaper.id}), follow=True) # Then self.assertEqual(response.status_code, 404) self.mod_group.user_set.add(self.user2) class TestPasswordReset(TestCase): def setUp(self): self.mod_group = Group.objects.create(name='moderator') # Create User with profile self.user1_plaintext_pass = 'demo1' self.user1 = User.objects.create_user( username='demo_user1', password=self.user1_plaintext_pass, first_name='first_name', last_name='last_name', email='demo1@test.com' ) Profile.objects.create( user=self.user1, roll_number=10, institute='IIT', department='Chemical', position='Student', timezone='UTC' ) def tearDown(self): self.user1.delete() self.mod_group.delete() def test_password_reset_post(self): """ POST request to password_reset view should return a valid response """ # When response = self.client.post( reverse('password_reset'), data={ 'email': self.user1.email, } ) # Then self.assertEqual(response.context['email'], self.user1.email) self.assertEqual(response.status_code, 302) self.assertRedirects(response, '/exam/reset/password_reset/mail_sent/') def test_password_change_post(self): """ POST request to password_change view should change the user password """ # Given self.client.login( username=self.user1.username, password=self.user1_plaintext_pass ) # When response = self.client.post( reverse('password_change'), data={ 'old_password': self.user1_plaintext_pass, 'new_password1': 'new_demo1_pass', 'new_password2': 'new_demo1_pass' } ) # Then self.assertIsNotNone(authenticate(username='demo_user1', password='new_demo1_pass')) self.assertEqual(response.status_code, 302) self.assertRedirects(response, '/exam/reset/password_change/done/') # Finally self.client.logout() class TestModeratorDashboard(TestCase): def setUp(self): self.client = Client() tzone = pytz.timezone("utc") self.mod_group = Group.objects.create(name='moderator') # student 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='user_first_name', last_name='user_last_name', email='demo@test.com' ) Profile.objects.create( user=self.user, roll_number=10, institute='IIT', department='Chemical', position='Moderator', timezone='UTC', is_moderator=True ) self.mod_no_profile_plaintext_pass = 'demo2' self.mod_no_profile = User.objects.create_user( username='demo_user2', password=self.mod_no_profile_plaintext_pass, first_name='user_first_name22', last_name='user_last_name', email='demo2@test.com' ) self.mod_group.user_set.add(self.user) self.course = Course.objects.create( name="Python Course", enrollment="Enroll Request", creator=self.user ) self.quiz = Quiz.objects.create( start_date_time=datetime(2014, 10, 9, 10, 8, 15, 0, tzone), end_date_time=datetime(2015, 10, 9, 10, 8, 15, 0, tzone), duration=30, active=True, instructions="Demo Instructions", attempts_allowed=-1, time_between_attempts=0, description='demo quiz', pass_criteria=40, creator=self.user ) self.question = Question.objects.create( summary="Test_question", description="Add two numbers", points=1.0, language="python", type="code", user=self.user ) self.question_paper = QuestionPaper.objects.create( quiz=self.quiz, total_marks=1.0, fixed_question_order=str(self.question.id) ) self.question_paper.fixed_questions.add(self.question) # student answerpaper user_answer = "def add(a, b)\n\treturn a+b" self.new_answer = Answer( question=self.question, answer=user_answer, correct=True, error=json.dumps([]), marks=0.5 ) self.new_answer.save() self.answerpaper = AnswerPaper.objects.create( user=self.student, question_paper=self.question_paper, attempt_number=1, start_time=datetime(2014, 10, 9, 10, 8, 15, 0, tzone), end_time=datetime(2014, 10, 9, 10, 15, 15, 0, tzone), user_ip="127.0.0.1", status="completed", passed=True, marks_obtained=0.5, course=self.course ) self.answerpaper.answers.add(self.new_answer) self.answerpaper.questions_answered.add(self.question) self.answerpaper.questions.add(self.question) # moderator trial answerpaper self.trial_course = Course.objects.create( name="Trial Course", enrollment="Enroll Request", creator=self.user, is_trial=True ) self.trial_quiz = Quiz.objects.create( start_date_time=datetime(2014, 10, 9, 10, 8, 15, 0, tzone), end_date_time=datetime(2015, 10, 9, 10, 8, 15, 0, tzone), duration=30, active=True, instructions="Demo Instructions", attempts_allowed=-1, time_between_attempts=0, description='trial quiz', pass_criteria=40, is_trial=True ) self.trial_question_paper = QuestionPaper.objects.create( quiz=self.trial_quiz, total_marks=1.0, fixed_question_order=str(self.question.id) ) self.trial_question_paper.fixed_questions.add(self.question) self.new_answer1 = Answer( question=self.question, answer=user_answer, correct=True, error=json.dumps([]), marks=0.5 ) self.new_answer1.save() self.trial_answerpaper = AnswerPaper.objects.create( user=self.user, question_paper=self.trial_question_paper, attempt_number=1, start_time=datetime(2014, 10, 9, 10, 8, 15, 0, tzone), end_time=datetime(2014, 10, 9, 10, 15, 15, 0, tzone), user_ip="127.0.0.1", status="completed", passed=True, marks_obtained=0.5, course=self.trial_course ) self.trial_answerpaper.answers.add(self.new_answer1) self.trial_answerpaper.questions_answered.add(self.question) self.trial_answerpaper.questions.add(self.question) def tearDown(self): self.client.logout() self.user.delete() self.quiz.delete() self.question_paper.delete() self.answerpaper.delete() self.new_answer.delete() self.mod_group.delete() def test_moderator_dashboard_denies_student(self): """ Check moderator dashboard denies student """ self.client.login( username=self.student.username, password=self.student_plaintext_pass ) response = self.client.get(reverse('yaksh:manage'), follow=True ) self.assertEqual(response.status_code, 200) def test_moderator_dashboard_get_for_user_without_profile(self): """ If no profile exists a blank profile form will be displayed """ self.client.login( username=self.mod_no_profile.username, password=self.mod_no_profile_plaintext_pass ) response = self.client.get(reverse('yaksh:quizlist_user')) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'yaksh/editprofile.html') def test_moderator_dashboard_get_for_user_with_profile(self): """ If profile exists a editprofile.html template will be rendered """ self.client.login( username=self.user.username, password=self.user_plaintext_pass ) response = self.client.get(reverse('yaksh:quizlist_user')) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'yaksh/quizzes_user.html') def test_moderator_dashboard_get_all_quizzes(self): """ Check moderator dashboard to get all the moderator created quizzes """ self.client.login( username=self.user.username, password=self.user_plaintext_pass ) 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): self.client = Client() self.mod_group = Group.objects.create(name='moderator') # Create Moderator 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='demo@test.com' ) Profile.objects.create( user=self.user1, roll_number=10, institute='IIT', department='Chemical', position='Moderator', timezone='UTC' ) def tearDown(self): self.client.logout() settings.IS_DEVELOPMENT = True self.user1.delete() self.mod_group.delete() def test_successful_user_login(self): """ Check if user is successfully logged in """ response = self.client.post( reverse('yaksh:login'), data={'username': self.user1.username, 'password': self.user1_plaintext_pass} ) self.assertEqual(response.status_code, 302) self.assertRedirects(response, '/exam/quizzes/') def test_unsuccessful_user_login(self): """ Check for failed login attempt for incorrect username/password """ response = self.client.post(reverse('yaksh:login'), data={'username': self.user1.username, 'password': "demo"} ) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'yaksh/login.html') def test_email_verified_decorator_for_user_login(self): """ Check email verified decorator to check for user login """ settings.IS_DEVELOPMENT = False response = self.client.post( reverse('yaksh:login'), data={'username': self.user1.username, 'password': self.user1_plaintext_pass} ) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, "yaksh/activation_status.html") class TestDownloadCsv(TestCase): def setUp(self): self.client = Client() tzone = pytz.timezone("utc") self.mod_group = Group.objects.create(name='moderator') # student 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='user_first_name', last_name='user_last_name', email='demo@test.com' ) Profile.objects.create( user=self.user, roll_number=10, institute='IIT', department='Chemical', position='Moderator', timezone='UTC', is_moderator=True ) self.mod_group.user_set.add(self.user) self.course = Course.objects.create( name="Python Course", enrollment="Enroll Request", creator=self.user ) self.course.students.add(self.student) self.quiz = Quiz.objects.create( start_date_time=datetime(2014, 10, 9, 10, 8, 15, 0, tzone), end_date_time=datetime(2015, 10, 9, 10, 8, 15, 0, tzone), duration=30, active=True, instructions="Demo Instructions", attempts_allowed=-1, time_between_attempts=0, description='demo quiz', pass_criteria=40, creator=self.user ) self.question = Question.objects.create( summary="Test_question", description="Add two numbers", points=1.0, language="python", type="code", user=self.user ) self.question_paper = QuestionPaper.objects.create( quiz=self.quiz, total_marks=1.0, fixed_question_order=str(self.question.id) ) self.question_paper.fixed_questions.add(self.question) # student answerpaper user_answer = "def add(a, b)\n\treturn a+b" self.new_answer = Answer( question=self.question, answer=user_answer, correct=True, error=json.dumps([]), marks=0.5 ) self.new_answer.save() self.answerpaper = AnswerPaper.objects.create( user=self.student, question_paper=self.question_paper, attempt_number=1, start_time=datetime(2014, 10, 9, 10, 8, 15, 0, tzone), end_time=datetime(2014, 10, 9, 10, 15, 15, 0, tzone), user_ip="127.0.0.1", status="completed", passed=True, marks_obtained=0.5, course=self.course ) self.answerpaper.answers.add(self.new_answer) self.answerpaper.questions_answered.add(self.question) self.answerpaper.questions.add(self.question) def tearDown(self): self.client.logout() self.user.delete() self.student.delete() self.quiz.delete() self.course.delete() self.mod_group.delete() def test_download_csv_denies_student(self): """ Check download csv denies student """ self.client.login( username=self.student.username, password=self.student_plaintext_pass ) response = self.client.get(reverse('yaksh:download_quiz_csv', kwargs={"course_id": self.course.id, "quiz_id": self.quiz.id}), follow=True ) self.assertEqual(response.status_code, 404) def test_download_course_csv_denies_student(self): """ Check download course csv denies student """ self.client.login( username=self.student.username, password=self.student_plaintext_pass ) response = self.client.get(reverse('yaksh:download_course_csv', kwargs={"course_id": self.course.id}), follow=True ) self.assertEqual(response.status_code, 404) def test_download_csv_denies_non_course_creator(self): """ Check download csv denies non course creator """ self.mod_group.user_set.add(self.student) self.client.login( username=self.student.username, password=self.student_plaintext_pass ) response = self.client.get(reverse('yaksh:download_quiz_csv', kwargs={"course_id": self.course.id, "quiz_id": self.quiz.id}), follow=True ) self.assertEqual(response.status_code, 404) def test_download_course_csv_denies_non_course_creator(self): """ Check download course csv denies non course creator """ self.mod_group.user_set.add(self.student) self.client.login( username=self.student.username, password=self.student_plaintext_pass ) response = self.client.get(reverse('yaksh:download_course_csv', kwargs={"course_id": self.course.id}), follow=True ) self.assertEqual(response.status_code, 404) def test_download_course_csv(self): """ Check for csv result of a course """ self.client.login( username=self.user.username, password=self.user_plaintext_pass ) response = self.client.get( reverse('yaksh:download_course_csv', kwargs={'course_id': self.course.id}), follow=True ) file_name = "{0}.csv".format(self.course.name.lower()) 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 """ self.client.login( username=self.user.username, password=self.user_plaintext_pass ) response = self.client.get( reverse('yaksh:download_quiz_csv', kwargs={"course_id": self.course.id, "quiz_id": self.quiz.id}), follow=True ) file_name = "{0}-{1}-attempt{2}.csv".format( self.course.name.replace('.', ''), self.quiz.description.replace('.', ''), 1 ) self.assertEqual(response.status_code, 200) self.assertEqual(response.get('Content-Disposition'), 'attachment; filename="{0}"'.format(file_name)) class TestShowQuestions(TestCase): def setUp(self): self.client = Client() self.mod_group = Group.objects.create(name='moderator') # student 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='user_first_name', last_name='user_last_name', email='demo@test.com' ) Profile.objects.create( user=self.user, roll_number=10, institute='IIT', department='Chemical', position='Moderator', timezone='UTC', is_moderator=True ) self.mod_group.user_set.add(self.user) self.question = Question.objects.create( summary="Test_question1", description="Add two numbers", points=2.0, language="python", type="code", user=self.user, active=True ) self.question1 = Question.objects.create( summary="Test_question2", description="Add two numbers", 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 tearDown(self): self.client.logout() User.objects.all().delete() Profile.objects.all().delete() Question.objects.all().delete() Group.objects.all().delete() def test_show_questions_denies_student(self): """ Check show questions denies student """ self.client.login( username=self.student.username, password=self.student_plaintext_pass ) response = self.client.get(reverse('yaksh:show_questions'), follow=True ) self.assertEqual(response.status_code, 404) def test_show_all_questions(self): """ Check if all the user created questions are shown """ self.client.login( username=self.user.username, password=self.user_plaintext_pass ) response = self.client.get( reverse('yaksh:show_questions'), follow=True ) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'yaksh/showquestions.html') self.assertEqual(response.context['questions'][0], self.question) def test_download_questions(self): """ Check for downloading questions zip file """ self.client.login( username=self.user.username, password=self.user_plaintext_pass ) response = self.client.post( reverse('yaksh:show_questions'), data={'question': [self.question.id], 'download': 'download'} ) file_name = "{0}_questions.zip".format(self.user) self.assertEqual(response.status_code, 200) self.assertEqual(response.get('Content-Disposition'), "attachment; filename={0}".format(file_name)) zip_file = string_io(response.content) zipped_file = zipfile.ZipFile(zip_file, 'r') self.assertIsNone(zipped_file.testzip()) self.assertIn('questions_dump.yaml', zipped_file.namelist()) zip_file.close() zipped_file.close() response = self.client.post( reverse('yaksh:show_questions'), data={'question': [], 'download': 'download'} ) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'yaksh/showquestions.html') self.assertIn("download", response.context['msg']) def test_upload_zip_questions(self): """ Check for uploading questions zip file """ self.client.login( username=self.user.username, password=self.user_plaintext_pass ) ques_file = os.path.join(FIXTURES_DIR_PATH, "demo_questions.zip") f = open(ques_file, 'rb') questions_file = SimpleUploadedFile(ques_file, f.read(), content_type="application/zip") response = self.client.post( reverse('yaksh:show_questions'), data={'file': questions_file, 'upload': 'upload'} ) summaries = ['Find the value of n', 'Print Output in Python2.x', 'Adding decimals', 'For Loop over String', 'Hello World in File', 'Arrange code to convert km to miles', 'Print Hello, World!', "Square of two numbers", 'Check Palindrome', 'Add 3 numbers', 'Reverse a string' ] uploaded_ques = Question.objects.filter( active=True, summary__in=summaries, user=self.user).count() self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'yaksh/showquestions.html') self.assertEqual(uploaded_ques, 11) f.close() dummy_file = SimpleUploadedFile("test.txt", b"test") response = self.client.post( reverse('yaksh:show_questions'), data={'file': dummy_file, 'upload': 'upload'} ) self.assertEqual(response.status_code, 200) 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 """ self.client.login( username=self.user.username, password=self.user_plaintext_pass ) response = self.client.post( reverse('yaksh:show_questions'), data={'question': [self.question.id], 'test': 'test'} ) trial_que_paper = QuestionPaper.objects.get( quiz__description="trial_questions" ) trial_course = Course.objects.get(name="trial_course") trial_module = trial_course.learning_module.all()[0] redirection_url = "/exam/start/1/{0}/{1}/{2}".format( trial_module.id, trial_que_paper.id, trial_course.id ) self.assertEqual(response.status_code, 302) self.assertRedirects(response, redirection_url, target_status_code=301) def test_ajax_questions_filter(self): """ Check for filter questions based type, marks and language of a question """ self.client.login( username=self.user.username, password=self.user_plaintext_pass ) response = self.client.post( reverse('yaksh:questions_filter'), data={'question_type': 'mcq', 'marks': '1.0', 'language': 'python' } ) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'yaksh/ajax_question_filter.html') self.assertEqual(response.context['questions'][0], self.question1) def test_download_question_yaml_template(self): """ Test to check download question yaml template """ self.client.login( username=self.student.username, password=self.student_plaintext_pass ) # Denies student to download question yaml template response = self.client.get(reverse('yaksh:download_yaml_template')) self.assertEqual(response.status_code, 404) # Moderator Login self.client.login( username=self.user.username, password=self.user_plaintext_pass ) response = self.client.get(reverse('yaksh:download_yaml_template')) self.assertEqual(response.status_code, 200) self.assertEqual(response.get('Content-Disposition'), 'attachment; filename="questions_dump.yaml"') def test_delete_questions(self): """ Test to check if questions are set to not active when deleted """ self.client.login( username=self.user.username, password=self.user_plaintext_pass ) response = self.client.post( reverse('yaksh:show_questions'), data={'question': [self.question.id], 'delete': 'delete'} ) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'yaksh/showquestions.html') updated_que = Question.objects.get(id=self.question.id) self.assertFalse(updated_que.active) def test_search_tags(self): """ Test to check if questions are obtained with tags """ self.client.login( username=self.user.username, password=self.user_plaintext_pass ) self.question.tags.add('code') response = self.client.post( reverse('yaksh:show_questions'), data={'question_tags': ['code']} ) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'yaksh/showquestions.html') self.assertEqual(response.context['questions'][0], self.question) class TestShowStatistics(TestCase): def setUp(self): self.client = Client() self.mod_group = Group.objects.create(name='moderator') tzone = pytz.timezone('UTC') # Create Moderator with profile 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', is_moderator=True ) # Create Student self.student_plaintext_pass = 'demo_student' self.student = User.objects.create_user( username='demo_student', password=self.student_plaintext_pass, first_name='student_first_name', last_name='student_last_name', email='demo_student@test.com' ) Profile.objects.create( user=self.student, roll_number=10, institute='IIT', department='Chemical', position='Moderator', timezone='UTC' ) # Add to moderator group self.mod_group.user_set.add(self.user) self.course = Course.objects.create( name="Python Course", enrollment="Open Enrollment", creator=self.user ) self.quiz = Quiz.objects.create( start_date_time=datetime(2014, 10, 9, 10, 8, 15, 0, tzone), end_date_time=datetime(2015, 10, 9, 10, 8, 15, 0, tzone), duration=30, active=True, instructions="Demo Instructions", attempts_allowed=-1, time_between_attempts=0, description='demo quiz', pass_criteria=40, creator=self.user ) self.question = Question.objects.create( summary="Test_question", description="Add two numbers", points=1.0, language="python", type="code", user=self.user ) self.question_paper = QuestionPaper.objects.create( quiz=self.quiz, total_marks=1.0, fixed_question_order=str(self.question) ) self.question_paper.fixed_questions.add(self.question) user_answer = "def add(a, b)\n\treturn a+b" self.new_answer = Answer( question=self.question, answer=user_answer, correct=True, error=json.dumps([]) ) self.new_answer.save() self.answerpaper = AnswerPaper.objects.create( user=self.student, question_paper=self.question_paper, attempt_number=1, start_time=datetime(2014, 10, 9, 10, 8, 15, 0, tzone), end_time=datetime(2014, 10, 9, 10, 15, 15, 0, tzone), user_ip="127.0.0.1", status="completed", passed=True, percent=1, marks_obtained=1, course=self.course ) self.answerpaper.answers.add(self.new_answer) self.answerpaper.questions_answered.add(self.question) self.answerpaper.questions.add(self.question) def tearDown(self): self.client.logout() self.user.delete() self.student.delete() self.quiz.delete() self.course.delete() self.answerpaper.delete() self.question.delete() self.question_paper.delete() self.new_answer.delete() self.mod_group.delete() def test_show_statistics_denies_student(self): """ Check show statistics denies student """ self.client.login( username=self.student.username, password=self.student_plaintext_pass ) response = self.client.get( reverse('yaksh:show_statistics', kwargs={"questionpaper_id": self.question_paper.id, "course_id": self.course.id}), follow=True ) self.assertEqual(response.status_code, 404) def test_show_statistics_for_student(self): """ Check for student statistics """ self.client.login( username=self.user.username, password=self.user_plaintext_pass ) response = self.client.get( reverse('yaksh:show_statistics', kwargs={'questionpaper_id': self.question_paper.id, "course_id": self.course.id}), follow=True ) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'yaksh/statistics_question.html') self.assertEqual(response.context['quiz'], self.quiz) self.assertEqual(response.context['attempts'][0], self.answerpaper.attempt_number) self.assertEqual(response.context['questionpaper_id'], str(self.question_paper.id)) def test_show_statistics_for_student_per_attempt(self): """ Check for student statistics per attempt """ self.client.login( username=self.user.username, password=self.user_plaintext_pass ) response = self.client.get( reverse('yaksh:show_statistics', kwargs={'questionpaper_id': self.question_paper.id, 'attempt_number': self.answerpaper.attempt_number, "course_id": self.course.id}), follow=True ) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'yaksh/statistics_question.html') self.assertSequenceEqual( response.context['question_stats'][self.question], [1, 1] ) self.assertEqual(response.context['attempts'][0], 1) self.assertEqual(response.context['total'], 1) class TestQuestionPaper(TestCase): def setUp(self): self.client = Client() self.mod_group = Group.objects.create(name='moderator') tzone = pytz.timezone('UTC') # Create Moderator with profile 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', is_moderator=True ) self.student_plaintext_pass = 'demo' self.student = User.objects.create_user( username='demo_student', password=self.student_plaintext_pass, first_name='first_name', last_name='last_name', email='demo@test.com' ) Profile.objects.create( user=self.student, roll_number=10, institute='IIT', department='Chemical', position='Student', timezone='UTC' ) self.user2_plaintext_pass = 'demo2' self.user2 = User.objects.create_user( username='demo_user2', password=self.user_plaintext_pass, first_name='first_name2', last_name='last_name2', email='demo2@test.com' ) Profile.objects.create( user=self.user2, roll_number=11, institute='IIT', department='Chemical', position='Student', timezone='UTC' ) self.teacher_plaintext_pass = 'demo_teacher' self.teacher = User.objects.create_user( username='demo_teacher', password=self.teacher_plaintext_pass, first_name='first_name', last_name='last_name', email='demo@test.com' ) Profile.objects.create( user=self.teacher, roll_number=10, institute='IIT', department='Chemical', position='Moderator', timezone='UTC', is_moderator=True ) # Add to moderator group self.mod_group.user_set.add(self.user) self.mod_group.user_set.add(self.teacher) self.course = Course.objects.create( name="Python Course", enrollment="Open Enrollment", creator=self.user) self.quiz = Quiz.objects.create( start_date_time=datetime(2014, 10, 9, 10, 8, 15, 0, tzone), end_date_time=datetime(2015, 10, 9, 10, 8, 15, 0, tzone), duration=30, active=True, instructions="Demo Instructions", attempts_allowed=-1, time_between_attempts=0, description='demo quiz', pass_criteria=40, creator=self.user ) self.demo_quiz = Quiz.objects.create( start_date_time=datetime(2014, 10, 9, 10, 8, 15, 0, tzone), end_date_time=datetime(2015, 10, 9, 10, 8, 15, 0, tzone), duration=30, active=True, instructions="Demo Instructions", attempts_allowed=-1, time_between_attempts=0, description='demo quiz 2', pass_criteria=40, creator=self.user ) self.quiz_without_qp = Quiz.objects.create( start_date_time=datetime(2014, 10, 9, 10, 8, 15, 0, tzone), end_date_time=datetime(2015, 10, 9, 10, 8, 15, 0, tzone), duration=30, active=True, instructions="Demo Instructions", attempts_allowed=-1, time_between_attempts=0, description='quiz without question paper', pass_criteria=40, creator=self.user ) self.learning_unit = LearningUnit.objects.create( order=1, type="quiz", quiz=self.quiz) self.learning_module = LearningModule.objects.create( order=1, name="test module", description="module", check_prerequisite=False, creator=self.user) self.learning_module.learning_unit.add(self.learning_unit.id) self.course.learning_module.add(self.learning_module) # Questions for random set self.random_que1 = Question.objects.create( summary="Random 1", description="Test Random 1", points=1.0, language="python", type="code", user=self.user ) self.random_que2 = Question.objects.create( summary="Random 2", description="Test Random 2", points=1.0, language="python", type="code", user=self.user ) # Mcq Question self.question_mcq = Question.objects.create( summary="Test_mcq_question", description="Test MCQ", points=1.0, language="python", type="mcq", user=self.user ) self.mcq_based_testcase = McqTestCase( options="a", question=self.question_mcq, correct=True, type='mcqtestcase' ) self.mcq_based_testcase.save() # Mcc Question self.question_mcc = Question.objects.create( summary="Test_mcc_question", description="Test MCC", points=1.0, language="python", type="mcq", user=self.user ) self.mcc_based_testcase = McqTestCase( options="a", question=self.question_mcc, correct=True, type='mcqtestcase' ) self.mcc_based_testcase.save() # Integer Question self.question_int = Question.objects.create( summary="Test_mcc_question", description="Test MCC", points=1.0, language="python", type="integer", user=self.user ) self.int_based_testcase = IntegerTestCase( correct=1, question=self.question_int, type='integertestcase' ) self.int_based_testcase.save() # String Question self.question_str = Question.objects.create( summary="Test_mcc_question", description="Test MCC", points=1.0, language="python", type="string", user=self.user ) self.str_based_testcase = StringTestCase( correct="abc", string_check="lower", question=self.question_str, type='stringtestcase' ) self.str_based_testcase.save() # Float Question self.question_float = Question.objects.create( summary="Test_mcc_question", description="Test MCC", points=1.0, language="python", type="float", user=self.user ) self.float_based_testcase = FloatTestCase( correct=2.0, error_margin=0, question=self.question_float, type='floattestcase' ) self.float_based_testcase.save() # Question with tag self.tagged_que = Question.objects.create( summary="Test_tag_question", description="Test Tag", points=1.0, language="python", type="float", user=self.teacher ) self.tagged_que.tags.add("test_tag") self.questions_list = [self.question_mcq, self.question_mcc, self.question_int, self.question_str, self.question_float] questions_order = ",".join([ str(self.question_mcq.id), str(self.question_mcc.id), str(self.question_int.id), str(self.question_str.id), str(self.question_float.id) ]) self.question_paper = QuestionPaper.objects.create( quiz=self.quiz, total_marks=5.0, fixed_question_order=questions_order ) self.fixed_que = Question.objects.create( summary="Test_fixed_question", description="Test Tag", points=1.0, language="python", type="float", user=self.teacher ) self.fixed_question_paper = QuestionPaper.objects.create( quiz=self.demo_quiz, total_marks=5.0 ) self.question_paper.fixed_questions.add(*self.questions_list) self.answerpaper = AnswerPaper.objects.create( user=self.user, question_paper=self.question_paper, attempt_number=1, start_time=datetime(2014, 10, 9, 10, 8, 15, 0, tzone), end_time=datetime(2014, 10, 9, 10, 15, 15, 0, tzone), user_ip="127.0.0.1", status="inprogress", passed=False, percent=0, marks_obtained=0, course=self.course ) self.answerpaper.questions.add(*self.questions_list) def tearDown(self): self.client.logout() self.user.delete() self.teacher.delete() self.quiz.delete() self.demo_quiz.delete() self.course.delete() self.answerpaper.delete() self.question_mcq.delete() self.question_mcc.delete() self.question_int.delete() self.question_paper.delete() self.learning_module.delete() self.learning_unit.delete() self.mod_group.delete() def test_preview_questionpaper_correct(self): self.client.login( username=self.user.username, password=self.user_plaintext_pass ) # Should successfully preview question paper response = self.client.get( reverse('yaksh:preview_questionpaper', kwargs={"questionpaper_id": self.question_paper.id} ) ) 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_preview_questionpaper_without_moderator(self): self.client.login( username=self.user2.username, password=self.user_plaintext_pass ) # Should raise an HTTP 404 response response = self.client.get( reverse('yaksh:preview_questionpaper', kwargs={"questionpaper_id": self.question_paper.id} ) ) self.assertEqual(response.status_code, 404) def test_preview_qustionpaper_without_quiz_owner(self): self.client.login( username=self.teacher.username, password=self.teacher_plaintext_pass ) # Should raise an HTTP 404 response response = self.client.get( reverse('yaksh:preview_questionpaper', kwargs={"questionpaper_id": self.question_paper.id} ) ) self.assertEqual(response.status_code, 404) def test_mcq_attempt_right_after_wrong(self): """ Case:- Check if answerpaper and answer marks are updated after attempting same mcq question with wrong answer and then right answer """ self.client.login( username=self.user.username, password=self.user_plaintext_pass ) # Given Wrong Answer wrong_user_answer = "25" # When self.client.post( reverse('yaksh:check', kwargs={"q_id": self.question_mcq.id, "attempt_num": 1, "questionpaper_id": self.question_paper.id, "course_id": self.course.id, "module_id": self.learning_module.id}), data={"answer": wrong_user_answer} ) # Then wrong_answer_paper = AnswerPaper.objects.get(id=self.answerpaper.id) self.assertEqual(wrong_answer_paper.marks_obtained, 0) # Given Right Answer right_user_answer = str(self.mcq_based_testcase.id) # When self.client.post( reverse('yaksh:check', kwargs={"q_id": self.question_mcq.id, "attempt_num": 1, "questionpaper_id": self.question_paper.id, "course_id": self.course.id, "module_id": self.learning_module.id}), data={"answer": right_user_answer} ) # Then updated_answerpaper = AnswerPaper.objects.get(id=self.answerpaper.id) self.assertEqual(updated_answerpaper.marks_obtained, 1) def test_mcq_question_attempt_wrong_after_right(self): """ Case:- Check if answerpaper and answer marks are updated after attempting same mcq question with right answer and then wrong answer """ self.client.login( username=self.user.username, password=self.user_plaintext_pass ) # Given Right Answer right_user_answer = str(self.mcq_based_testcase.id) # When self.client.post( reverse('yaksh:check', kwargs={"q_id": self.question_mcq.id, "attempt_num": 1, "questionpaper_id": self.question_paper.id, "course_id": self.course.id, "module_id": self.learning_module.id}), data={"answer": right_user_answer} ) # Then updated_answerpaper = AnswerPaper.objects.get(id=self.answerpaper.id) self.assertEqual(updated_answerpaper.marks_obtained, 1) # Given Wrong Answer wrong_user_answer = "25" # When self.client.post( reverse('yaksh:check', kwargs={"q_id": self.question_mcq.id, "attempt_num": 1, "questionpaper_id": self.question_paper.id, "course_id": self.course.id, "module_id": self.learning_module.id}), data={"answer": wrong_user_answer} ) # Then wrong_answer_paper = AnswerPaper.objects.get(id=self.answerpaper.id) self.assertEqual(wrong_answer_paper.marks_obtained, 0) def test_mcc_question_attempt_wrong_after_right(self): """ Case:- Check if answerpaper and answer marks are updated after attempting same mcc question with right answer and then wrong answer """ self.client.login( username=self.user.username, password=self.user_plaintext_pass ) # Given Right Answer right_user_answer = str(self.mcc_based_testcase.id) # When self.client.post( reverse('yaksh:check', kwargs={"q_id": self.question_mcc.id, "attempt_num": 1, "questionpaper_id": self.question_paper.id, "course_id": self.course.id, "module_id": self.learning_module.id}), data={"answer": right_user_answer} ) # Then updated_answerpaper = AnswerPaper.objects.get(id=self.answerpaper.id) self.assertEqual(updated_answerpaper.marks_obtained, 1) # Given Wrong Answer wrong_user_answer = "b" # When self.client.post( reverse('yaksh:check', kwargs={"q_id": self.question_mcc.id, "attempt_num": 1, "questionpaper_id": self.question_paper.id, "course_id": self.course.id, "module_id": self.learning_module.id}), data={"answer": wrong_user_answer} ) # Then wrong_answer_paper = AnswerPaper.objects.get(id=self.answerpaper.id) self.assertEqual(wrong_answer_paper.marks_obtained, 0) def test_integer_question_attempt_wrong_after_right(self): """ Case:- Check if answerpaper and answer marks are updated after attempting same integer question with right answer and then wrong answer """ self.client.login( username=self.user.username, password=self.user_plaintext_pass ) # Given Right Answer right_user_answer = 1 # When self.client.post( reverse('yaksh:check', kwargs={"q_id": self.question_int.id, "attempt_num": 1, "questionpaper_id": self.question_paper.id, "course_id": self.course.id, "module_id": self.learning_module.id}), data={"answer": right_user_answer} ) # Then updated_answerpaper = AnswerPaper.objects.get(id=self.answerpaper.id) self.assertEqual(updated_answerpaper.marks_obtained, 1) # Given Wrong Answer wrong_user_answer = -1 # When self.client.post( reverse('yaksh:check', kwargs={"q_id": self.question_int.id, "attempt_num": 1, "questionpaper_id": self.question_paper.id, "course_id": self.course.id, "module_id": self.learning_module.id}), data={"answer": wrong_user_answer} ) # Then wrong_answer_paper = AnswerPaper.objects.get(id=self.answerpaper.id) self.assertEqual(wrong_answer_paper.marks_obtained, 0) def test_string_question_attempt_wrong_after_right(self): """ Case:- Check if answerpaper and answer marks are updated after attempting same string question with right answer and then wrong answer """ self.client.login( username=self.user.username, password=self.user_plaintext_pass ) # Given Right Answer right_user_answer = "abc" # When self.client.post( reverse('yaksh:check', kwargs={"q_id": self.question_str.id, "attempt_num": 1, "questionpaper_id": self.question_paper.id, "course_id": self.course.id, "module_id": self.learning_module.id}), data={"answer": right_user_answer} ) # Then updated_answerpaper = AnswerPaper.objects.get(id=self.answerpaper.id) self.assertEqual(updated_answerpaper.marks_obtained, 1) # Given Wrong Answer wrong_user_answer = "c" # When self.client.post( reverse('yaksh:check', kwargs={"q_id": self.question_str.id, "attempt_num": 1, "questionpaper_id": self.question_paper.id, "course_id": self.course.id, "module_id": self.learning_module.id}), data={"answer": wrong_user_answer} ) # Then wrong_answer_paper = AnswerPaper.objects.get(id=self.answerpaper.id) self.assertEqual(wrong_answer_paper.marks_obtained, 0) def test_float_question_attempt_wrong_after_right(self): """ Case:- Check if answerpaper and answer marks are updated after attempting same float question with right answer and then wrong answer """ self.client.login( username=self.user.username, password=self.user_plaintext_pass ) # Given Right Answer right_user_answer = 2.0 # When self.client.post( reverse('yaksh:check', kwargs={"q_id": self.question_float.id, "attempt_num": 1, "questionpaper_id": self.question_paper.id, "course_id": self.course.id, "module_id": self.learning_module.id}), data={"answer": right_user_answer} ) # Then updated_answerpaper = AnswerPaper.objects.get(id=self.answerpaper.id) self.assertEqual(updated_answerpaper.marks_obtained, 1) # Given Wrong Answer wrong_user_answer = -1 # When self.client.post( reverse('yaksh:check', kwargs={"q_id": self.question_float.id, "attempt_num": 1, "questionpaper_id": self.question_paper.id, "course_id": self.course.id, "module_id": self.learning_module.id}), data={"answer": wrong_user_answer} ) # Then wrong_answer_paper = AnswerPaper.objects.get(id=self.answerpaper.id) self.assertEqual(wrong_answer_paper.marks_obtained, 0) def test_design_questionpaper(self): """ Test design Question Paper """ # Should fail if Question paper is not the one which is associated # with a quiz self.client.login( username=self.user.username, password=self.user_plaintext_pass ) response = self.client.get( reverse('yaksh:designquestionpaper', kwargs={"quiz_id": self.demo_quiz.id, "questionpaper_id": self.question_paper.id})) self.assertEqual(response.status_code, 404) # Design question paper for a quiz response = self.client.post( reverse('yaksh:design_questionpaper', kwargs={"quiz_id": self.quiz_without_qp.id}), data={"marks": "1.0", "question_type": "code"}) self.assertEqual(response.status_code, 200) self.assertIsNotNone(response.context['questions']) # Student should not be able to design question paper self.client.login( username=self.student.username, password=self.student_plaintext_pass ) response = self.client.get( reverse('yaksh:designquestionpaper', kwargs={"quiz_id": self.demo_quiz.id, "questionpaper_id": self.question_paper.id})) self.assertEqual(response.status_code, 404) self.client.login( username=self.teacher.username, password=self.teacher_plaintext_pass ) # Should not allow teacher to view question paper response = self.client.get( reverse('yaksh:designquestionpaper', kwargs={"quiz_id": self.quiz.id, "questionpaper_id": self.question_paper.id})) self.assertEqual(response.status_code, 404) # Should not allow teacher to view question paper response = self.client.get( reverse('yaksh:designquestionpaper', kwargs={"quiz_id": self.quiz.id, "course_id": self.course.id, "questionpaper_id": self.question_paper.id})) self.assertEqual(response.status_code, 404) # Should allow course teacher to view question paper # Add teacher to the course self.course.teachers.add(self.teacher) response = self.client.get( reverse('yaksh:designquestionpaper', kwargs={"quiz_id": self.quiz.id, "course_id": self.course.id, "questionpaper_id": self.question_paper.id})) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'yaksh/design_questionpaper.html') self.assertEqual(response.context['fixed_questions'], self.questions_list) self.assertEqual(response.context['qpaper'], self.question_paper) # Get questions using tags for question paper search_tag = [tag for tag in self.tagged_que.tags.all()] response = self.client.post( reverse('yaksh:designquestionpaper', kwargs={"quiz_id": self.quiz.id, "course_id": self.course.id, "questionpaper_id": self.question_paper.id}), data={"question_tags": search_tag}) self.assertEqual(response.context["questions"][0], self.tagged_que) # Add random questions in question paper response = self.client.post( reverse('yaksh:designquestionpaper', kwargs={"quiz_id": self.quiz.id, "course_id": self.course.id, "questionpaper_id": self.question_paper.id}), data={'random_questions': [self.random_que1.id, self.random_que2.id], 'marks': ['1.0'], 'question_type': ['code'], 'add-random': ['']} ) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'yaksh/design_questionpaper.html') random_set = response.context['random_sets'][0] added_random_ques = random_set.questions.all() self.assertIn(self.random_que1, added_random_ques) self.assertIn(self.random_que2, added_random_ques) # Check if questions already exists self.client.login( username=self.user.username, password=self.user_plaintext_pass ) response = self.client.post( reverse('yaksh:designquestionpaper', kwargs={"quiz_id": self.quiz.id, "course_id": self.course.id, "questionpaper_id": self.question_paper.id}), data={'marks': ['1.0'], 'question_type': ['code'], 'add-fixed': ['']} ) self.assertEqual(response.status_code, 200) self.assertEqual(response.context["questions"].count(), 0) # Add fixed question in question paper response = self.client.post( reverse('yaksh:designquestionpaper', kwargs={"quiz_id": self.demo_quiz.id, "course_id": self.course.id, "questionpaper_id": self.fixed_question_paper.id}), data={'checked_ques': [self.fixed_que.id], 'add-fixed': ''} ) self.assertEqual(response.status_code, 200) self.assertEqual(response.context['qpaper'], self.fixed_question_paper) self.assertEqual(response.context['fixed_questions'][0], self.fixed_que) # Add one more fixed question in question paper response = self.client.post( reverse('yaksh:designquestionpaper', kwargs={"quiz_id": self.demo_quiz.id, "course_id": self.course.id, "questionpaper_id": self.fixed_question_paper.id}), data={'checked_ques': [self.question_float.id], 'add-fixed': ''} ) self.assertEqual(response.status_code, 200) self.assertEqual(response.context['qpaper'], self.fixed_question_paper) self.assertEqual(response.context['fixed_questions'], [self.fixed_que, self.question_float]) # Remove fixed question from question paper response = self.client.post( reverse('yaksh:designquestionpaper', kwargs={"quiz_id": self.demo_quiz.id, "course_id": self.course.id, "questionpaper_id": self.fixed_question_paper.id}), data={'added-questions': [self.fixed_que.id], 'remove-fixed': ''} ) self.assertEqual(response.status_code, 200) self.assertEqual(response.context['qpaper'], self.fixed_question_paper) self.assertEqual(response.context['fixed_questions'], [self.question_float]) # Remove one more fixed question from question paper response = self.client.post( reverse('yaksh:designquestionpaper', kwargs={"quiz_id": self.demo_quiz.id, "course_id": self.course.id, "questionpaper_id": self.fixed_question_paper.id}), data={'added-questions': [self.question_float.id], 'remove-fixed': ''} ) self.assertEqual(response.status_code, 200) self.assertEqual(response.context['qpaper'], self.fixed_question_paper) self.assertEqual(response.context['fixed_questions'], []) # Remove random questions from question paper random_que_set = self.question_paper.random_questions.all().first() response = self.client.post( reverse('yaksh:designquestionpaper', kwargs={"quiz_id": self.quiz.id, "course_id": self.course.id, "questionpaper_id": self.question_paper.id}), data={'random_sets': random_que_set.id, 'remove-random': ''} ) self.assertEqual(response.status_code, 200) self.assertEqual(response.context['qpaper'], self.question_paper) self.assertEqual(len(response.context['random_sets']), 0) class TestLearningModule(TestCase): def setUp(self): self.client = Client() self.mod_group = Group.objects.create(name='moderator') tzone = pytz.timezone('UTC') # Create Moderator with profile 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', is_moderator=True ) # Create a student self.student_plaintext_pass = 'demo_student' self.student = User.objects.create_user( username='demo_student', password=self.student_plaintext_pass, first_name='first_name', last_name='last_name', email='demo@student.com' ) # Create a teacher to add to the course self.teacher_plaintext_pass = 'demo_teacher' self.teacher = User.objects.create_user( username='demo_teacher', password=self.teacher_plaintext_pass, first_name='first_name', last_name='last_name', email='demo@teacher.com', ) Profile.objects.create( user=self.teacher, roll_number=10, institute='IIT', department='Chemical', position='Moderator', timezone='UTC', is_moderator=True ) # Add to moderator group self.mod_group.user_set.add(self.user) self.mod_group.user_set.add(self.teacher) self.course = Course.objects.create( name="Python Course", enrollment="Open Enrollment", creator=self.user) self.question = Question.objects.create( summary="Test_question", description="Add two numbers", points=1.0, language="python", type="code", user=self.user ) self.quiz = Quiz.objects.create( start_date_time=datetime(2014, 10, 9, 10, 8, 15, 0, tzone), end_date_time=datetime(2199, 10, 9, 10, 8, 15, 0, tzone), duration=30, active=True, instructions="Demo Instructions", attempts_allowed=-1, time_between_attempts=0, description='demo quiz', pass_criteria=40, creator=self.user ) self.question_paper = QuestionPaper.objects.create( quiz=self.quiz, total_marks=5.0, fixed_question_order=str(self.question.id) ) self.question_paper.fixed_questions.add(self.question) self.lesson = Lesson.objects.create( name="test lesson", description="test description", creator=self.user) # create quiz learning unit self.learning_unit = LearningUnit.objects.create( order=0, type="quiz", quiz=self.quiz) # create lesson learning unit self.learning_unit1 = LearningUnit.objects.create( order=1, type="lesson", lesson=self.lesson) # create learning module self.learning_module = LearningModule.objects.create( order=0, name="test module", description="module", check_prerequisite=False, creator=self.user) self.learning_module.learning_unit.add(self.learning_unit) self.learning_module.learning_unit.add(self.learning_unit1) self.learning_module1 = LearningModule.objects.create( order=0, name="my module", description="my description", check_prerequisite=False, creator=self.user) self.course.teachers.add(self.teacher) self.course.learning_module.add(self.learning_module) self.course.learning_module.add(self.learning_module1) self.expected_url = "/exam/manage/courses/all_learning_module/" def tearDown(self): self.user.delete() self.student.delete() self.teacher.delete() self.quiz.delete() self.course.delete() self.learning_unit.delete() self.learning_module.delete() self.mod_group.delete() def test_add_new_module_denies_non_moderator(self): self.client.login( username=self.student.username, password=self.student_plaintext_pass ) # Student tries to add learning module response = self.client.post(reverse('yaksh:add_module'), data={"name": "test module1", "description": "my test1", "Save": "Save"}) self.assertEqual(response.status_code, 404) # Student tries to view learning modules response = self.client.get(reverse('yaksh:show_all_modules')) self.assertEqual(response.status_code, 404) def test_add_new_module(self): """ Check if new module is created """ self.client.login( username=self.user.username, password=self.user_plaintext_pass ) # Test add new module response = self.client.post(reverse('yaksh:add_module'), data={"name": "test module1", "description": "my test1", "Save": "Save"}) self.assertEqual(response.status_code, 302) self.assertRedirects(response, self.expected_url) learning_module = LearningModule.objects.get(name="test module1") self.assertEqual(learning_module.description, "my test1") self.assertEqual(learning_module.creator, self.user) self.assertTrue(learning_module.check_prerequisite) self.assertEqual(learning_module.html_data, Markdown().convert("my test1")) def test_edit_module(self): """ Check if existing module is editable """ self.client.login( username=self.user.username, password=self.user_plaintext_pass ) # Test add new module response = self.client.post( reverse('yaksh:edit_module', kwargs={"module_id": self.learning_module.id}), data={"name": "test module2", "description": "my test2", "Save": "Save"}) self.assertEqual(response.status_code, 302) self.assertRedirects(response, self.expected_url) learning_module = LearningModule.objects.get(name="test module2") self.assertEqual(learning_module.description, "my test2") self.assertEqual(learning_module.creator, self.user) self.assertFalse(learning_module.check_prerequisite) self.assertEqual(learning_module.html_data, Markdown().convert("my test2")) def test_show_all_modules(self): """Try to get all learning modules""" self.client.login( username=self.user.username, password=self.user_plaintext_pass ) 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.learning_module) def test_teacher_can_edit_module(self): """ Check if teacher can edit the module """ self.client.login( username=self.teacher.username, password=self.teacher_plaintext_pass ) response = self.client.post( reverse('yaksh:edit_module', kwargs={"module_id": self.learning_module.id, "course_id": self.course.id}), data={"name": "teacher module 2", "description": "teacher module 2", "Save": "Save"}) self.assertEqual(response.status_code, 302) self.assertRedirects(response, "/exam/manage/courses/") learning_module = LearningModule.objects.get(name="teacher module 2") self.assertEqual(learning_module.description, "teacher module 2") self.assertEqual(learning_module.creator, self.user) def test_teacher_can_design_module(self): """ Check if teacher can design the module i.e add learning units """ self.client.login( username=self.teacher.username, password=self.teacher_plaintext_pass ) response = self.client.post( reverse('yaksh:design_module', kwargs={"module_id": self.learning_module1.id, "course_id": self.course.id}), data={"Add": "Add", "chosen_list": ",".join([str(self.quiz.id)+":"+"quiz"]) }) # Test add learning unit self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'yaksh/add_module.html') learning_unit = self.learning_module1.learning_unit.all().first() self.assertEqual(self.quiz, learning_unit.quiz) self.assertEqual(learning_unit.type, "quiz") self.assertEqual(learning_unit.order, 1) # Test change order of learning unit response = self.client.post( reverse('yaksh:design_module', kwargs={"module_id": self.learning_module1.id, "course_id": self.course.id}), data={"Change": "Change", "ordered_list": ",".join([str(learning_unit.id)+":"+"0"]) }) updated_learning_unit = LearningUnit.objects.get(id=learning_unit.id) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'yaksh/add_module.html') self.assertEqual(updated_learning_unit.order, 0) # Test change check prerequisite response = self.client.post( reverse('yaksh:design_module', kwargs={"module_id": self.learning_module1.id, "course_id": self.course.id}), data={"Change_prerequisite": "Change_prerequisite", "check_prereq": [str(learning_unit.id)] }) updated_learning_unit = LearningUnit.objects.get(id=learning_unit.id) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'yaksh/add_module.html') self.assertFalse(updated_learning_unit.check_prerequisite) # Test to remove learning unit from learning module response = self.client.post( reverse('yaksh:design_module', kwargs={"module_id": self.learning_module1.id, "course_id": self.course.id}), data={"Remove": "Remove", "delete_list": [str(learning_unit.id)] }) updated_learning_unit = LearningUnit.objects.filter( id=learning_unit.id).exists() self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'yaksh/add_module.html') self.assertFalse(updated_learning_unit) self.assertFalse(self.learning_module1.learning_unit.all().exists()) def test_get_learning_units_for_design(self): self.client.login( username=self.teacher.username, password=self.teacher_plaintext_pass ) response = self.client.get( reverse('yaksh:design_module', kwargs={"module_id": self.learning_module1.id, "course_id": self.course.id})) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'yaksh/add_module.html') self.assertEqual(response.context['quiz_les_list'], set()) self.assertEqual(len(response.context['learning_units']), 0) self.assertEqual(response.context['status'], "design") self.assertEqual(response.context['module_id'], str(self.learning_module1.id)) self.assertEqual(response.context['course_id'], str(self.course.id)) def test_view_module(self): """ Student tries to view a module containing learning units """ # Student is not enrolled in the course is thrown out self.client.login( username=self.student.username, password=self.student_plaintext_pass ) response = self.client.get( reverse('yaksh:view_module', kwargs={"module_id": self.learning_module.id, "course_id": self.course.id})) self.assertEqual(response.status_code, 404) # add student to the course self.course.students.add(self.student.id) # check if enrolled student can view module response = self.client.get( reverse('yaksh:view_module', kwargs={"module_id": self.learning_module.id, "course_id": self.course.id})) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, "yaksh/show_video.html") self.assertEqual(response.context['learning_units'][0], self.learning_unit) self.assertEqual(response.context["state"], "module") self.assertEqual(response.context["first_unit"], self.learning_unit) self.assertEqual(response.context["user"], self.student) def test_get_next_unit(self): """ Check if we get correct next unit """ # Student who is not enrolled is thrown out self.client.login( username=self.student.username, password=self.student_plaintext_pass ) response = self.client.get( reverse('yaksh:view_module', kwargs={"module_id": self.learning_module.id, "course_id": self.course.id})) self.assertEqual(response.status_code, 404) # Enroll student in the course self.course.students.add(self.student.id) # Get First unit as next unit response = self.client.get( reverse('yaksh:next_unit', kwargs={"module_id": self.learning_module.id, "course_id": self.course.id, "current_unit_id": self.learning_unit.id, "first_unit": "1"}), follow=True) self.assertEqual(response.status_code, 200) self.assertEqual(response.context['attempt_num'], 1) self.assertEqual(response.context["questionpaper"], self.question_paper) self.assertEqual(response.context["course"], self.course) self.assertEqual(response.context["module"], self.learning_module) self.assertEqual(response.context["user"], self.student) # Get next unit after first unit response = self.client.get( reverse('yaksh:next_unit', kwargs={"module_id": self.learning_module.id, "course_id": self.course.id, "current_unit_id": self.learning_unit.id}), follow=True) self.assertEqual(response.status_code, 200) self.assertEqual(response.context["state"], "lesson") self.assertEqual(response.context["current_unit"].id, self.learning_unit1.id) # Go to next module with empty module response = self.client.get( reverse('yaksh:next_unit', kwargs={"module_id": self.learning_module1.id, "course_id": self.course.id}), follow=True) self.assertEqual(response.status_code, 200) self.assertEqual(response.context["state"], "module") self.assertEqual(response.context["learning_module"].id, self.learning_module.id) # Go to next module from last unit of previous unit response = self.client.get( reverse('yaksh:next_unit', kwargs={"module_id": self.learning_module.id, "course_id": self.course.id, "current_unit_id": self.learning_unit1.id}), follow=True) self.assertEqual(response.status_code, 200) self.assertEqual(response.context["state"], "module") self.assertEqual(response.context["learning_module"].id, self.learning_module1.id) class TestLessons(TestCase): def setUp(self): self.client = Client() self.mod_group = Group.objects.create(name='moderator') # Create Moderator with profile 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', is_moderator=True ) # Create a student self.student_plaintext_pass = 'demo_student' self.student = User.objects.create_user( username='demo_student', password=self.student_plaintext_pass, first_name='first_name', last_name='last_name', email='demo@student.com' ) Profile.objects.create( user=self.student, roll_number=10, institute='IIT', department='Chemical', position='student', timezone='UTC' ) # Create a teacher to add to the course self.teacher_plaintext_pass = 'demo_teacher' self.teacher = User.objects.create_user( username='demo_teacher', password=self.teacher_plaintext_pass, first_name='first_name', last_name='last_name', email='demo@student.com' ) Profile.objects.create( user=self.teacher, roll_number=10, institute='IIT', department='Chemical', position='Moderator', timezone='UTC', is_moderator=True ) # Add to moderator group self.mod_group.user_set.add(self.user) self.mod_group.user_set.add(self.teacher) self.course = Course.objects.create( name="Python Course", enrollment="Open Enrollment", creator=self.user) self.lesson = Lesson.objects.create( name="test lesson", description="test description", creator=self.user) self.lesson2 = Lesson.objects.create( name="test lesson2", description="test description2", creator=self.user) self.learning_unit = LearningUnit.objects.create( order=0, type="lesson", lesson=self.lesson ) self.learning_unit2 = LearningUnit.objects.create( order=0, type="lesson", lesson=self.lesson2 ) self.learning_module = LearningModule.objects.create( order=0, name="test module", description="module", check_prerequisite=False, creator=self.user ) self.learning_module2 = LearningModule.objects.create( order=1, name="test module 2", description="module 2", check_prerequisite=True, creator=self.user ) self.learning_module.learning_unit.add(self.learning_unit.id) self.learning_module2.learning_unit.add(self.learning_unit2.id) self.course.learning_module.add(*[ 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() self.teacher.delete() self.course.delete() self.learning_unit.delete() self.learning_unit2.delete() self.learning_module.delete() self.learning_module2.delete() self.lesson.delete() self.lesson2.delete() self.mod_group.delete() def test_edit_lesson_denies_non_moderator(self): """ Student should not be allowed to edit lesson """ self.client.login( username=self.student.username, password=self.student_plaintext_pass ) # Student tries to edit lesson response = self.client.get( reverse('yaksh:edit_lesson', kwargs={"lesson_id": self.lesson.id, "course_id": self.course.id})) self.assertEqual(response.status_code, 404) def test_teacher_can_edit_lesson(self): """ Teacher should be allowed to edit lesson """ self.client.login( username=self.teacher.username, 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, "course_id": self.course.id}), data={"name": "updated lesson", "description": "updated description", "Lesson_files": dummy_file, "video_file": video_file, "Save": "Save"} ) # Teacher edits existing lesson and adds file self.assertEqual(response.status_code, 302) self.assertRedirects(response, self.expected_url) 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") lesson_files = LessonFile.objects.filter( lesson=self.lesson).first() self.assertIn("test.txt", lesson_files.file.name) lesson_file_path = lesson_files.file.path # Teacher removes the lesson file response = self.client.post( reverse('yaksh:edit_lesson', kwargs={"lesson_id": self.lesson.id, "course_id": self.course.id}), data={"delete_files": [str(lesson_files.id)], "Delete": "Delete"} ) self.assertEqual(response.status_code, 302) self.assertRedirects(response, self.expected_url) lesson_file_exists = LessonFile.objects.filter( 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 """ self.client.login( username=self.student.username, password=self.student_plaintext_pass ) response = self.client.get( reverse('yaksh:show_lesson', kwargs={"lesson_id": self.lesson.id, "module_id": self.learning_module.id, "course_id": self.course.id})) # Student is unable to view lesson self.assertEqual(response.status_code, 404) # Add student to course self.course.students.add(self.student.id) response = self.client.get( reverse('yaksh:show_lesson', kwargs={"lesson_id": self.lesson.id, "module_id": self.learning_module.id, "course_id": self.course.id})) self.assertEqual(response.status_code, 200) self.assertEqual(response.context["state"], "lesson") self.assertEqual(response.context["current_unit"], self.learning_unit) # Check unit module prerequisite completion status response = self.client.get( reverse('yaksh:show_lesson', kwargs={"lesson_id": self.lesson2.id, "module_id": self.learning_module2.id, "course_id": self.course.id})) err_msg = "You have not completed the module previous to {0}".format( self.learning_module2.name) self.assertEqual(response.status_code, 200) self.assertEqual(response.context["msg"], err_msg) # Check if lesson is active self.lesson.active = False self.lesson.save() response = self.client.get( reverse('yaksh:show_lesson', kwargs={"lesson_id": self.lesson.id, "module_id": self.learning_module.id, "course_id": self.course.id})) err_msg = "{0} is not active".format(self.lesson.name) self.assertEqual(response.status_code, 200) self.assertEqual(response.context["msg"], err_msg) # Check if module is active self.learning_module2.active = False self.learning_module2.save() response = self.client.get( reverse('yaksh:show_lesson', kwargs={"lesson_id": self.lesson2.id, "module_id": self.learning_module2.id, "course_id": self.course.id})) err_msg = "{0} is not active".format(self.learning_module2.name) self.assertEqual(response.status_code, 200) self.assertEqual(response.context["msg"], err_msg) def test_show_all_lessons(self): """ Moderator should be able to see all created lessons""" self.client.login( username=self.user.username, password=self.user_plaintext_pass ) 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.assertEqual(response.context["lessons"][0], self.lesson) def test_preview_lesson_description(self): """ Test preview lesson description converted from md to html""" self.client.login( username=self.teacher.username, password=self.teacher_plaintext_pass ) lesson = json.dumps({'description': self.lesson.description}) response = self.client.post( reverse('yaksh:preview_html_text'), data=lesson, content_type="application/json" ) self.assertEqual(response.status_code, 200) self.assertEqual(response.json()['data'], '
test description
')