def test_get_close_db(app): with app.app_context(): db = get_db().db assert db is get_db().db # Out of app context, db should be closed. with pytest.raises(sqlite3.ProgrammingError) as e: db.execute("SELECT 1") assert "closed" in str(e.value)
def create(): json = request.json # Get params required_params = ["user_id", "file_name", "file_path", "content_total"] if any(x not in json for x in required_params): error_message = "missing required params." current_app.logger.debug(error_message) return make_response({"message": error_message}, 400) user_id, file_name, file_path, content_total = itemgetter( "user_id", "file_name", "file_path", "content_total", )(json) file_path = secure_filename(file_path) # Notify disk service repo = service.get_disk_repo() repo.create(file_path, content_total) # Save to database repo = get_db() file_id = repo.create(file_name, user_id, file_path) upload_url = service.build_upload_url(file_path) current_app.logger.debug( "create_file " + str({ "file_name": file_name, "file_size": content_total }), ) return {"file_id": file_id, "upload_url": upload_url}
def test_create_super_user_command(runner, app): with app.app_context(): result = runner.invoke(args=["create-superuser", "admin"], input="test\ntest\n") assert "Created" in result.output db = get_db() assert len(db.search(lambda x: x["username"] == "admin")) == 1 user = db.search(lambda x: x["username"] == "admin")[0] assert user["permissions"] == [ { "access_level": "read", "scope": "files" }, { "access_level": "write", "scope": "files" }, { "access_level": "read", "scope": "disk_storage" }, { "access_level": "write", "scope": "disk_storage" }, ] del db
def load_logged_in_user(): user_id = session.get("user_id") if user_id is None: g.user = None else: g.user = (get_db().execute("SELECT * FROM user WHERE id = ?", (user_id, )).fetchone())
def get_user_permissions(user_id: int) -> list: db = get_db() user = db.get_by_id(user_id) permissions = user["permissions"] permissions = [x["access_level"] + ": " + x["scope"] for x in permissions] return permissions
def index(): repo = get_db() files: list = repo.index() for file in files: file["download_url"] = service.build_download_url(file["file_path"]) file["upload_url"] = service.build_upload_url(file["file_path"]) return {"files": files}
def delete(id): repo = get_db() db_file = repo.get_by_id(id) if not db_file: abort(404) service.delete_file(id) return "File deleted successfully."
def delete_file(id): """ Deletes a file from storage. """ files_repo = get_db() file_path = files_repo.get_by_id(id)["file_path"] files_repo.delete(id) # Notify disk service disk_repo = get_disk_repo() disk_repo.delete(file_path)
def get_download_url(id) -> str: """ Consumes a file id and produces a url. """ repo = get_db() db_file = repo.get_by_id(id) if db_file is None or "file_path" not in db_file.keys(): return None file_path = db_file["file_path"] return build_download_url(file_path)
def test_register(client, app): response = client.post("/auth/register", data={ "username": "******", "password": "******" }) assert "http://localhost/auth/login" == response.headers["Location"] with app.app_context(): assert (get_db().execute( "select * from user where username = '******'", ).fetchone() is not None)
def file_info(id): """ Returns the file info given a file id. """ repo = get_db() file_info = repo.get_by_id(id) if not file_info: return NotFound(f"File Id {id} does not exist.") file_info["download_url"] = service.build_download_url( file_info["file_path"]) file_info["upload_url"] = service.build_upload_url(file_info["file_path"]) return file_info
def find_user(username: str) -> dict: db = get_db() user = db.search(lambda x: x["username"] == username)[0] return user