feat(FastAPI): add /pkgbase/{name}/comments/{id}/unpin (post)

Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2021-10-01 17:51:39 -07:00
parent 0895dd07ee
commit 2efd254974
No known key found for this signature in database
GPG key ID: F7E46DED420788F3
2 changed files with 48 additions and 0 deletions

View file

@ -369,3 +369,24 @@ async def pkgbase_comment_pin(request: Request, name: str, id: int):
return RedirectResponse(f"/pkgbase/{name}", return RedirectResponse(f"/pkgbase/{name}",
status_code=int(HTTPStatus.SEE_OTHER)) status_code=int(HTTPStatus.SEE_OTHER))
@router.post("/pkgbase/{name}/comments/{id}/unpin")
@auth_required(True)
async def pkgbase_comment_unpin(request: Request, name: str, id: int):
pkgbase = get_pkg_or_base(name, PackageBase)
comment = get_pkgbase_comment(pkgbase, id)
has_cred = request.user.has_credential("CRED_COMMENT_PIN",
approved=[pkgbase.Maintainer])
if not has_cred:
_ = l10n.get_translator_for_request(request)
raise HTTPException(
status_code=int(HTTPStatus.UNAUTHORIZED),
detail=_("You are not allowed to unpin this comment."))
with db.begin():
comment.PinnedTS = 0
return RedirectResponse(f"/pkgbase/{name}",
status_code=int(HTTPStatus.SEE_OTHER))

View file

@ -1152,11 +1152,25 @@ def test_pkgbase_comment_pin(client: TestClient,
cookies = {"AURSID": maintainer.login(Request(), "testPassword")} cookies = {"AURSID": maintainer.login(Request(), "testPassword")}
comment_id = comment.ID comment_id = comment.ID
pkgbasename = package.PackageBase.Name pkgbasename = package.PackageBase.Name
# Pin the comment.
endpoint = f"/pkgbase/{pkgbasename}/comments/{comment_id}/pin" endpoint = f"/pkgbase/{pkgbasename}/comments/{comment_id}/pin"
with client as request: with client as request:
resp = request.post(endpoint, cookies=cookies) resp = request.post(endpoint, cookies=cookies)
assert resp.status_code == int(HTTPStatus.SEE_OTHER) assert resp.status_code == int(HTTPStatus.SEE_OTHER)
# Assert that PinnedTS got set.
assert comment.PinnedTS > 0
# Unpin the comment we just pinned.
endpoint = f"/pkgbase/{pkgbasename}/comments/{comment_id}/unpin"
with client as request:
resp = request.post(endpoint, cookies=cookies)
assert resp.status_code == int(HTTPStatus.SEE_OTHER)
# Let's assert that PinnedTS was unset.
assert comment.PinnedTS == 0
def test_pkgbase_comment_pin_unauthorized(client: TestClient, def test_pkgbase_comment_pin_unauthorized(client: TestClient,
user: User, user: User,
@ -1169,3 +1183,16 @@ def test_pkgbase_comment_pin_unauthorized(client: TestClient,
with client as request: with client as request:
resp = request.post(endpoint, cookies=cookies) resp = request.post(endpoint, cookies=cookies)
assert resp.status_code == int(HTTPStatus.UNAUTHORIZED) assert resp.status_code == int(HTTPStatus.UNAUTHORIZED)
def test_pkgbase_comment_unpin_unauthorized(client: TestClient,
user: User,
package: Package,
comment: PackageComment):
cookies = {"AURSID": user.login(Request(), "testPassword")}
comment_id = comment.ID
pkgbasename = package.PackageBase.Name
endpoint = f"/pkgbase/{pkgbasename}/comments/{comment_id}/unpin"
with client as request:
resp = request.post(endpoint, cookies=cookies)
assert resp.status_code == int(HTTPStatus.UNAUTHORIZED)