示例#1
0
    def setUpGradings(self):
        self.passGrade = Grading(title='passed', means_passed=True)
        self.passGrade.save()
        self.failGrade = Grading(title='failed', means_passed=False)
        self.failGrade.save()

        self.passFailGrading = GradingScheme(title='Pass/Fail Grading Scheme')
        self.passFailGrading.save()
        self.passFailGrading.gradings.add(self.passGrade)
        self.passFailGrading.gradings.add(self.failGrade)
        self.passFailGrading.save()
示例#2
0
class SubmitTestCase(TestCase):
    current_user = None

    def createUser(self, user_dict):
        args = dict(user_dict)
        args['password'] = make_password(args['password'])
        user_obj = User(**args)
        user_obj.save()

        user_profile = UserProfile(user=user_obj)
        user_profile.save()

        user_dict['user'] = user_obj
        user_dict['profile'] = user_profile
        user_struct = AnonStruct(user_dict)
        return user_struct

    def loginUser(self, user_struct):
        assert(self.c.login(username=user_struct.username, password=user_struct.password))
        uid = self.c.session['_auth_user_id']
        user_struct.user = User.objects.get(pk=uid)
        self.current_user = user_struct

    def setUpUsers(self):
        self.c = Client()

        self.admin_dict = {
            'username': 'testrunner_admin',
            'password': 'PNZabhExaL6H',
            'email': '[email protected]',
            'is_staff': True,
            'is_superuser': True
        }
        self.admin = self.createUser(self.admin_dict)

        self.teacher_dict = {
            'username': 'testrunner_teacher',
            'password': '2tVvWzdknP56',
            'email': '[email protected]',
            'is_staff': True,
            'is_superuser': False
        }
        self.teacher = self.createUser(self.teacher_dict)

        self.another_teacher_dict = {
            'username': 'testrunner_anotherTeacher',
            'password': 'LW8vhgQWz5kT',
            'email': '[email protected]',
            'is_staff': True,
            'is_superuser': False
        }
        self.another_teacher = self.createUser(self.another_teacher_dict)

        self.tutor_dict = {
            'username': 'testrunner_tutor',
            'password': '2tVP56vMadkn',
            'email': '[email protected]',
            'is_staff': True,
            'is_superuser': False
        }
        self.tutor = self.createUser(self.tutor_dict)

        self.enrolled_students = list()
        for i in range(0, 5):
            enrolled_student_dict = {
                'username': 'testrunner_enrolled_student{}'.format(i),
                'password': 'very{}secret'.format(i),
                'email': 'testrunner_enrolled_student{}@django.localhost.local'.format(i),
                'is_staff': False,
                'is_superuser': False,
                'first_name': 'Harold',
                'last_name': 'Finch'
            }
            self.enrolled_students.append(self.createUser(enrolled_student_dict))

        self.not_enrolled_students = list()
        for i in range(0, 5):
            not_enrolled_student_dict = {
                'username': 'testrunner_not_enrolled_student{}'.format(i),
                'password': 'not.very{}secret'.format(i),
                'email': 'testrunner_not_enrolled_student{}@django.localhost.local'.format(i),
                'is_staff': False,
                'is_superuser': False
            }
            self.not_enrolled_students.append(self.createUser(not_enrolled_student_dict))

    def setUpCourses(self):
        self.all_courses = []

        self.course = Course(
            title='Active test course',
            active=True,
            owner=self.teacher.user,
            max_authors=3,
        )
        self.course.save()
        self.course.tutors.add(self.tutor.user)
        for student in self.enrolled_students:
            self.course.participants.add(student.profile)
        self.all_courses.append(self.course)

        self.anotherCourse = Course(
            title='Another active test course',
            active=True,
            owner=self.another_teacher.user,
            max_authors=1,
        )
        self.anotherCourse.save()
        self.all_courses.append(self.anotherCourse)        
        
        self.inactiveCourse = Course(
            title='Inactive test course',
            active=False,
            owner=self.another_teacher.user,
            max_authors=1,
        )
        self.inactiveCourse.save()
        self.all_courses.append(self.inactiveCourse)

    def setUpGradings(self):
        self.passGrade = Grading(title='passed', means_passed=True)
        self.passGrade.save()
        self.failGrade = Grading(title='failed', means_passed=False)
        self.failGrade.save()

        self.passFailGrading = GradingScheme(title='Pass/Fail Grading Scheme')
        self.passFailGrading.save()
        self.passFailGrading.gradings.add(self.passGrade)
        self.passFailGrading.gradings.add(self.failGrade)
        self.passFailGrading.save()

    def setUpAssignments(self):
        from django.core.files import File as DjangoFile

        today = timezone.now()
        last_week = today - datetime.timedelta(weeks=1)
        yesterday = today - datetime.timedelta(days=1)
        tomorrow = today + datetime.timedelta(days=1)
        next_week = today + datetime.timedelta(weeks=1)

        self.allAssignments = []

        self.openAssignment = Assignment(
            title='Open assignment',
            course=self.course,
            download='http://example.org/assignments/1/download',
            gradingScheme=self.passFailGrading,
            publish_at=last_week,
            soft_deadline=tomorrow,
            hard_deadline=next_week,
            has_attachment=False
        )
        self.openAssignment.save()
        self.allAssignments.append(self.openAssignment)

        self.validatedAssignment = Assignment(
            title='Validated assignment',
            course=self.course,
            download='http://example.org/assignments/1/download',
            gradingScheme=self.passFailGrading,
            publish_at=last_week,
            soft_deadline=tomorrow,
            hard_deadline=next_week,
            has_attachment=True,
            validity_script_download=True,
            attachment_test_validity=DjangoFile(open(rootdir+'/opensubmit/tests/validators/working.zip')),
            attachment_test_full=DjangoFile(open(rootdir+'/opensubmit/tests/validators/working.zip'))
        )
        self.validatedAssignment.save()
        self.allAssignments.append(self.validatedAssignment)


        self.softDeadlinePassedAssignment = Assignment(
            title='Soft deadline passed assignment',
            course=self.course,
            download='http://example.org/assignments/2/download',
            gradingScheme=self.passFailGrading,
            publish_at=last_week,
            soft_deadline=yesterday,
            hard_deadline=tomorrow,
            has_attachment=False,
        )
        self.softDeadlinePassedAssignment.save()
        self.allAssignments.append(self.softDeadlinePassedAssignment)

        self.hardDeadlinePassedAssignment = Assignment(
            title='Hard deadline passed assignment',
            course=self.course,
            download='http://example.org/assignments/3/download',
            gradingScheme=self.passFailGrading,
            publish_at=last_week,
            soft_deadline=yesterday,
            hard_deadline=yesterday,
            has_attachment=False,
        )
        self.hardDeadlinePassedAssignment.save()
        self.allAssignments.append(self.hardDeadlinePassedAssignment)        

        self.unpublishedAssignment = Assignment(
            title='Unpublished assignment',
            course=self.course,
            download='http://example.org/assignments/4/download',
            gradingScheme=self.passFailGrading,
            publish_at=tomorrow,
            soft_deadline=next_week,
            hard_deadline=next_week,
            has_attachment=False,
        )
        self.unpublishedAssignment.save()
        self.allAssignments.append(self.unpublishedAssignment)        

    def setUp(self):
        super(SubmitTestCase, self).setUp()
        self.logger = logging.getLogger('OpenSubmit')
        self.loggerLevelOld = self.logger.level
        self.logger.setLevel(logging.WARN)
        self.setUpUsers()
        self.setUpCourses()
        self.setUpGradings()
        self.setUpAssignments()

    def tearDown(self):
        self.logger.setLevel(self.loggerLevelOld)

    def createTestMachine(self, test_host):
        '''
            Create test machine entry. The configuration information
            is expected to be some JSON dictionary, since this is
            normally directly rendered in the machine details view.
        '''
        self.machine = TestMachine(
            last_contact=datetime.datetime.now(),
            host = test_host,
            config=json.dumps({'Operating system':'Plan 9'}))
        self.machine.save()
        return self.machine

    def createSubmissionFile(self):
        from django.core.files import File as DjangoFile
        sf = SubmissionFile(attachment=DjangoFile(open(rootdir+"/opensubmit/tests/submfiles/working_withsubdir.zip"), unicode("working_withsubdir.zip")))
        sf.save()  
        return sf      

    def createTestedSubmissionFile(self, test_machine):
        '''
            Create finalized test result in the database.
        '''
        sf = self.createSubmissionFile()
        result_compile  = SubmissionTestResult(
            kind=SubmissionTestResult.COMPILE_TEST,
            result="Compilation ok.",
            machine=test_machine,
            submission_file=sf
            ).save()
        result_validity = SubmissionTestResult(
            kind=SubmissionTestResult.VALIDITY_TEST,
            result="Validation ok.",
            machine=self.machine,
            perf_data = "41;42;43",
            submission_file=sf).save()
        result_full     = SubmissionTestResult(
            kind=SubmissionTestResult.FULL_TEST,
            result="Full test ok.",
            perf_data = "77;88;99",
            machine=self.machine,
            submission_file=sf).save()
        return sf

    def createValidatableSubmission(self, user):
        '''
            Create a submission that can be validated by executor.
        '''
        sf = self.createSubmissionFile()
        sub = Submission(
            assignment=self.validatedAssignment,
            submitter=user.user,
            notes="This is a validatable submission.",
            state=Submission.TEST_COMPILE_PENDING,
            file_upload=sf
        )
        sub.save()
        return sub

    def createValidatedSubmission(self, user, test_host='127.0.0.1'):
        '''
            Create a submission that already has test results in the database.
        '''
        machine = self.createTestMachine(test_host)
        sf = self.createTestedSubmissionFile(machine)
        sub = Submission(
            assignment=self.validatedAssignment,
            submitter=user.user,
            notes="This is an already validated submission.",
            state=Submission.SUBMITTED_TESTED,
            file_upload=sf
        )
        sub.save()
        return sub

    def createSubmission(self, user, assignment, authors=[]):
        sub = Submission(
            assignment=assignment,
            submitter=user.user,
            notes="This is a submission.",
            state=Submission.SUBMITTED
        )
        sub.save()

        if authors:
            [sub.authors.add(author) for author in authors]
        sub.save()

        return sub
