From 0dfff2bcb24d8915f5fd317c79f5e750f0897e5a Mon Sep 17 00:00:00 2001 From: Kevin Morris Date: Tue, 5 Oct 2021 21:13:51 -0700 Subject: [PATCH] feat(FastAPI): add /pkgbase/{name}/[un]notify (post) Signed-off-by: Kevin Morris --- aurweb/routers/packages.py | 36 ++++++++++++++++++++++++++++++++++++ test/test_packages_routes.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/aurweb/routers/packages.py b/aurweb/routers/packages.py index 8790327f..ced9ea3d 100644 --- a/aurweb/routers/packages.py +++ b/aurweb/routers/packages.py @@ -733,3 +733,39 @@ async def pkgbase_unflag(request: Request, name: str): return RedirectResponse(f"/pkgbase/{name}", status_code=int(HTTPStatus.SEE_OTHER)) + + +@router.post("/pkgbase/{name}/notify") +@auth_required(True, redirect="/pkgbase/{name}") +async def pkgbase_notify(request: Request, name: str): + pkgbase = get_pkg_or_base(name, PackageBase) + + notif = db.query(pkgbase.notifications.filter( + PackageNotification.UserID == request.user.ID + ).exists()).scalar() + has_cred = request.user.has_credential("CRED_PKGBASE_NOTIFY") + if has_cred and not notif: + with db.begin(): + db.create(PackageNotification, + PackageBase=pkgbase, + User=request.user) + + return RedirectResponse(f"/pkgbase/{name}", + status_code=int(HTTPStatus.SEE_OTHER)) + + +@router.post("/pkgbase/{name}/unnotify") +@auth_required(True, redirect="/pkgbase/{name}") +async def pkgbase_unnotify(request: Request, name: str): + pkgbase = get_pkg_or_base(name, PackageBase) + + notif = pkgbase.notifications.filter( + PackageNotification.UserID == request.user.ID + ).first() + has_cred = request.user.has_credential("CRED_PKGBASE_NOTIFY") + if has_cred and notif: + with db.begin(): + db.session.delete(notif) + + return RedirectResponse(f"/pkgbase/{name}", + status_code=int(HTTPStatus.SEE_OTHER)) diff --git a/test/test_packages_routes.py b/test/test_packages_routes.py index db36d1a9..1d7fe17c 100644 --- a/test/test_packages_routes.py +++ b/test/test_packages_routes.py @@ -1720,3 +1720,36 @@ def test_pkgbase_flag(client: TestClient, user: User, maintainer: User, resp = request.post(endpoint, cookies=cookies) assert resp.status_code == int(HTTPStatus.SEE_OTHER) assert pkgbase.Flagger is None + + +def test_pkgbase_notify(client: TestClient, user: User, package: Package): + pkgbase = package.PackageBase + + # We have no notif record yet; assert that. + notif = pkgbase.notifications.filter( + PackageNotification.UserID == user.ID + ).first() + assert notif is None + + # Enable notifications. + cookies = {"AURSID": user.login(Request(), "testPassword")} + endpoint = f"/pkgbase/{pkgbase.Name}/notify" + with client as request: + resp = request.post(endpoint, cookies=cookies) + assert resp.status_code == int(HTTPStatus.SEE_OTHER) + + notif = pkgbase.notifications.filter( + PackageNotification.UserID == user.ID + ).first() + assert notif is not None + + # Disable notifications. + endpoint = f"/pkgbase/{pkgbase.Name}/unnotify" + with client as request: + resp = request.post(endpoint, cookies=cookies) + assert resp.status_code == int(HTTPStatus.SEE_OTHER) + + notif = pkgbase.notifications.filter( + PackageNotification.UserID == user.ID + ).first() + assert notif is None