2011-04-02 19:32:37 +04:00
|
|
|
from django.test import TestCase
|
|
|
|
from django import forms
|
|
|
|
from djangorestframework.compat import RequestFactory
|
2011-05-04 12:21:17 +04:00
|
|
|
from djangorestframework.views import BaseView
|
2011-05-12 18:11:14 +04:00
|
|
|
from djangorestframework.resources import FormResource
|
2011-04-02 19:32:37 +04:00
|
|
|
import StringIO
|
|
|
|
|
|
|
|
class UploadFilesTests(TestCase):
|
|
|
|
"""Check uploading of files"""
|
|
|
|
def setUp(self):
|
|
|
|
self.factory = RequestFactory()
|
|
|
|
|
|
|
|
def test_upload_file(self):
|
|
|
|
|
|
|
|
|
|
|
|
class FileForm(forms.Form):
|
|
|
|
file = forms.FileField
|
|
|
|
|
2011-05-12 15:55:13 +04:00
|
|
|
class MockResource(FormResource):
|
|
|
|
form = FileForm
|
|
|
|
|
2011-05-04 12:21:17 +04:00
|
|
|
class MockView(BaseView):
|
2011-04-25 04:03:23 +04:00
|
|
|
permissions = ()
|
2011-05-12 15:55:13 +04:00
|
|
|
resource = MockResource
|
2011-04-02 19:32:37 +04:00
|
|
|
|
2011-04-11 20:13:11 +04:00
|
|
|
def post(self, request, *args, **kwargs):
|
2011-05-12 19:03:14 +04:00
|
|
|
return {'FILE_NAME': self.CONTENT['file'][0].name,
|
|
|
|
'FILE_CONTENT': self.CONTENT['file'][0].read()}
|
2011-04-02 19:32:37 +04:00
|
|
|
|
|
|
|
file = StringIO.StringIO('stuff')
|
|
|
|
file.name = 'stuff.txt'
|
|
|
|
request = self.factory.post('/', {'file': file})
|
2011-05-04 12:21:17 +04:00
|
|
|
view = MockView.as_view()
|
2011-04-02 19:32:37 +04:00
|
|
|
response = view(request)
|
|
|
|
self.assertEquals(response.content, '{"FILE_CONTENT": "stuff", "FILE_NAME": "stuff.txt"}')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|