Skip to content

Commit

Permalink
Filter draft content sets from api
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit Vermeulen committed Feb 7, 2024
1 parent e7f01f3 commit b634ce8
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
11 changes: 10 additions & 1 deletion home/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,20 @@ class OrderedContentSetViewSet(BaseAPIViewSet):
"name",
"profile_fields",
]
known_query_parameters = BaseAPIViewSet.known_query_parameters.union(["page"])
known_query_parameters = BaseAPIViewSet.known_query_parameters.union(["page", "qa"])
pagination_class = PageNumberPagination
search_fields = ["name", "profile_fields"]
filter_backends = (SearchFilter,)

def get_queryset(self):
qa = self.request.query_params.get("qa")

if qa:
queryset = OrderedContentSet.objects.all()
else:
queryset = OrderedContentSet.objects.filter(live=True)
return queryset


api_router = WagtailAPIRouter("wagtailapi")

Expand Down
66 changes: 66 additions & 0 deletions home/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,72 @@ def test_orderedcontent_detail_endpoint_tags_flag(self, uclient):
}
assert content["pages"][0]["tags"] == [t.name for t in self.page1.tags.all()]

def test_orderedcontent_endpoint_with_drafts(self, uclient):
"""
Unpublished ordered content sets are returned if the qa param is set.
"""
self.ordered_content_set.unpublish()
url = "/api/v2/orderedcontent/?qa=True"
# it should return a list of ordered content sets with the unpublished one included
response = uclient.get(url)
content = json.loads(response.content)

# the content set is not live but content is returned
assert not self.ordered_content_set.live
assert content["count"] == 2
assert content["results"][0]["name"] == self.ordered_content_set.name
assert content["results"][0]["profile_fields"][0] == {
"profile_field": "gender",
"value": "female",
}
def test_orderedcontent_endpoint_without_drafts(self, uclient):
"""
Unpublished ordered content sets are not returned if the qa param is not set.
"""
self.ordered_content_set.unpublish()
url = "/api/v2/orderedcontent/"
# it should return a list of ordered content sets with the unpublished one excluded
response = uclient.get(url)
content = json.loads(response.content)

# the content set is not live but content is returned
assert not self.ordered_content_set.live
assert content["count"] == 1
assert content["results"][0]["name"] == self.ordered_content_set_timed.name
assert content["results"][0]["profile_fields"][0] == {
"profile_field": "gender",
"value": "female",
}

def test_orderedcontent_detail_endpoint_with_drafts(self, uclient):
"""
Unpublished ordered content sets are returned if the qa param is set.
"""
self.ordered_content_set.unpublish()
url = f"/api/v2/orderedcontent/{self.ordered_content_set.id}/?qa=True"
# it should return specific ordered content set that is in draft
response = uclient.get(url)
content = json.loads(response.content)

# the content set is not live but content is returned
assert not self.ordered_content_set.live
assert content["name"] == self.ordered_content_set.name
assert content["profile_fields"][0] == {
"profile_field": "gender",
"value": "female",
}

def test_orderedcontent_detail_endpoint_without_drafts(self, uclient):
"""
Unpublished ordered content sets are not returned if the qa param is not set.
"""
self.ordered_content_set.unpublish()
url = f"/api/v2/orderedcontent/{self.ordered_content_set.id}"
# it should return nothing
response = uclient.get(url)

# it redirects :TODO is it possible to resolve the redirect?
assert response.status_code == 301

@pytest.mark.django_db
class TestContentPageAPI2:
Expand Down

0 comments on commit b634ce8

Please sign in to comment.