Ejemplo n.º 1
0
 def setUp(self):
     # create test user with permissions
     self.student_username = '******'
     self.instructor_username = '******'
     self.password = '******'
     self.student_user = User.objects.create_user(
         username=self.student_username, password=self.password)
     group = permissions.get_or_create_student_permissions()
     group.user_set.add(self.student_user)
     self.instructor_user = User.objects.create_user(
         username=self.instructor_username, password=self.password)
     group = permissions.get_or_create_instructor_permissions()
     group.user_set.add(self.instructor_user)
     self.client.login(username=self.instructor_username,
                       password=self.password)
     # populate test database
     self.course = Course(name='Bounty Hunting 101')
     self.course.save()
     self.instructor = Instructor(user=self.instructor_user,
                                  wwuid='9994141')
     self.instructor.save()
     self.group = LabGroup(course=self.course,
                           instructor=self.instructor,
                           term='before',
                           enroll_key='4')
     self.group.save()
     self.student = Student(user=self.student_user,
                            wwuid='9994141',
                            labgroup=self.group)
     self.student.save()
     self.template = AssignmentTemplate(course=self.course,
                                        name='Royalty Kidnapping Section A')
     self.template.save()
     # retrieve the view
     self.view_name = 'api:assignment-lc'
Ejemplo n.º 2
0
    def setUp(self):
        # create test user with permissions
        self.username = '******'
        self.password = '******'
        self.user = User.objects.create_user(username=self.username, password=self.password)
        Instructor(user=self.user, wwuid='9999999').save()
        group = permissions.get_or_create_instructor_permissions()
        group.user_set.add(self.user)
        self.client.login(username=self.username, password=self.password)
        # add courses and or templates to database
        self.course = Course(name="Astrophysics 820")
        self.course.save()
        self.template = AssignmentTemplate(name='Gravitational Vortices', course=self.course)
        self.template.save()
        self.template_2 = AssignmentTemplate(name='Cosmic Drift', course=self.course)
        self.template_2.save()

        self.task_template_1 = TaskTemplate(assignment_template=self.template, problem_num=1,
                                            summary='This is a summary.', prompt='This is the prompt1',
                                            prompt_format='CAF-citation', image_urls='all', attempts_allowed=1,
                                            numeric_accuracy=1, numeric_only=False)
        self.task_template_1.save()
        self.task_template_2 = TaskTemplate(assignment_template=self.template, problem_num=2,
                                            summary='This is a summary.', prompt='This is the prompt2',
                                            prompt_format='CAF-citation', image_urls='all', attempts_allowed=2,
                                            numeric_accuracy=2, numeric_only=False)
        self.task_template_2.save()
        self.task_template_3 = TaskTemplate(assignment_template=self.template, problem_num=3,
                                            summary='This is a summary.', prompt='This is the prompt3',
                                            prompt_format='SIGFIGS', image_urls='all', attempts_allowed=3,
                                            numeric_accuracy=3, numeric_only=True)
        self.task_template_3.save()

        # retrieve the view
        self.view_name = 'api:task-template-rud'
Ejemplo n.º 3
0
    def test_assignment_template_list_instructor(self):
        """
        Tests that assignment templates are properly listed for instructors.
        """
        # add assignment templates to database

        AssignmentTemplate(name='test name 1', course=self.course).save()
        AssignmentTemplate(name='test name 2', course=self.course).save()
        # request
        response = self.client.get(reverse(self.view_name))
        response_body = json.loads(response.content.decode('utf-8'))
        # test response
        assignments = AssignmentTemplate.objects.all()
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response_body['templates'][0]['pk'],
                         assignments[0].id)
        self.assertEqual(response_body['templates'][0]['course'],
                         assignments[0].course.id)
        self.assertEqual(response_body['templates'][0]['name'],
                         assignments[0].name)
        self.assertEqual(response_body['templates'][1]['pk'],
                         assignments[1].id)
        self.assertEqual(response_body['templates'][1]['course'],
                         assignments[1].course.id)
        self.assertEqual(response_body['templates'][1]['name'],
                         assignments[1].name)
Ejemplo n.º 4
0
 def test_assignment_update_template_course_incompatible(self):
     """
     Tests that an assignment is not created when the assignment template doesn't belong to a shared course.
     """
     # create different course
     course = Course(name='other course')
     course.save()
     # create different template
     template = AssignmentTemplate(course=course, name='other template')
     template.save()
     # request
     current_time = datetime.now(timezone(settings.TIME_ZONE))
     request_body = {
         'assignment_template':
         template.id,
         'labgroup':
         self.group.id,
         'open_date':
         (current_time - timedelta(days=1)).strftime('%Y-%m-%dT%H:%M:%SZ'),
         'close_date':
         (current_time + timedelta(days=1)).strftime('%Y-%m-%dT%H:%M:%SZ'),
     }
     response = self.client.put(
         reverse(self.view_name, args=[self.assignment.id]), request_body)
     # test response
     self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
     # test database
     assignment = Assignment.objects.get(id=self.assignment.id)
     self.assertNotEqual(assignment.assignment_template, template)
Ejemplo n.º 5
0
 def setUp(self):
     # create test user with permissions
     self.username = '******'
     self.password = '******'
     self.instructor_user = User.objects.create_user(username=self.username,
                                                     password=self.password)
     self.instructor_user.user_permissions.add(
         Permission.objects.get(codename='add_assignment'))
     self.instructor_user.user_permissions.add(
         Permission.objects.get(codename='change_assignment'))
     self.instructor_user.user_permissions.add(
         Permission.objects.get(codename='delete_assignment'))
     self.client.login(username=self.username, password=self.password)
     # populate test database
     self.instructor = Instructor(user=self.instructor_user,
                                  wwuid='9994141')
     self.instructor.save()
     self.course = Course(name='Bounty Hunting 101')
     self.course.save()
     self.group = LabGroup(course=self.course,
                           instructor=self.instructor,
                           term='before',
                           enroll_key='4')
     self.group.save()
     self.template = AssignmentTemplate(course=self.course,
                                        name='Royalty Kidnapping Section A')
     self.template.save()
     self.assignment = Assignment(assignment_template=self.template,
                                  labgroup=self.group,
                                  open_date='2013-12-10T22:22:22Z',
                                  close_date='2014-12-10T22:22:22Z')
     self.assignment.save()
     self.assignment2 = Assignment(assignment_template=self.template,
                                   labgroup=self.group,
                                   open_date='2014-12-10T22:22:22Z',
                                   close_date='2015-12-10T22:22:22Z')
     self.assignment2.save()
     self.assignment3 = Assignment(assignment_template=self.template,
                                   labgroup=self.group,
                                   open_date='1492-12-10T22:22:22Z',
                                   close_date='3012-12-10T22:22:22Z')
     self.assignment3.save()
     # retrieve the view
     self.view_name = 'api:assignment-rud'
Ejemplo n.º 6
0
    def setUp(self):
        # create test user with permissions
        self.username = '******'
        self.password = '******'
        self.user = User.objects.create_user(username=self.username, password=self.password)
        Instructor(user=self.user, wwuid='9999999').save()
        group = permissions.get_or_create_instructor_permissions()
        group.user_set.add(self.user)
        self.client.login(username=self.username, password=self.password)
        # retrieve the view
        self.view_name = 'api:task-template-lc'

        # make a fake course
        self.course = Course(name="TestCourse")
        self.course.save()

        # make a fake assignment_template
        self.assignment_template = AssignmentTemplate(course=self.course, name="TestAssignmentTemplate")
        self.assignment_template.save()
