Rafał Pitoń 11 лет назад
Родитель
Сommit
9abfbde35e
2 измененных файлов с 59 добавлено и 10 удалено
  1. 16 10
      misago/core/fileserver.py
  2. 43 0
      misago/core/tests/test_fileserver.py

+ 16 - 10
misago/core/fileserver.py

@@ -10,22 +10,19 @@ SERVED_PATHS = (
 )
 
 
-def send_file(file_path, content_type, attachment=None):
+def make_file_response(file_path, content_type, attachment=None):
     file_size = os.path.getsize(file_path)
 
+    response_args = (file_path, content_type, file_size, attachment)
     if settings.MISAGO_SENDFILE_HEADER:
-        return send_header(file_path, content_type, file_size, attachment)
+        return make_header_response(*response_args)
     else:
-        return send_stream(file_path, content_type, file_size, attachment)
+        return make_stream_response(*response_args)
 
 
-def send_header(file_path, content_type, file_size, attachment=None):
+def make_header_response(file_path, content_type, file_size, attachment=None):
     if settings.MISAGO_SENDFILE_LOCATIONS_PATH:
-        for path in SERVED_PATHS:
-            if file_path.startswith(path):
-                file_path = file_path[len(path):]
-                file_path = '/%s' % settings.MISAGO_SENDFILE_LOCATIONS_PATH
-                break
+        file_path = rewrite_file_path(file_path)
 
     response = HttpResponse()
     response[settings.MISAGO_SENDFILE_HEADER] = file_path
@@ -34,7 +31,16 @@ def send_header(file_path, content_type, file_size, attachment=None):
     return response
 
 
-def send_stream(file_path, content_type, file_size, attachment=None):
+def rewrite_file_path(file_path):
+    for path in SERVED_PATHS:
+        if file_path.startswith(path):
+            suffix = file_path[len(path):]
+            return '/%s%s' % (settings.MISAGO_SENDFILE_LOCATIONS_PATH, suffix)
+    else:
+        raise ValueError("'%s' path is not supported" % file_path)
+
+
+def make_stream_response(file_path, content_type, file_size, attachment=None):
     response = StreamingHttpResponse(open(file_path, 'r'))
 
     response['Content-Type'] = content_type

+ 43 - 0
misago/core/tests/test_fileserver.py

@@ -0,0 +1,43 @@
+import os
+
+from django.conf import settings
+from django.http import HttpResponse, StreamingHttpResponse
+from django.test import TestCase
+
+from misago.core import fileserver
+
+
+class FileServerTests(TestCase):
+    def test_send_file(self):
+        """send file produces valid response"""
+        with self.settings(MISAGO_SENDFILE_HEADER='X-Test'):
+            response = fileserver.make_file_response(__file__, 'text/python')
+            self.assertTrue(isinstance(response, HttpResponse))
+            self.assertEqual(response['X-Test'], __file__)
+
+        with self.settings(MISAGO_SENDFILE_HEADER=''):
+            response = fileserver.make_file_response(__file__, 'text/python')
+            self.assertTrue(isinstance(response, StreamingHttpResponse))
+
+    def test_rewrite_file_path(self):
+        """file paths are rewritten"""
+        with self.settings(MISAGO_SENDFILE_LOCATIONS_PATH='mymisagopath'):
+            test_path = '%s/somefile.png' % settings.MISAGO_AVATAR_CACHE
+            rewritten_path = fileserver.rewrite_file_path(test_path)
+            self.assertTrue(rewritten_path.startswith('/mymisagopath/'))
+
+            with self.assertRaises(ValueError):
+                fileserver.rewrite_file_path('some/non_rewrriten/path.zip')
+
+    def test_send_header(self):
+        """call to header response function returns HttpResponse"""
+        with self.settings(MISAGO_SENDFILE_HEADER='X-Test'):
+            response = fileserver.make_header_response(__file__, 'text/python')
+            self.assertTrue(isinstance(response, HttpResponse))
+            self.assertEqual(response['X-Test'], __file__)
+
+
+    def test_send_stream(self):
+        """call to streaming response function returns StreamingHttpResponse"""
+        response = fileserver.make_stream_response(__file__, 'text/python')
+        self.assertTrue(isinstance(response, StreamingHttpResponse))