Beispiel #1
0
    def test_get(self):
        student = factory.Student()
        assignment = factory.Assignment(courses=[self.course])

        api.get(self,
                'assignments',
                params={'pk': assignment.pk},
                user=student,
                status=403)
        factory.Participation(user=student,
                              course=self.course,
                              role=Role.objects.get(course=self.course,
                                                    name='Student'))
        resp = api.get(self,
                       'assignments',
                       params={
                           'pk': assignment.pk,
                           'course_id': self.course.pk
                       },
                       user=student)['assignment']
        assert resp[
            'journal'] is not None, 'Response should include student serializer'

        resp = api.get(self,
                       'assignments',
                       params={
                           'pk': assignment.pk,
                           'course_id': self.course.pk
                       },
                       user=self.teacher)['assignment']
        assert resp[
            'journals'] is not None, 'Response should include teacher serializer'
Beispiel #2
0
    def test_list(self):
        course2 = factory.Course(author=self.teacher)
        factory.Assignment(courses=[self.course])
        factory.Assignment(courses=[self.course, course2])
        assignment = factory.Assignment()

        resp = api.get(self,
                       'assignments',
                       params={'course_id': self.course.pk},
                       user=self.teacher)['assignments']
        assert len(resp) == 2, 'All connected courses should be returned'
        resp = api.get(self,
                       'assignments',
                       params={'course_id': course2.pk},
                       user=self.teacher)['assignments']
        assert len(resp) == 1, 'Not connected courses should not be returned'

        # Connected assignment
        course = assignment.courses.first()
        factory.Participation(user=self.teacher, course=course)
        # Not connected assignment
        factory.Assignment()

        resp = api.get(self, 'assignments', user=self.teacher)['assignments']
        assert len(
            resp
        ) == 3, 'Without a course supplied, it should return all assignments connected to user'
Beispiel #3
0
    def test_get(self):
        assignment = self.journal.assignment
        course = assignment.courses.first()
        payload = {'assignment_id': assignment.pk, 'course_id': course.pk}
        # Test list
        api.get(self, 'journals', params=payload, user=self.student, status=403)
        api.get(self, 'journals', params=payload, user=self.teacher)

        # Test get
        api.get(self, 'journals', params={'pk': self.journal.pk}, user=self.student)
        api.get(self, 'journals', params={'pk': self.journal.pk}, user=self.teacher)
        api.get(self, 'journals', params={'pk': self.journal.pk}, user=factory.Teacher(), status=403)
Beispiel #4
0
def get_jwt(obj,
            request_body={},
            timestamp=str(int(time.time())),
            nonce=str(oauth2.generate_nonce()),
            user=None,
            status=200,
            response_msg='',
            assert_msg='',
            response_value=None,
            delete_field=False,
            access=None):
    request = create_request(request_body, timestamp, nonce, delete_field)
    jwt_params = lti_view.encode_lti_params(request)
    response = api.get(obj,
                       '/get_lti_params_from_jwt/{0}/'.format(jwt_params),
                       user=user,
                       status=status,
                       access=access)
    if response_msg:
        if 'description' in response:
            assert response_msg in response['description'], assert_msg
        else:
            assert response_msg in response['detail'], assert_msg
    elif response_value:
        assert response['params']['state'] in response_value, assert_msg
    return response
Beispiel #5
0
    def check_comment_update(self, comment, user, should_succeed):
        update_msg = ''.join(
            random.choice(string.ascii_letters + string.digits)
            for _ in range(50))
        comment_before_op = Comment.objects.get(pk=comment.pk)

        if should_succeed:
            old_comment_resp = api.get(self,
                                       'comments',
                                       params={'pk': comment.pk},
                                       user=user)['comment']

        comment_resp = api.update(self,
                                  'comments',
                                  params={
                                      'pk': comment.pk,
                                      'text': update_msg
                                  },
                                  user=user,
                                  status=200 if should_succeed else 403)

        comment_after_op = Comment.objects.get(pk=comment.pk)

        if should_succeed:
            comment_resp = comment_resp['comment']
            assert comment_resp['text'] == update_msg, 'Text should be updated'
            assert comment_resp['creation_date'] == old_comment_resp[
                'creation_date'], 'Creation date modified'
            assert comment_resp['last_edited'] != old_comment_resp[
                'last_edited'], 'Last edited not updated'
            assert comment_resp[
                'last_edited_by'] == user.full_name, 'Last edited by incorrect'
        else:
            assert_comments_are_equal(comment_before_op, comment_after_op)
