s.username = '******' s.set_password('s') db.session.add(s) db.session.commit() from homework_server.models import Course c = Course() c.name = 'c' c.description = 'c' c.teacher_id = t.id c.students.append(s) db.session.add(c) db.session.commit() from homework_server.models import Homework from datetime import datetime h = Homework() h.name = 'h' h.description = 'h' h.deadline = datetime.utcnow() h.headcount = 4 h.self_assignable = False h.course_id = c.id h.students.append(s) db.session.add(h) db.session.commit() app_context.pop() app.run('localhost', 5000)
def test_apply_for_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 not self assignable 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.post(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.post(f'/api/v1/student/homework/{homework.id}', headers=self.token_auth_header(token)) self.assertEquals(rv.status_code, 409) # make homework self assignable homework.self_assignable = True db.session.add(homework) # access with token rv = self.client.post(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, True) self.assertEquals(homework.course_id, course.id) self.assertEquals(len(homework.students), 1) self.assertEquals(homework.students[0].name, 'student')