Пример #1
0
 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))
Пример #2
0
 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))
Пример #3
0
 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))
Пример #4
0
    def update(instance: Assignment, data: dict) -> Assignment:
        print(instance)
        print(data)

        client = Manager_Projects.get_mturk_api(instance.hit.batch.use_sandbox)

        success = True

        if 'status_external' in data:
            status_external = data.get('status_external')
            # check if the status changed
            if status_external != instance.status_external:
                now = timezone.now()

                days = (now - instance.datetime_submit).days

                if days < 30:
                    if status_external == assignments.STATUS_EXTERNAL.APPROVED:
                        response = client.approve_assignment(
                            AssignmentId=instance.id_assignment,
                            OverrideRejection=True,
                        )
                    else:
                        response = client.reject_assignment(
                            AssignmentId=instance.id_assignment,
                            RequesterFeedback=
                            'Your results did not match our checkinstances')

                    try:
                        if response['ResponseMetadata'][
                                'HTTPStatusCode'] == 200:
                            instance.status_external = status_external
                    except KeyError:
                        success = False
                else:
                    success = False

        if success == True:
            if 'status_internal' in data:
                instance.status_internal = data.get('status_internal')

        instance.save()

        return instance
Пример #5
0
    def get(self, request):
        """
        GET next view
        """
        next_assignment = Assignment.next_assignment_of(request.user)

        if next_assignment:
            # Ok, next assignment, redirect there
            return redirect('article_view', pk=next_assignment.article.id)
        return redirect('completed')
Пример #6
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'
Пример #7
0
    def create(self, request):
        data = request.data
        print('data >> ', data)

        assignment = Assignment()
        teacher = User.objects.get(username=data['teacher'])
        assignment.teacher = teacher
        assignment.title = data['title']
        assignment.save()

        order = 1
        for q in data['questions']:
            newQ = Question()
            newQ.question = q['title']
            newQ.order = order
            newQ.save()

            for c in q['choices']:
                newC = Choice()
                newC.title = c
                newC.save()
                newQ.choices.add(newC)

            newQ.answer = Choice.objects.get(title=q['answer'])
            newQ.assignment = assignment
            newQ.save()
            order += 1
        return assignment
Пример #8
0
    def create(self, request, *args, **kwargs):
        data = request.data
        try:
            with transaction.atomic():
                print("DATA ASIGNATURAS:", data)
                if len(data) > 0:

                    _school_cycle = SchoolCycle.objects.filter(
                        activo=True).last()

                    assignment_list = [
                        Assignment(description=assignment_i.get(
                            'description', ""),
                                   grade=Grade.objects.get(
                                       id=assignment_i.get("grade")),
                                   section=Section.objects.get(
                                       id=assignment_i.get("section")),
                                   schoolcycle=_school_cycle,
                                   course=Course.objects.get(
                                       id=assignment_i.get("course")))
                        for assignment_i in data
                    ]
                    print("Array asignaturas:", assignment_list)

                    assignment_created = Assignment.objects.bulk_create(
                        assignment_list)

                    return Response(
                        {"grade": "All assignments created succesfully"},
                        status=status.HTTP_200_OK)

                else:
                    return Response(
                        {
                            "detail":
                            "You didnt send all the necessary data or empty array"
                        },
                        status=status.HTTP_400_BAD_REQUEST)

                return Response(
                    {
                        "detail":
                        "Somenthing was wrong, its not possible to create assignment"
                    },
                    status=status.HTTP_400_BAD_REQUEST)
        except User.DoesNotExist:
            return Response({"detail": "User not found"},
                            status=status.HTTP_404_NOT_FOUND)
        except KeyError as e:
            return Response(
                {"detail": "{} is a required field".format(str(e))},
                status=status.HTTP_400_BAD_REQUEST)
Пример #9
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'
Пример #10
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)
Пример #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)
Пример #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)
Пример #13
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)
Пример #14
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)
Пример #15
0
 def alter_assignment(self, uuid_: uuid.UUID, item: Assignment):
     update_data = item.dict(exclude_unset=True)
     self.tasks[uuid_] = self.tasks[uuid_].copy(update=update_data)