feat(FastAPI): add /pkgbase/{name}/request (post)

This change implements the FastAPI version of the
/pkgbase/{name}/request form's action.

Changes from PHP:

- Additional errors are now displayed for the **merge_into** field,
  which are only displayed when the Merge type is selected.
    - If the **merge_into** field is empty, a new error is displayed:
      'The "Merge into" field must not be empty.'
    - If the **merge_into** field is given the name of a package base
      which does not exist, a new error is displayed:
      "The package base you want to merge into does not exist."
    - If the **merge_into** field is given the name of the package
      base that a request is being created for, a new error is
      displayed: "You cannot merge a package base into itself."
- When an error is encountered, users are now brought back to
  the request form which they submitted and an error is displayed
  at the top of the page.
- If an invalid type is provided, users are returned to a BAD_REQUEST
  status rendering of the request form.

Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2021-09-13 17:26:25 -07:00
parent ad8369395e
commit 1c031638c6
No known key found for this signature in database
GPG key ID: F7E46DED420788F3
4 changed files with 247 additions and 1 deletions

View file

@ -1420,3 +1420,164 @@ def test_pkgbase_request(client: TestClient, user: User, package: Package):
with client as request:
resp = request.get(endpoint, cookies=cookies)
assert resp.status_code == int(HTTPStatus.OK)
def test_pkgbase_request_post_deletion(client: TestClient, user: User,
package: Package):
endpoint = f"/pkgbase/{package.PackageBase.Name}/request"
cookies = {"AURSID": user.login(Request(), "testPassword")}
with client as request:
resp = request.post(endpoint, data={
"type": "deletion",
"comments": "We want to delete this."
}, cookies=cookies, allow_redirects=False)
assert resp.status_code == int(HTTPStatus.SEE_OTHER)
pkgreq = db.query(PackageRequest).filter(
PackageRequest.PackageBaseID == package.PackageBase.ID
).first()
assert pkgreq is not None
assert pkgreq.RequestType.Name == "deletion"
assert pkgreq.PackageBaseName == package.PackageBase.Name
assert pkgreq.Comments == "We want to delete this."
def test_pkgbase_request_post_orphan(client: TestClient, user: User,
package: Package):
endpoint = f"/pkgbase/{package.PackageBase.Name}/request"
cookies = {"AURSID": user.login(Request(), "testPassword")}
with client as request:
resp = request.post(endpoint, data={
"type": "orphan",
"comments": "We want to disown this."
}, cookies=cookies, allow_redirects=False)
assert resp.status_code == int(HTTPStatus.SEE_OTHER)
pkgreq = db.query(PackageRequest).filter(
PackageRequest.PackageBaseID == package.PackageBase.ID
).first()
assert pkgreq is not None
assert pkgreq.RequestType.Name == "orphan"
assert pkgreq.PackageBaseName == package.PackageBase.Name
assert pkgreq.Comments == "We want to disown this."
def test_pkgbase_request_post_merge(client: TestClient, user: User,
package: Package):
with db.begin():
pkgbase2 = db.create(PackageBase, Name="new-pkgbase",
Submitter=user, Maintainer=user, Packager=user)
target = db.create(Package, PackageBase=pkgbase2,
Name=pkgbase2.Name, Version="1.0.0")
endpoint = f"/pkgbase/{package.PackageBase.Name}/request"
cookies = {"AURSID": user.login(Request(), "testPassword")}
with client as request:
resp = request.post(endpoint, data={
"type": "merge",
"merge_into": target.PackageBase.Name,
"comments": "We want to merge this."
}, cookies=cookies, allow_redirects=False)
assert resp.status_code == int(HTTPStatus.SEE_OTHER)
pkgreq = db.query(PackageRequest).filter(
PackageRequest.PackageBaseID == package.PackageBase.ID
).first()
assert pkgreq is not None
assert pkgreq.RequestType.Name == "merge"
assert pkgreq.PackageBaseName == package.PackageBase.Name
assert pkgreq.MergeBaseName == target.PackageBase.Name
assert pkgreq.Comments == "We want to merge this."
def test_pkgbase_request_post_not_found(client: TestClient, user: User):
cookies = {"AURSID": user.login(Request(), "testPassword")}
with client as request:
resp = request.post("/pkgbase/fake/request", data={
"type": "fake"
}, cookies=cookies)
assert resp.status_code == int(HTTPStatus.NOT_FOUND)
def test_pkgbase_request_post_invalid_type(client: TestClient,
user: User,
package: Package):
endpoint = f"/pkgbase/{package.PackageBase.Name}/request"
cookies = {"AURSID": user.login(Request(), "testPassword")}
with client as request:
resp = request.post(endpoint, data={"type": "fake"}, cookies=cookies)
assert resp.status_code == int(HTTPStatus.BAD_REQUEST)
def test_pkgbase_request_post_no_comment_error(client: TestClient,
user: User,
package: Package):
endpoint = f"/pkgbase/{package.PackageBase.Name}/request"
cookies = {"AURSID": user.login(Request(), "testPassword")}
with client as request:
resp = request.post(endpoint, data={
"type": "deletion",
"comments": "" # An empty comment field causes an error.
}, cookies=cookies)
assert resp.status_code == int(HTTPStatus.OK)
root = parse_root(resp.text)
error = root.xpath('//ul[@class="errorlist"]/li')[0]
expected = "The comment field must not be empty."
assert error.text.strip() == expected
def test_pkgbase_request_post_merge_not_found_error(client: TestClient,
user: User,
package: Package):
endpoint = f"/pkgbase/{package.PackageBase.Name}/request"
cookies = {"AURSID": user.login(Request(), "testPassword")}
with client as request:
resp = request.post(endpoint, data={
"type": "merge",
"merge_into": "fake", # There is no PackageBase.Name "fake"
"comments": "We want to merge this."
}, cookies=cookies, allow_redirects=False)
assert resp.status_code == int(HTTPStatus.OK)
root = parse_root(resp.text)
error = root.xpath('//ul[@class="errorlist"]/li')[0]
expected = "The package base you want to merge into does not exist."
assert error.text.strip() == expected
def test_pkgbase_request_post_merge_no_merge_into_error(client: TestClient,
user: User,
package: Package):
endpoint = f"/pkgbase/{package.PackageBase.Name}/request"
cookies = {"AURSID": user.login(Request(), "testPassword")}
with client as request:
resp = request.post(endpoint, data={
"type": "merge",
"merge_into": "", # There is no PackageBase.Name "fake"
"comments": "We want to merge this."
}, cookies=cookies, allow_redirects=False)
assert resp.status_code == int(HTTPStatus.OK)
root = parse_root(resp.text)
error = root.xpath('//ul[@class="errorlist"]/li')[0]
expected = 'The "Merge into" field must not be empty.'
assert error.text.strip() == expected
def test_pkgbase_request_post_merge_self_error(client: TestClient, user: User,
package: Package):
endpoint = f"/pkgbase/{package.PackageBase.Name}/request"
cookies = {"AURSID": user.login(Request(), "testPassword")}
with client as request:
resp = request.post(endpoint, data={
"type": "merge",
"merge_into": package.PackageBase.Name,
"comments": "We want to merge this."
}, cookies=cookies, allow_redirects=False)
assert resp.status_code == int(HTTPStatus.OK)
root = parse_root(resp.text)
error = root.xpath('//ul[@class="errorlist"]/li')[0]
expected = "You cannot merge a package base into itself."
assert error.text.strip() == expected