Repository navigation
Expand file tree
/
Copy pathtests.py
More file actions
130 lines (117 loc) · 5.02 KB
/
Copy pathtests.py
File metadata and controls
130 lines (117 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import os
import tempfile
import unittest
from unittest.mock import MagicMock
import pzip
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.core.files.base import ContentFile
from pzip_storage import PZipStorage, bad_keys, needs_encryption, needs_rotation
class CompressedStorageTests(unittest.TestCase):
def setUp(self):
self.storage = PZipStorage()
def test_improper_config(self):
with self.assertRaises(ImproperlyConfigured):
PZipStorage(keys=[])
# PZipStorage does not check callables at creation time.
storage = PZipStorage(keys=list)
with self.assertRaises(ImproperlyConfigured):
storage.save("testfile", ContentFile(b"test data"))
def test_round_trip(self):
plaintext = b"Hello world!"
name = self.storage.save("hello.txt", ContentFile(plaintext))
with self.storage.open(name) as f:
self.assertEqual(plaintext, f.read())
self.assertEqual(len(plaintext), self.storage.size(name))
self.assertTrue(self.storage.exists(name))
self.storage.delete(name)
self.assertFalse(self.storage.exists(name))
def test_multiple_keys(self):
plaintext = (
b"Answer to the Ultimate Question of Life, The Universe, and Everything."
)
keys = [b"first"]
handler = MagicMock()
needs_rotation.connect(handler, sender=PZipStorage)
storage = PZipStorage(keys=lambda: keys)
name = storage.save("testfile", ContentFile(plaintext))
keys.insert(0, b"second")
keys.insert(0, b"third")
with storage.open(name) as f:
self.assertEqual(plaintext, f.read())
handler.assert_called_once_with(
signal=needs_rotation,
sender=PZipStorage,
storage=storage,
name=name,
key=b"first",
)
storage.delete(name)
def test_no_compression(self):
name = self.storage.save("test.jpg", ContentFile(b"JPEG data"))
with self.storage.open(name) as f:
self.assertIsInstance(f, pzip.PZip)
self.assertEqual(f.compression, pzip.Compression.NONE)
# Compress everything.
compressed = PZipStorage(nocompress=False)
with compressed.open("test.jpg", "wb") as f:
self.assertIsInstance(f, pzip.PZip)
self.assertEqual(f.compression, pzip.Compression.GZIP)
# Compress nothing.
raw = PZipStorage(nocompress=True)
with raw.open("test.txt", "wb") as f:
self.assertIsInstance(f, pzip.PZip)
self.assertEqual(f.compression, pzip.Compression.NONE)
def test_unencrypted(self):
handler = MagicMock()
needs_encryption.connect(handler, sender=PZipStorage)
self.assertEqual(self.storage.size("unencrypted"), 11)
with self.storage.open("unencrypted") as f:
self.assertNotIsInstance(f, pzip.PZip)
self.assertEqual(f.read(), b"hello world")
handler.assert_called_once_with(
signal=needs_encryption,
sender=PZipStorage,
storage=self.storage,
name="unencrypted",
)
def test_bad_keys(self):
handler = MagicMock()
bad_keys.connect(handler, sender=PZipStorage)
with self.storage.open("encrypted" + PZipStorage.DEFAULT_EXTENSION) as f:
self.assertNotEqual(f.read(), b"unrecoverable data")
handler.assert_called_once_with(
signal=bad_keys,
sender=PZipStorage,
storage=self.storage,
name="encrypted" + PZipStorage.DEFAULT_EXTENSION,
)
def test_write(self):
plaintext = b"Hello world!"
with self.storage.open("writable", "wb") as f:
self.assertIsInstance(f, pzip.PZip)
f.write(b"initial")
with self.storage.open("writable", "wb") as f:
self.assertIsInstance(f, pzip.PZip)
f.write(plaintext)
with self.storage.open("writable") as f:
# TODO: should probably be io.UnsupportedOperation in pzip?
with self.assertRaises(NotImplementedError):
f.write(b"nope")
self.assertEqual(plaintext, f.read())
if __name__ == "__main__":
with tempfile.TemporaryDirectory() as tempdir:
# Write a pre-existing unencrypted file to the storage root.
with open(os.path.join(tempdir, "unencrypted"), "wb") as f:
f.write(b"hello world")
# Write a pre-existing encrypted file (with a random key) to the storage root.
random_key = os.urandom(32)
with pzip.open(
os.path.join(tempdir, "encrypted" + PZipStorage.DEFAULT_EXTENSION),
"wb",
key=random_key,
) as f:
f.write(b"unrecoverable data")
# Set up Django settings to have a stable SECRET_KEY and MEDIA_ROOT.
settings.configure(SECRET_KEY=os.urandom(32), MEDIA_ROOT=tempdir)
unittest.main()