2011-04-02 19:32:37 +04:00
|
|
|
from django.test import TestCase
|
|
|
|
from django import forms
|
|
|
|
from djangorestframework.compat import RequestFactory
|
2011-05-24 13:27:24 +04:00
|
|
|
from djangorestframework.views import View
|
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):
|
2011-06-12 23:23:40 +04:00
|
|
|
file = forms.FileField()
|
2011-05-12 15:55:13 +04:00
|
|
|
|
2011-05-24 13:27:24 +04:00
|
|
|
class MockView(View):
|
2011-04-25 04:03:23 +04:00
|
|
|
permissions = ()
|
2011-06-12 23:23:40 +04:00
|
|
|
form = FileForm
|
2011-04-02 19:32:37 +04:00
|
|
|
|
2011-04-11 20:13:11 +04:00
|
|
|
def post(self, request, *args, **kwargs):
|
2011-06-12 23:23:40 +04:00
|
|
|
return {'FILE_NAME': self.CONTENT['file'].name,
|
|
|
|
'FILE_CONTENT': self.CONTENT['file'].read()}
|
2011-12-13 17:48:33 +04:00
|
|
|
|
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"}')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|