api: adjust search for files paths to be partial

This makes it consistent with the partial search available when
searching playbook paths.

Change-Id: I2e4951a91b7ef39044b0c5f1b47d9cb8ad5858aa
This commit is contained in:
David Moreau Simard 2020-04-23 10:02:29 -04:00
parent 7a192ac1b7
commit df41b04b7a
No known key found for this signature in database
GPG Key ID: 938880DAFC753E80
2 changed files with 15 additions and 1 deletions

View File

@ -164,7 +164,7 @@ class ResultFilter(DateFilter):
class FileFilter(BaseFilter):
playbook = django_filters.NumberFilter(field_name="playbook__id", lookup_expr="exact")
path = django_filters.CharFilter(field_name="path", lookup_expr="exact")
path = django_filters.CharFilter(field_name="path", lookup_expr="icontains")
# fmt: off
order = django_filters.OrderingFilter(

View File

@ -164,3 +164,17 @@ class FileTestCase(APITestCase):
for field in order_fields:
request = self.client.get("/api/v1/files?order=-%s" % field)
self.assertEqual(request.data["results"][0]["id"], second_file.id)
def test_get_file_by_path(self):
# Create two files with similar paths
first_file = factories.FileFactory(path="/root/file.yaml")
factories.FileFactory(path="/root/some/path/file.yaml")
# Exact search should match one
request = self.client.get("/api/v1/files?path=/root/file.yaml")
self.assertEqual(1, len(request.data["results"]))
self.assertEqual(first_file.path, request.data["results"][0]["path"])
# Partial match should match both files
request = self.client.get("/api/v1/files?path=file.yaml")
self.assertEqual(2, len(request.data["results"]))