Beispiel #6
0
    def test_copyable(self):
        teacher = factory.Teacher()
        course = factory.Course(author=teacher)
        assignment = factory.TemplateAssignment(courses=[course])
        factory.TemplateFormat(assignment=assignment)
        assignment2 = factory.TemplateAssignment(courses=[course])
        factory.TemplateFormat(assignment=assignment2)
        journal = factory.Journal(assignment=assignment2)
        [factory.Entry(node__journal=journal) for _ in range(4)]

        resp = api.get(self,
                       'assignments/copyable',
                       params={'pk': assignment.pk},
                       user=teacher)['data']
        assert len(resp[0]['assignments']) == 1
        assert resp[0]['course'][
            'id'] == course.id, 'copyable assignments should be displayed'

        before_id = api.get(self,
                            'formats',
                            params={'pk': assignment2.pk},
                            user=teacher)['format']['templates'][0]['id']
        before_from_id = api.get(self,
                                 'formats',
                                 params={'pk': assignment.pk},
                                 user=teacher)['format']['templates'][0]['id']
        resp = api.update(self,
                          'formats/copy',
                          params={
                              'pk': assignment2.pk,
                              'from_assignment_id': assignment.pk
                          },
                          user=teacher)
        after_id = api.get(self,
                           'formats',
                           params={'pk': assignment2.pk},
                           user=teacher)['format']['templates'][0]['id']

        after_from_id = api.get(self,
                                'formats',
                                params={'pk': assignment.pk},
                                user=teacher)['format']['templates'][0]['id']
        assert before_id != after_id, 'Assignment should be changed'
        assert before_from_id == after_from_id, 'From assignment templates should be unchanged'

        assert len(utils.get_journal_entries(
            journal)) == 4, 'Old entries should not be removed'
Beispiel #7
0
    def test_names(self):
        assignment = self.journal.assignment
        course = assignment.courses.first()
        url = 'names/{}/{}/{}'.format(course.id, assignment.id,
                                      self.journal.id)

        # Check if these users can view the names
        api.get(self, url, user=self.student)
        api.get(self, url, user=self.teacher)
        api.get(self, url, user=factory.Admin())

        # CHeck if a random student cannot view the names
        api.get(self, url, user=factory.Student(), status=403)
Beispiel #8
0
    def test_get(self):
        student = factory.Student()
        admin = factory.Admin()
        journal = factory.Journal(user=student)
        teacher = journal.assignment.courses.first().author

        # Test get all users
        api.get(self, 'users', user=student, status=403)
        resp = api.get(self, 'users', user=admin)['users']
        assert len(resp) == User.objects.count(
        ), 'Test if the admin got all the users'

        # Test get own user
        resp = api.get(self, 'users', params={'pk': 0}, user=student)['user']
        assert 'id' in resp, 'Test if the student got userdata'
        assert 'verified_email' in resp, 'Test if the student got all their userdata'

        resp = api.get(self, 'users', params={'pk': 0}, user=admin)['user']
        assert resp[
            'is_superuser'], 'Admin user should be flagged as superuser.'

        # Check if a user cant see other users data
        api.get(self,
                'users',
                params={'pk': admin.pk},
                user=student,
                status=403)

        # Test get user as supervisor
        assert permissions.is_user_supervisor_of(
            teacher, student), 'Teacher should be supervisor of student'
        resp = api.get(self, 'users', params={'pk': student.pk},
                       user=teacher)['user']
        assert 'username' in resp, 'Supervisor can retrieve basic supervisee data'
        assert 'full_name' in resp, 'Supervisor can retrieve basic supervisee data'
        assert 'verified_email' not in resp, 'Supervisor can\'t retrieve all supervisee data'
        assert 'email' not in resp, 'Supervisor can\'t retrieve all supervisee data'

        # Test get user as admin
        resp = api.get(self, 'users', params={'pk': student.pk},
                       user=admin)['user']
        assert 'id' in resp, 'Admin can retrieve basic user data'
        assert 'verified_email' in resp, 'Admin can retrieve all user data'
        assert 'email' in resp, 'Admin can retrieve all user data'
