Exemplo n.º 1
0
class ServeViewTestCase(TestCase):
    def setUp(self):
        self.view = DocumentServeView()
        self.request = RequestFactory().get("/")

    def test_local_document_returns_file_contents(self):
        doc = create_document("test.txt")
        response = self.view.get(self.request, doc.pk, doc.filename)
        self.assertEqual(response.status_code, 200)
        self.assertIsInstance(response, StreamingHttpResponse)
        self.assertEqual(b"".join(response.streaming_content), b"Test content")

    @override_settings(DEFAULT_FILE_STORAGE=(
        "wagtail.tests.dummy_external_storage.DummyExternalStorage"))
    def test_external_document_uses_redirect(self):
        doc = create_document("test.txt")
        response = self.view.get(self.request, doc.pk, doc.filename)
        self.assertEqual(response.status_code, 302)

        # The redirect URL may or may not alter the original filename
        # depending on what else is stored in the dummy storage. If a file
        # named test.txt already exists, the filename will get 7 random
        # alphanumeric characters appended. See Django docs:
        # https://docs.djangoproject.com/en/stable/howto/custom-file-storage/#django.core.files.storage.get_available_name
        self.assertRegexpMatches(response["Location"],
                                 "/test(_[a-zA-Z0-9]{7})?.txt$")

    def test_missing_document_returns_404(self):
        with self.assertRaises(Http404):
            self.view.get(self.request, 9999, "missing.txt")
Exemplo n.º 2
0
class ServeViewTestCase(TestCase):
    def setUp(self):
        self.view = DocumentServeView()
        self.request = RequestFactory().get('/')

    def test_local_document_returns_file_contents(self):
        doc = create_document('test.txt')
        response = self.view.get(self.request, doc.pk, doc.filename)
        self.assertEqual(response.status_code, 200)
        self.assertIsInstance(response, StreamingHttpResponse)
        self.assertEqual(b''.join(response.streaming_content), b'Test content')

    @override_settings(DEFAULT_FILE_STORAGE=(
        'wagtail.tests.dummy_external_storage.DummyExternalStorage'
    ))
    def test_external_document_uses_redirect(self):
        doc = create_document('test.txt')
        response = self.view.get(self.request, doc.pk, doc.filename)
        self.assertEqual(response.status_code, 302)

        # The redirect URL may or may not alter the original filename
        # depending on what else is stored in the dummy storage. If a file
        # named test.txt already exists, the filename will get 7 random
        # alphanumeric characters appended. See Django docs:
        # https://docs.djangoproject.com/en/2.1/howto/custom-file-storage/#django.core.files.storage.get_available_name
        self.assertRegexpMatches(
            response['Location'],
            '/test(_[a-zA-Z0-9]{7})?.txt$'
        )

    def test_missing_document_returns_404(self):
        with self.assertRaises(Http404):
            self.view.get(self.request, 9999, 'missing.txt')
Exemplo n.º 3
0
class ServeViewTestCase(TestCase):
    def setUp(self):
        self.view = DocumentServeView()
        self.request = Mock()

    def test_local_document_uses_wagtail_serve(self):
        filename = 'test.txt'
        doc = create_document(filename)

        with patch('v1.views.documents.wagtail_serve') as p:
            self.view.get(self.request, doc.pk, filename)
            p.assert_called_once_with(self.request, doc.pk, filename)

    @override_settings(
        AWS_QUERYSTRING_AUTH=False,
        AWS_STORAGE_BUCKET_NAME='test_s3_bucket',
        AWS_S3_CALLING_FORMAT='boto.s3.connection.OrdinaryCallingFormat',
        AWS_S3_SECURE_URLS=True,
        DEFAULT_FILE_STORAGE='v1.s3utils.MediaRootS3BotoStorage',
        MEDIA_URL='https://test.s3.amazonaws.com/f/',
        AWS_S3_ACCESS_KEY_ID='test-access-key',
        AWS_S3_SECRET_ACCESS_KEY='test-secret-access-key'
    )
    def test_document_serve_view_s3(self):
        filename = 'test.txt'
        with patch(
            'storages.backends.s3boto.S3BotoStorage._save',
            return_value=filename
        ):
            doc = create_document(filename)

        response = self.view.get(self.request, doc.pk, filename)
        self.assertEqual(response.status_code, 302)
        self.assertEqual(
            response['Location'],
            'https://s3.amazonaws.com/test_s3_bucket/f/test.txt'
        )

    def test_missing_document_returns_404(self):
        with self.assertRaises(Http404):
            self.view.get(self.request, 9999, 'missing.txt')
Exemplo n.º 4
0
class ServeViewTestCase(TestCase):
    def setUp(self):
        self.view = DocumentServeView()
        self.request = Mock()

    def test_local_document_uses_wagtail_serve(self):
        filename = 'test.txt'
        doc = create_document(filename)

        with patch('v1.views.documents.wagtail_serve') as p:
            self.view.get(self.request, doc.pk, filename)
            p.assert_called_once_with(self.request, doc.pk, filename)

    @override_settings(
        AWS_QUERYSTRING_AUTH=False,
        AWS_STORAGE_BUCKET_NAME='test_s3_bucket',
        AWS_S3_CALLING_FORMAT='boto.s3.connection.OrdinaryCallingFormat',
        AWS_S3_SECURE_URLS=True,
        DEFAULT_FILE_STORAGE='v1.s3utils.MediaRootS3BotoStorage',
        MEDIA_URL='https://test.s3.amazonaws.com/f/',
        AWS_S3_ACCESS_KEY_ID='test-access-key',
        AWS_S3_SECRET_ACCESS_KEY='test-secret-access-key')
    def test_document_serve_view_s3(self):
        filename = 'test.txt'
        with patch('storages.backends.s3boto.S3BotoStorage._save',
                   return_value=filename):
            doc = create_document(filename)

        response = self.view.get(self.request, doc.pk, filename)
        self.assertEqual(response.status_code, 302)
        self.assertEqual(response['Location'],
                         'https://s3.amazonaws.com/test_s3_bucket/f/test.txt')

    def test_missing_document_returns_404(self):
        with self.assertRaises(Http404):
            self.view.get(self.request, 9999, 'missing.txt')