| 1 |
"""Test suite around :mod:`django_downloadview.api` and deprecation plan.""" |
| 2 |
|
| 3 |
from importlib import import_module, reload |
| 4 |
import unittest |
| 5 |
import warnings |
| 6 |
|
| 7 |
from django.core.exceptions import ImproperlyConfigured |
| 8 |
import django.test |
| 9 |
from django.test.utils import override_settings |
| 10 |
|
| 11 |
|
| 12 |
class APITestCase(unittest.TestCase): |
| 13 |
"""Make sure django_downloadview exposes API.""" |
| 14 |
|
| 15 |
def assert_module_attributes(self, module_path, attribute_names): |
| 16 |
"""Assert imported ``module_path`` has ``attribute_names``.""" |
| 17 |
module = import_module(module_path) |
| 18 |
missing_attributes = [] |
| 19 |
for attribute_name in attribute_names: |
| 20 |
if not hasattr(module, attribute_name): |
| 21 |
missing_attributes.append(attribute_name) |
| 22 |
if missing_attributes: |
| 23 |
self.fail( |
| 24 |
'Missing attributes in "{module_path}": {", ".join(missing_attributes)}' |
| 25 |
) |
| 26 |
|
| 27 |
def test_root_attributes(self): |
| 28 |
"""API is exposed in django_downloadview root package. |
| 29 |
|
| 30 |
The goal of this test is to make sure that main items of project's API |
| 31 |
are easy to import... and prevent refactoring from breaking main API. |
| 32 |
|
| 33 |
If this test is broken by refactoring, a :class:`DeprecationWarning` or |
| 34 |
simimar should be raised. |
| 35 |
|
| 36 |
""" |
| 37 |
api = [ |
| 38 |
# Views: |
| 39 |
"ObjectDownloadView", |
| 40 |
"StorageDownloadView", |
| 41 |
"PathDownloadView", |
| 42 |
"HTTPDownloadView", |
| 43 |
"VirtualDownloadView", |
| 44 |
"BaseDownloadView", |
| 45 |
"DownloadMixin", |
| 46 |
# File wrappers: |
| 47 |
"StorageFile", |
| 48 |
"HTTPFile", |
| 49 |
"VirtualFile", |
| 50 |
# Responses: |
| 51 |
"DownloadResponse", |
| 52 |
"ProxiedDownloadResponse", |
| 53 |
# Middlewares: |
| 54 |
"BaseDownloadMiddleware", |
| 55 |
"DownloadDispatcherMiddleware", |
| 56 |
"SmartDownloadMiddleware", |
| 57 |
# Testing: |
| 58 |
"assert_download_response", |
| 59 |
"setup_view", |
| 60 |
"temporary_media_root", |
| 61 |
# Utilities: |
| 62 |
"StringIteratorIO", |
| 63 |
"sendfile", |
| 64 |
] |
| 65 |
self.assert_module_attributes("django_downloadview", api) |
| 66 |
|
| 67 |
def test_nginx_attributes(self): |
| 68 |
"""Nginx-related API is exposed in django_downloadview.nginx.""" |
| 69 |
api = [ |
| 70 |
"XAccelRedirectResponse", |
| 71 |
"XAccelRedirectMiddleware", |
| 72 |
"x_accel_redirect", |
| 73 |
"assert_x_accel_redirect", |
| 74 |
] |
| 75 |
self.assert_module_attributes("django_downloadview.nginx", api) |
| 76 |
|
| 77 |
def test_apache_attributes(self): |
| 78 |
"""Apache-related API is exposed in django_downloadview.apache.""" |
| 79 |
api = [ |
| 80 |
"XSendfileResponse", |
| 81 |
"XSendfileMiddleware", |
| 82 |
"x_sendfile", |
| 83 |
"assert_x_sendfile", |
| 84 |
] |
| 85 |
self.assert_module_attributes("django_downloadview.apache", api) |
| 86 |
|
| 87 |
def test_lighttpd_attributes(self): |
| 88 |
"""Lighttpd-related API is exposed in django_downloadview.lighttpd.""" |
| 89 |
api = [ |
| 90 |
"XSendfileResponse", |
| 91 |
"XSendfileMiddleware", |
| 92 |
"x_sendfile", |
| 93 |
"assert_x_sendfile", |
| 94 |
] |
| 95 |
self.assert_module_attributes("django_downloadview.lighttpd", api) |
| 96 |
|
| 97 |
|
| 98 |
class DeprecatedAPITestCase(django.test.SimpleTestCase): |
| 99 |
"""Make sure using deprecated items raise DeprecationWarning.""" |
| 100 |
|
| 101 |
def test_nginx_x_accel_redirect_middleware(self): |
| 102 |
"XAccelRedirectMiddleware in settings triggers ImproperlyConfigured." |
| 103 |
with override_settings( |
| 104 |
MIDDLEWARE=["django_downloadview.nginx.XAccelRedirectMiddleware"], |
| 105 |
): |
| 106 |
with self.assertRaises(ImproperlyConfigured): |
| 107 |
import django_downloadview.nginx.settings |
| 108 |
|
| 109 |
reload(django_downloadview.nginx.settings) |
| 110 |
|
| 111 |
def test_nginx_x_accel_redirect_global_settings(self): |
| 112 |
"""Global settings for Nginx middleware are deprecated.""" |
| 113 |
settings_overrides = { |
| 114 |
"NGINX_DOWNLOAD_MIDDLEWARE_WITH_BUFFERING": True, |
| 115 |
"NGINX_DOWNLOAD_MIDDLEWARE_LIMIT_RATE": 32, |
| 116 |
"NGINX_DOWNLOAD_MIDDLEWARE_EXPIRES": 3600, |
| 117 |
"NGINX_DOWNLOAD_MIDDLEWARE_MEDIA_ROOT": "/", |
| 118 |
"NGINX_DOWNLOAD_MIDDLEWARE_SOURCE_DIR": "/", |
| 119 |
"NGINX_DOWNLOAD_MIDDLEWARE_SOURCE_URL": "/", |
| 120 |
"NGINX_DOWNLOAD_MIDDLEWARE_MEDIA_URL": "/", |
| 121 |
"NGINX_DOWNLOAD_MIDDLEWARE_DESTINATION_URL": "/", |
| 122 |
} |
| 123 |
import django_downloadview.nginx.settings |
| 124 |
|
| 125 |
missed_warnings = [] |
| 126 |
for setting_name, setting_value in settings_overrides.items(): |
| 127 |
warnings.resetwarnings() |
| 128 |
warnings.simplefilter("always") |
| 129 |
with warnings.catch_warnings(record=True) as warning_list: |
| 130 |
with override_settings(**{setting_name: setting_value}): |
| 131 |
reload(django_downloadview.nginx.settings) |
| 132 |
caught = False |
| 133 |
for warning_item in warning_list: |
| 134 |
if warning_item.category is DeprecationWarning: |
| 135 |
if "deprecated" in str(warning_item.message): |
| 136 |
if setting_name in str(warning_item.message): |
| 137 |
caught = True |
| 138 |
break |
| 139 |
if not caught: |
| 140 |
missed_warnings.append(setting_name) |
| 141 |
if missed_warnings: |
| 142 |
self.fail( |
| 143 |
f"No DeprecationWarning raised about following settings: " |
| 144 |
f'{", ".join(missed_warnings)}.' |
| 145 |
) |