Beispiel #9
0
    def test_get(self):
        factory.Participation(user=self.teacher2, course=self.course2)

        # Get all courses
        get_resp = api.get(self, 'courses', user=self.teacher1)['courses']
        assert len(
            get_resp
        ) == 2, 'The teacher did not get all the courses it is the author of'
        assert {self.course1.pk, self.course2.pk} == set([c['id'] for c in get_resp]), \
            'The teacher did not get all the courses it is the author of'

        get_resp = api.get(self, 'courses', user=self.teacher2)['courses']
        assert len(
            get_resp
        ) == 2, 'The teacher did not get all the courses it is the author of or is participating in'
        assert is_response(get_resp, self.course2, self.course3), \
            'The teacher did not get all the courses it is the author of'

        # Get author course
        get_resp = api.get(self,
                           'courses',
                           params={'pk': self.course1.pk},
                           user=self.teacher1)

        # Check not participating
        get_resp = api.get(self,
                           'courses',
                           params={'pk': self.course1.pk},
                           user=self.teacher2,
                           status=403)

        # Check participating
        get_resp = api.get(self,
                           'courses',
                           params={'pk': self.course2.pk},
                           user=self.teacher2)
Beispiel #10
0
 def test_get(self):
     api.get(self,
             'nodes',
             params={'journal_id': self.journal.pk},
             user=self.student)
     api.get(self,
             'nodes',
             params={'journal_id': self.journal.pk},
             user=factory.Admin())
     api.get(self,
             'nodes',
             params={'journal_id': self.journal.pk},
             user=factory.Teacher(),
             status=403)
     api.get(self,
             'nodes',
             params={'journal_id': self.journal.pk},
             user=self.teacher)
Beispiel #11
0
    def test_delete(self):
        user = factory.Student()
        user2 = factory.Student()
        user3 = factory.Student()
        admin = factory.Admin()
        admin2 = factory.Admin()

        # Test to delete user as other user
        api.delete(self,
                   'users',
                   params={'pk': user2.pk},
                   user=user,
                   status=403)

        # Test to delete own user
        api.delete(self, 'users', params={'pk': user.pk}, user=user)
        api.get(self, 'users', params={'pk': user.pk}, user=admin, status=404)
        api.delete(self, 'users', params={'pk': 0}, user=user2)
        api.get(self, 'users', params={'pk': user2.pk}, user=admin, status=404)

        # Test to delete user as admin
        api.delete(self, 'users', params={'pk': user3.pk}, user=admin)
        api.get(self, 'users', params={'pk': user3.pk}, user=admin, status=404)

        # Test to see if the last admin cannot be removed
        api.delete(self, 'users', params={'pk': admin2.pk}, user=admin)
        api.delete(self,
                   'users',
                   params={'pk': admin.pk},
                   user=admin,
                   status=400)
        api.get(self,
                'users',
                params={'pk': admin2.pk},
                user=admin,
                status=404)
Beispiel #12
0
    def test_get(self):
        api.get(self,
                'participations',
                params={'course_id': self.course.pk},
                user=self.student,
                status=403)
        api.get(self,
                'participations',
                params={'course_id': self.course.pk},
                user=self.teacher)

        resp = api.get(self,
                       'participations',
                       params={'pk': self.course.pk},
                       user=self.student)
        assert resp['participant']['user']['id'] == self.student.pk
        resp = api.get(self,
                       'participations',
                       params={'pk': self.course.pk},
                       user=self.teacher)
        assert resp['participant']['user']['id'] == self.teacher.pk
Beispiel #13
0
    def test_unenrolled(self):
        api.get(self,
                'participations/unenrolled',
                params={
                    'course_id': self.course.pk,
                    'unenrolled_query': ''
                },
                user=self.student,
                status=403)
        resp = api.get(self,
                       'participations/unenrolled',
                       params={
                           'course_id': self.course.pk,
                           'unenrolled_query': ''
                       },
                       user=self.teacher)
        assert len(resp['participants']) == 0

        # Check perfect name that is small
        resp = api.get(self,
                       'participations/unenrolled',
                       params={
                           'course_id': self.course.pk,
                           'unenrolled_query': self.not_connected.username
                       },
                       user=self.teacher)
        assert len(resp['participants']) == 1

        # Check first and last name
        resp = api.get(self,
                       'participations/unenrolled',
                       params={
                           'course_id':
                           self.course.pk,
                           'unenrolled_query':
                           self.not_connected.full_name[:5] + ' ' +
                           'invalid_last_name'
                       },
                       user=self.teacher)
        assert len(resp['participants']) == 0
        resp = api.get(self,
                       'participations/unenrolled',
                       params={
                           'course_id': self.course.pk,
                           'unenrolled_query': self.not_connected.full_name
                       },
                       user=self.teacher)
        assert len(resp['participants']) == 1

        # Check subnames of longer names
        other_not_conn = factory.Student(
            username='******', full_name='longfirstname longlastname')
        resp = api.get(self,
                       'participations/unenrolled',
                       params={
                           'course_id': self.course.pk,
                           'unenrolled_query': other_not_conn.full_name[:6]
                       },
                       user=self.teacher)
        assert len(resp['participants']) == 1
        resp = api.get(self,
                       'participations/unenrolled',
                       params={
                           'course_id': self.course.pk,
                           'unenrolled_query': other_not_conn.full_name[-6:]
                       },
                       user=self.teacher)
        assert len(resp['participants']) == 1
        resp = api.get(self,
                       'participations/unenrolled',
                       params={
                           'course_id': self.course.pk,
                           'unenrolled_query': other_not_conn.username[:6]
                       },
                       user=self.teacher)
        assert len(resp['participants']) == 1

        # Check too small, not found
        resp = api.get(self,
                       'participations/unenrolled',
                       params={
                           'course_id': self.course.pk,
                           'unenrolled_query': other_not_conn.full_name[:4]
                       },
                       user=self.teacher)
        assert len(resp['participants']) == 0
