From a1a88ea8729f4eafee396197d40fa8a290716bfa Mon Sep 17 00:00:00 2001 From: Kevin Morris Date: Tue, 8 Mar 2022 19:00:19 -0800 Subject: [PATCH] fix(rpc): suggestions should only suggest based on % Previously, Python code was looking for suggestions based on `%%`. This was inconsistent with PHP's suggestion implementation and cause more records to be bundled with a suggestion, along with supplying misleading suggestions. Closes #343 Signed-off-by: Kevin Morris --- aurweb/rpc.py | 5 +++-- test/test_rpc.py | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/aurweb/rpc.py b/aurweb/rpc.py index 70d8c2fd..5bc6b80d 100644 --- a/aurweb/rpc.py +++ b/aurweb/rpc.py @@ -332,7 +332,7 @@ class RPC: models.PackageBase ).filter( and_(models.PackageBase.PackagerUID.isnot(None), - models.Package.Name.like(f"%{arg}%")) + models.Package.Name.like(f"{arg}%")) ).order_by(models.Package.Name.asc()).limit(20) return [pkg.Name for pkg in packages] @@ -341,9 +341,10 @@ class RPC: if not args: return [] + arg = args[0] packages = db.query(models.PackageBase.Name).filter( and_(models.PackageBase.PackagerUID.isnot(None), - models.PackageBase.Name.like(f"%{args[0]}%")) + models.PackageBase.Name.like(f"{arg}%")) ).order_by(models.PackageBase.Name.asc()).limit(20) return [pkg.Name for pkg in packages] diff --git a/test/test_rpc.py b/test/test_rpc.py index 0d6b2931..2f7f7860 100644 --- a/test/test_rpc.py +++ b/test/test_rpc.py @@ -551,6 +551,14 @@ def test_rpc_suggest_pkgbase(client: TestClient, packages: List[Package]): data = response.json() assert data == [] + # Test that suggestions are only given based on the beginning + # of the keyword string. + params["arg"] = "ther-pkg" + with client as request: + response = request.get("/rpc", params=params) + data = response.json() + assert data == [] + def test_rpc_suggest(client: TestClient, packages: List[Package]): params = {"v": 5, "type": "suggest", "arg": "other"} @@ -573,6 +581,14 @@ def test_rpc_suggest(client: TestClient, packages: List[Package]): data = response.json() assert data == [] + # Test that suggestions are only given based on the beginning + # of the keyword string. + params["arg"] = "ther-pkg" + with client as request: + response = request.get("/rpc", params=params) + data = response.json() + assert data == [] + def mock_config_getint(section: str, key: str): if key == "request_limit":