Пример #1
0
    def test_missing_quizzes_check_true(self, m):
        m.register_uri(
            'GET',
            '/api/v1/courses/1/quizzes',
            json=[{'id': 1, 'title': 'Quiz 1'}]
        )

        course_id = 1

        course = Course(
            canvas_id=course_id,
            course_name='test'
        )
        views.db.session.add(course)
        views.db.session.commit()

        extension = Extension(
            course_id=course.id,
            user_id=5,
            percent=200
        )
        views.db.session.add(extension)
        views.db.session.commit()

        response = self.client.get('/missing_quizzes/{}/'.format(course_id))

        self.assert_200(response)
        self.assertEqual(response.data, 'true')
Пример #2
0
    users[addr_hash] = user_object.get_dict()
    db.session.add(user_object)
    db.session.commit()
    return "success"

'''
Initialize database with local extensions
'''
db.app = app
db.init_app(app)
db.create_all(app=app)
#db.create_all(app=app)
extensions = [f for f in listdir(join('static', 'scripts', 'extensions')) if not isfile(join('static', 'scripts', 'extensions', f))]
for extension in extensions:
    if "config.json" in listdir(join('static', 'scripts', 'extensions', extension)):
        config_text = open(join('static', 'scripts', 'extensions', extension, 'config.json')).read()
        config_json = json.loads(config_text)
        ext_object = Extension(
            name=extension,
            author=config_json["author"],
            description=config_json["description"],
            field=config_json["field"],
            rating_points = 0,
            total_ratings = 0
        )
        db.session.add(ext_object)
        db.session.commit()

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=80, threaded=True) #debug=True can be added for debugging
Пример #3
0
    def test_refresh_background_update_success(self, m):
        from views import refresh_background

        with self.client.session_transaction() as sess:
            sess['canvas_user_id'] = 1234
            sess['lti_logged_in'] = True
            sess['is_admin'] = True

        course_id = 1

        m.register_uri(
            'GET',
            '/api/v1/courses/{}'.format(course_id),
            json={
                'id': course_id,
                'name': 'Example Course'
            }
        )
        m.register_uri(
            'GET',
            '/api/v1/courses/1/quizzes',
            json=[
                {'id': 1, 'title': 'Quiz 1', 'time_limit': 10},
                {'id': 2, 'title': 'Quiz 2', 'time_limit': 30}
            ]
        )
        m.register_uri(
            'POST',
            '/api/v1/courses/1/quizzes/1/extensions',
            status_code=200
        )
        m.register_uri(
            'POST',
            '/api/v1/courses/1/quizzes/2/extensions',
            status_code=200
        )
        m.register_uri(
            'GET',
            '/api/v1/courses/1/users/12345',
            json={
                'id': 12345,
                'sortable_name': 'John Smith',
                'enrollments': [{'type': 'StudentEnrollment', 'enrollment_state': 'active'}]
            }
        )

        course = Course(course_id, course_name='Example Course')
        views.db.session.add(course)

        user = User(12345, sortable_name="John Smith")
        views.db.session.add(user)

        views.db.session.commit()

        ext = Extension(course.id, user.id)
        views.db.session.add(ext)

        # Add an inactive extension to be ignored.
        ext_inactive = Extension(course.id, user.id)
        ext_inactive.active = False
        views.db.session.add(ext_inactive)

        views.db.session.commit()

        job = self.queue.enqueue_call(func=refresh_background, args=(course_id,))
        self.worker.work(burst=True)
        self.assertTrue(job.is_finished)
        job_result = job.result

        meta_keys = ['status', 'status_msg', 'percent', 'error']
        self.assertTrue(all(key in job_result for key in meta_keys))

        self.assertEqual(job_result['status'], 'complete')
        self.assertFalse(job_result['error'])
        self.assertEqual(
            job_result['status_msg'],
            '2 quizzes have been updated.'
        )
        self.assertEqual(job_result['percent'], 100)
Пример #4
0
    def test_refresh_background_inactive_user(self, m):
        from views import refresh_background

        with self.client.session_transaction() as sess:
            sess['canvas_user_id'] = 1234
            sess['lti_logged_in'] = True
            sess['is_admin'] = True

        course_id = 1
        user_id = 9001

        m.register_uri(
            'GET',
            '/api/v1/courses/{}'.format(course_id),
            json={
                'id': course_id,
                'name': 'Example Course'
            }
        )
        m.register_uri(
            'GET',
            '/api/v1/courses/{}/quizzes'.format(course_id),
            json=[
                {'id': 1, 'title': 'Quiz 1', 'time_limit': 10},
                {'id': 2, 'title': 'Quiz 2', 'time_limit': 30}
            ]
        )
        m.register_uri(
            'POST',
            '/api/v1/courses/{}/quizzes/1/extensions'.format(course_id),
            status_code=200
        )
        m.register_uri(
            'POST',
            '/api/v1/courses/{}/quizzes/2/extensions'.format(course_id),
            status_code=200
        )
        m.register_uri(
            'GET',
            '/api/v1/courses/{}/users/{}'.format(course_id, user_id),
            status_code=200,
            json={
                'id': 9001,
                'sortable_name': 'John Smith',
                'enrollments': [{'type': 'StudentEnrollment', 'enrollment_state': 'inactive'}]
            }
        )

        course = Course(course_id, course_name='Example Course')
        views.db.session.add(course)
        user = User(user_id, sortable_name="Missing User")
        views.db.session.add(user)

        views.db.session.commit()

        ext = Extension(course.id, user.id)
        views.db.session.add(ext)

        views.db.session.commit()

        # Check that the extension is active first
        self.assertTrue(ext.active)
        ext_id = ext.id

        job = self.queue.enqueue_call(func=refresh_background, args=(course_id,))
        self.worker.work(burst=True)
        self.assertTrue(job.is_finished)

        # Ensure extension has been marked as inactive.
        extension = views.db.session.query(Extension).filter_by(id=ext_id).first()
        self.assertFalse(extension.active)