示例#3
0
class SubmitTestCase(LiveServerTestCase):
    current_user = None

    def createUser(self, user_dict):
        user_obj = User.objects.create_user(
            username=user_dict['username'],
            password=user_dict['password'],
            email=user_dict['email'],
        )

        if 'is_superuser' in user_dict:
            user_obj.is_superuser = user_dict['is_superuser']
        if 'is_staff' in user_dict:
            user_obj.is_staff = user_dict['is_staff']

        user_obj.save()
        user_profile = UserProfile(user=user_obj)
        user_profile.save()

        user_dict['user'] = user_obj
        user_dict['profile'] = user_profile
        user_struct = AnonStruct(user_dict)
        return user_struct

    def loginUser(self, user_struct):
        result = self.c.login(username=user_struct.username, password=user_struct.password)
        if result:
            self.current_user = user_struct
        return result

    def setUpUsers(self):
        self.c = Client()
        self.admin_dict = {
            'username': 'testrunner_admin',
            'password': 'PNZabhExaL6H',
            'email': '[email protected]',
            'is_superuser': True,
            'is_staff': True,
        }
        self.admin = self.createUser(self.admin_dict)

        self.teacher_dict = {
            'username': 'testrunner_teacher',
            'password': '2tVvWzdknP56',
            'email': '[email protected]',
            'is_staff': True,
        }
        self.teacher = self.createUser(self.teacher_dict)

        self.another_teacher_dict = {
            'username': 'testrunner_anotherTeacher',
            'password': 'LW8vhgQWz5kT',
            'email': '[email protected]',
            'is_staff': True,
        }
        self.another_teacher = self.createUser(self.another_teacher_dict)

        self.tutor_dict = {
            'username': 'testrunner_tutor',
            'password': '2tVP56vMadkn',
            'email': '[email protected]',
            'is_staff': True,
        }
        self.tutor = self.createUser(self.tutor_dict)

        self.enrolled_students = list()
        for i in range(0, 5):
            enrolled_student_dict = {
                'username': 'testrunner_enrolled_student{}'.format(i),
                'password': 'very{}secret'.format(i),
                'email': 'testrunner_enrolled_student{}@django.localhost.local'.format(i),
            }
            self.enrolled_students.append(self.createUser(enrolled_student_dict))

        self.not_enrolled_students = list()
        for i in range(0, 5):
            not_enrolled_student_dict = {
                'username': 'testrunner_not_enrolled_student{}'.format(i),
                'password': 'not.very{}secret'.format(i),
                'email': 'testrunner_not_enrolled_student{}@django.localhost.local'.format(i),
            }
            self.not_enrolled_students.append(self.createUser(not_enrolled_student_dict))

    def setUpCourses(self):
        self.course = Course(
            title='Active test course',
            active=True,
            owner=self.teacher.user,
            max_authors=3,
        )
        self.course.save()
        self.course.tutors.add(self.tutor.user)
        for student in self.enrolled_students:
            self.course.participants.add(student.profile)
        
        self.anotherCourse = Course(
            title='Another active test course',
            active=True,
            owner=self.another_teacher.user,
            max_authors=1,
        )
        self.anotherCourse.save()
        
        self.inactiveCourse = Course(
            title='Inactive test course',
            active=False,
            owner=self.another_teacher.user,
            max_authors=1,
        )
        self.inactiveCourse.save()

    def setUpGradings(self):
        self.passGrade = Grading(title='passed', means_passed=True)
        self.passGrade.save()
        self.failGrade = Grading(title='failed', means_passed=False)
        self.failGrade.save()

        self.passFailGrading = GradingScheme(title='Pass/Fail Grading Scheme')
        self.passFailGrading.save()
        self.passFailGrading.gradings.add(self.passGrade)
        self.passFailGrading.gradings.add(self.failGrade)
        self.passFailGrading.save()

    def setUpAssignments(self):
        today = timezone.now()
        last_week = today - datetime.timedelta(weeks=1)
        yesterday = today - datetime.timedelta(days=1)
        tomorrow = today + datetime.timedelta(days=1)
        next_week = today + datetime.timedelta(weeks=1)

        self.openAssignment = Assignment(
            title='Open assignment',
            course=self.course,
            download='http://example.org/assignments/1/download',
            gradingScheme=self.passFailGrading,
            publish_at=last_week,
            soft_deadline=tomorrow,
            hard_deadline=next_week,
            has_attachment=False,
        )
        self.openAssignment.save()

        self.softDeadlinePassedAssignment = Assignment(
            title='Soft deadline passed assignment',
            course=self.course,
            download='http://example.org/assignments/2/download',
            gradingScheme=self.passFailGrading,
            publish_at=last_week,
            soft_deadline=yesterday,
            hard_deadline=tomorrow,
            has_attachment=False,
        )
        self.softDeadlinePassedAssignment.save()

        self.hardDeadlinePassedAssignment = Assignment(
            title='Hard deadline passed assignment',
            course=self.course,
            download='http://example.org/assignments/3/download',
            gradingScheme=self.passFailGrading,
            publish_at=last_week,
            soft_deadline=yesterday,
            hard_deadline=yesterday,
            has_attachment=False,
        )
        self.hardDeadlinePassedAssignment.save()

        self.unpublishedAssignment = Assignment(
            title='Unpublished assignment',
            course=self.course,
            download='http://example.org/assignments/4/download',
            gradingScheme=self.passFailGrading,
            publish_at=tomorrow,
            soft_deadline=next_week,
            hard_deadline=next_week,
            has_attachment=False,
        )
        self.unpublishedAssignment.save()

    def setUp(self):
        super(SubmitTestCase, self).setUp()
        self.logger = logging.getLogger('OpenSubmit')
        self.loggerLevelOld = self.logger.level
        self.logger.setLevel(logging.WARN)
        self.setUpUsers()
        self.setUpCourses()
        self.setUpGradings()
        self.setUpAssignments()

    def tearDown(self):
        self.logger.setLevel(self.loggerLevelOld)

    def createSubmission(self, user, assignment, authors=[]):
        sub = Submission(
            assignment=assignment,
            submitter=user.user,
            notes="This is a submission.",
        )
        sub.save()

        if authors:
            [sub.authors.add(author) for author in authors]
        sub.save()

        return sub