Ejemplo n.º 7
0
 def setUp(self):
     # create test user with permissions
     self.student_username = '******'
     self.instructor_username = '******'
     self.password = '******'
     self.student_user = User.objects.create_user(
         username=self.student_username, password=self.password)
     self.instructor_user = User.objects.create_user(
         username=self.instructor_username, password=self.password)
     self.client.login(username=self.student_username,
                       password=self.password)
     # populate test database
     self.instructor = Instructor(user=self.instructor_user,
                                  wwuid='9994141')
     self.instructor.save()
     self.course = Course(name='Bounty Hunting 101')
     self.course.save()
     self.group = LabGroup(course=self.course,
                           instructor=self.instructor,
                           term=get_current_term(),
                           enroll_key='4',
                           group_name='Group A')
     self.group.save()
     self.student = Student(user=self.student_user,
                            labgroup=self.group,
                            wwuid='1111111')
     self.student.save()
     self.template = AssignmentTemplate(course=self.course,
                                        name='Royalty Kidnapping Section A')
     self.template.save()
     self.assignment = Assignment(
         assignment_template=self.template,
         labgroup=self.group,
         open_date=datetime.now(timezone(settings.TIME_ZONE)),
         close_date=datetime.now(timezone(settings.TIME_ZONE)) +
         timedelta(days=1))
     self.assignment.save()
     self.assignment_entry = AssignmentEntry(student=self.student,
                                             assignment=self.assignment)
     self.assignment_entry.save()
     # retrieve the view
     self.view_name = 'api:assignment-entry-submit'
Ejemplo n.º 8
0
 def setUp(self):
     # create test user with permissions
     self.username = '******'
     self.password = '******'
     self.user = User.objects.create_user(username=self.username,
                                          password=self.password)
     Instructor(user=self.user, wwuid='9999999').save()
     group = permissions.get_or_create_instructor_permissions()
     group.user_set.add(self.user)
     self.client.login(username=self.username, password=self.password)
     # add courses and or templates to database
     self.course = Course(name="testcourse")
     self.course.save()
     self.template_1 = AssignmentTemplate(name='test name 1',
                                          course=self.course)
     self.template_1.save()
     self.template_2 = AssignmentTemplate(name='test name 2',
                                          course=self.course)
     self.template_2.save()
     self.template_3 = AssignmentTemplate(name='test name 3',
                                          course=self.course)
     self.template_3.save()
     # retrieve the view
     self.view_name = 'api:template-rud'
Ejemplo n.º 9
0
class AssignmentRUDTest(APITestCase):
    """
    Test cases for retrieve, update, and destroy requests on AssignmentRUDView.
    """
    def setUp(self):
        # create test user with permissions
        self.username = '******'
        self.password = '******'
        self.instructor_user = User.objects.create_user(username=self.username,
                                                        password=self.password)
        self.instructor_user.user_permissions.add(
            Permission.objects.get(codename='add_assignment'))
        self.instructor_user.user_permissions.add(
            Permission.objects.get(codename='change_assignment'))
        self.instructor_user.user_permissions.add(
            Permission.objects.get(codename='delete_assignment'))
        self.client.login(username=self.username, password=self.password)
        # populate test database
        self.instructor = Instructor(user=self.instructor_user,
                                     wwuid='9994141')
        self.instructor.save()
        self.course = Course(name='Bounty Hunting 101')
        self.course.save()
        self.group = LabGroup(course=self.course,
                              instructor=self.instructor,
                              term='before',
                              enroll_key='4')
        self.group.save()
        self.template = AssignmentTemplate(course=self.course,
                                           name='Royalty Kidnapping Section A')
        self.template.save()
        self.assignment = Assignment(assignment_template=self.template,
                                     labgroup=self.group,
                                     open_date='2013-12-10T22:22:22Z',
                                     close_date='2014-12-10T22:22:22Z')
        self.assignment.save()
        self.assignment2 = Assignment(assignment_template=self.template,
                                      labgroup=self.group,
                                      open_date='2014-12-10T22:22:22Z',
                                      close_date='2015-12-10T22:22:22Z')
        self.assignment2.save()
        self.assignment3 = Assignment(assignment_template=self.template,
                                      labgroup=self.group,
                                      open_date='1492-12-10T22:22:22Z',
                                      close_date='3012-12-10T22:22:22Z')
        self.assignment3.save()
        # retrieve the view
        self.view_name = 'api:assignment-rud'

    def test_assignment_retrieve(self):
        """
        Tests that an assignment is properly retrieved.
        """
        # request
        response = self.client.get(
            reverse(self.view_name, args=[self.assignment.id]))
        response_body = json.loads(response.content.decode('utf-8'))
        # test response
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response_body['assignment_template'],
                         self.assignment.assignment_template.id)
        self.assertEqual(response_body['name'],
                         self.assignment.assignment_template.name)
        self.assertEqual(response_body['labgroup'],
                         self.assignment.labgroup.id)
        self.assertEqual(response_body['open_date'], self.assignment.open_date)
        self.assertEqual(response_body['close_date'],
                         self.assignment.close_date)

    def test_assignment_update(self):
        """
        Tests that an assignment is properly updated.
        """
        # modify values
        request_body = {
            'assignment_template': self.template.id,
            'labgroup': self.group.id,
            'open_date': '1890-10-10T22:22:22Z',
            'close_date': '1892-10-10T22:22:22Z',
        }
        # request
        response = self.client.put(
            reverse(self.view_name, args=[self.assignment.id]), request_body)
        response_body = json.loads(response.content.decode('utf-8'))
        # test database
        assignment_tester = Assignment.objects.filter(
            pk=self.assignment.id).first()
        self.assertEqual(assignment_tester.assignment_template.id,
                         request_body['assignment_template'])
        self.assertEqual(assignment_tester.labgroup.id,
                         request_body['labgroup'])
        self.assertEqual(
            assignment_tester.open_date.replace(tzinfo=None),
            datetime.strptime(request_body['open_date'], '%Y-%m-%dT%H:%M:%SZ'))
        self.assertEqual(
            assignment_tester.close_date.replace(tzinfo=None),
            datetime.strptime(request_body['close_date'],
                              '%Y-%m-%dT%H:%M:%SZ'))
        # test response
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response_body['assignment_template'],
                         request_body['assignment_template'])
        self.assertEqual(response_body['labgroup'], request_body['labgroup'])
        self.assertEqual(response_body['open_date'], request_body['open_date'])
        self.assertEqual(response_body['close_date'],
                         request_body['close_date'])

    def test_assignment_update_check_valid_dates(self):
        """
        Tests that an assignment is not updated when the open and close dates are incompatible.
        """
        # get current time
        current_time = datetime.now(timezone(settings.TIME_ZONE))
        # request
        request_body = {
            'assignment_template':
            self.template.id,
            'labgroup':
            self.group.id,
            'open_date':
            (current_time + timedelta(days=1)).strftime('%Y-%m-%dT%H:%M:%SZ'),
            'close_date':
            current_time.strftime('%Y-%m-%dT%H:%M:%SZ'),
        }
        response = self.client.put(
            reverse(self.view_name, args=[self.assignment.id]), request_body)
        # test response
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        # test database
        self.assertEqual(self.assignment.assignment_template.id,
                         request_body['assignment_template'])
        self.assertEqual(self.assignment.labgroup.id, request_body['labgroup'])
        self.assertNotEqual(self.assignment.open_date,
                            request_body['open_date'])
        self.assertNotEqual(self.assignment.close_date,
                            request_body['close_date'])

    def test_assignment_update_template_course_incompatible(self):
        """
        Tests that an assignment is not created when the assignment template doesn't belong to a shared course.
        """
        # create different course
        course = Course(name='other course')
        course.save()
        # create different template
        template = AssignmentTemplate(course=course, name='other template')
        template.save()
        # request
        current_time = datetime.now(timezone(settings.TIME_ZONE))
        request_body = {
            'assignment_template':
            template.id,
            'labgroup':
            self.group.id,
            'open_date':
            (current_time - timedelta(days=1)).strftime('%Y-%m-%dT%H:%M:%SZ'),
            'close_date':
            (current_time + timedelta(days=1)).strftime('%Y-%m-%dT%H:%M:%SZ'),
        }
        response = self.client.put(
            reverse(self.view_name, args=[self.assignment.id]), request_body)
        # test response
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        # test database
        assignment = Assignment.objects.get(id=self.assignment.id)
        self.assertNotEqual(assignment.assignment_template, template)

    def test_assignment_destroy(self):
        """
        Tests that an assignment is properly destroyed.
        """
        # request
        response = self.client.delete(
            reverse(self.view_name, args=[self.assignment2.id]))
        # test database
        assignments = Assignment.objects.all()
        self.assertTrue(self.assignment in assignments)
        self.assertTrue(self.assignment2 not in assignments)
        self.assertTrue(self.assignment3 in assignments)
        # test response
        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