Пример #5
0
    def test_refresh_background_update_error(self, m):
        from views import refresh_background

        with self.client.session_transaction() as sess:
            sess['canvas_user_id'] = 1234
            sess['lti_logged_in'] = True
            sess['is_admin'] = True

        course_id = 1

        m.register_uri(
            'GET',
            '/api/v1/courses/{}'.format(course_id),
            json={'id': course_id, 'name': 'Example Course'}
        )
        m.register_uri(
            'GET',
            '/api/v1/courses/1/quizzes',
            json=[{'id': 1, 'title': 'Quiz 1', 'time_limit': 10}]
        )
        m.register_uri(
            'POST',
            '/api/v1/courses/1/quizzes/1/extensions',
            status_code=404
        )
        m.register_uri(
            'GET',
            '/api/v1/courses/1/users/12345',
            json={
                'id': 12345,
                'sortable_name': 'John Smith',
                'enrollments': [{'type': 'StudentEnrollment', 'enrollment_state': 'active'}]
            }
        )

        course = Course(course_id, course_name='Example Course')
        views.db.session.add(course)

        user = User(12345, sortable_name="John Smith")
        views.db.session.add(user)

        views.db.session.commit()

        ext = Extension(course.id, user.id)
        views.db.session.add(ext)
        views.db.session.commit()

        job = self.queue.enqueue_call(func=refresh_background, args=(course_id,))
        self.worker.work(burst=True)

        self.assertTrue(job.is_finished)
        job_result = job.result

        meta_keys = ['status', 'status_msg', 'percent', 'error']
        self.assertTrue(all(key in job_result for key in meta_keys))

        self.assertEqual(job_result['status'], 'failed')
        self.assertTrue(job_result['error'])
        self.assertEqual(
            job_result['status_msg'],
            (
                'Some quizzes couldn\'t be updated. Error creating extension '
                'for quiz #1. Canvas status code: 404'
            )
        )
Пример #6
0
    def test_update_background(self, m):
        from views import update_background

        with self.client.session_transaction() as sess:
            sess['canvas_user_id'] = 1234
            sess['lti_logged_in'] = True
            sess['is_admin'] = True

        course_id = 1

        m.register_uri(
            'GET',
            '/api/v1/courses/1',
            json={'id': 1, 'name': 'Example Course'}
        )
        m.register_uri(
            'GET',
            '/api/v1/courses/1/quizzes',
            json=[
                {'id': 4, 'title': 'Quiz 4', 'time_limit': 10},
                {'id': 5, 'title': 'Quiz 5', 'time_limit': 30},
                {'id': 6, 'title': 'Quiz 6', 'time_limit': None},
                {'id': 7, 'title': 'Quiz 7'}
            ]
        )
        m.register_uri(
            'GET',
            '/api/v1/courses/1/users/11',
            json={'id': 11, 'sortable_name': 'Joe Smyth'}
        )
        m.register_uri(
            'GET',
            '/api/v1/courses/1/users/12',
            status_code=404
        )
        m.register_uri(
            'GET',
            '/api/v1/courses/1/users/13',
            json={'id': 13, 'sortable_name': 'Jack Smith'}
        )
        m.register_uri(
            'POST',
            '/api/v1/courses/1/quizzes/4/extensions',
            status_code=200
        )
        m.register_uri(
            'POST',
            '/api/v1/courses/1/quizzes/5/extensions',
            status_code=200
        )

        course = Course(course_id, course_name='Example Course')
        views.db.session.add(course)

        user = User(11, sortable_name='Joe Smyth')
        views.db.session.add(user)
        user2 = User(13, sortable_name='Jack Smith')
        views.db.session.add(user2)

        views.db.session.commit()

        ext = Extension(course.id, user.id)
        views.db.session.add(ext)
        ext2 = Extension(course.id, user2.id)
        views.db.session.add(ext2)

        views.db.session.commit()

        job = self.queue.enqueue_call(
            func=update_background,
            args=(course_id, {'percent': '200', 'user_ids': ['11', '12', '13']})
        )
        self.worker.work(burst=True)

        self.assertTrue(job.is_finished)
        job_result = job.result

        meta_keys = ['status', 'status_msg', 'percent', 'error']
        self.assertTrue(all(key in job_result for key in meta_keys))

        self.assertEqual(job_result['status'], 'complete')
        self.assertFalse(job_result['error'])
        self.assertEqual(job_result['percent'], 100)
        self.assertEqual(
            job_result['status_msg'],
            (
                'Success! 2 quizzes have been updated for 3 student(s) to have '
                '200% time. 2 quizzes have no time limit and were left unchanged.'
            )
        )