Beispiel #14
0
    def test_get(self):
        comments = api.get(self,
                           'comments',
                           params={'entry_id': self.teacher_comment.entry.pk},
                           user=self.student)['comments']
        assert len(
            comments
        ) == self.entry_published_comments, 'Student can only see published comments'

        api.get(self,
                'comments',
                params={'pk': self.teacher_comment.pk},
                user=self.student,
                status=403)
        api.get(self,
                'comments',
                params={'pk': self.teacher_comment.pk},
                user=self.teacher)

        comments = api.get(self,
                           'comments',
                           params={'entry_id': self.comment.entry.pk},
                           user=self.teacher)['comments']
        assert len(
            comments
        ) == self.entry_comments, 'Teacher should be able to see all comments'

        self.teacher_comment.published = True
        self.teacher_comment.save()
        self.entry_published_comments = self.entry_published_comments + 1
        self.entry_unpublished_comments = self.entry_unpublished_comments - 1

        comments = api.get(self,
                           'comments',
                           params={'entry_id': self.teacher_comment.entry.pk},
                           user=self.student)['comments']
        assert len(
            comments
        ) == self.entry_published_comments, 'Student should be able to see published comments'

        api.get(self,
                'comments',
                params={'entry_id': self.comment.entry.pk},
                user=factory.Student(),
                status=403)
        api.get(self,
                'comments',
                params={'entry_id': self.comment.entry.pk},
                user=factory.Admin())
Beispiel #15
0
 def test_get(self):
     # Check if PK does not matter
     resp = api.get(self, 'instance', params={'pk': 0})
     assert resp['instance']['name'] == 'eJournal'
     resp = api.get(self, 'instance', params={'pk': 0})
     assert resp['instance']['name'] == 'eJournal'
Beispiel #16
0
    def test_gdpr(self):
        user = factory.Student()
        user2 = factory.Student()
        admin = factory.Admin()

        # Test if users cant access other data
        api.get(self,
                'users/GDPR',
                params={'pk': user2.pk},
                user=user,
                status=403)

        # Test all the gdpr calls
        for _ in range(
                int(api_settings.DEFAULT_THROTTLE_RATES['gdpr'].split('/')
                    [0])):
            api.get(self, 'users/GDPR', params={'pk': 0}, user=user)
        # Test timeout
        api.get(self, 'users/GDPR', params={'pk': 0}, user=user, status=429)

        # Test admin
        api.get(self, 'users/GDPR', params={'pk': user.pk}, user=admin)

        # Test no timeout for admin
        for _ in range(
                int(api_settings.DEFAULT_THROTTLE_RATES['gdpr'].split('/')
                    [0])):
            api.get(self, 'users/GDPR', params={'pk': 0}, user=admin)
        api.get(self, 'users/GDPR', params={'pk': 0}, user=admin)