Ejemplo n.º 10
0
class AssignmentLCTest(APITestCase):
    """
    Test cases for list and create requests on AssignmentLCView.
    """
    def setUp(self):
        # create test user with permissions
        self.student_username = '******'
        self.instructor_username = '******'
        self.password = '******'
        self.student_user = User.objects.create_user(
            username=self.student_username, password=self.password)
        group = permissions.get_or_create_student_permissions()
        group.user_set.add(self.student_user)
        self.instructor_user = User.objects.create_user(
            username=self.instructor_username, password=self.password)
        group = permissions.get_or_create_instructor_permissions()
        group.user_set.add(self.instructor_user)
        self.client.login(username=self.instructor_username,
                          password=self.password)
        # populate test database
        self.course = Course(name='Bounty Hunting 101')
        self.course.save()
        self.instructor = Instructor(user=self.instructor_user,
                                     wwuid='9994141')
        self.instructor.save()
        self.group = LabGroup(course=self.course,
                              instructor=self.instructor,
                              term='before',
                              enroll_key='4')
        self.group.save()
        self.student = Student(user=self.student_user,
                               wwuid='9994141',
                               labgroup=self.group)
        self.student.save()
        self.template = AssignmentTemplate(course=self.course,
                                           name='Royalty Kidnapping Section A')
        self.template.save()
        # retrieve the view
        self.view_name = 'api:assignment-lc'

    def test_assignment_create(self):
        """
        Tests that an assignment is properly created.
        """
        # request
        request_body = {
            'assignment_template': self.template.id,
            'labgroup': self.group.id,
            'open_date': '2013-12-12T22:22:22Z',
            'close_date': '2014-12-12T22:22:22Z',
        }
        response = self.client.post(reverse(self.view_name), request_body)
        response_body = json.loads(response.content.decode('utf-8'))
        # test database
        assignment = Assignment.objects.first()
        self.assertEqual(assignment.assignment_template.id,
                         request_body['assignment_template'])
        self.assertEqual(assignment.labgroup.id, request_body['labgroup'])
        self.assertEqual(
            assignment.open_date.replace(tzinfo=None),
            datetime.strptime(request_body['open_date'], '%Y-%m-%dT%H:%M:%SZ'))
        self.assertEqual(
            assignment.close_date.replace(tzinfo=None),
            datetime.strptime(request_body['close_date'],
                              '%Y-%m-%dT%H:%M:%SZ'))
        # test response
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(response_body['assignment_template'],
                         request_body['assignment_template'])
        self.assertEqual(response_body['labgroup'], request_body['labgroup'])
        self.assertEqual(response_body['open_date'], request_body['open_date'])
        self.assertEqual(response_body['close_date'],
                         request_body['close_date'])

    def test_assignment_create_check_valid_dates(self):
        """
        Tests that an assignment is not created when the open and close dates are incompatible.
        """
        # get current time
        current_time = datetime.now(timezone(settings.TIME_ZONE))
        # request
        request_body = {
            'assignment_template':
            self.template.id,
            'labgroup':
            self.group.id,
            'open_date':
            (current_time + timedelta(days=1)).strftime('%Y-%m-%dT%H:%M:%SZ'),
            'close_date':
            current_time.strftime('%Y-%m-%dT%H:%M:%SZ'),
        }
        response = self.client.post(reverse(self.view_name), request_body)
        # test response
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        # test database
        self.assertFalse(Assignment.objects.exists())

    def test_assignment_create_template_course_incompatible(self):
        """
        Tests that an assignment is not created when the assignment template doesn't belong to a shared course.
        """
        # create different course
        course = Course(name='other course')
        course.save()
        # create different template
        template = AssignmentTemplate(course=course, name='other template')
        template.save()
        # request
        current_time = datetime.now(timezone(settings.TIME_ZONE))
        request_body = {
            'assignment_template':
            template.id,
            'labgroup':
            self.group.id,
            'open_date':
            (current_time - timedelta(days=1)).strftime('%Y-%m-%dT%H:%M:%SZ'),
            'close_date':
            (current_time + timedelta(days=1)).strftime('%Y-%m-%dT%H:%M:%SZ'),
        }
        response = self.client.post(reverse(self.view_name), request_body)
        # test response
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        # test database
        self.assertFalse(Assignment.objects.exists())

    def test_assignment_list_instructor(self):
        """
        Tests that assignments are properly listed.
        """
        # add assignments to database
        current_time = datetime.now(timezone(settings.TIME_ZONE))
        Assignment(assignment_template=self.template,
                   labgroup=self.group,
                   open_date=current_time,
                   close_date=current_time + timedelta(days=1)).save()
        Assignment(assignment_template=self.template,
                   labgroup=self.group,
                   open_date=current_time,
                   close_date=current_time + timedelta(days=1)).save()
        # request
        response = self.client.get(reverse(self.view_name))
        response_body = json.loads(response.content.decode('utf-8'))
        # test response
        assignments = Assignment.objects.all()
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(
            response_body['assignments'][0]['assignment_template'],
            assignments[0].assignment_template.id)
        self.assertEqual(response_body['assignments'][0]['name'],
                         assignments[0].assignment_template.name)
        self.assertEqual(response_body['assignments'][0]['labgroup'],
                         assignments[0].labgroup.id)
        self.assertEqual(
            datetime.strptime(response_body['assignments'][0]['open_date'],
                              '%Y-%m-%dT%H:%M:%S.%fZ'),
            assignments[0].open_date.replace(tzinfo=None))
        self.assertEqual(
            datetime.strptime(response_body['assignments'][0]['close_date'],
                              '%Y-%m-%dT%H:%M:%S.%fZ'),
            assignments[0].close_date.replace(tzinfo=None))
        self.assertEqual(
            response_body['assignments'][1]['assignment_template'],
            assignments[1].assignment_template.id)
        self.assertEqual(response_body['assignments'][1]['name'],
                         assignments[1].assignment_template.name)
        self.assertEqual(response_body['assignments'][1]['labgroup'],
                         assignments[1].labgroup.id)
        self.assertEqual(
            datetime.strptime(response_body['assignments'][1]['open_date'],
                              '%Y-%m-%dT%H:%M:%S.%fZ'),
            assignments[1].open_date.replace(tzinfo=None))
        self.assertEqual(
            datetime.strptime(response_body['assignments'][1]['close_date'],
                              '%Y-%m-%dT%H:%M:%S.%fZ'),
            assignments[1].close_date.replace(tzinfo=None))

    def test_assignment_list_instructor_does_not_own(self):
        """
        Tests that assignments are properly listed.
        """
        # create other instructor and labgroup
        user = User.objects.create_user(username='******',
                                        password='******')
        instructor = Instructor(wwuid='2027616', user=user)
        instructor.save()
        labgroup = LabGroup(course=self.course,
                            instructor=instructor,
                            term=get_current_term(),
                            enroll_key='ABC')
        labgroup.save()
        # add assignments to database
        current_time = datetime.now(timezone(settings.TIME_ZONE))
        assignment_1 = Assignment(assignment_template=self.template,
                                  labgroup=self.group,
                                  open_date=current_time,
                                  close_date=current_time + timedelta(days=1))
        assignment_1.save()
        Assignment(assignment_template=self.template,
                   labgroup=labgroup,
                   open_date=current_time,
                   close_date=current_time + timedelta(days=1)).save()
        # request
        response = self.client.get(reverse(self.view_name))
        response_body = json.loads(response.content.decode('utf-8'))
        # test response
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(len(response_body['assignments']), 1)
        self.assertEqual(
            response_body['assignments'][0]['assignment_template'],
            assignment_1.assignment_template.id)
        self.assertEqual(response_body['assignments'][0]['name'],
                         assignment_1.assignment_template.name)
        self.assertEqual(response_body['assignments'][0]['labgroup'],
                         assignment_1.labgroup.id)
        self.assertEqual(
            datetime.strptime(response_body['assignments'][0]['open_date'],
                              '%Y-%m-%dT%H:%M:%S.%fZ'),
            assignment_1.open_date.replace(tzinfo=None))
        self.assertEqual(
            datetime.strptime(response_body['assignments'][0]['close_date'],
                              '%Y-%m-%dT%H:%M:%S.%fZ'),
            assignment_1.close_date.replace(tzinfo=None))

    def test_assignment_list_student(self):
        """
        Tests that assignments are properly listed.
        """
        # login the student
        self.client.logout()
        self.client.login(username=self.student_username,
                          password=self.password)
        # add assignments to database
        current_time = datetime.now(timezone(settings.TIME_ZONE))
        Assignment(assignment_template=self.template,
                   labgroup=self.group,
                   open_date=current_time,
                   close_date=current_time + timedelta(days=1)).save()
        Assignment(assignment_template=self.template,
                   labgroup=self.group,
                   open_date=current_time,
                   close_date=current_time + timedelta(days=1)).save()
        # request
        response = self.client.get(reverse(self.view_name))
        response_body = json.loads(response.content.decode('utf-8'))
        # test response
        assignments = Assignment.objects.all()
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(
            response_body['assignments'][0]['assignment_template'],
            assignments[0].assignment_template.id)
        self.assertEqual(response_body['assignments'][0]['name'],
                         assignments[0].assignment_template.name)
        self.assertEqual(response_body['assignments'][0]['labgroup'],
                         assignments[0].labgroup.id)
        self.assertEqual(
            datetime.strptime(response_body['assignments'][0]['open_date'],
                              '%Y-%m-%dT%H:%M:%S.%fZ'),
            assignments[0].open_date.replace(tzinfo=None))
        self.assertEqual(
            datetime.strptime(response_body['assignments'][0]['close_date'],
                              '%Y-%m-%dT%H:%M:%S.%fZ'),
            assignments[0].close_date.replace(tzinfo=None))
        self.assertEqual(
            response_body['assignments'][1]['assignment_template'],
            assignments[1].assignment_template.id)
        self.assertEqual(response_body['assignments'][1]['name'],
                         assignments[1].assignment_template.name)
        self.assertEqual(response_body['assignments'][1]['labgroup'],
                         assignments[1].labgroup.id)
        self.assertEqual(
            datetime.strptime(response_body['assignments'][1]['open_date'],
                              '%Y-%m-%dT%H:%M:%S.%fZ'),
            assignments[1].open_date.replace(tzinfo=None))
        self.assertEqual(
            datetime.strptime(response_body['assignments'][1]['close_date'],
                              '%Y-%m-%dT%H:%M:%S.%fZ'),
            assignments[1].close_date.replace(tzinfo=None))

    def test_assignment_list_student_different_labgroup(self):
        """
        Tests that assignments are not listed if they are for a different labgroup.
        """
        # login the student
        self.client.logout()
        self.client.login(username=self.student_username,
                          password=self.password)
        # create other labgroup
        labgroup = LabGroup(course=self.course,
                            instructor=self.instructor,
                            term=get_current_term(),
                            enroll_key='ABC')
        labgroup.save()
        # add assignments to database
        current_time = datetime.now(timezone(settings.TIME_ZONE))
        assignment_1 = Assignment(assignment_template=self.template,
                                  labgroup=self.group,
                                  open_date=current_time,
                                  close_date=current_time + timedelta(days=1))
        assignment_1.save()
        Assignment(assignment_template=self.template,
                   labgroup=labgroup,
                   open_date=current_time,
                   close_date=current_time + timedelta(days=1)).save()
        # request
        response = self.client.get(reverse(self.view_name))
        response_body = json.loads(response.content.decode('utf-8'))
        # test response
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(len(response_body['assignments']), 1)
        self.assertEqual(
            response_body['assignments'][0]['assignment_template'],
            assignment_1.assignment_template.id)
        self.assertEqual(response_body['assignments'][0]['name'],
                         assignment_1.assignment_template.name)
        self.assertEqual(response_body['assignments'][0]['labgroup'],
                         assignment_1.labgroup.id)
        self.assertEqual(
            datetime.strptime(response_body['assignments'][0]['open_date'],
                              '%Y-%m-%dT%H:%M:%S.%fZ'),
            assignment_1.open_date.replace(tzinfo=None))
        self.assertEqual(
            datetime.strptime(response_body['assignments'][0]['close_date'],
                              '%Y-%m-%dT%H:%M:%S.%fZ'),
            assignment_1.close_date.replace(tzinfo=None))