Пример #7
0
    def test_update_background_extension_error(self, m):
        from views import update_background

        with self.client.session_transaction() as sess:
            sess['canvas_user_id'] = 1234
            sess['lti_logged_in'] = True
            sess['is_admin'] = True

        course_id = 1

        m.register_uri(
            'GET',
            '/api/v1/courses/1',
            json={'id': 1, 'name': 'Example Course'}
        )
        m.register_uri(
            'GET',
            '/api/v1/courses/1/quizzes',
            json=[
                {'id': 4, 'title': 'Quiz 4', 'time_limit': 10},
                {'id': 5, 'title': 'Quiz 5', 'time_limit': 30},
                {'id': 6, 'title': 'Quiz 6', 'time_limit': None},
                {'id': 7, 'title': 'Quiz 7'}
            ]
        )
        m.register_uri(
            'GET',
            '/api/v1/courses/1/users/11',
            json={'id': 11, 'sortable_name': 'Joe Smyth'}
        )
        m.register_uri(
            'GET',
            '/api/v1/courses/1/users/12',
            status_code=404
        )
        m.register_uri(
            'GET',
            '/api/v1/courses/1/users/13',
            json={
                'id': 13,
                'sortable_name': 'Jack Smith',
                'enrollments': [{'type': 'StudentEnrollment', 'enrollment_state': 'active'}]
            }
        )
        m.register_uri(
            'POST',
            '/api/v1/courses/1/quizzes/4/extensions',
            status_code=404
        )
        m.register_uri(
            'POST',
            '/api/v1/courses/1/quizzes/5/extensions',
            status_code=200
        )

        course = Course(course_id, course_name='Example Course')
        views.db.session.add(course)

        user = User(11, sortable_name='Joe Smyth')
        views.db.session.add(user)
        user2 = User(13, sortable_name='Jack Smith')
        views.db.session.add(user2)

        views.db.session.commit()

        ext = Extension(course.id, user.id)
        views.db.session.add(ext)
        ext2 = Extension(course.id, user2.id)
        views.db.session.add(ext2)

        views.db.session.commit()

        job = self.queue.enqueue_call(
            func=update_background,
            args=(course_id, {'percent': '200', 'user_ids': ['11', '12', '13']})
        )
        self.worker.work(burst=True)

        self.assertTrue(job.is_finished)
        job_result = job.result

        meta_keys = ['status', 'status_msg', 'percent', 'error']
        self.assertTrue(all(key in job_result for key in meta_keys))

        self.assertEqual(job_result['status'], 'failed')
        self.assertTrue(job_result['error'])
        self.assertEqual(
            job_result['status_msg'],
            'Error creating extension for quiz #4. Canvas status code: 404'
        )
Пример #8
0
    def test_update_background_no_quizzes(self, m):
        from views import update_background

        with self.client.session_transaction() as sess:
            sess['canvas_user_id'] = 1234
            sess['lti_logged_in'] = True
            sess['is_admin'] = True

        course_id = 1

        m.register_uri(
            'GET',
            '/api/v1/courses/1',
            json={'id': 1, 'name': 'Example Course'}
        )
        m.register_uri(
            'GET',
            '/api/v1/courses/1/quizzes',
            json=[]
        )
        m.register_uri(
            'GET',
            '/api/v1/courses/1/users/11',
            json={'id': 11, 'sortable_name': 'Joe Smyth'}
        )
        m.register_uri(
            'GET',
            '/api/v1/courses/1/users/12',
            json={'id': 12, 'sortable_name': 'Jack Smith'}
        )
        course = Course(course_id, course_name='Example Course')
        views.db.session.add(course)

        user = User(11, sortable_name='Joe Smyth')
        views.db.session.add(user)
        user2 = User(12, sortable_name='Jack Smith')
        views.db.session.add(user2)

        views.db.session.commit()

        ext = Extension(course.id, user.id)
        views.db.session.add(ext)
        ext2 = Extension(course.id, user2.id)
        views.db.session.add(ext2)

        views.db.session.commit()

        job = self.queue.enqueue_call(
            func=update_background,
            args=(course_id, {'percent': '200', 'user_ids': ['11', '12']})
        )
        self.worker.work(burst=True)

        self.assertTrue(job.is_finished)
        job_result = job.result

        meta_keys = ['status', 'status_msg', 'percent', 'error']
        self.assertTrue(all(key in job_result for key in meta_keys))

        self.assertEqual(job_result['status'], 'failed')
        self.assertTrue(job_result['error'])
        self.assertEqual(
            job_result['status_msg'],
            'Sorry, there are no quizzes for this course.'
        )