Parent: [ef4b46] (diff)

Child: [ddf08c] (diff)

Download this file

test_filesystem.py    177 lines (156 with data), 6.2 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# -*- coding: utf-8 -*-
import os
from unittest import TestCase
from cStringIO import StringIO
from io import BytesIO
from pylons import response
from pylons import tmpl_context as c
from ming.orm import session, Mapper
from nose.tools import assert_equal
from allura import model as M
from alluratest.controller import setup_unit_test
class File(M.File):
class __mongometa__:
session = M.session.main_orm_session
Mapper.compile_all()
class TestFile(TestCase):
def setUp(self):
setup_unit_test()
self.session = session(File)
self.conn = M.session.main_doc_session.db._connection
self.db = M.session.main_doc_session.db
self.db.fs.remove()
self.db.fs.files.remove()
self.db.fs.chunks.remove()
def test_from_stream(self):
f = File.from_stream('test1.txt', StringIO('test1'))
self.session.flush()
assert self.db.fs.count() == 1
assert self.db.fs.files.count() == 1
assert self.db.fs.chunks.count() == 1
assert f.filename == 'test1.txt'
assert f.content_type == 'text/plain'
self._assert_content(f, 'test1')
def test_from_data(self):
f = File.from_data('test2.txt', 'test2')
self.session.flush(f)
assert self.db.fs.count() == 1
assert self.db.fs.files.count() == 1
assert self.db.fs.chunks.count() == 1
assert f.filename == 'test2.txt'
assert f.content_type == 'text/plain'
self._assert_content(f, 'test2')
def test_from_path(self):
path = __file__.rstrip('c')
f = File.from_path(path)
self.session.flush()
assert self.db.fs.count() == 1
assert self.db.fs.files.count() == 1
assert self.db.fs.chunks.count() >= 1
assert f.filename == os.path.basename(path)
text = f.rfile().read()
assert text.startswith('# -*-')
def test_delete(self):
f = File.from_data('test1.txt', 'test1')
self.session.flush()
assert self.db.fs.count() == 1
assert self.db.fs.files.count() == 1
assert self.db.fs.chunks.count() == 1
f.delete()
self.session.flush()
assert self.db.fs.count() == 0
assert self.db.fs.files.count() == 0
assert self.db.fs.chunks.count() == 0
def test_remove(self):
File.from_data('test1.txt', 'test1')
File.from_data('test2.txt', 'test2')
self.session.flush()
assert self.db.fs.count() == 2
assert self.db.fs.files.count() == 2
assert self.db.fs.chunks.count() == 2
File.remove(dict(filename='test1.txt'))
self.session.flush()
assert self.db.fs.count() == 1
assert self.db.fs.files.count() == 1
assert self.db.fs.chunks.count() == 1
def test_overwrite(self):
f = File.from_data('test1.txt', 'test1')
self.session.flush()
assert self.db.fs.count() == 1
assert self.db.fs.files.count() == 1
assert self.db.fs.chunks.count() == 1
self._assert_content(f, 'test1')
with f.wfile() as fp:
fp.write('test2')
self.session.flush()
assert self.db.fs.count() == 1
assert self.db.fs.files.count() == 2
assert self.db.fs.chunks.count() == 2
self._assert_content(f, 'test2')
def test_serve(self):
f = File.from_data(u'te s\u0b6e1.txt', 'test1')
self.session.flush()
assert [ 'test1' ] == list(f.serve())
assert response.content_type == f.content_type
assert 'Content-Disposition' not in response.headers
assert [ 'test1' ] == list(f.serve(False))
assert response.content_type == f.content_type
assert response.headers['Content-Disposition'] == \
'attachment;filename="te s\xe0\xad\xae1.txt"'
def test_image(self):
path = os.path.join(
os.path.dirname(__file__), '..', 'data', 'user.png')
with open(path) as fp:
f, t = File.save_image(
'user.png',
fp,
thumbnail_size=(16,16),
square=True,
save_original=True)
self.session.flush()
assert f.content_type == 'image/png'
assert f.is_image()
assert t.content_type == 'image/png'
assert t.is_image()
assert f.filename == t.filename
assert self.db.fs.count() == 2
assert self.db.fs.files.count() == 2
assert self.db.fs.chunks.count() == 2
def test_not_image(self):
f, t = File.save_image(
'file.txt',
StringIO('blah'),
thumbnail_size=(16,16),
square=True,
save_original=True)
assert f == None
assert t == None
def test_invalid_image(self):
f, t = File.save_image(
'bogus.png',
StringIO('bogus data here!'),
thumbnail_size=(16,16),
square=True,
save_original=True)
assert f == None
assert t == None
def test_partial_image_as_attachment(self):
path = os.path.join(os.path.dirname(__file__), '..', 'data', 'user.png')
fp = BytesIO(open(path, 'rb').read(500))
c.app.config._id = None
attachment = M.BaseAttachment.save_attachment('user.png', fp,
save_original=True)
assert type(attachment) != tuple # tuple is for (img, thumb) pairs
assert_equal(attachment.length, 500)
assert_equal(attachment.filename, 'user.png')
def test_attachment_name_encoding(self):
path = os.path.join(os.path.dirname(__file__), '..', 'data', 'user.png')
fp = open(path, 'rb')
c.app.config._id = None
attachment = M.BaseAttachment.save_attachment(b'Strukturpr\xfcfung.dvi', fp,
save_original=True)
assert type(attachment) != tuple # tuple is for (img, thumb) pairs
assert_equal(attachment.filename, u'Strukturpr\xfcfung.dvi')
def _assert_content(self, f, content):
result = f.rfile().read()
assert result == content, result