def create_admin(username, password): if len(password) < 8: click.echo("Password length too short") else: user = User(username=username, password=password, is_admin=True) if user.insert(): click.echo(f"User {username} successfully created.") else: click.echo("User with given username already exists.")
def test_app(): """Instantiate the app for each test with its own temporary data directory Each test using this fixture will use its own db.json and its own data directory, and then delete them. """ # create a temporary file to isolate the database for each test global _app if _app is None: _app = create_click_web_app(cli, cli.cli, app) app_dir = tempfile.mkdtemp() _app.config["INTERNAL_DIR"] = app_dir _app.config["USER_DIR"] = app_dir data_dir = os.path.join(app_dir, "data") os.mkdir(data_dir) _app.config["TESTING"] = True _app.config["WTF_CSRF_ENABLED"] = False # This setups a TinyDB instance, using the `app_dir` temporary # directory defined above # Required so that `flask.current_app` can be called in data.py and # models.py # See https://flask.palletsprojects.com/en/1.1.x/appcontext/ for more # information. with _app.app_context(): _ = get_db() user = {"username": "******", "password": "******"} User(**user).insert() yield _app # close and remove the temporary database shutil.rmtree(app_dir)
def login(): """ Logs in the API client using [HTTP Basic Auth](https://en.wikipedia.org/wiki/Basic_access_authentication). Pass in the username and password of your account. """ db = get_db() user = db.search(Query().username == request.authorization["username"]) if (user and check_password_hash(user[0]["hashed_password"], request.authorization["password"])): # user is verified so we can log him in from the db user = User.from_db(user[0]) login_user(user, remember=True) return Response(status=200) return Response(status=401)
def login(): form = forms.UserForm() if form.validate_on_submit(): db = get_db() user = db.search((Query().username == form.username.data) & (Query().type == "user")) if user and check_password_hash(user[0]["hashed_password"], form.password.data): user = User.from_db(user[0]) login_user(user, remember=True) flash("Login successful!", "success") next_url = request.args.get("next") return redirect(next_url or "/") flash("Invalid credentials", "error") return redirect("/login") return render_template("users/login.html", form=form, title="Login")
def load_user(user_id): db = helpers.get_db() res = db.get(doc_id=int(user_id)) if res and res["type"] == "user": return User.from_db(res) return None
@login_manager.user_loader def load_user(user_id): db = extensions.get_db() res = db.get(doc_id=int(user_id)) if res and res["type"] == "user": return User.from_db(res) return None # prevent pytest from hanging because of running thread if 'pytest' not in sys.argv[0]: Thread(target=run_watcher, args=[app]).start() app.jinja_options["extensions"].append("jinja2.ext.do") # create admin user if it does not exist with app.app_context(): db = extensions.get_db() user_query = Query() # noqa here because tinydb requires us to explicitly specify is_admin == True if not db.search((user_query.type == "user") & (user_query.is_admin == True)): # noqa: password = token_urlsafe(32) user = User(username="******", password=password, is_admin=True) if user.insert(): app.logger.info(f"""Archivy has created an admin user as it did not exist. Username: '******', password: '******' """) from archivy import routes # noqa:
def user_fixture(test_app): user = {"username": "******", "password": "******"} user = User(**user) user.insert() return user