def create_homework(id): data = request.get_json() or {} if not all(field in data for field in ['name', 'description', 'deadline', 'headcount', 'self_assignable']): return '', 400 homework = Homework() homework.from_dict(data) homework.course_id = id db.session.add(homework) db.session.commit() return '', 200
def test_get_homeworks_for_course(self): # create a course course = Course() course.from_dict({'name': 'course', 'description': 'course'}) course.teacher_id = self.teacher.id course.students.append(self.student) db.session.add(course) db.session.commit() # create a homework homework = Homework() homework.from_dict({ 'name': 'homework', 'description': 'homework', 'deadline': '2018-11-08 08:48:11', 'headcount': 4, 'self_assignable': False }) homework.course_id = course.id db.session.add(homework) db.session.commit() # try to access with basic authentication rv = self.client.get(f'/api/v1/student/course/{course.id}/homeworks', headers=self.basic_auth_header( 'student', 'student')) self.assertEquals(rv.status_code, 401) # get token rv = self.client.post('/api/v1/auth/token', headers=self.basic_auth_header( 'student', 'student')) self.assertEquals(rv.status_code, 200) token = json.loads(rv.data.decode())['token'] # access with token rv = self.client.get(f'/api/v1/student/course/{course.id}/homeworks', headers=self.token_auth_header(token)) self.assertEquals(rv.status_code, 200) # check returned data data = json.loads(rv.data.decode()) self.assertTrue( all(item in data for item in ['homeworks', 'next', 'prev'])) self.assertEquals(len(data['homeworks']), 1) self.assertTrue(all(item in data['homeworks'][0] for item in \ ['id', 'name', 'description', 'deadline', 'headcount', 'self_assignable', 'course'])) self.assertIsNotNone(data['homeworks'][0]['id']) self.assertEquals(data['homeworks'][0]['name'], 'homework') self.assertEquals(data['homeworks'][0]['description'], 'homework') self.assertEquals(data['homeworks'][0]['deadline'], '2018-11-08 08:48:11') self.assertEquals(data['homeworks'][0]['headcount'], 4) self.assertEquals(data['homeworks'][0]['self_assignable'], False) self.assertEquals(data['homeworks'][0]['course'], 'course')
def test_abandon_homework(self): # create a course course = Course() course.from_dict({'name': 'course', 'description': 'course'}) course.teacher_id = self.teacher.id course.students.append(self.student) db.session.add(course) db.session.commit() # create a homework homework = Homework() homework.from_dict({ 'name': 'homework', 'description': 'homework', 'deadline': '2018-11-08 08:48:11', 'headcount': 4, 'self_assignable': False }) homework.course_id = course.id homework.students.append(self.student) db.session.add(homework) db.session.commit() # try to access with basic authentication rv = self.client.delete(f'/api/v1/student/homework/{homework.id}', headers=self.basic_auth_header( 'student', 'student')) self.assertEquals(rv.status_code, 401) # get token rv = self.client.post('/api/v1/auth/token', headers=self.basic_auth_header( 'student', 'student')) self.assertEquals(rv.status_code, 200) token = json.loads(rv.data.decode())['token'] # access with token rv = self.client.delete(f'/api/v1/student/homework/{homework.id}', headers=self.token_auth_header(token)) self.assertEquals(rv.status_code, 200) # check data homework = Homework.query.filter_by(name='homework').first() self.assertIsNotNone(homework) self.assertEquals(homework.name, 'homework') self.assertEquals(homework.description, 'homework') self.assertEquals( homework.deadline, datetime.strptime('2018-11-08 08:48:11', '%Y-%m-%d %H:%M:%S')) self.assertEquals(homework.headcount, 4) self.assertEquals(homework.self_assignable, False) self.assertEquals(homework.course_id, course.id) self.assertEquals(len(homework.students), 0)
def test_remove_homework(self): # create a homework course = Course() course.from_dict({'name': 'course', 'description': 'course'}) course.teacher_id = self.teacher.id db.session.add(course) db.session.commit() # create a homework homework = Homework() homework.from_dict({ 'name': 'homework', 'description': 'homework', 'deadline': '2018-11-08 08:48:11', 'headcount': 4, 'self_assignable': False }) homework.course_id = course.id db.session.add(homework) db.session.commit() number_of_homeworks_before = len(Homework.query.all()) # try to access with basic authentication rv = self.client.delete(f'/api/v1/teacher/homework/{homework.id}', headers=self.basic_auth_header( 'teacher', 'teacher')) self.assertEquals(rv.status_code, 401) # get token rv = self.client.post('/api/v1/auth/token', headers=self.basic_auth_header( 'teacher', 'teacher')) self.assertEquals(rv.status_code, 200) token = json.loads(rv.data.decode())['token'] # access with token rv = self.client.delete(f'/api/v1/teacher/homework/{homework.id}', headers=self.token_auth_header(token)) self.assertEquals(rv.status_code, 200) number_of_homeworks_after = len(Homework.query.all()) self.assertEquals( number_of_homeworks_before - number_of_homeworks_after, 1) # try to remove a nonexistent homework rv = self.client.delete(f'/api/v1/teacher/homework/{homework.id}', headers=self.token_auth_header(token)) self.assertEquals(rv.status_code, 410)
def test_get_solution(self): # create a homework course = Course() course.from_dict({'name': 'course', 'description': 'course'}) course.teacher_id = self.teacher.id db.session.add(course) db.session.commit() # create a homework homework = Homework() homework.from_dict({ 'name': 'homework', 'description': 'homework', 'deadline': '2018-11-08 08:48:11', 'headcount': 4, 'self_assignable': False }) homework.course_id = course.id homework.students.append(self.student) db.session.add(homework) db.session.commit() # create a solution solution = Solution() solution.from_dict({'status': 'status'}) solution.file_path = 'f' solution.homework_id = homework.id db.session.add(solution) db.session.commit() submitted_at = datetime.utcnow() # try to access with basic authentication rv = self.client.get(f'/api/v1/student/solution/{solution.id}', headers=self.basic_auth_header( 'student', 'student')) self.assertEquals(rv.status_code, 401) # get token rv = self.client.post('/api/v1/auth/token', headers=self.basic_auth_header( 'student', 'student')) self.assertEquals(rv.status_code, 200) token = json.loads(rv.data.decode())['token'] # access with token rv = self.client.get(f'/api/v1/student/solution/{solution.id}', headers=self.token_auth_header(token)) self.assertEquals(rv.status_code, 200) # check returned data data = json.loads(rv.data.decode()) self.assertTrue('solution' in data) self.assertTrue( all(item in data['solution'] for item in ['id', 'status', 'submitted_at'])) self.assertIsNotNone(data['solution']['id']) self.assertEquals(data['solution']['status'], 'status') self.assertTrue( abs((submitted_at - datetime.strptime(data['solution']['submitted_at'], '%Y-%m-%d %H:%M:%S')).seconds) < 1)
def test_submit_solution(self): # create a course course = Course() course.from_dict({'name': 'course', 'description': 'course'}) course.teacher_id = self.teacher.id course.students.append(self.student) db.session.add(course) db.session.commit() # create a homework homework = Homework() homework.from_dict({ 'name': 'homework', 'description': 'homework', 'deadline': '2018-11-08 08:48:11', 'headcount': 4, 'self_assignable': False }) homework.course_id = course.id homework.students.append(self.student) db.session.add(homework) db.session.commit() # create another student student2 = Student() student2.from_dict({'name': 'student2', 'username': '******'}) student2.set_password('student2') db.session.add(student2) db.session.commit() # try to access with basic authentication rv = self.client.post(f'/api/v1/student/homework/{homework.id}/submit', headers=self.basic_auth_header('student', 'student'), \ content_type='multipart/form-data', data={'file': (BytesIO(b'tmp'), 'tmp.txt')}) self.assertEquals(rv.status_code, 401) # get token rv = self.client.post('/api/v1/auth/token', headers=self.basic_auth_header( 'student', 'student')) self.assertEquals(rv.status_code, 200) token = json.loads(rv.data.decode())['token'] # access with token rv = self.client.post(f'/api/v1/student/homework/{homework.id}/submit', headers=self.token_auth_header(token), \ content_type='multipart/form-data', data={'file': (BytesIO(b'tmp'), 'tmp.txt')}) self.assertEquals(rv.status_code, 200) submitted_at = datetime.utcnow() # check data solutions = Solution.query.all() self.assertEquals(len(solutions), 1) solution = Solution.query.filter_by(status='').first() self.assertTrue( all(item in solution.to_dict() for item in ['status', 'submitted_at'])) self.assertEquals(solution.status, '') self.assertTrue( abs((submitted_at - solution.submitted_at).seconds) < 1) self.assertIsNotNone(solution.file_path) # get token for student2 rv = self.client.post('/api/v1/auth/token', headers=self.basic_auth_header( 'student2', 'student2')) self.assertEquals(rv.status_code, 200) token = json.loads(rv.data.decode())['token'] # access with student2's token rv = self.client.post(f'/api/v1/student/homework/{homework.id}/submit', headers=self.token_auth_header(token), \ content_type='multipart/form-data', data={'file': (BytesIO(b'tmp'), 'tmp.txt')}) self.assertEquals(rv.status_code, 409) # clean up the upload folder shutil.rmtree(self.app.config['UPLOAD_FOLDER'])
def test_modify_solution(self): # create a homework course = Course() course.from_dict({'name': 'course', 'description': 'course'}) course.teacher_id = self.teacher.id db.session.add(course) db.session.commit() # create a homework homework = Homework() homework.from_dict({ 'name': 'homework', 'description': 'homework', 'deadline': '2018-11-08 08:48:11', 'headcount': 4, 'self_assignable': False }) homework.course_id = course.id db.session.add(homework) db.session.commit() # create a solution solution = Solution() solution.from_dict({'status': 'status'}) solution.file_path = 'f' solution.homework_id = homework.id db.session.add(solution) db.session.commit() submitted_at = datetime.utcnow() # try to access with basic authentication rv = self.client.put(f'/api/v1/teacher/solution/{solution.id}', headers=self.basic_auth_header('teacher', 'teacher'), \ data=json.dumps({'status': 'f1'})) self.assertEquals(rv.status_code, 401) # get token rv = self.client.post('/api/v1/auth/token', headers=self.basic_auth_header( 'teacher', 'teacher')) self.assertEquals(rv.status_code, 200) token = json.loads(rv.data.decode())['token'] # access with token rv = self.client.put(f'/api/v1/teacher/solution/{solution.id}', headers=self.token_auth_header(token), \ data=json.dumps({'status': 'f1'})) self.assertEquals(rv.status_code, 200) # check if solution was modified solution = Solution.query.filter_by(status='f1').first() self.assertIsNotNone(solution) self.assertEquals(solution.status, 'f1') self.assertEquals(solution.file_path, 'f') # try to modify homework without all the necessary fields data = {'status': 'f1'} request_datas = self.insufficient_datas(data) for d in request_datas: rv = self.client.put(f'/api/v1/teacher/solution/{solution.id}', \ headers=self.token_auth_header(token), data=json.dumps(d)) self.assertEquals(rv.status_code, 400)
def test_modify_homeworks(self): # create a course course = Course() course.from_dict({'name': 'course', 'description': 'course'}) course.teacher_id = self.teacher.id db.session.add(course) db.session.commit() # create a homework homework = Homework() homework.from_dict({ 'name': 'homework', 'description': 'homework', 'deadline': '2018-11-08 08:48:11', 'headcount': 4, 'self_assignable': False }) homework.course_id = course.id db.session.add(homework) db.session.commit() # try to access with basic authentication rv = self.client.put(f'/api/v1/teacher/homework/{homework.id}', headers=self.basic_auth_header('teacher', 'teacher'), \ data=json.dumps({'name': 'f1', 'description': 'f2', 'deadline': '2018-11-08 08:48:11', \ 'headcount': 4, 'self_assignable': False})) self.assertEquals(rv.status_code, 401) # get token rv = self.client.post('/api/v1/auth/token', headers=self.basic_auth_header( 'teacher', 'teacher')) self.assertEquals(rv.status_code, 200) token = json.loads(rv.data.decode())['token'] # access with token rv = self.client.put(f'/api/v1/teacher/homework/{homework.id}', headers=self.token_auth_header(token), \ data=json.dumps({'name': 'f1', 'description': 'f2', 'deadline': '2018-11-08 08:48:11', \ 'headcount': 4, 'self_assignable': False})) self.assertEquals(rv.status_code, 200) # check if homework was modified homework = Homework.query.filter_by(name='f1').first() self.assertIsNotNone(homework) self.assertEquals(homework.name, 'f1') self.assertEquals(homework.description, 'f2') self.assertEquals( homework.deadline, datetime.strptime('2018-11-08 08:48:11', '%Y-%m-%d %H:%M:%S')) self.assertEquals(homework.headcount, 4) self.assertEquals(homework.self_assignable, False) self.assertEquals(homework.course_id, course.id) self.assertEquals(len(homework.students), 0) # try to modify homework with any of the necessary fields data = { 'name': 'f1', 'description': 'f2', 'deadline': '2018-11-08 08:48:11', 'headcount': 4, 'self_assignable': False, 'students': [ self.student.id, ] } request_datas = self.insufficient_datas(data) for d in request_datas: rv = self.client.put(f'/api/v1/teacher/homework/{homework.id}', \ headers=self.token_auth_header(token), data=json.dumps(d)) self.assertEquals(rv.status_code, 200)
def test_student(self): # create invalid student student = Student() db.session.add(student) self.assertRaises(IntegrityError, db.session.commit) db.session.rollback() # create valid student student.from_dict({ 'name': 'student', 'username': '******', 'password': '******' }) db.session.add(student) db.session.commit() # check data self.assertEquals(len(Student.query.all()), 1) self.assertIsNotNone(student.id) self.assertNotEquals(student.password_hash, 'student') self.assertEquals(student.type, 'students') d = student.to_dict() self.assertIsNotNone(d['id']) self.assertEquals(d['username'], 'student') self.assertEquals(d['name'], 'student') # try to create student with the same username student2 = Student() student2.from_dict({ 'name': 'student2', 'username': '******', 'password': '******' }) db.session.add(student2) self.assertRaises(IntegrityError, db.session.commit) db.session.rollback() # create a teacher teacher = Teacher() teacher.from_dict({ 'name': 'teacher', 'username': '******', 'password': '******' }) db.session.add(teacher) db.session.commit() # create a course and add student to it course = Course() course.from_dict({'name': 'course', 'description': 'course'}) course.teacher_id = teacher.id course.students.append(student) db.session.add(course) db.session.commit() # check if student has one course self.assertEquals(len(student.courses), 1) # create a homework and add student to it homework = Homework() homework.from_dict({ 'name': 'homework', 'description': 'homework', 'deadline': datetime.utcnow(), 'headcount': 2, 'self_assignable': True }) homework.course_id = course.id homework.students.append(student) db.session.add(homework) db.session.commit() # check if student has one homework self.assertEquals(len(student.homeworks), 1)
def test_solution(self): # create invalid solution solution = Solution() db.session.add(solution) self.assertRaises(IntegrityError, db.session.commit) db.session.rollback() # try to create solution without file_path solution.from_dict({'status': 'status'}) db.session.add(solution) self.assertRaises(IntegrityError, db.session.commit) db.session.rollback() # try to create solution without a homework solution.file_path = 'file_path' db.session.add(solution) self.assertRaises(IntegrityError, db.session.commit) db.session.rollback() # create a teacher teacher = Teacher() teacher.from_dict({ 'name': 'teacher', 'username': '******', 'password': '******' }) db.session.add(teacher) db.session.commit() # create a course course = Course() course.from_dict({'name': 'course', 'description': 'course'}) course.teacher_id = teacher.id db.session.add(course) db.session.commit() # create a homework homework = Homework() homework.from_dict({ 'name': 'homework', 'description': 'homework', 'deadline': '2018-10-30 10:20:32', 'headcount': 2, 'self_assignable': True }) homework.course_id = course.id db.session.add(homework) db.session.commit() # add homework to solution solution.homework_id = homework.id db.session.add(solution) db.session.commit() # check data submitted_at = datetime.utcnow() self.assertEquals(len(Solution.query.all()), 1) self.assertIsNotNone(solution.id) self.assertEquals(solution.homework_id, homework.id) d = solution.to_dict() self.assertIsNotNone(d['id']) self.assertEquals(sorted(d.keys()), ['id', 'status', 'submitted_at']) self.assertEquals(d['status'], 'status') self.assertTrue( abs((submitted_at - datetime.strptime(d['submitted_at'], '%Y-%m-%d %H:%M:%S') ).seconds) < 1)
def test_homework(self): # create invalid homework homework = Homework() db.session.add(homework) self.assertRaises(IntegrityError, db.session.commit) db.session.rollback() # try to create homework without a course homework.from_dict({ 'name': 'homework', 'description': 'homework', 'deadline': '2018-10-30 10:20:32', 'headcount': 2, 'self_assignable': True }) db.session.add(homework) self.assertRaises(IntegrityError, db.session.commit) db.session.rollback() # create a teacher teacher = Teacher() teacher.from_dict({ 'name': 'teacher', 'username': '******', 'password': '******' }) db.session.add(teacher) db.session.commit() # create a course course = Course() course.from_dict({'name': 'course', 'description': 'course'}) course.teacher_id = teacher.id db.session.add(course) db.session.commit() # add course to homework homework.course_id = course.id db.session.add(homework) db.session.commit() # check data self.assertEquals(len(Homework.query.all()), 1) self.assertIsNotNone(homework.id) self.assertEquals(homework.course_id, course.id) d = homework.to_dict() self.assertIsNotNone(d['id']) self.assertEquals(d['name'], 'homework') self.assertEquals(d['description'], 'homework') self.assertEquals(d['deadline'], '2018-10-30 10:20:32') self.assertEquals(d['headcount'], 2) self.assertTrue(d['self_assignable']) self.assertEquals(d['course'], 'course') # try to create homework with the same name homework2 = Homework() homework2.from_dict({ 'name': 'homework', 'description': 'homework2', 'deadline': datetime.utcnow(), 'headcount': 3, 'self_assignable': False }) homework2.course_id = course.id db.session.add(homework2) self.assertRaises(IntegrityError, db.session.commit) db.session.rollback()