def add_student(username, password, realname): role = model.role.STUDENT salt = os.urandom(16) password_hash = hash(password, salt) user = model.user.User(username=username, password_hash=password_hash, salt=salt, role=role, realname=realname) user.save() print(username, realname)
def add_student(): username = input('Username:'******'Password:') role = model.role.STUDENT salt = os.urandom(16) password_hash = hash(password, salt) user = model.user.User(username=username, password_hash=password_hash, salt=salt, role=role) user.save() print("Add student success!")
def add_admin(): username = '******' password = '******' role = model.role.ADMIN salt = os.urandom(16) password_hash = hash(password, salt) user = model.user.User(username=username, password_hash=password_hash, salt=salt, role=role) user.save() print("Add admin success!")
def test_user_model(): username = ''.join(random.sample('abcdefghijklmnopqrstuvwxyz', 6)) email = username + '@' + 'qq.com' password = username user.save({'username': username, 'email': email, 'password': password}) account = user.findOneByName(username) assert account['username'] == username id = account['id'] password = ''.join(random.sample('abcdefghijklmnopqrstuvwxyz', 6)) user.change_password(id, password) username = ''.join(random.sample('abcdefghijklmnopqrstuvwxyz', 6)) email = username + '@' + 'qq.com' user.update({'username': username, 'email': email, 'id': id}) assert user.findOneById(id)['password'] == password assert user.findOneByEmail(email)['email'] == email
def reg(): '''用户注册 POST /api/user Body: JSON UserRegSchema: * username = fields.String(required=True) * email = fields.Email(required=True) * password = fields.String(required=True) Returns: 1. Success 200 {id:userid} 2. Error * 400 {msg:{错误字段:字段提示信息}} * 500 ''' if not request.is_json: return jsonify({"msg": "Missing JSON in request"}), 400 schema = UserRegSchema() data, errors = schema.load(request.json) if errors: return jsonify({"msg": errors}), 400 if user.findOneByEmail(data['email']): return jsonify({"msg": ('email', '邮箱已存在')}), 400 if user.findOneByName(data['username']): return jsonify({"msg": ('username', '用户名已存在')}), 400 data['password'] = argon2.hash(data['password']) account = user.save(data) access_token = create_access_token(identity={ 'id': account.id, 'username': account.username, 'email': account.email }, fresh=True) resp = jsonify({'access_token': access_token}) set_access_cookies(resp, access_token) session['user_id'] = account.id return resp
def test_exercice_test_job(): config.db_name = 'test' db = mongoengine.connect(config.db_name) db.drop_database(config.db_name) user = utils.sample_user() user.save() exercise = model.exercise.Exercise(author=user, title='Blah Bleh', description='Bleuh', boilerplate_code='b', reference_code='#') test = model.exercise.Test(input='1\n', output='1') test.save() exercise.tests.append(test) test = model.exercise.Test(input='2\n', output='1') test.save() exercise.tests.append(test) test = model.exercise.Test(input='3\n', output='2') test.save() exercise.tests.append(test) exercise.save() code = '\n'.join([ '#include <iostream>', '', 'int main() {', 'int i;', 'std::cin >> i;', 'std::cout << i;', 'if (i > 2) return 1;', 'return 0;', '}' ]) user = model.user.User(email='test@{}'.format(config.email_domain), username='******', secret_hash='hash', salt='salt') user.save() submission = job.Submission(exercise=exercise, code=code, user=user) submission.save() greedy_app = greedy.Greedy(db) greedy_app.fetch_and_process() submission.reload() assert not submission.compilation_error assert submission.test_results[0].passed == True assert submission.test_results[0].return_code == 0 # assert not submission.test_results[0].stdout # assert not submission.test_results[0].stderr assert submission.test_results[1].passed == False assert submission.test_results[1].return_code == 0 # assert not submission.test_results[1].stdout # assert not submission.test_results[1].stderr assert submission.test_results[2].passed == False assert submission.test_results[2].return_code == 1