Ejemplo n.º 11
0
class AssignmentEntryStartTest(APITestCase):
    """
    Test cases for starting assignments on AssignmentEntryStartView.
    """
    def setUp(self):
        # create test user with permissions
        self.student_username = '******'
        self.instructor_username = '******'
        self.password = '******'
        self.student_user = User.objects.create_user(
            username=self.student_username, password=self.password)
        self.instructor_user = User.objects.create_user(
            username=self.instructor_username, password=self.password)
        self.client.login(username=self.student_username,
                          password=self.password)
        # populate test database
        self.instructor = Instructor(user=self.instructor_user,
                                     wwuid='9994141')
        self.instructor.save()
        self.course = Course(name='Bounty Hunting 101')
        self.course.save()
        self.group = LabGroup(course=self.course,
                              instructor=self.instructor,
                              term=get_current_term(),
                              enroll_key='4',
                              group_name='Group A')
        self.group.save()
        self.student = Student(user=self.student_user,
                               labgroup=self.group,
                               wwuid='1111111')
        self.student.save()
        self.template = AssignmentTemplate(course=self.course,
                                           name='Royalty Kidnapping Section A')
        self.template.save()
        self.assignment = Assignment(
            assignment_template=self.template,
            labgroup=self.group,
            open_date=datetime.now(timezone(settings.TIME_ZONE)),
            close_date=datetime.now(timezone(settings.TIME_ZONE)) +
            timedelta(days=1))
        self.assignment.save()
        # retrieve the view
        self.view_name = 'api:assignment-entry-start'

    def test_assignment_start(self):
        """
        Tests that an assignment entry is properly created.
        """
        # request
        response = self.client.post(
            reverse(self.view_name, args=[self.assignment.id]))
        response_body = json.loads(response.content.decode('utf-8'))
        # test return code
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        # test database
        assignment_entry = AssignmentEntry.objects.get(id=response_body['pk'])
        self.assertEqual(assignment_entry.student, self.student)
        self.assertEqual(assignment_entry.assignment, self.assignment)
        self.assertNotEqual(assignment_entry.start_date, None)
        self.assertEqual(assignment_entry.submit_date, None)
        # test response
        self.assertEqual(response_body['pk'], assignment_entry.id)
        self.assertEqual(response_body['student'], assignment_entry.student.id)
        self.assertEqual(
            datetime.strptime(response_body['start_date'],
                              '%Y-%m-%dT%H:%M:%S.%fZ'),
            assignment_entry.start_date.replace(tzinfo=None))
        self.assertEqual(response_body['submit_date'], None)

    def test_assignment_start_duplicate(self):
        """
        Tests that nothing happens when the assignment is started more than once.
        """
        # create extra assignment entry
        self.client.post(reverse(self.view_name, args=[self.assignment.id]))
        # request
        response = self.client.post(
            reverse(self.view_name, args=[self.assignment.id]))
        # test database
        self.assertEqual(len(AssignmentEntry.objects.all()), 1)
        # test response
        self.assertEqual(response.status_code, status.HTTP_409_CONFLICT)

    def test_assignment_does_not_exist(self):
        """
        Tests that nothing happens when the assignment does not exist.
        """
        response = self.client.post(reverse(self.view_name, args=[0]))
        # test database
        self.assertEqual(len(AssignmentEntry.objects.all()), 0)
        # test response
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

    def test_student_not_assigned_assignment(self):
        """
        Tests that nothing happens when the user tries to start an assignment not assigned to their labgroup.
        """
        # add user to different labgroup
        new_labgroup = LabGroup(course=self.course,
                                instructor=self.instructor,
                                term=get_current_term(),
                                enroll_key='ABC',
                                group_name='Group B')
        new_labgroup.save()
        self.student.labgroup = new_labgroup
        self.student.save()
        # request
        response = self.client.post(
            reverse(self.view_name, args=[self.assignment.id]))
        # test database
        self.assertEqual(len(AssignmentEntry.objects.all()), 0)
        # test response
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

    def test_assignment_not_open(self):
        """
        Tests that nothing happens when the user tries to start an assignment that is not open.
        """
        # modify open dates
        self.assignment.open_date = datetime.now(timezone(
            settings.TIME_ZONE)) + timedelta(days=1)
        self.assignment.close_date = datetime.now(timezone(
            settings.TIME_ZONE)) + timedelta(days=2)
        self.assignment.save()
        # request
        response = self.client.post(
            reverse(self.view_name, args=[self.assignment.id]))
        # test response
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
        # test database
        self.assertEqual(len(AssignmentEntry.objects.all()), 0)

    def test_assignment_closed(self):
        """
        Tests that nothing happens when the user tries to start an assignment that has been closed.
        """
        # modify open dates
        self.assignment.open_date = datetime.now(timezone(
            settings.TIME_ZONE)) - timedelta(days=2)
        self.assignment.close_date = datetime.now(timezone(
            settings.TIME_ZONE)) - timedelta(days=1)
        self.assignment.save()
        # request
        response = self.client.post(
            reverse(self.view_name, args=[self.assignment.id]))
        # test response
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
        # test database
        self.assertEqual(len(AssignmentEntry.objects.all()), 0)
