Debian Codegraph @

demo/demoproject/storage/tests.py

raw
1 import datetime
2 import unittest
3
4 from django.core.files.base import ContentFile
5 from django.http.response import HttpResponseNotModified
6 import django.test
7 from django.urls import reverse
8
9 from django_downloadview import (
10 assert_download_response,
11 setup_view,
12 temporary_media_root,
13 )
14
15 from demoproject.storage import views
16
17 # Fixtures.
18 file_content = "Hello world!\n"
19
20
21 def setup_file(path):
22 views.storage.save(path, ContentFile(file_content))
23
24
25 class StaticPathTestCase(django.test.TestCase):
26 @temporary_media_root()
27 def test_download_response(self):
28 """'storage:static_path' streams file by path."""
29 setup_file("1.txt")
30 url = reverse("storage:static_path", kwargs={"path": "1.txt"})
31 response = self.client.get(url)
32 assert_download_response(
33 self,
34 response,
35 content=file_content,
36 basename="1.txt",
37 mime_type="text/plain",
38 )
39
40 @temporary_media_root()
41 def test_not_modified_download_response(self):
42 """'storage:static_path' sends not modified response if unmodified."""
43 setup_file("1.txt")
44 url = reverse("storage:static_path", kwargs={"path": "1.txt"})
45 year = datetime.date.today().year + 4
46 response = self.client.get(
47 url, headers={"if-modified-since": f"Sat, 29 Oct {year} 19:43:31 GMT"}
48 )
49 self.assertTrue(isinstance(response, HttpResponseNotModified))
50
51 @temporary_media_root()
52 def test_modified_since_download_response(self):
53 """'storage:static_path' streams file if modified."""
54 setup_file("1.txt")
55 url = reverse("storage:static_path", kwargs={"path": "1.txt"})
56 response = self.client.get(
57 url, headers={"if-modified-since": "Sat, 29 Oct 1980 19:43:31 GMT"}
58 )
59 assert_download_response(
60 self,
61 response,
62 content=file_content,
63 basename="1.txt",
64 mime_type="text/plain",
65 )
66
67
68 class DynamicPathIntegrationTestCase(django.test.TestCase):
69 """Integration tests around ``storage:dynamic_path`` URL."""
70
71 @temporary_media_root()
72 def test_download_response(self):
73 """'dynamic_path' streams file by generated path.
74
75 As we use ``self.client``, this test involves the whole Django stack,
76 including settings, middlewares, decorators... So we need to setup a
77 file, the storage, and an URL.
78
79 This test actually asserts the URL ``storage:dynamic_path`` streams a
80 file in storage.
81
82 """
83 setup_file("1.TXT")
84 url = reverse("storage:dynamic_path", kwargs={"path": "1.txt"})
85 response = self.client.get(url)
86 assert_download_response(
87 self,
88 response,
89 content=file_content,
90 basename="1.TXT",
91 mime_type="text/plain",
92 )
93
94
95 class DynamicPathUnitTestCase(unittest.TestCase):
96 """Unit tests around ``views.DynamicStorageDownloadView``."""
97
98 def test_get_path(self):
99 """DynamicStorageDownloadView.get_path() returns uppercase path.
100
101 Uses :func:`~django_downloadview.test.setup_view` to target only
102 overriden methods.
103
104 This test does not involve URLconf, middlewares or decorators. It is
105 fast. It has clear scope. It does not assert ``storage:dynamic_path``
106 URL works. It targets only custom ``DynamicStorageDownloadView`` class.
107
108 """
109 view = setup_view(
110 views.DynamicStorageDownloadView(),
111 django.test.RequestFactory().get("/fake-url"),
112 path="dummy path",
113 )
114 path = view.get_path()
115 self.assertEqual(path, "DUMMY PATH")