/usr/lib/python2.7/dist-packages/formalchemy/tests/test_binary.py is in python-formalchemy 1.4.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | import cgi
import shutil
import tempfile
from StringIO import StringIO
from formalchemy.fields import FileFieldRenderer
from formalchemy.ext import fsblob
from formalchemy.tests import *
from webtest import TestApp, selenium
from webob import multidict
BOUNDARY='testdata'
ENVIRON = {
'REQUEST_METHOD':'POST',
'CONTENT_TYPE': 'multipart/form-data;boundary="%s"' % BOUNDARY
}
TEST_DATA = '''--%s
Content-Disposition: form-data; name="Binaries--file"; filename="test.js"
Content-Type: application/x-javascript
var test = null;
--%s--
''' % (BOUNDARY, BOUNDARY)
EMPTY_DATA = '''--%s
Content-Disposition: form-data; name="Binaries--file"; filename=""
Content-Type: application/x-javascript
--%s--
''' % (BOUNDARY, BOUNDARY)
REMOVE_DATA = '''--%s
Content-Disposition: form-data; name="Binaries--file--remove"
1
--%s
Content-Disposition: form-data; name="Binaries--file"; filename=""
Content-Type: application/x-javascript
--%s--
''' % (BOUNDARY, BOUNDARY, BOUNDARY)
def get_fields(data):
return multidict.MultiDict.from_fieldstorage(cgi.FieldStorage(fp=StringIO(data), environ=ENVIRON))
def test_binary():
r"""
Notice that those tests assume that the FileFieldRenderer work with LargeBinary type
*and* String type if you only want to store file path in your DB.
Configure a fieldset with a file field
>>> fs = FieldSet(Three)
>>> record = fs.model
>>> fs.configure(include=[fs.bar.with_renderer(FileFieldRenderer)])
>>> isinstance(fs.bar.renderer, FileFieldRenderer)
True
At creation time only the input field is rendered
>>> print fs.render()
<div>
<label class="field_opt" for="Three--bar">
Bar
</label>
<input id="Three--bar" name="Three--bar" type="file" />
</div>
<script type="text/javascript">
//<![CDATA[
document.getElementById("Three--bar").focus();
//]]>
</script>
If the field has a value then we add a check box to remove it
>>> record.bar = '/path/to/file'
>>> print fs.render()
<div>
<label class="field_opt" for="Three--bar">
Bar
</label>
<input id="Three--bar" name="Three--bar" type="file" />
<input id="Three--bar--remove" name="Three--bar--remove" type="checkbox" value="1" />
<label for="Three--bar--remove">
Remove
</label>
</div>
<script type="text/javascript">
//<![CDATA[
document.getElementById("Three--bar").focus();
//]]>
</script>
Now submit form with empty value
>>> fs.rebind(data={'Three--bar':''})
>>> fs.validate()
True
>>> fs.sync()
The field value does not change
>>> print record.bar
/path/to/file
Try to remove it by checking the checkbox
>>> fs.rebind(data={'Three--bar':'', 'Three--bar--remove':'1'})
>>> fs.validate()
True
>>> fs.sync()
The field value is removed
>>> print record.bar
<BLANKLINE>
Also check that this work with cgi.FieldStorage
>>> fs = FieldSet(Binaries)
>>> record = fs.model
We need test data
>>> data = get_fields(TEST_DATA)
>>> print data.getone('Binaries--file')
FieldStorage(u'Binaries--file', u'test.js')
>>> fs.rebind(data=data)
>>> if fs.validate(): fs.sync()
We get the file, yeah.
>>> print record.file
var test = null;
<BLANKLINE>
Now submit form with empty value
>>> data = get_fields(EMPTY_DATA)
>>> fs.rebind(data=data)
>>> if fs.validate(): fs.sync()
The field value dos not change
>>> print record.file
var test = null;
<BLANKLINE>
Remove file
>>> data = get_fields(REMOVE_DATA)
>>> fs.rebind(data=data)
>>> if fs.validate(): fs.sync()
The field value is now empty
>>> print record.file
<BLANKLINE>
See what append in read only mode
>>> record.file = 'e'*1000
>>> print fs.file.render_readonly()
1 KB
>>> record.file = 'e'*1000*1024
>>> print fs.file.render_readonly()
1000.00 KB
>>> record.file = 'e'*2*1024*1024
>>> print fs.file.render_readonly()
2.00 MB
"""
class BlobTestCase(unittest.TestCase):
renderer = fsblob.FileFieldRenderer
def setUp(self):
self.wd = tempfile.mkdtemp()
self.binary = Three()
self.fs = FieldSet(Three)
self.fs.configure(include=[self.fs.bar, self.fs.foo])
self.fs.foo.set(renderer=self.renderer.new(
storage_path=self.wd,
url_prefix='/media'))
self.app = TestApp(application(self.binary, self.fs))
def test_file(self):
resp = self.app.get('/')
resp.mustcontain('type="file"')
resp = self.app.post('/', {"Three--bar":'bar'},
upload_files=[('Three--foo', 'foo.txt', 'data')])
resp = self.app.get('/')
resp.mustcontain('<a href="/media/', '/foo.txt">', 'foo.txt (1 KB)')
self.assert_(self.binary.foo.endswith('foo.txt'), repr(self.binary.foo))
resp = self.app.get('/')
resp.mustcontain('name="Three--foo--remove"',
'<a href="/media/', '/foo.txt">', 'foo.txt (1 KB)')
# no change
form = resp.form
resp = form.submit()
resp.mustcontain('<a href="/media/', '/foo.txt">', 'foo.txt (1 KB)')
self.assert_(self.binary.foo.endswith('foo.txt'), repr(self.binary.foo))
# remove file
resp = self.app.get('/')
form = resp.form
form['Three--foo--remove'] = '1'
resp = form.submit()
self.assert_(self.binary.foo == '', repr(self.binary.foo))
resp.mustcontain(no='foo.txt (1 KB)')
def tearDown(self):
shutil.rmtree(self.wd)
|