Ejemplo n.º 12
0
class AssignmentEntryEntrySubmitTest(APITestCase):
    """
    Test cases for submitting assignment on AssignmentEntrySubmitView.
    """
    def setUp(self):
        # create test user with permissions
        self.student_username = '******'
        self.instructor_username = '******'
        self.password = '******'
        self.student_user = User.objects.create_user(
            username=self.student_username, password=self.password)
        self.instructor_user = User.objects.create_user(
            username=self.instructor_username, password=self.password)
        self.client.login(username=self.student_username,
                          password=self.password)
        # populate test database
        self.instructor = Instructor(user=self.instructor_user,
                                     wwuid='9994141')
        self.instructor.save()
        self.course = Course(name='Bounty Hunting 101')
        self.course.save()
        self.group = LabGroup(course=self.course,
                              instructor=self.instructor,
                              term=get_current_term(),
                              enroll_key='4',
                              group_name='Group A')
        self.group.save()
        self.student = Student(user=self.student_user,
                               labgroup=self.group,
                               wwuid='1111111')
        self.student.save()
        self.template = AssignmentTemplate(course=self.course,
                                           name='Royalty Kidnapping Section A')
        self.template.save()
        self.assignment = Assignment(
            assignment_template=self.template,
            labgroup=self.group,
            open_date=datetime.now(timezone(settings.TIME_ZONE)),
            close_date=datetime.now(timezone(settings.TIME_ZONE)) +
            timedelta(days=1))
        self.assignment.save()
        self.assignment_entry = AssignmentEntry(student=self.student,
                                                assignment=self.assignment)
        self.assignment_entry.save()
        # retrieve the view
        self.view_name = 'api:assignment-entry-submit'

    def test_assignment_entry_submit(self):
        """
        Tests that an assignment is properly submitted.
        """
        # request
        response = self.client.post(
            reverse(self.view_name, args=[self.assignment.id]))
        response_body = json.loads(response.content.decode('utf-8'))
        # test database
        assignment_entry = AssignmentEntry.objects.get(
            id=self.assignment_entry.id)
        self.assertEqual(assignment_entry.student, self.student)
        self.assertEqual(assignment_entry.assignment, self.assignment)
        self.assertNotEqual(assignment_entry.start_date, None)
        self.assertNotEqual(assignment_entry.submit_date, None)
        # test response
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response_body['pk'], assignment_entry.id)
        self.assertEqual(response_body['student'], self.student.id)
        self.assertEqual(response_body['assignment'], self.assignment.id)
        self.assertTrue('start_date' in response_body.keys())
        self.assertTrue('submit_date' in response_body.keys())

    def test_assignment_entry_does_not_exist(self):
        """
        Tests that nothing happens when the assignment does not exist.
        """
        # request
        response = self.client.post(reverse(self.view_name, args=[0]))
        # test database
        assignment_entry = AssignmentEntry.objects.get(
            id=self.assignment_entry.id)
        self.assertEqual(assignment_entry.submit_date, None)
        # test response
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

    def test_assignment_entry_not_started(self):
        """
        Tests that nothing happens when the assignment entry has not been started.
        """
        # delete assignment_entry
        self.assignment_entry.delete()
        # request
        response = self.client.post(
            reverse(self.view_name, args=[self.assignment.id]))
        # test database
        self.assertEqual(len(AssignmentEntry.objects.all()), 0)
        # test response
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

    def test_assignment_entry_already_submitted(self):
        """
        Tests that nothing happens when the assignment entry has already been submitted.
        """
        # submit assignment
        self.assignment_entry.submit_date = datetime.now(
            timezone(settings.TIME_ZONE))
        self.assignment_entry.save()
        # request
        response = self.client.post(
            reverse(self.view_name, args=[self.assignment.id]))
        # test response
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        # test database
        self.assertEqual(
            AssignmentEntry.objects.get(
                id=self.assignment_entry.id).submit_date,
            self.assignment_entry.submit_date)

    def test_assignment_closed(self):
        """
        Tests that nothing happens when the user tries to start an assignment that has been closed.
        """
        # modify open dates
        self.assignment.open_date = datetime.now(timezone(
            settings.TIME_ZONE)) - timedelta(days=2)
        self.assignment.close_date = datetime.now(timezone(
            settings.TIME_ZONE)) - timedelta(days=1)
        self.assignment.save()
        # request
        response = self.client.post(
            reverse(self.view_name, args=[self.assignment.id]))
        # test response
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
        # test database
        self.assertEqual(self.assignment_entry.submit_date, None)
Ejemplo n.º 13
0
 def test_assignment_template_list_student(self):
     """
     Tests that assignment templates are properly listed for students.
     """
     # create a labgroup
     labgroup_1 = LabGroup(group_name='A',
                           course=self.course,
                           instructor=self.instructor,
                           term=get_current_term(),
                           enroll_key='ABC')
     labgroup_1.save()
     # create an unused labgroup
     labgroup_2 = LabGroup(group_name='B',
                           course=self.course,
                           instructor=self.instructor,
                           term=get_current_term(),
                           enroll_key='ABC')
     labgroup_2.save()
     # create student user
     student_user = User.objects.create_user(username='******',
                                             password=self.password)
     Student(user=student_user, wwuid='1111111', labgroup=labgroup_1).save()
     group = permissions.get_or_create_student_permissions()
     group.user_set.add(student_user)
     self.client.logout()
     self.client.login(username=student_user.username,
                       password=self.password)
     # add assignment templates to database
     at1 = AssignmentTemplate(name='test name 1', course=self.course)
     at1.save()
     at2 = AssignmentTemplate(name='test name 2', course=self.course)
     at2.save()
     # assign student the first assignment template
     assignment = Assignment(
         assignment_template=at1,
         labgroup=labgroup_1,
         open_date=datetime.now(timezone(settings.TIME_ZONE)) -
         timedelta(days=1),
         close_date=datetime.now(timezone(settings.TIME_ZONE)) +
         timedelta(days=1))
     assignment.save()
     # create different assignment
     Assignment(assignment_template=at2,
                labgroup=labgroup_2,
                open_date=datetime.now(timezone(settings.TIME_ZONE)) -
                timedelta(days=1),
                close_date=datetime.now(timezone(settings.TIME_ZONE)) +
                timedelta(days=1))
     # request
     response = self.client.get(reverse(self.view_name))
     response_body = json.loads(response.content.decode('utf-8'))
     # test response
     assignments = AssignmentTemplate.objects.all()
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     self.assertEqual(len(response_body['templates']), 1)
     self.assertEqual(response_body['templates'][0]['pk'],
                      assignments[0].id)
     self.assertEqual(response_body['templates'][0]['course'],
                      assignments[0].course.id)
     self.assertEqual(response_body['templates'][0]['name'],
                      assignments[0].name)
Ejemplo n.º 14
0
class TemplateRUDTest(APITestCase):
    """
    Test cases for retrieve, update, and destroy requests on TemplateRUDView.
    """
    def setUp(self):
        # create test user with permissions
        self.username = '******'
        self.password = '******'
        self.user = User.objects.create_user(username=self.username,
                                             password=self.password)
        Instructor(user=self.user, wwuid='9999999').save()
        group = permissions.get_or_create_instructor_permissions()
        group.user_set.add(self.user)
        self.client.login(username=self.username, password=self.password)
        # add courses and or templates to database
        self.course = Course(name="testcourse")
        self.course.save()
        self.template_1 = AssignmentTemplate(name='test name 1',
                                             course=self.course)
        self.template_1.save()
        self.template_2 = AssignmentTemplate(name='test name 2',
                                             course=self.course)
        self.template_2.save()
        self.template_3 = AssignmentTemplate(name='test name 3',
                                             course=self.course)
        self.template_3.save()
        # retrieve the view
        self.view_name = 'api:template-rud'

    def test_assignment_template_retrieve(self):
        """
        Tests that a assignment template is properly retrieved.
        """
        # request
        response = self.client.get(
            reverse(self.view_name, args=[self.template_2.id]))
        response_body = json.loads(response.content.decode('utf-8'))
        # test response
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response_body['pk'], self.template_2.id)
        self.assertEqual(response_body['name'], self.template_2.name)
        self.assertEqual(response_body['course'], self.course.id)

    def test_assignment_template_update(self):
        """
        Tests that a template is properly updated.
        """
        # modify values
        request_body = {'name': 'name changed', 'course': self.course.id}
        # request
        response = self.client.put(
            reverse(self.view_name, args=[self.template_2.id]), request_body)
        response_body = json.loads(response.content.decode('utf-8'))
        # test database
        assignment_template = AssignmentTemplate.objects.filter(
            name=request_body['name'], course=request_body['course']).first()
        self.assertEqual(assignment_template.id, self.template_2.id)
        self.assertEqual(assignment_template.course.id, request_body['course'])
        self.assertEqual(assignment_template.name, request_body['name'])
        # test response
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response_body['pk'], self.template_2.id)
        self.assertEqual(response_body['course'], request_body['course'])
        self.assertEqual(response_body['name'], request_body['name'])

    def test_assignment_template_destroy(self):
        """
        Tests that a template is properly destroyed.
        """
        # request
        response = self.client.delete(
            reverse(self.view_name, args=[self.template_2.id]))
        # test database
        assignment_templates = AssignmentTemplate.objects.all()
        self.assertTrue(self.template_1 in assignment_templates)
        self.assertTrue(self.template_2 not in assignment_templates)
        self.assertTrue(self.template_3 in assignment_templates)
        # test response
        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
