Commit c2abe452 by zubair-arbi

ENT-960 Fix catalogs creation bug due to request data format

parent 31ac8f5c
...@@ -129,11 +129,10 @@ class CatalogViewSetTests(ElasticsearchTestMixin, SerializationMixin, OAuth2Mixi ...@@ -129,11 +129,10 @@ class CatalogViewSetTests(ElasticsearchTestMixin, SerializationMixin, OAuth2Mixi
data = { data = {
'name': 'Test Catalog', 'name': 'Test Catalog',
'query': '*:*', 'query': '*:*',
'viewers': ','.join(viewers) 'viewers': str(viewers),
} }
# NOTE: We explicitly avoid using the JSON data type so that we properly test string parsing. response = self.client.post(self.catalog_list_url, data, format='json')
response = self.client.post(self.catalog_list_url, data)
self.assertEqual(response.status_code, 201) self.assertEqual(response.status_code, 201)
catalog = Catalog.objects.latest() catalog = Catalog.objects.latest()
...@@ -145,10 +144,10 @@ class CatalogViewSetTests(ElasticsearchTestMixin, SerializationMixin, OAuth2Mixi ...@@ -145,10 +144,10 @@ class CatalogViewSetTests(ElasticsearchTestMixin, SerializationMixin, OAuth2Mixi
""" Verify no users are created if an error occurs while processing a create request. """ """ Verify no users are created if an error occurs while processing a create request. """
# The missing name and query fields should trigger an error # The missing name and query fields should trigger an error
data = { data = {
'viewers': ['new-guy'] 'viewers': str(['new-guy'])
} }
original_user_count = User.objects.count() original_user_count = User.objects.count()
response = self.client.post(self.catalog_list_url, data) response = self.client.post(self.catalog_list_url, data, format='json')
self.assertEqual(response.status_code, 400) self.assertEqual(response.status_code, 400)
self.assertEqual(User.objects.count(), original_user_count) self.assertEqual(User.objects.count(), original_user_count)
......
import ast
import datetime import datetime
from django.db import transaction from django.db import transaction
...@@ -32,13 +33,13 @@ class CatalogViewSet(viewsets.ModelViewSet): ...@@ -32,13 +33,13 @@ class CatalogViewSet(viewsets.ModelViewSet):
@transaction.atomic @transaction.atomic
def create(self, request, *args, **kwargs): def create(self, request, *args, **kwargs):
""" Create a new catalog. """ """ Create a new catalog. """
data = request.data.copy() data = dict(request.data.copy())
usernames = request.data.get('viewers', ()) usernames = request.data.get('viewers', ())
# Add support for parsing a comma-separated list from Swagger # Add support for parsing a comma-separated list from Swagger
if isinstance(usernames, str): if isinstance(usernames, str):
usernames = usernames.split(',') usernames = ast.literal_eval(usernames)
data.setlist('viewers', usernames) data['viewers'] = usernames
# Ensure the users exist # Ensure the users exist
for username in usernames: for username in usernames:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment