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')
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")
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')
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')
def setUp(self): self.view = DocumentServeView() self.request = RequestFactory().get("/")
"""If flag is set, serve page from Wagtail, otherwise raise 404.""" def this_view_always_raises_http404(request, *args, **kwargs): raise Http404('flag {} not set'.format(flag_name)) return flagged_url( flag_name, regex_path, lambda request: ServeView.as_view()(request, request.path), fallback=this_view_always_raises_http404, name=url_name, ) urlpatterns = [ url(r'^documents/(?P<document_id>\d+)/(?P<document_filename>.*)$', DocumentServeView.as_view(), name='wagtaildocs_serve'), url(r'^home/(?P<path>.*)$', RedirectView.as_view(url='/%(path)s', permanent=True)), url( r'^owning-a-home/static/(?P<path>.*)$', RedirectView.as_view(url='/static/owning-a-home/static/%(path)s', permanent=True)), url( r'^owning-a-home/resources/(?P<path>.*)$', RedirectView.as_view(url='/static/owning-a-home/resources/%(path)s', permanent=True)), url(r'^owning-a-home/closing-disclosure/$', TemplateView.as_view( template_name='owning-a-home/closing-disclosure/index.html'), name='closing-disclosure'),
change_password, check_permissions, login_with_lockout, password_reset_confirm, welcome) from v1.views.documents import DocumentServeView fin_ed = SheerSite('fin-ed-resources') oah = SheerSite('owning-a-home') es_conv_flag = partial(flagged_url, 'ES_CONV_FLAG') urlpatterns = [ url(r'^documents/(?P<document_id>\d+)/(?P<document_filename>.*)$', DocumentServeView.as_view(), name='wagtaildocs_serve'), # TODO: Enable search route when search is available. # url(r'^search/$', 'search.views.search', name='search'), url(r'^home/(?P<path>.*)$', RedirectView.as_view(url='/%(path)s', permanent=True)), url(r'^owning-a-home/static/(?P<path>.*)$', RedirectView.as_view( url='/static/owning-a-home/static/%(path)s', permanent=True)), url(r'^owning-a-home/resources/(?P<path>.*)$', RedirectView.as_view( url='/static/owning-a-home/resources/%(path)s', permanent=True)),
def setUp(self): self.view = DocumentServeView() self.request = Mock()
def setUp(self): self.view = DocumentServeView() self.request = Mock()
def setUp(self): self.view = DocumentServeView() self.request = RequestFactory().get('/')