Ejemplo n.º 15
0
class TaskTemplateRUDTest(APITestCase):
    """
    Test cases for retrieve, update, and destroy requests on TaskTemplateRUDView.
    """
    def setUp(self):
        # create test user with permissions
        self.username = '******'
        self.password = '******'
        self.user = User.objects.create_user(username=self.username, password=self.password)
        Instructor(user=self.user, wwuid='9999999').save()
        group = permissions.get_or_create_instructor_permissions()
        group.user_set.add(self.user)
        self.client.login(username=self.username, password=self.password)
        # add courses and or templates to database
        self.course = Course(name="Astrophysics 820")
        self.course.save()
        self.template = AssignmentTemplate(name='Gravitational Vortices', course=self.course)
        self.template.save()
        self.template_2 = AssignmentTemplate(name='Cosmic Drift', course=self.course)
        self.template_2.save()

        self.task_template_1 = TaskTemplate(assignment_template=self.template, problem_num=1,
                                            summary='This is a summary.', prompt='This is the prompt1',
                                            prompt_format='CAF-citation', image_urls='all', attempts_allowed=1,
                                            numeric_accuracy=1, numeric_only=False)
        self.task_template_1.save()
        self.task_template_2 = TaskTemplate(assignment_template=self.template, problem_num=2,
                                            summary='This is a summary.', prompt='This is the prompt2',
                                            prompt_format='CAF-citation', image_urls='all', attempts_allowed=2,
                                            numeric_accuracy=2, numeric_only=False)
        self.task_template_2.save()
        self.task_template_3 = TaskTemplate(assignment_template=self.template, problem_num=3,
                                            summary='This is a summary.', prompt='This is the prompt3',
                                            prompt_format='SIGFIGS', image_urls='all', attempts_allowed=3,
                                            numeric_accuracy=3, numeric_only=True)
        self.task_template_3.save()

        # retrieve the view
        self.view_name = 'api:task-template-rud'

    def test_task_template_retrieve(self):
        """
        Tests that a task template is properly retrieved.
        """
        # request
        response = self.client.get(reverse(self.view_name, args=[self.template_2.id, self.task_template_1.id]))
        response_body = json.loads(response.content.decode('utf-8'))
        # test response
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response_body['pk'], self.task_template_1.id)
        self.assertEqual(response_body['assignment_template'], self.task_template_1.assignment_template.id)
        self.assertEqual(response_body['problem_num'], self.task_template_1.problem_num)
        self.assertEqual(response_body['summary'], self.task_template_1.summary)
        self.assertEqual(response_body['prompt'], self.task_template_1.prompt)
        self.assertEqual(response_body['prompt_format'], self.task_template_1.prompt_format)
        self.assertEqual(response_body['image_urls'], self.task_template_1.image_urls)
        self.assertEqual(response_body['attempts_allowed'], self.task_template_1.attempts_allowed)
        self.assertEqual(response_body['numeric_accuracy'], self.task_template_1.numeric_accuracy)
        self.assertEqual(response_body['numeric_only'], self.task_template_1.numeric_only)

    def test_task_template_update(self):
        """
        Tests that a task template is properly updated.
        """
        # modify values
        request_body = {
            'problem_num': 10,
            'summary': 'new summary',
            'prompt': 'new prompt',
            'prompt_format': 'CAC-Citation',
            'image_urls': 'none',
            'attempts_allowed': 64,
            'numeric_accuracy': 64,
            'numeric_only': True,
        }
        # request
        response = self.client.put(reverse(self.view_name, args=[self.template_2.id, self.task_template_2.id]), request_body)
        response_body = json.loads(response.content.decode('utf-8'))
        # test database
        task_template = TaskTemplate.objects.filter(problem_num=request_body['problem_num']).first()
        self.assertEqual(task_template.id, self.task_template_2.id)
        self.assertEqual(task_template.problem_num, request_body['problem_num'])
        self.assertEqual(task_template.summary, request_body['summary'])
        self.assertEqual(task_template.prompt, request_body['prompt'])
        self.assertEqual(task_template.prompt_format, request_body['prompt_format'])
        self.assertEqual(task_template.image_urls, request_body['image_urls'])
        self.assertEqual(task_template.attempts_allowed, request_body['attempts_allowed'])
        self.assertEqual(task_template.numeric_accuracy, request_body['numeric_accuracy'])
        self.assertEqual(task_template.numeric_only, request_body['numeric_only'])

        # test response
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response_body['pk'], self.task_template_2.id)
        self.assertEqual(response_body['problem_num'], request_body['problem_num'])
        self.assertEqual(response_body['summary'], request_body['summary'])
        self.assertEqual(response_body['prompt'], request_body['prompt'])
        self.assertEqual(response_body['prompt_format'], request_body['prompt_format'])
        self.assertEqual(response_body['image_urls'], request_body['image_urls'])
        self.assertEqual(response_body['attempts_allowed'], request_body['attempts_allowed'])
        self.assertEqual(response_body['numeric_accuracy'], request_body['numeric_accuracy'])
        self.assertEqual(response_body['numeric_only'], request_body['numeric_only'])

    def test_task_template_destroy(self):
        """
        Tests that a task template is properly destroyed.
        """
        # request
        response = self.client.delete(reverse(self.view_name, args=[self.template_2.id, self.task_template_2.id]))
        # test database
        task_templates = TaskTemplate.objects.all()
        self.assertTrue(self.task_template_1 in task_templates)
        self.assertTrue(self.task_template_2 not in task_templates)
        self.assertTrue(self.task_template_3 in task_templates)
        # test response
        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
