示例#1
0
def user_register():
    # data = request.json
    # account = data['account']
    # password = data['password']

    # 1、request.data 会自动传入ClientForm
    form = ClientForm()
    # 2、对ClientForm对实例进行校验
    if form.validate():
        # 3.1、查询用户名是否已经存在
        if User.query.filter_by(account=form.account.data).first():
            # 4、如果用户名存在返回报错601
            return ParameterException(error_code=601, msg='用户名已经存在')
        else:
            # 5、若用户名不存在,尝试注册用户
            User.add_user(account=form.account.data,
                          password=form.password.data)
            return NoException(msg='注册成功')

    else:
        # 若form不满足校验规则,返回报错600,后续可以细化
        raise ParameterException()
示例#2
0
def new_user():
    form = NewUserForm()

    if form.validate_on_submit():
        for user in form.username.data.split(","):
            username = user.strip()
            db_user = User.add_user(username)
            Tweet.add_user_tweets(db_user)
            train()
            print(f"done with {user.strip()}")
            print("\n")
        return redirect(f'/user/{db_user.username}')
    return render_template('user/add.html', form=form)
示例#3
0
def test_get_user(client):
    email = "Foo"
    password = "******"
    user, email_hash = User.add_user(
        User.from_dict({
            "email": email,
            "password": password
        }))

    assert user is not None
    assert User.get_user_by_email(email.upper()) is not None
    assert email.lower() == jwt.decode(email_hash,
                                       'secret-key',
                                       algorithms=['HS256'])['email']
    # user details with correct auth
    rv = client.get("/user",
                    headers=get_headers(basic_auth=email + ":" + password))
    body = json.loads(rv.get_data(as_text=True))
    assert rv.status_code == 200
    assert body["email"] == user.email
    assert body["active"] == False
    # user details with correct auth but different case
    rv = client.get("/user",
                    headers=get_headers(basic_auth=email.upper() + ":" +
                                        password))
    body = json.loads(rv.get_data(as_text=True))
    assert rv.status_code == 200
    assert body["email"] == user.email
    # user details with correct auth
    rv = client.get("/user",
                    headers=get_headers(basic_auth=email + ":" + password))
    body = json.loads(rv.get_data(as_text=True))
    assert rv.status_code == 200
    assert body["email"] == user.email
    # user details with incorrect password
    rv = client.get("/user",
                    headers=get_headers(basic_auth=email + ":" + password +
                                        "lkajfs"))
    assert rv.status_code == 401
    # user details with incorrect email
    rv = client.get("/user",
                    headers=get_headers(basic_auth=email + "kalfd" + ":" +
                                        password))
    assert rv.status_code == 401
    # user details with no auth
    rv = client.get("/user")
    assert rv.status_code == 401
示例#4
0
def test_get_user_token(client):
    email = "foo"
    password = "******"

    user, email_hash = User.add_user(
        User.from_dict({
            "email": email,
            "password": password
        }))

    # generate token
    rv = client.get("/user/token",
                    headers=get_headers(basic_auth=email + ":" + password))
    body = json.loads(rv.get_data(as_text=True))
    assert rv.status_code == 201
    return_email = models.user.User.verify_auth_token(body["token"]).email
    assert return_email == user.email
示例#5
0
def test_get_user_activation(client):
    email = "foo"
    password = "******"

    user, email_hash = User.add_user(
        User.from_dict({
            "email": email,
            "password": password
        }))

    rv = client.get("/user/activate?email_hash={}".format(email_hash),
                    headers=get_headers())
    assert rv.status_code == 204

    assert User.get_user_by_email(email).active

    rv = client.get("/user/activate?email_hash={}".format('notahash'),
                    headers=get_headers())
    assert rv.status_code == 400