def test_head_method_serializes_no_content(self): """No response must be included in HEAD requests.""" resp = self.client.head('/') self.assertEqual(resp.status_code, DUMMYSTATUS) self.assertEqual(resp['Content-Type'], RendererA.media_type + '; charset=utf-8') self.assertEqual(resp.content, six.b(''))
def test_standard_behaviour_determines_non_form_content_PUT(self): """ Ensure request.DATA returns content for PUT request with non-form content. """ content = six.b('qwerty') content_type = 'text/plain' request = Request(factory.put('/', content, content_type=content_type)) request.parsers = (PlainTextParser(), ) self.assertEqual(request.DATA, content)
def test_delete_instance_view(self): """ DELETE requests to RetrieveUpdateDestroyAPIView should delete an object. """ request = factory.delete('/1') response = self.view(request, pk=1).render() self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual(response.content, six.b('')) ids = [obj.id for obj in self.objects.all()] self.assertEqual(ids, [2, 3])
def test_delete_instance_view(self): """ DELETE requests to RetrieveUpdateDestroyAPIView should delete an object. """ request = factory.delete('/1') response = self.view(request, pk=1).render() self.assertEquals(response.status_code, status.HTTP_204_NO_CONTENT) self.assertEquals(response.content, six.b('')) ids = [obj.id for obj in self.objects.all()] self.assertEquals(ids, [2, 3])
def test_request_DATA_with_text_content(self): """ Ensure request.DATA returns content for POST request with non-form content. """ content = six.b('qwerty') content_type = 'text/plain' request = Request(factory.post('/', content, content_type=content_type)) request.parsers = (PlainTextParser(),) self.assertEqual(request.DATA, content)
def test_create(self): now = datetime.datetime.now() file = BytesIO(six.b('stuff')) file.name = 'stuff.txt' file.size = len(file.getvalue()) serializer = UploadedFileSerializer(data={'created': now}, files={'file': file}) uploaded_file = UploadedFile(file=file, created=now) self.assertTrue(serializer.is_valid()) self.assertEquals(serializer.object.created, uploaded_file.created) self.assertEquals(serializer.object.file, uploaded_file.file) self.assertFalse(serializer.object is uploaded_file)
def test_create(self): now = datetime.datetime.now() file = BytesIO(six.b("stuff")) file.name = "stuff.txt" file.size = len(file.getvalue()) serializer = UploadedFileSerializer(data={"created": now}, files={"file": file}) uploaded_file = UploadedFile(file=file, created=now) self.assertTrue(serializer.is_valid()) self.assertEqual(serializer.object.created, uploaded_file.created) self.assertEqual(serializer.object.file, uploaded_file.file) self.assertFalse(serializer.object is uploaded_file)
def test_validation_with_no_data(self): """ Validation should still function when no data dictionary is provided. """ now = datetime.datetime.now() file = BytesIO(six.b('stuff')) file.name = 'stuff.txt' file.size = len(file.getvalue()) uploaded_file = UploadedFile(file=file, created=now) serializer = UploadedFileSerializer(files={'file': file}) self.assertFalse(serializer.is_valid())
def test_remove_with_empty_string(self): """ Passing empty string as data should cause file to be removed Test for: https://github.com/tomchristie/django-rest-framework/issues/937 """ now = datetime.datetime.now() file = BytesIO(six.b('stuff')) file.name = 'stuff.txt' file.size = len(file.getvalue()) uploaded_file = UploadedFile(file=file, created=now) serializer = UploadedFileSerializer(instance=uploaded_file, data={'created': now, 'file': ''}) self.assertTrue(serializer.is_valid()) self.assertEqual(serializer.object.created, uploaded_file.created) self.assertIsNone(serializer.object.file)
def test_remove_with_empty_string(self): """ Passing empty string as data should cause file to be removed Test for: https://github.com/tomchristie/django-rest-framework/issues/937 """ now = datetime.datetime.now() file = BytesIO(six.b('stuff')) file.name = 'stuff.txt' file.size = len(file.getvalue()) uploaded_file = UploadedFile(file=file, created=now) serializer = UploadedFileSerializer(instance=uploaded_file, data={ 'created': now, 'file': '' }) self.assertTrue(serializer.is_valid()) self.assertEqual(serializer.object.created, uploaded_file.created) self.assertIsNone(serializer.object.file)
def test_permission_denied_html_view(self): response = self.client.get('/permission_denied') self.assertEquals(response.status_code, 403) self.assertEquals(response.content, six.b("403 Forbidden")) self.assertEquals(response['Content-Type'], 'text/html')
def test_permission_denied_html_view(self): response = self.client.get('/permission_denied') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) self.assertEqual(response.content, six.b("403 Forbidden")) self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8')
def test_not_found_html_view(self): response = self.client.get('/not_found') self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) self.assertEqual(response.content, six.b("404 Not Found")) self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8')
def test_not_found_html_view_with_template(self): response = self.client.get('/not_found') self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) self.assertEqual(response.content, six.b("404: Not found")) self.assertEqual(response['Content-Type'], 'text/html')
def test_permission_denied_html_view_with_template(self): response = self.client.get('/permission_denied') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) self.assertEqual(response.content, six.b("403: Permission denied")) self.assertEqual(response['Content-Type'], 'text/html')
def test_not_found_html_view(self): response = self.client.get('/not_found') self.assertEquals(response.status_code, 404) self.assertEquals(response.content, six.b("404 Not Found")) self.assertEquals(response['Content-Type'], 'text/html')