Ejemplo n.º 16
0
class AssignmentEntryTest(APITestCase):
    """
    Test cases for viewing the user's assignment entry on AssignmentEntryView.
    """
    def setUp(self):
        # create test user with permissions
        self.student_username = '******'
        self.instructor_username = '******'
        self.password = '******'
        self.student_user = User.objects.create_user(
            username=self.student_username, password=self.password)
        self.instructor_user = User.objects.create_user(
            username=self.instructor_username, password=self.password)
        self.client.login(username=self.student_username,
                          password=self.password)
        # populate test database
        self.instructor = Instructor(user=self.instructor_user,
                                     wwuid='9994141')
        self.instructor.save()
        self.course = Course(name='Bounty Hunting 101')
        self.course.save()
        self.group = LabGroup(course=self.course,
                              instructor=self.instructor,
                              term=get_current_term(),
                              enroll_key='4',
                              group_name='Group A')
        self.group.save()
        self.student = Student(user=self.student_user,
                               labgroup=self.group,
                               wwuid='1111111')
        self.student.save()
        self.template = AssignmentTemplate(course=self.course,
                                           name='Royalty Kidnapping Section A')
        self.template.save()
        self.assignment = Assignment(
            assignment_template=self.template,
            labgroup=self.group,
            open_date=datetime.now(timezone(settings.TIME_ZONE)),
            close_date=datetime.now(timezone(settings.TIME_ZONE)) +
            timedelta(days=1))
        self.assignment.save()
        # retrieve the view
        self.view_name = 'api:assignment-entry'

    def test_view_assignment_entry(self):
        """
        Tests that an assignment entry is properly retrieved.
        """
        # create assignment entry
        assignment_entry = AssignmentEntry(student=self.student,
                                           assignment=self.assignment)
        assignment_entry.save()
        # request
        response = self.client.get(
            reverse(self.view_name, args=[self.assignment.id]))
        response_body = json.loads(response.content.decode('utf-8'))
        # test response
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response_body['pk'], assignment_entry.id)
        self.assertEqual(response_body['student'], self.student.id)
        self.assertEqual(response_body['assignment'], self.assignment.id)
        self.assertTrue('start_date' in response_body.keys())
        self.assertTrue('submit_date' in response_body.keys())

    def test_view_assignment_entry_not_started(self):
        """
        Tests that an assignment entry is not retrieved if it has not been started.
        """
        # request
        response = self.client.get(
            reverse(self.view_name, args=[self.assignment.id]))
        # test response
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
Ejemplo n.º 17
0
class TaskTemplateLCTest(APITestCase):
    """
    Test cases for list and create requests on TaskTemplateLCView.
    """

    def setUp(self):
        # create test user with permissions
        self.username = '******'
        self.password = '******'
        self.user = User.objects.create_user(username=self.username, password=self.password)
        Instructor(user=self.user, wwuid='9999999').save()
        group = permissions.get_or_create_instructor_permissions()
        group.user_set.add(self.user)
        self.client.login(username=self.username, password=self.password)
        # retrieve the view
        self.view_name = 'api:task-template-lc'

        # make a fake course
        self.course = Course(name="TestCourse")
        self.course.save()

        # make a fake assignment_template
        self.assignment_template = AssignmentTemplate(course=self.course, name="TestAssignmentTemplate")
        self.assignment_template.save()

    def test_task_template_create(self):
        """
        Tests that a task template is properly created.
        """
        # request
        request_body = {
            'problem_num': 1,
            'summary': 'test summary',
            'prompt': 'test prompt',
            'prompt_format': 'test prompt format',
            'image_urls': 'test urls',
            'attempts_allowed': 3,
            'numeric_accuracy': 2,
            'numeric_only': False

        }
        # create task template
        response = self.client.post(reverse(viewname=self.view_name, args=[self.assignment_template.id]), request_body)
        response_body = json.loads(response.content.decode('utf-8'))

        # test database
        temp = TaskTemplate.objects.first()
        self.assertEqual(temp.assignment_template.id, self.assignment_template.id)
        self.assertEqual(temp.problem_num, request_body['problem_num'])
        self.assertEqual(temp.summary, request_body['summary'])
        self.assertEqual(temp.prompt, request_body['prompt'])
        self.assertEqual(temp.prompt_format, request_body['prompt_format'])
        self.assertEqual(temp.image_urls, request_body['image_urls'])
        self.assertEqual(temp.attempts_allowed, request_body['attempts_allowed'])
        self.assertEqual(temp.numeric_accuracy, request_body['numeric_accuracy'])
        self.assertEqual(temp.numeric_only, request_body['numeric_only'])
        # test response
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(response_body['assignment_template'], self.assignment_template.id)
        self.assertEqual(response_body['problem_num'], request_body['problem_num'])
        self.assertEqual(response_body['summary'], request_body['summary'])
        self.assertEqual(response_body['prompt'], request_body['prompt'])
        self.assertEqual(response_body['prompt_format'], request_body['prompt_format'])
        self.assertEqual(response_body['image_urls'], request_body['image_urls'])
        self.assertEqual(response_body['attempts_allowed'], request_body['attempts_allowed'])
        self.assertEqual(response_body['numeric_accuracy'], request_body['numeric_accuracy'])
        self.assertEqual(response_body['numeric_only'], request_body['numeric_only'])

    def test_task_template_create_include_template_key(self):
        """
        Tests that a task template is properly created even when 'assignment_template' is included in the request body.
        """
        # request
        request_body = {
            'assignment_template': 0,
            'problem_num': 1,
            'summary': 'test summary',
            'prompt': 'test prompt',
            'prompt_format': 'test prompt format',
            'image_urls': 'test urls',
            'attempts_allowed': 3,
            'numeric_accuracy': 2,
            'numeric_only': False
        }
        # create task template
        response = self.client.post(reverse(viewname=self.view_name, args=[self.assignment_template.id]), request_body)
        response_body = json.loads(response.content.decode('utf-8'))

        # test database
        temp = TaskTemplate.objects.first()
        self.assertEqual(temp.assignment_template.id, self.assignment_template.id)
        self.assertEqual(temp.problem_num, request_body['problem_num'])
        self.assertEqual(temp.summary, request_body['summary'])
        self.assertEqual(temp.prompt, request_body['prompt'])
        self.assertEqual(temp.prompt_format, request_body['prompt_format'])
        self.assertEqual(temp.image_urls, request_body['image_urls'])
        self.assertEqual(temp.attempts_allowed, request_body['attempts_allowed'])
        self.assertEqual(temp.numeric_accuracy, request_body['numeric_accuracy'])
        self.assertEqual(temp.numeric_only, request_body['numeric_only'])
        # test response
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(response_body['assignment_template'], self.assignment_template.id)
        self.assertEqual(response_body['problem_num'], request_body['problem_num'])
        self.assertEqual(response_body['summary'], request_body['summary'])
        self.assertEqual(response_body['prompt'], request_body['prompt'])
        self.assertEqual(response_body['prompt_format'], request_body['prompt_format'])
        self.assertEqual(response_body['image_urls'], request_body['image_urls'])
        self.assertEqual(response_body['attempts_allowed'], request_body['attempts_allowed'])
        self.assertEqual(response_body['numeric_accuracy'], request_body['numeric_accuracy'])
        self.assertEqual(response_body['numeric_only'], request_body['numeric_only'])

    def test_task_template_list(self):
        """
        Tests that task templates are properly listed.
        """
        # add task templates to database

        TaskTemplate(assignment_template=self.assignment_template,
                     problem_num=1,
                     summary='test summary 1',
                     prompt='test prompt 1',
                     prompt_format='test format 1',
                     image_urls='test image 1',
                     attempts_allowed=3,
                     numeric_accuracy=2,
                     numeric_only=False).save()
        TaskTemplate(assignment_template=self.assignment_template,
                     problem_num=2,
                     summary='test summary 2',
                     prompt='test prompt 2',
                     prompt_format='test format 2',
                     image_urls='test image 2',
                     attempts_allowed=3,
                     numeric_accuracy=2,
                     numeric_only=False).save()
        # request
        response = self.client.get(reverse(self.view_name, args=[self.assignment_template.id]))
        response_body = json.loads(response.content.decode('utf-8'))
        # test response
        task_templates = TaskTemplate.objects.all()
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response_body['task_templates'][0]['pk'], task_templates[0].id)
        self.assertEqual(response_body['task_templates'][0]['assignment_template'], task_templates[0].assignment_template.id)
        self.assertEqual(response_body['task_templates'][0]['problem_num'], task_templates[0].problem_num)
        self.assertEqual(response_body['task_templates'][0]['summary'], task_templates[0].summary)
        self.assertEqual(response_body['task_templates'][0]['prompt'], task_templates[0].prompt)
        self.assertEqual(response_body['task_templates'][0]['prompt_format'], task_templates[0].prompt_format)
        self.assertEqual(response_body['task_templates'][0]['image_urls'], task_templates[0].image_urls)
        self.assertEqual(response_body['task_templates'][0]['attempts_allowed'], task_templates[0].attempts_allowed)
        self.assertEqual(response_body['task_templates'][0]['numeric_accuracy'], task_templates[0].numeric_accuracy)
        self.assertEqual(response_body['task_templates'][0]['numeric_only'], task_templates[0].numeric_only)
        self.assertEqual(response_body['task_templates'][1]['pk'], task_templates[1].id)
        self.assertEqual(response_body['task_templates'][1]['assignment_template'], task_templates[1].assignment_template.id)
        self.assertEqual(response_body['task_templates'][1]['problem_num'], task_templates[1].problem_num)
        self.assertEqual(response_body['task_templates'][1]['summary'], task_templates[1].summary)
        self.assertEqual(response_body['task_templates'][1]['prompt'], task_templates[1].prompt)
        self.assertEqual(response_body['task_templates'][1]['prompt_format'], task_templates[1].prompt_format)
        self.assertEqual(response_body['task_templates'][1]['image_urls'], task_templates[1].image_urls)
        self.assertEqual(response_body['task_templates'][1]['attempts_allowed'], task_templates[1].attempts_allowed)
        self.assertEqual(response_body['task_templates'][1]['numeric_accuracy'], task_templates[1].numeric_accuracy)
        self.assertEqual(response_body['task_templates'][1]['numeric_only'], task_templates[1].numeric_only)