def test_teacher_course_get_fail(self): user = User(id=4, username="******", password=hash_password("password"), firstname="firstname", lastname="lastname", role="teacher") db.session.add(user) db.session.commit() credentials = b64encode(b"fail:password") test = self.client.get("/course", headers={"Authorization": f"Basic {credentials}"}) self.assertEqual(404, test.status_code)
def test_user_delete(self): user = User(id=3, username="******", password=hash_password("password"), firstname="firstname", lastname="lastname", role="student") db.session.add(user) db.session.commit() credentials = b64encode(b"delete:password") test = self.client.delete("/user", headers={"Authorization": f"Basic {credentials}"}) self.assertEqual(204, test.status_code)
def test_user_put(self): user = User(id=4, username="******", password=hash_password("password"), firstname="firstname", lastname="lastname", role="student") db.session.add(user) db.session.commit() credentials = b64encode(b"username:password") test = self.client.put("/user", headers={"Authorization": f"Basic {credentials}"}, data={"firstname": "testf", "lastname": "testl", "username": "******", "password": "******"}) print(test.data) self.assertEqual(200, test.status_code)
def setUp(self): db.create_all() db.session.commit() student = User(id=11, username="******", password=hash_password("password"), firstname="firstname", lastname="lastname", role="student") db.session.add(student) teacher = User(id=12, username="******", password=hash_password("password"), firstname="firstname", lastname="lastname", role="teacher") course = Course(id=11, title="title", filling="filling", creator_id=teacher.id) course2 = Course(id=12, title="title1", filling="filling1", creator_id=teacher.id) db.session.add(teacher) db.session.add(course) db.session.add(course2) db.session.commit()
def post(self): args = user_post_args.parse_args() password = hash_password(args['password']) user = User( username=args['username'], password=password, firstname=args['firstname'], lastname=args['lastname'], role=args['role'] ) user = db.session.merge(user) user_copy = copy.deepcopy(user) db.session.add(user) db.session.commit() db.session.close() return user_copy, 201
def put(self): args = user_put_args.parse_args() result = User.query.get(ident=auth.current_user().id) if not result: abort(404, message="User doesn't exist, cannot update") if args['username']: result.username = args['username'] if args['password']: result.password = hash_password(args['password']) if args['firstname']: result.firstname = args['firstname'] if args['lastname']: result.lastname = args['lastname'] result = db.session.merge(result) user_copy = copy.deepcopy(result) db.session.add(result) db.session.commit() db.session.close() return user_copy, 200