-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for downloading EntityList dataset (#2678)
* add support for downloading EntityList dataset * update docs * remove whitespace in docs * download EntityList on endpoint `/api/v2/entity-lists/<id>.csv * refactor code * refactor code
- Loading branch information
1 parent
87f43ec
commit 085b54e
Showing
3 changed files
with
171 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -505,6 +505,25 @@ def test_soft_deleted(self): | |
response = self.view(request, pk=self.entity_list.pk) | ||
self.assertEqual(response.status_code, 404) | ||
|
||
def test_render_csv(self): | ||
"""Render in CSV format""" | ||
request = self.factory.get("/", **self.extra) | ||
# Using `.csv` suffix | ||
response = self.view(request, pk=self.entity_list.pk, format="csv") | ||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual( | ||
response.get("Content-Disposition"), "attachment; filename=trees.csv" | ||
) | ||
self.assertEqual(response["Content-Type"], "application/csv") | ||
# Using `Accept` header | ||
request = self.factory.get("/", HTTP_ACCEPT="text/csv", **self.extra) | ||
response = self.view(request, pk=self.entity_list.pk) | ||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual( | ||
response.get("Content-Disposition"), "attachment; filename=trees.csv" | ||
) | ||
self.assertEqual(response["Content-Type"], "application/csv") | ||
|
||
|
||
class DeleteEntityListTestCase(TestAbstractViewSet): | ||
"""Tests for deleting a single EntityList""" | ||
|
@@ -1331,3 +1350,99 @@ def test_delete_via_kwarg_invalid(self): | |
request = self.factory.delete("/", **self.extra) | ||
response = self.view(request, pk=self.entity_list.pk, entity_pk=self.entity.pk) | ||
self.assertEqual(response.status_code, 405) | ||
|
||
|
||
class DownloadEntityListTestCase(TestAbstractViewSet): | ||
"""Tests for `download` action""" | ||
|
||
def setUp(self): | ||
super().setUp() | ||
|
||
self.view = EntityListViewSet.as_view({"get": "download"}) | ||
self.project = get_user_default_project(self.user) | ||
self.entity_list = EntityList.objects.create(name="trees", project=self.project) | ||
OwnerRole.add(self.user, self.entity_list) | ||
|
||
def test_download(self): | ||
"""EntityList dataset is downloaded""" | ||
request = self.factory.get("/", **self.extra) | ||
response = self.view(request, pk=self.entity_list.pk) | ||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual( | ||
response["Content-Disposition"], "attachment; filename=trees.csv" | ||
) | ||
self.assertEqual(response["Content-Type"], "application/csv") | ||
# Using `.csv` suffix | ||
request = self.factory.get("/", **self.extra) | ||
response = self.view(request, pk=self.entity_list.pk, format="csv") | ||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual( | ||
response["Content-Disposition"], "attachment; filename=trees.csv" | ||
) | ||
self.assertEqual(response["Content-Type"], "application/csv") | ||
# Using `Accept` header | ||
request = self.factory.get("/", HTTP_ACCEPT="text/csv", **self.extra) | ||
response = self.view(request, pk=self.entity_list.pk) | ||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual( | ||
response.get("Content-Disposition"), "attachment; filename=trees.csv" | ||
) | ||
self.assertEqual(response["Content-Type"], "application/csv") | ||
# Unsupported suffix | ||
request = self.factory.get("/", **self.extra) | ||
response = self.view(request, pk=self.entity_list.pk, format="json") | ||
self.assertEqual(response.status_code, 404) | ||
# Unsupported accept header | ||
request = self.factory.get("/", HTTP_ACCEPT="application/json", **self.extra) | ||
response = self.view(request, pk=self.entity_list.pk) | ||
self.assertEqual(response.status_code, 404) | ||
|
||
def test_anonymous_user(self): | ||
"""Anonymous user cannot download a private EntityList""" | ||
# Anonymous user cannot view private EntityList | ||
request = self.factory.get("/") | ||
response = self.view(request, pk=self.entity_list.pk) | ||
self.assertEqual(response.status_code, 404) | ||
# Anonymous user can view public EntityList | ||
self.project.shared = True | ||
self.project.save() | ||
request = self.factory.get("/") | ||
response = self.view(request, pk=self.entity_list.pk) | ||
self.assertEqual(response.status_code, 200) | ||
|
||
def test_invalid_entity_list(self): | ||
"""Invalid EntityList is handled""" | ||
request = self.factory.get("/", **self.extra) | ||
response = self.view(request, pk=sys.maxsize) | ||
self.assertEqual(response.status_code, 404) | ||
|
||
def test_object_permissions(self): | ||
"""User must have object view level permissions""" | ||
alice_data = { | ||
"username": "alice", | ||
"email": "[email protected]", | ||
"password1": "password12345", | ||
"password2": "password12345", | ||
"first_name": "Alice", | ||
"last_name": "Hughes", | ||
} | ||
alice_profile = self._create_user_profile(alice_data) | ||
extra = {"HTTP_AUTHORIZATION": f"Token {alice_profile.user.auth_token}"} | ||
|
||
for role in ROLES: | ||
ShareProject(self.project, "alice", role).save() | ||
request = self.factory.get("/", **extra) | ||
response = self.view(request, pk=self.entity_list.pk) | ||
|
||
if role in ["owner", "manager"]: | ||
self.assertEqual(response.status_code, 200) | ||
|
||
else: | ||
self.assertEqual(response.status_code, 404) | ||
|
||
def test_soft_deleted(self): | ||
"""Soft deleted dataset cannot be retrieved""" | ||
self.entity_list.soft_delete() | ||
request = self.factory.get("/", **self.extra) | ||
response = self.view(request, pk=self.entity_list.pk) | ||
self.assertEqual(response.status_code, 404) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters