Skip to content

Commit

Permalink
Simplify API for now
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit Vermeulen committed Feb 15, 2024
1 parent 9882b9d commit 2729058
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 43 deletions.
10 changes: 5 additions & 5 deletions home/api.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@

from django.db.models.manager import BaseManager
from rest_framework.exceptions import ValidationError
from rest_framework.filters import SearchFilter
from rest_framework.pagination import PageNumberPagination
Expand Down Expand Up @@ -153,12 +155,10 @@ class OrderedContentSetViewSet(BaseAPIViewSet):
filter_backends = (SearchFilter,)

def get_queryset(self):
qa = self.request.query_params.get("qa")
#TODO: Filter using the qa Param
#qa = self.request.query_params.get("qa")

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


Expand Down
115 changes: 77 additions & 38 deletions home/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,12 +481,14 @@ def create_test_data(self):
with path.open(mode="rb") as f:
import_content(f, "CSV", queue.Queue())
self.page1 = ContentPage.objects.first()
# self.page1.save_revision().publish()
self.ordered_content_set = OrderedContentSet(name="Test set")
self.ordered_content_set.pages.append(("pages", {"contentpage": self.page1}))
self.ordered_content_set.profile_fields.append(("gender", "female"))
self.ordered_content_set.save()
# self.ordered_content_set.save_revision().publish()

self.ordered_content_set_timed = OrderedContentSet(name="Test set")
self.ordered_content_set_timed = OrderedContentSet(name="Test set timed")
self.ordered_content_set_timed.pages.append(
(
"pages",
Expand Down Expand Up @@ -610,24 +612,25 @@ 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",
}
# TODO: Add this when we add support for qa param
# 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):
"""
Expand All @@ -641,30 +644,31 @@ def test_orderedcontent_endpoint_without_drafts(self, uclient):

# 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["count"] == 2 #TODO: Change this when we add support for qa param
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_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",
}
# TODO: Add this when we add support for qa param
# 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):
"""
Expand All @@ -678,6 +682,41 @@ def test_orderedcontent_detail_endpoint_without_drafts(self, uclient):
# it redirects :TODO is it possible to resolve the redirect?
assert response.status_code == 301

def test_orderedcontent_new_draft(self, uclient):
"""
New revisions are returned if the qa param is set
"""
self.ordered_content_set.profile_fields.append(
("relationship", "in_a_relationship")
)
self.ordered_content_set.save_revision()

response = uclient.get("/api/v2/orderedcontent/")
content = json.loads(response.content)

assert self.ordered_content_set.live

assert content["count"] == 2
assert len(content["results"][0]["profile_fields"]) == 1
assert content["results"][0]["name"] == self.ordered_content_set.name
assert content["results"][0]["profile_fields"][0] == {
"profile_field": "gender",
"value": "female",
}

# TODO: When the qa param is introduced
# response = uclient.get("/api/v2/orderedcontent/?qa=True")
# content = json.loads(response.content)
# assert len(content["results"][0]["profile_fields"]) == 2
# assert content["results"][0]["profile_fields"][0] == {
# "profile_field": "gender",
# "value": "female",
# }
# assert content["results"][0]["profile_fields"][1] == {
# "profile_field": "relationship",
# "value": "in_a_relationship",
# }


@pytest.mark.django_db
class TestContentPageAPI2:
Expand Down

0 comments on commit 2729058

Please sign in to comment.