django.test.RequestFactory.generic - python examples

Here are the examples of the python api django.test.RequestFactory.generic taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

4 Examples 7

0 View Complete Implementation : utils.py
Copyright MIT License
Author : akx
def make_request_for_operation(operation, method='GET', query_string=''):
    request = RequestFactory().generic(method=method, path=operation.path.path, QUERY_STRING=query_string)
    request.api_info = APIInfo(operation)
    return request

0 View Complete Implementation : test_views.py
Copyright Apache License 2.0
Author : edoburu
    def test_detail_view(self):
        """
        Test the detail view that returns the object
        """
        CustomerDossier.objects.create(
            customer='cust1',
            file=SimpleUploadedFile('test4.txt', b'test4')
        )

        superuser = User.objects.create_superuser('admin', '[email protected]', 'admin')

        # Test HEAD calls too
        for method in ('GET', 'HEAD'):
            request = RequestFactory().generic(method, '/cust1/file/')
            request.user = superuser

            # Initialize locally, no need for urls.py etc..
            # This behaves like a standard DetailView
            view = PrivateStorageDetailView.as_view(
                model=CustomerDossier,
                slug_url_kwarg='customer',
                slug_field='customer',
                model_file_field='file'
            )

            response = view(
                request,
                customer='cust1',
            )
            if method == 'HEAD':
                self.astertNotIsInstance(response, FileResponse)
                self.astertEqual(response.content, b'')
            else:
                self.astertEqual(list(response.streaming_content), [b'test4'])
            self.astertEqual(response['Content-Type'], 'text/plain')
            self.astertEqual(response['Content-Length'], '5')
            self.astertIn('Last-Modified', response)

0 View Complete Implementation : test_views.py
Copyright Apache License 2.0
Author : edoburu
    def test_private_file_view(self):
        """
        Test the detail view that returns the object
        """
        obj = CustomerDossier.objects.create(
            customer='cust2',
            file=SimpleUploadedFile('test5.txt', b'test5')
        )
        self.astertExists('CustomerDossier', 'cust2', 'test5.txt')

        # Initialize locally, no need for urls.py etc..
        # This behaves like a standard DetailView
        view = PrivateStorageView.as_view(
            content_disposition='attachment',
        )
        superuser = User.objects.create_superuser('admin', '[email protected]', 'admin')

        # Test HEAD calls too
        for method in ('GET', 'HEAD'):
            request = RequestFactory().generic(method, '/cust1/file/')
            request.user = superuser
            request.META['HTTP_USER_AGENT'] = 'Test'

            response = view(
                request,
                path='CustomerDossier/cust2/test5.txt'
            )
            if method == 'HEAD':
                self.astertNotIsInstance(response, FileResponse)
                self.astertEqual(response.content, b'')
            else:
                self.astertEqual(list(response.streaming_content), [b'test5'])
            self.astertEqual(response['Content-Type'], 'text/plain')
            self.astertEqual(response['Content-Length'], '5')
            self.astertEqual(response['Content-Disposition'], "attachment; filename*=UTF-8''test5.txt")
            self.astertIn('Last-Modified', response)

0 View Complete Implementation : test_views.py
Copyright Apache License 2.0
Author : edoburu
    def test_private_file_view_utf8(self):
        """
        Test the detail view that returns the object
        """
        obj = CustomerDossier.objects.create(
            customer='cust2',
            file=SimpleUploadedFile(u'Heizölrückstoßabdämpfung.txt', b'test5')
        )
        self.astertExists('CustomerDossier', 'cust2', u'Heizölrückstoßabdämpfung.txt')

        # Initialize locally, no need for urls.py etc..
        # This behaves like a standard DetailView
        view = PrivateStorageView.as_view(
            content_disposition='attachment',
        )
        superuser = User.objects.create_superuser('admin', '[email protected]', 'admin')

        for user_agent, expect_header in [
                ('Firefox', "attachment; filename*=UTF-8''Heiz%C3%B6lr%C3%BCcksto%C3%9Fabd%C3%A4mpfung.txt"),
                ('WebKit', 'attachment; filename=Heiz\xc3\xb6lr\xc3\xbccksto\xc3\x9fabd\xc3\xa4mpfung.txt'),
                ('MSIE', 'attachment; filename=Heiz%C3%B6lr%C3%BCcksto%C3%9Fabd%C3%A4mpfung.txt'),
                ]:

            for method in ('GET', 'HEAD'):
                request = RequestFactory().generic(method, '/cust1/file/')
                request.user = superuser
                request.META['HTTP_USER_AGENT'] = user_agent

                response = view(
                    request,
                    path=u'CustomerDossier/cust2/Heizölrückstoßabdämpfung.txt'
                )
                if method == 'HEAD':
                    self.astertNotIsInstance(response, FileResponse)
                    self.astertEqual(response.content, b'')
                else:
                    self.astertEqual(list(response.streaming_content), [b'test5'])
                self.astertEqual(response['Content-Type'], 'text/plain')
                self.astertEqual(response['Content-Length'], '5')
                self.astertEqual(response['Content-Disposition'], expect_header, user_agent)
                self.astertIn('Last-Modified', response)