From dfa4b04f5569359b7b4bbe36ea4bec5cfabf3851 Mon Sep 17 00:00:00 2001 From: beatrice-acasandrei <69891317+beatrice-acasandrei@users.noreply.github.com> Date: Fri, 15 Nov 2024 12:09:13 +0200 Subject: [PATCH] Bug 1902029 - Search input criteria of 12-40 characters is too restrictive (#8258) * Add new field author_contains to PushViewSet * Add iexact to the existing author param * Add test coverage --- tests/webapp/api/test_push_api.py | 65 +++++++++++++++++++++++++++++++ treeherder/webapp/api/push.py | 8 +++- 2 files changed, 71 insertions(+), 2 deletions(-) diff --git a/tests/webapp/api/test_push_api.py b/tests/webapp/api/test_push_api.py index ea2b41d04e2..5b82f0b8b76 100644 --- a/tests/webapp/api/test_push_api.py +++ b/tests/webapp/api/test_push_api.py @@ -281,6 +281,16 @@ def test_push_author(client, test_repository): assert len(results) == 2 # would have 3 if filter not applied assert set([result["id"] for result in results]) == set([1, 2]) + # iexact looks for a case-insensitive match + resp = client.get( + reverse("push-list", kwargs={"project": test_repository.name}) + "?author=FoO@bar.com" + ) + assert resp.status_code == 200 + + results = resp.json()["results"] + assert len(results) == 2 # would have 3 if filter not applied + assert set([result["id"] for result in results]) == set([1, 2]) + resp = client.get( reverse("push-list", kwargs={"project": test_repository.name}) + "?author=foo2@bar.com" ) @@ -299,6 +309,61 @@ def test_push_author(client, test_repository): assert len(results) == 2 # would have 3 if filter not applied assert set([result["id"] for result in results]) == set([1, 2]) + # iexact looks for a case-insensitive match + resp = client.get( + reverse("push-list", kwargs={"project": test_repository.name}) + "?author=-FOo2@bar.com" + ) + assert resp.status_code == 200 + + results = resp.json()["results"] + assert len(results) == 2 # would have 3 if filter not applied + assert set([result["id"] for result in results]) == set([1, 2]) + + +def test_push_author_contains(client, test_repository): + """ + test the author parameter + """ + for revision, author in [ + ("1234abcd", "foo@bar.com"), + ("2234abcd", "foo2@bar.com"), + ("3234abcd", "qux@bar.com"), + ]: + Push.objects.create( + repository=test_repository, + revision=revision, + author=author, + time=datetime.datetime.now(), + ) + + # icontains - case-insensitive containment test + resp = client.get( + reverse("push-list", kwargs={"project": test_repository.name}) + "?author_contains=fOo" + ) + assert resp.status_code == 200 + + results = resp.json()["results"] + assert len(results) == 2 + assert set([result["id"] for result in results]) == set([1, 2]) + + resp = client.get( + reverse("push-list", kwargs={"project": test_repository.name}) + "?author_contains=foO2" + ) + assert resp.status_code == 200 + + results = resp.json()["results"] + assert len(results) == 1 + assert results[0]["id"] == 2 + + resp = client.get( + reverse("push-list", kwargs={"project": test_repository.name}) + "?author_contains=qux" + ) + assert resp.status_code == 200 + + results = resp.json()["results"] + assert len(results) == 1 + assert results[0]["id"] == 3 + def test_push_reviewbot(client, test_repository): """ diff --git a/treeherder/webapp/api/push.py b/treeherder/webapp/api/push.py index 78b4ca1c236..f523c33f5b1 100644 --- a/treeherder/webapp/api/push.py +++ b/treeherder/webapp/api/push.py @@ -155,9 +155,13 @@ def list(self, request, project): if author: if author.startswith("-"): author = author[1::] - pushes = pushes.exclude(author=author) + pushes = pushes.exclude(author__iexact=author) else: - pushes = pushes.filter(author=author) + pushes = pushes.filter(author__iexact=author) + + author_contains = filter_params.get("author_contains") + if author_contains: + pushes = pushes.filter(author__icontains=author_contains) if filter_params.get("hide_reviewbot_pushes") == "true": pushes = pushes.exclude(author="reviewbot")