def test_delete_user(self, user, login, client): login() user = User(username="******", email="test", password="******") user.save() assert "test" == user.username, "Should save user" res = client.get(f"/user/{user.id}/delete") assert 200 == res.status_code, "Should return 200 status" assert ( User.query.filter_by(username="******").first() is None ), "Query should return None"
def create_user_command(): """Quick create a user for development env""" from flashlearn.models import User username = click.prompt("Username", type=str) if User.query.filter_by(username=username).first() is None: password = click.prompt( "Password", type=str, hide_input=True, confirmation_prompt=True, ) email = click.prompt("Email", default=None) u = User(username=username, password=password, email=email) u.save() click.echo("User created successfully.") else: click.echo("Username not available. Try again with a different one.")
def test_user_model(self, client): """Client fixture is only used to provide app_context and create db tables""" u = User(username="******", password="******", email="*****@*****.**") u.save() assert u.password != "12345" assert u.state == "Active" u.password = Bcrypt().generate_password_hash("TheShrubbery@007").decode() u.save() assert u.password_is_valid("TheShrubbery@007"), "Should confirm password change" User.query.filter_by(username="******").delete() assert ( User.query.filter_by(username="******").first() is None ), "Should delete the user"
def change_username(): username = request.form.get("username", None) if not username: abort(400) username_check = User.check_username(username, g.user.id) if username_check["status"] == 1: if username != g.user.username: g.user.username = username g.user.save() username_check["message"] = "Username changed successfully" return render_template( "dashboard/settings.html", username_response=username_check, user=g.user, )
def change_email(): email = request.form.get("email", None) if not email: abort(400) check_email = User.check_email(email, g.user.id) if check_email["status"] == 1: if email != g.user.email: g.user.email = email g.user.is_verified = False g.user.save() check_email["message"] = "Email changed successfully" return render_template( "dashboard/settings.html", email_response=check_email, user=g.user, )
def register(): if request.method == "GET": return render_template("register.html") elif request.method == "POST": username = request.form.get("username") password = request.form.get("password") password_confirm = request.form.get("password_confirm") user = User.query.filter_by(username=username).first() if user is not None: flash("Username is taken") return render_template("register.html") if password == password_confirm: user = User(username=username, password=password) user.save() return redirect(url_for("user.login")) else: flash("Password and password confirm don't match") return render_template("register.html")
def list_users(): users = [user.to_json for user in User.all()] return jsonify(users)
def super_user(client) -> User: bob = User(username="******", email="*****@*****.**") bob.set_password("password") bob.is_superuser = True bob.save() return bob
def user(client) -> User: alice = User(username="******", email="*****@*****.**") alice.set_password("password") alice.save() return alice