aboutsummaryrefslogtreecommitdiffstats
path: root/djangorestframework/tests/files.py
blob: 25aad9b495aa48e96f7191027c7d96b80a5c3ab2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from django.test import TestCase
from django import forms
from djangorestframework.compat import RequestFactory
from djangorestframework.views import View
from djangorestframework.resources import FormResource
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

        class MockResource(FormResource):
            form = FileForm

        class MockView(View):
            permissions = ()
            resource = MockResource

            def post(self, request, *args, **kwargs):
                return {'FILE_NAME': self.CONTENT['file'][0].name,
                        'FILE_CONTENT': self.CONTENT['file'][0].read()}
                
        file = StringIO.StringIO('stuff')
        file.name = 'stuff.txt'
        request = self.factory.post('/', {'file': file})
        view = MockView.as_view()
        response = view(request)
        self.assertEquals(response.content, '{"FILE_CONTENT": "stuff", "FILE_NAME": "stuff.txt"}')