1
# Copyright 2012 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
4
"""Tests for the FileStorage model."""
6
from __future__ import (
16
from io import BytesIO
18
from maasserver.models import FileStorage
19
from maasserver.testing.factory import factory
20
from maasserver.testing.testcase import TestCase
23
class FileStorageTest(TestCase):
24
"""Testing of the :class:`FileStorage` model."""
26
def make_data(self, including_text='data'):
27
"""Return arbitrary data.
29
:param including_text: Text to include in the data. Leave something
30
here to make failure messages more recognizable.
31
:type including_text: basestring
32
:return: A string of bytes, including `including_text`.
35
# Note that this won't automatically insert any non-ASCII bytes.
36
# Proper handling of real binary data is tested separately.
37
text = "%s %s" % (including_text, factory.getRandomString())
38
return text.encode('ascii')
40
def test_save_file_creates_storage(self):
41
filename = factory.getRandomString()
42
content = self.make_data()
43
user = factory.make_user()
44
storage = FileStorage.objects.save_file(
45
filename, BytesIO(content), user)
47
(filename, content, user),
48
(storage.filename, storage.content, storage.owner))
50
def test_storage_can_be_retrieved(self):
51
filename = factory.getRandomString()
52
content = self.make_data()
53
factory.make_file_storage(filename=filename, content=content)
54
storage = FileStorage.objects.get(filename=filename)
57
(storage.filename, storage.content))
59
def test_stores_binary_data(self):
60
# This horrible binary data could never, ever, under any
61
# encoding known to man be interpreted as text(1). Switch the
62
# bytes of the byte-order mark around and by design you get an
63
# invalid codepoint; put a byte with the high bit set between bytes
64
# that have it cleared, and you have a guaranteed non-UTF-8
67
# (1) Provided, of course, that man know only about ASCII and
69
binary_data = codecs.BOM64_LE + codecs.BOM64_BE + b'\x00\xff\x00'
71
# And yet, because FileStorage supports binary data, it comes
73
storage = factory.make_file_storage(filename="x", content=binary_data)
74
self.assertEqual(binary_data, storage.content)
76
def test_overwrites_file(self):
77
# If a file of the same name has already been stored, the
78
# reference to the old data gets overwritten with one to the new
80
filename = factory.make_name('filename')
81
old_storage = factory.make_file_storage(
82
filename=filename, content=self.make_data('old data'))
83
new_data = self.make_data('new-data')
84
new_storage = factory.make_file_storage(
85
filename=filename, content=new_data)
86
self.assertEqual(old_storage.filename, new_storage.filename)
88
new_data, FileStorage.objects.get(filename=filename).content)
90
def test_key_gets_generated(self):
91
# The generated system_id looks good.
92
storage = factory.make_file_storage()
93
self.assertEqual(len(storage.key), 36)
95
def test_key_includes_random_part(self):
96
storage1 = factory.make_file_storage()
97
storage2 = factory.make_file_storage()
98
self.assertNotEqual(storage1.key, storage2.key)