| 1 |
"""Tests around :mod:`django_downloadview.views`.""" |
| 2 |
|
| 3 |
import calendar |
| 4 |
from datetime import datetime |
| 5 |
import os |
| 6 |
import unittest |
| 7 |
from unittest import mock |
| 8 |
|
| 9 |
from django.core.files import File |
| 10 |
from django.http import Http404 |
| 11 |
from django.http.response import HttpResponseNotModified |
| 12 |
import django.test |
| 13 |
|
| 14 |
from django_downloadview import exceptions, views |
| 15 |
from django_downloadview.test import setup_view |
| 16 |
|
| 17 |
|
| 18 |
class DownloadMixinTestCase(unittest.TestCase): |
| 19 |
"""Test suite around :class:`django_downloadview.views.DownloadMixin`.""" |
| 20 |
|
| 21 |
def test_get_file(self): |
| 22 |
"""DownloadMixin.get_file() raise NotImplementedError. |
| 23 |
|
| 24 |
Subclasses must implement it! |
| 25 |
|
| 26 |
""" |
| 27 |
mixin = views.DownloadMixin() |
| 28 |
with self.assertRaises(NotImplementedError): |
| 29 |
mixin.get_file() |
| 30 |
|
| 31 |
def test_get_basename(self): |
| 32 |
"""DownloadMixin.get_basename() returns basename attribute.""" |
| 33 |
mixin = views.DownloadMixin() |
| 34 |
self.assertEqual(mixin.get_basename(), None) |
| 35 |
mixin.basename = "fake" |
| 36 |
self.assertEqual(mixin.get_basename(), "fake") |
| 37 |
|
| 38 |
def test_was_modified_since_specific(self): |
| 39 |
"""DownloadMixin.was_modified_since() delegates to file wrapper.""" |
| 40 |
file_wrapper = mock.Mock() |
| 41 |
file_wrapper.was_modified_since = mock.Mock( |
| 42 |
return_value=mock.sentinel.return_value |
| 43 |
) |
| 44 |
mixin = views.DownloadMixin() |
| 45 |
since = mock.sentinel.since |
| 46 |
return_value = mixin.was_modified_since(file_wrapper, since) |
| 47 |
self.assertEqual(return_value, mock.sentinel.return_value) |
| 48 |
file_wrapper.was_modified_since.assert_called_once_with(since) |
| 49 |
|
| 50 |
def test_was_modified_since_not_implemented(self): |
| 51 |
"""DownloadMixin.was_modified_since() returns True if file wrapper |
| 52 |
does not support ``modified_time`` or ``size`` attributes.""" |
| 53 |
fields = ["modified_time", "size"] |
| 54 |
side_effects = [AttributeError("fake"), NotImplementedError("fake")] |
| 55 |
for field in fields: |
| 56 |
for side_effect in side_effects: |
| 57 |
file_wrapper = mock.Mock() |
| 58 |
setattr( |
| 59 |
file_wrapper, field, mock.Mock(side_effect=AttributeError("fake")) |
| 60 |
) |
| 61 |
mixin = views.DownloadMixin() |
| 62 |
since = mock.sentinel.since |
| 63 |
self.assertTrue(mixin.was_modified_since(file_wrapper, since)) |
| 64 |
|
| 65 |
def test_was_modified_since_file(self): |
| 66 |
"""DownloadMixin.was_modified_since() tries (1) file's implementation. |
| 67 |
|
| 68 |
:meth:`django_downloadview.views.base.DownloadMixin.was_modified_since` |
| 69 |
first tries to delegate computations to file wrapper's implementation. |
| 70 |
|
| 71 |
""" |
| 72 |
file_wrapper = mock.Mock() |
| 73 |
file_wrapper.was_modified_since = mock.Mock( |
| 74 |
return_value=mock.sentinel.was_modified |
| 75 |
) |
| 76 |
mixin = views.DownloadMixin() |
| 77 |
self.assertIs( |
| 78 |
mixin.was_modified_since(file_wrapper, mock.sentinel.since), |
| 79 |
mock.sentinel.was_modified, |
| 80 |
) |
| 81 |
file_wrapper.was_modified_since.assert_called_once_with(mock.sentinel.since) |
| 82 |
|
| 83 |
def test_was_modified_since_django(self): |
| 84 |
"""DownloadMixin.was_modified_since() tries (2) files attributes. |
| 85 |
|
| 86 |
When calling file wrapper's ``was_modified_since()`` raises |
| 87 |
``NotImplementedError`` or ``AttributeError``, |
| 88 |
:meth:`django_downloadview.views.base.DownloadMixin.was_modified_since` |
| 89 |
tries to pass file wrapper's ``modified_time`` to |
| 90 |
:func:`django.views.static import was_modified_since`. |
| 91 |
|
| 92 |
""" |
| 93 |
file_wrapper = mock.Mock() |
| 94 |
file_wrapper.was_modified_since = mock.Mock(side_effect=AttributeError) |
| 95 |
file_wrapper.modified_time = datetime.now() |
| 96 |
was_modified_since_mock = mock.Mock(return_value=mock.sentinel.was_modified) |
| 97 |
mixin = views.DownloadMixin() |
| 98 |
with mock.patch( |
| 99 |
"django_downloadview.views.base.was_modified_since", |
| 100 |
new=was_modified_since_mock, |
| 101 |
): |
| 102 |
self.assertIs( |
| 103 |
mixin.was_modified_since(file_wrapper, mock.sentinel.since), |
| 104 |
mock.sentinel.was_modified, |
| 105 |
) |
| 106 |
was_modified_since_mock.assert_called_once_with( |
| 107 |
mock.sentinel.since, |
| 108 |
calendar.timegm(file_wrapper.modified_time.utctimetuple()), |
| 109 |
) |
| 110 |
|
| 111 |
def test_was_modified_since_fallback(self): |
| 112 |
"""DownloadMixin.was_modified_since() fallbacks to `True`. |
| 113 |
|
| 114 |
When: |
| 115 |
|
| 116 |
* calling file wrapper's ``was_modified_since()`` raises |
| 117 |
``NotImplementedError`` or ``AttributeError``; |
| 118 |
|
| 119 |
* and accessing ``modified_time`` from file wrapper raises |
| 120 |
``NotImplementedError`` or ``AttributeError``... |
| 121 |
|
| 122 |
... then |
| 123 |
:meth:`django_downloadview.views.base.DownloadMixin.was_modified_since` |
| 124 |
returns ``True``. |
| 125 |
|
| 126 |
""" |
| 127 |
file_wrapper = mock.Mock() |
| 128 |
file_wrapper.was_modified_since = mock.Mock(side_effect=NotImplementedError) |
| 129 |
type(file_wrapper).modified_time = mock.PropertyMock( |
| 130 |
side_effect=NotImplementedError |
| 131 |
) |
| 132 |
mixin = views.DownloadMixin() |
| 133 |
self.assertIs(mixin.was_modified_since(file_wrapper, "fake since"), True) |
| 134 |
|
| 135 |
def test_not_modified_response(self): |
| 136 |
"DownloadMixin.not_modified_response returns HttpResponseNotModified." |
| 137 |
mixin = views.DownloadMixin() |
| 138 |
response = mixin.not_modified_response() |
| 139 |
self.assertTrue(isinstance(response, HttpResponseNotModified)) |
| 140 |
|
| 141 |
def test_download_response(self): |
| 142 |
"DownloadMixin.download_response() returns download response instance." |
| 143 |
mixin = views.DownloadMixin() |
| 144 |
mixin.file_instance = mock.sentinel.file_wrapper |
| 145 |
response_factory = mock.Mock(return_value=mock.sentinel.response) |
| 146 |
mixin.response_class = response_factory |
| 147 |
response_kwargs = { |
| 148 |
"dummy": "value", |
| 149 |
"file_instance": mock.sentinel.file_wrapper, |
| 150 |
"attachment": True, |
| 151 |
"basename": None, |
| 152 |
"file_mimetype": None, |
| 153 |
"file_encoding": None, |
| 154 |
} |
| 155 |
response = mixin.download_response(**response_kwargs) |
| 156 |
self.assertIs(response, mock.sentinel.response) |
| 157 |
response_factory.assert_called_once_with(**response_kwargs) # Not args |
| 158 |
|
| 159 |
def test_render_to_response_not_modified(self): |
| 160 |
"""DownloadMixin.render_to_response() respects HTTP_IF_MODIFIED_SINCE |
| 161 |
header (calls ``not_modified_response()``).""" |
| 162 |
# Setup. |
| 163 |
mixin = views.DownloadMixin() |
| 164 |
mixin.request = django.test.RequestFactory().get( |
| 165 |
"/dummy-url", HTTP_IF_MODIFIED_SINCE=mock.sentinel.http_if_modified_since |
| 166 |
) |
| 167 |
mixin.was_modified_since = mock.Mock(return_value=False) |
| 168 |
mixin.not_modified_response = mock.Mock( |
| 169 |
return_value=mock.sentinel.http_not_modified_response |
| 170 |
) |
| 171 |
mixin.get_file = mock.Mock(return_value=mock.sentinel.file_wrapper) |
| 172 |
# Run. |
| 173 |
response = mixin.render_to_response() |
| 174 |
# Check. |
| 175 |
self.assertIs(response, mock.sentinel.http_not_modified_response) |
| 176 |
mixin.get_file.assert_called_once_with() |
| 177 |
mixin.was_modified_since.assert_called_once_with( |
| 178 |
mock.sentinel.file_wrapper, mock.sentinel.http_if_modified_since |
| 179 |
) |
| 180 |
mixin.not_modified_response.assert_called_once_with() |
| 181 |
|
| 182 |
def test_render_to_response_modified(self): |
| 183 |
"""DownloadMixin.render_to_response() calls download_response().""" |
| 184 |
# Setup. |
| 185 |
mixin = views.DownloadMixin() |
| 186 |
mixin.request = django.test.RequestFactory().get( |
| 187 |
"/dummy-url", HTTP_IF_MODIFIED_SINCE=None |
| 188 |
) |
| 189 |
mixin.was_modified_since = mock.Mock() |
| 190 |
mixin.download_response = mock.Mock( |
| 191 |
return_value=mock.sentinel.download_response |
| 192 |
) |
| 193 |
mixin.get_file = mock.Mock(return_value=mock.sentinel.file_wrapper) |
| 194 |
# Run. |
| 195 |
response = mixin.render_to_response() |
| 196 |
# Check. |
| 197 |
self.assertIs(response, mock.sentinel.download_response) |
| 198 |
mixin.get_file.assert_called_once_with() |
| 199 |
self.assertEqual(mixin.was_modified_since.call_count, 0) |
| 200 |
mixin.download_response.assert_called_once_with() |
| 201 |
|
| 202 |
def test_render_to_response_file_not_found(self): |
| 203 |
"DownloadMixin.render_to_response() calls file_not_found_response()." |
| 204 |
# Setup. |
| 205 |
mixin = views.DownloadMixin() |
| 206 |
mixin.request = django.test.RequestFactory().get("/dummy-url") |
| 207 |
mixin.get_file = mock.Mock(side_effect=exceptions.FileNotFound) |
| 208 |
mixin.file_not_found_response = mock.Mock() |
| 209 |
# Run. |
| 210 |
mixin.render_to_response() |
| 211 |
# Check. |
| 212 |
mixin.file_not_found_response.assert_called_once_with() |
| 213 |
|
| 214 |
def test_file_not_found_response(self): |
| 215 |
"""DownloadMixin.file_not_found_response() raises Http404.""" |
| 216 |
mixin = views.DownloadMixin() |
| 217 |
with self.assertRaises(Http404): |
| 218 |
mixin.file_not_found_response() |
| 219 |
|
| 220 |
|
| 221 |
class BaseDownloadViewTestCase(unittest.TestCase): |
| 222 |
"Tests around :class:`django_downloadviews.views.base.BaseDownloadView`." |
| 223 |
|
| 224 |
def test_get(self): |
| 225 |
"""BaseDownloadView.get() calls render_to_response().""" |
| 226 |
request = django.test.RequestFactory().get("/dummy-url") |
| 227 |
args = ["dummy-arg"] |
| 228 |
kwargs = {"dummy": "kwarg"} |
| 229 |
view = setup_view(views.BaseDownloadView(), request, *args, **kwargs) |
| 230 |
view.render_to_response = mock.Mock(return_value=mock.sentinel.response) |
| 231 |
response = view.get(request, *args, **kwargs) |
| 232 |
self.assertIs(response, mock.sentinel.response) |
| 233 |
view.render_to_response.assert_called_once_with() |
| 234 |
|
| 235 |
|
| 236 |
class PathDownloadViewTestCase(unittest.TestCase): |
| 237 |
"Tests for :class:`django_downloadviews.views.path.PathDownloadView`." |
| 238 |
|
| 239 |
def test_get_file_ok(self): |
| 240 |
"PathDownloadView.get_file() returns ``File`` instance." |
| 241 |
view = setup_view(views.PathDownloadView(path=__file__), "fake request") |
| 242 |
file_wrapper = view.get_file() |
| 243 |
self.assertTrue(isinstance(file_wrapper, File)) |
| 244 |
|
| 245 |
def test_get_file_does_not_exist(self): |
| 246 |
"""PathDownloadView.get_file() raises FileNotFound if field does not |
| 247 |
exist. |
| 248 |
|
| 249 |
""" |
| 250 |
view = setup_view(views.PathDownloadView(path="i-do-no-exist"), "fake request") |
| 251 |
with self.assertRaises(exceptions.FileNotFound): |
| 252 |
view.get_file() |
| 253 |
|
| 254 |
def test_get_file_is_directory(self): |
| 255 |
"""PathDownloadView.get_file() raises FileNotFound if file is a |
| 256 |
directory.""" |
| 257 |
view = setup_view( |
| 258 |
views.PathDownloadView(path=os.path.dirname(__file__)), "fake request" |
| 259 |
) |
| 260 |
with self.assertRaises(exceptions.FileNotFound): |
| 261 |
view.get_file() |
| 262 |
|
| 263 |
|
| 264 |
class ObjectDownloadViewTestCase(unittest.TestCase): |
| 265 |
"Tests for :class:`django_downloadviews.views.object.ObjectDownloadView`." |
| 266 |
|
| 267 |
def test_get_file_ok(self): |
| 268 |
"ObjectDownloadView.get_file() returns ``file`` field by default." |
| 269 |
view = setup_view(views.ObjectDownloadView(), "fake request") |
| 270 |
view.object = mock.Mock(spec=["file"]) |
| 271 |
view.get_file() |
| 272 |
|
| 273 |
def test_get_file_wrong_field(self): |
| 274 |
"""ObjectDownloadView.get_file() raises AttributeError if field does |
| 275 |
not exist. |
| 276 |
|
| 277 |
``AttributeError`` is expected because this is a configuration error, |
| 278 |
i.e. it is related to Python code. |
| 279 |
|
| 280 |
""" |
| 281 |
view = setup_view( |
| 282 |
views.ObjectDownloadView(file_field="other_field"), "fake request" |
| 283 |
) |
| 284 |
view.object = mock.Mock(spec=["file"]) |
| 285 |
with self.assertRaises(AttributeError): |
| 286 |
view.get_file() |
| 287 |
|
| 288 |
def test_get_file_empty_field(self): |
| 289 |
"""ObjectDownloadView.get_file() raises FileNotFound if field does not |
| 290 |
exist.""" |
| 291 |
view = setup_view( |
| 292 |
views.ObjectDownloadView(file_field="other_field"), "fake request" |
| 293 |
) |
| 294 |
view.object = mock.Mock() |
| 295 |
view.object.other_field = None |
| 296 |
with self.assertRaises(exceptions.FileNotFound): |
| 297 |
view.get_file() |
| 298 |
|
| 299 |
|
| 300 |
class VirtualDownloadViewTestCase(unittest.TestCase): |
| 301 |
"""Test suite around |
| 302 |
:py:class:`django_downloadview.views.VirtualDownloadView`.""" |
| 303 |
|
| 304 |
def test_was_modified_since_specific(self): |
| 305 |
"""VirtualDownloadView.was_modified_since() delegates to file wrapper.""" |
| 306 |
file_wrapper = mock.Mock() |
| 307 |
file_wrapper.was_modified_since = mock.Mock( |
| 308 |
return_value=mock.sentinel.from_file_wrapper |
| 309 |
) |
| 310 |
view = views.VirtualDownloadView() |
| 311 |
since = mock.sentinel.since |
| 312 |
return_value = view.was_modified_since(file_wrapper, since) |
| 313 |
self.assertTrue(return_value is mock.sentinel.from_file_wrapper) |
| 314 |
file_wrapper.was_modified_since.assert_called_once_with(since) |
| 315 |
|
| 316 |
def test_was_modified_since_not_implemented(self): |
| 317 |
"""VirtualDownloadView.was_modified_since() returns True if file |
| 318 |
wrapper does not implement ``was_modified_since()``.""" |
| 319 |
file_wrapper = mock.Mock() |
| 320 |
file_wrapper.was_modified_since = mock.Mock(side_effect=AttributeError) |
| 321 |
modified_time = mock.PropertyMock() |
| 322 |
setattr(file_wrapper, "modified_time", modified_time) |
| 323 |
size = mock.PropertyMock() |
| 324 |
setattr(file_wrapper, "size", size) |
| 325 |
view = views.VirtualDownloadView() |
| 326 |
since = mock.sentinel.since |
| 327 |
result = view.was_modified_since(file_wrapper, since) |
| 328 |
self.assertTrue(result is True) |
| 329 |
self.assertFalse(modified_time.called) |
| 330 |
self.assertFalse(size.called) |