Beispiel #17
0
    def test_get(self):
        # Test list only done by teachers
        api.get(self,
                'roles',
                params={'course_id': self.course.pk},
                user=self.student,
                status=403)
        api.get(self,
                'roles',
                params={'course_id': self.course.pk},
                user=self.teacher)

        # Test not in course/assignment
        api.get(self,
                'roles',
                params={
                    'pk': 0,
                    'course_id': self.course.pk
                },
                user=factory.Student(),
                status=403)
        api.get(self,
                'roles',
                params={
                    'pk': 0,
                    'assignment_id': self.assignment.pk
                },
                user=factory.Student(),
                status=403)

        # Test get own role
        api.get(self,
                'roles',
                params={
                    'pk': 0,
                    'course_id': self.course.pk
                },
                user=self.student)
        api.get(self,
                'roles',
                params={
                    'pk': 0,
                    'assignment_id': self.assignment.pk
                },
                user=self.student)
        api.get(self,
                'roles',
                params={
                    'pk': self.student.pk,
                    'course_id': self.course.pk
                },
                user=self.student)

        # Test other role
        api.get(self,
                'roles',
                params={
                    'pk': self.teacher.pk,
                    'course_id': self.course.pk
                },
                user=self.student,
                status=403)
        api.get(self,
                'roles',
                params={
                    'pk': self.student.pk,
                    'course_id': self.course.pk
                },
                user=self.teacher)
Beispiel #18
0
    def test_deadline(self):
        journal = factory.Journal(
            assignment__format=factory.TemplateFormatFactory())
        assignment = journal.assignment
        teacher = assignment.courses.first().author
        assignment.points_possible = 10

        resp = api.get(self, 'assignments/upcoming',
                       user=journal.user)['upcoming']
        assert resp[0]['deadline']['name'] == 'End of assignment', \
            'Default end of assignment should be shown'

        resp = api.get(self, 'assignments/upcoming', user=teacher)['upcoming']
        assert resp[0]['deadline']['date'] is None, \
            'Default no deadline for a teacher be shown'

        progress = VLE.factory.make_progress_node(
            assignment.format,
            timezone.now() + datetime.timedelta(days=3), 7)
        utils.update_journals(assignment.journal_set.all(), progress)

        resp = api.get(self, 'assignments/upcoming',
                       user=journal.user)['upcoming']
        assert resp[0]['deadline']['name'] == '0/7 points', \
            'When not having completed an progress node, that should be shown'

        entrydeadline = VLE.factory.make_entrydeadline_node(
            assignment.format,
            timezone.now() + datetime.timedelta(days=1),
            assignment.format.template_set.first())
        utils.update_journals(assignment.journal_set.all(), entrydeadline)

        resp = api.get(self, 'assignments/upcoming',
                       user=journal.user)['upcoming']
        assert resp[0]['deadline']['name'] == assignment.format.template_set.first().name, \
            'When not having completed an entry deadline, that should be shown'

        entry = factory.Entry(node=Node.objects.get(preset=entrydeadline))

        resp = api.get(self, 'assignments/upcoming', user=teacher)['upcoming']
        assert resp[0]['deadline']['date'] is not None, \
            'With ungraded entry a deadline for a teacher be shown'

        api.create(self,
                   'grades',
                   params={
                       'entry_id': entry.pk,
                       'grade': 5,
                       'published': False
                   },
                   user=teacher)
        resp = api.get(self, 'assignments/upcoming', user=teacher)['upcoming']
        assert resp[0]['deadline']['date'] is not None, \
            'With only graded & NOT published entries a deadline for a teacher be shown'

        api.create(self,
                   'grades',
                   params={
                       'entry_id': entry.pk,
                       'grade': 5,
                       'published': True
                   },
                   user=teacher)
        resp = api.get(self, 'assignments/upcoming', user=teacher)['upcoming']
        assert resp[0]['deadline']['date'] is None, \
            'With only graded & published entries no deadline for a teacher be shown'

        resp = api.get(self, 'assignments/upcoming',
                       user=journal.user)['upcoming']
        assert resp[0]['deadline']['name'] == '5/7 points', \
            'With only graded & published entries progres node should be the deadline'

        api.create(self,
                   'grades',
                   params={
                       'entry_id': entry.pk,
                       'grade': 7,
                       'published': True
                   },
                   user=teacher)
        resp = api.get(self, 'assignments/upcoming',
                       user=journal.user)['upcoming']
        assert resp[0]['deadline']['name'] == 'End of assignment', \
            'With full points of progress node, end of assignment should be shown'

        api.create(self,
                   'grades',
                   params={
                       'entry_id': entry.pk,
                       'grade': 10,
                       'published': True
                   },
                   user=teacher)
        resp = api.get(self, 'assignments/upcoming',
                       user=journal.user)['upcoming']
        assert resp[0]['deadline']['name'] is None, \
            'With full points of assignment, no deadline should be shown'