2008-03-18 20:40:39 +00:00
# -*- coding: utf8 -*-
2008-09-11 07:36:12 +00:00
import os
2008-02-23 22:17:02 +00:00
import time
2008-02-06 01:34:12 +00:00
import types
2008-03-18 22:22:19 +00:00
import urllib
2009-03-20 07:57:14 +00:00
import httplib
2008-02-23 23:16:39 +00:00
import cherrypy
2008-08-08 23:39:53 +00:00
from nose . tools import raises
2008-02-23 22:17:02 +00:00
from threading import Thread
2008-02-23 08:14:19 +00:00
from StringIO import StringIO
2008-04-02 05:34:50 +00:00
from PIL import Image
2008-02-06 01:34:12 +00:00
from Test_controller import Test_controller
from model . Notebook import Notebook
from model . Note import Note
from model . User import User
from model . Invite import Invite
2008-02-23 08:14:19 +00:00
from model . File import File
2008-09-11 07:36:12 +00:00
from model . Download_access import Download_access
2008-02-06 01:34:12 +00:00
from controller . Notebooks import Access_error
2008-08-08 23:39:53 +00:00
from controller . Files import Upload_file , Parse_error
2008-02-06 01:34:12 +00:00
class Test_files ( Test_controller ) :
def setUp ( self ) :
Test_controller . setUp ( self )
self . notebook = None
self . anon_notebook = None
self . username = u " mulder "
self . password = u " trustno1 "
self . email_address = u " outthere@example.com "
self . username2 = u " deepthroat "
self . password2 = u " mmmtobacco "
self . email_address2 = u " parkinglot@example.com "
self . user = None
self . user2 = None
self . anonymous = None
self . session_id = None
2008-02-23 08:59:39 +00:00
self . file_id = " 22 "
2008-02-06 01:34:12 +00:00
self . filename = " file.png "
2008-03-18 20:40:39 +00:00
self . unicode_filename = u " ümlaut.png "
2008-02-23 22:59:36 +00:00
self . new_filename = " newfile.png "
2008-02-24 01:01:50 +00:00
self . file_data = " foobar \x07 `-=[] \ ; ' ,./ ~!@#$ % ^&*()_+ {} |: \" <>? " * 100
2008-02-23 22:59:36 +00:00
self . weird_filename = self . file_data + " .png "
2008-02-23 08:14:19 +00:00
self . content_type = " image/png "
2008-02-23 23:57:05 +00:00
self . upload_thread = None
2008-02-23 08:14:19 +00:00
# make Upload_file deal in fake files rather than actually using the filesystem
2008-02-23 08:59:39 +00:00
Upload_file . fake_files = { } # map of file_id to fake file object
2008-02-23 08:14:19 +00:00
@staticmethod
def open_file ( file_id , mode = None ) :
2008-02-23 08:59:39 +00:00
fake_file = Upload_file . fake_files . get ( file_id )
2008-02-23 08:14:19 +00:00
if fake_file :
return fake_file
if mode not in ( " w " , " w+ " ) :
raise IOError ( )
fake_file = StringIO ( )
Upload_file . fake_files [ file_id ] = fake_file
return fake_file
2008-04-02 05:34:50 +00:00
@staticmethod
def open_image ( file_id ) :
fake_file = Upload_file . fake_files . get ( file_id )
return Image . open ( fake_file )
2008-02-23 08:14:19 +00:00
@staticmethod
def delete_file ( file_id ) :
2008-02-23 08:59:39 +00:00
fake_file = Upload_file . fake_files . get ( file_id )
2008-02-23 08:14:19 +00:00
if fake_file is None :
raise IOError ( )
2008-02-23 08:59:39 +00:00
del ( Upload_file . fake_files [ file_id ] )
2008-02-23 08:14:19 +00:00
2008-02-23 22:17:02 +00:00
@staticmethod
def exists ( file_id ) :
fake_file = Upload_file . fake_files . get ( file_id )
return fake_file is not None
def close ( self ) :
2009-03-20 07:57:14 +00:00
pass
2008-02-23 22:17:02 +00:00
2008-02-23 08:14:19 +00:00
Upload_file . open_file = open_file
2008-04-02 05:34:50 +00:00
Upload_file . open_image = open_image
2008-02-23 08:14:19 +00:00
Upload_file . delete_file = delete_file
2008-02-23 22:17:02 +00:00
Upload_file . exists = exists
Upload_file . close = close
2008-02-06 01:34:12 +00:00
2008-09-11 07:36:12 +00:00
# write a test product file
test_product_file = file ( u " products/test.exe " , " wb " )
test_product_file . write ( self . file_data )
test_product_file . close ( )
2008-02-06 01:34:12 +00:00
self . make_users ( )
self . make_notebooks ( )
self . database . commit ( )
def make_notebooks ( self ) :
user_id = self . user . object_id
self . trash = Notebook . create ( self . database . next_id ( Notebook ) , u " trash " , user_id = user_id )
self . database . save ( self . trash , commit = False )
self . notebook = Notebook . create ( self . database . next_id ( Notebook ) , u " notebook " , self . trash . object_id , user_id = user_id )
self . database . save ( self . notebook , commit = False )
note_id = self . database . next_id ( Note )
self . note = Note . create ( note_id , u " <h3>my title</h3>blah " , notebook_id = self . notebook . object_id , startup = True , user_id = user_id )
self . database . save ( self . note , commit = False )
self . anon_notebook = Notebook . create ( self . database . next_id ( Notebook ) , u " anon_notebook " , user_id = user_id )
self . database . save ( self . anon_notebook , commit = False )
self . database . execute ( self . user . sql_save_notebook ( self . notebook . object_id , read_write = True , owner = True ) )
self . database . execute ( self . user . sql_save_notebook ( self . notebook . trash_id , read_write = True , owner = True ) )
self . database . execute ( self . user . sql_save_notebook ( self . anon_notebook . object_id , read_write = False , owner = False ) )
def make_users ( self ) :
self . user = User . create ( self . database . next_id ( User ) , self . username , self . password , self . email_address )
self . database . save ( self . user , commit = False )
self . user2 = User . create ( self . database . next_id ( User ) , self . username2 , self . password2 , self . email_address2 )
2008-02-23 22:17:02 +00:00
self . user2 . rate_plan = 1
2008-02-06 01:34:12 +00:00
self . database . save ( self . user2 , commit = False )
self . anonymous = User . create ( self . database . next_id ( User ) , u " anonymous " )
self . database . save ( self . anonymous , commit = False )
2008-02-23 23:57:05 +00:00
def tearDown ( self ) :
if self . upload_thread :
self . upload_thread . join ( )
2008-09-11 07:36:12 +00:00
os . remove ( u " products/test.exe " )
2009-03-18 22:11:46 +00:00
def test_download ( self , filename = None , quote_filename = None , file_data = None , preview = None , expected_file_data = None ) :
2008-02-23 23:57:05 +00:00
self . login ( )
2009-03-18 22:11:46 +00:00
if expected_file_data is None :
expected_file_data = file_data
if file_data is None :
expected_file_data = self . file_data
2008-02-23 23:57:05 +00:00
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-02-23 23:57:05 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
2008-03-18 20:40:39 +00:00
filename = filename or self . filename ,
2008-04-02 05:34:50 +00:00
file_data = file_data or self . file_data ,
2008-02-23 23:57:05 +00:00
content_type = self . content_type ,
session_id = self . session_id ,
)
2008-03-18 22:22:19 +00:00
if quote_filename is None :
2008-04-02 05:34:50 +00:00
quote_param = " "
2008-03-18 22:22:19 +00:00
elif quote_filename is True :
2008-04-02 05:34:50 +00:00
quote_param = " "e_filename=True "
else :
quote_param = " "e_filename=False "
if preview is None :
preview_param = " "
elif preview is True :
preview_param = " &preview=True "
2008-03-18 22:22:19 +00:00
else :
2008-04-02 05:34:50 +00:00
preview_param = " &preview=False "
result = self . http_get (
" /files/download?file_id= %s %s %s " % ( self . file_id , quote_param , preview_param ) ,
session_id = self . session_id ,
)
2008-02-23 23:57:05 +00:00
headers = result [ u " headers " ]
assert headers
assert headers [ u " Content-Type " ] == self . content_type
2008-03-18 22:22:19 +00:00
filename = ( filename or self . filename ) . encode ( " utf8 " )
if quote_filename is True :
filename = urllib . quote ( filename )
assert headers [ u " Content-Disposition " ] == ' attachment; filename= " %s " ' % filename
2008-02-23 23:57:05 +00:00
gen = result [ u " body " ]
assert isinstance ( gen , types . GeneratorType )
pieces = [ ]
try :
for piece in gen :
pieces . append ( piece )
except AttributeError , exc :
if u " session_storage " not in str ( exc ) :
raise exc
2009-03-18 22:11:46 +00:00
received_file_data = " " . join ( pieces )
assert received_file_data == expected_file_data
return result
def test_download_with_nginx ( self ) :
cherrypy . root . files . _Files__web_server = u " nginx "
result = self . test_download ( self . filename , expected_file_data = " " )
headers = result [ u " headers " ]
assert headers [ u " X-Accel-Redirect " ] == u " /download/ %s " % self . file_id
2008-02-23 23:57:05 +00:00
2008-03-18 20:40:39 +00:00
def test_download_with_unicode_filename ( self ) :
self . test_download ( self . unicode_filename )
2008-03-18 22:22:19 +00:00
def test_download_with_unicode_quoted_filename ( self ) :
self . test_download ( self . unicode_filename , quote_filename = True )
def test_download_with_unicode_unquoted_filename ( self ) :
self . test_download ( self . unicode_filename , quote_filename = False )
2008-04-02 05:34:50 +00:00
IMAGE_DATA = ' \x89 PNG \r \n \x1a \n \x00 \x00 \x00 \r IHDR \x00 \x00 \x00 \x01 \x00 \x00 \x00 \x01 \x08 \x02 \x00 \x00 \x00 \x90 wS \xde \x00 \x00 \x00 \x06 bKGD \x00 \xff \x00 \xff \x00 \xff \xa0 \xbd \xa7 \x93 \x00 \x00 \x00 \t pHYs \x00 \x00 \x0b \x13 \x00 \x00 \x0b \x13 \x01 \x00 \x9a \x9c \x18 \x00 \x00 \x00 \x07 tIME \x07 \xd7 \x06 \r \x13 (:; \xf4 \xc1 { \x00 \x00 \x00 \x1d tEXtComment \x00 Created with The GIMP \xef d % n \x00 \x00 \x00 \x0c IDAT \x08 \xd7 c \xf8 \xf5 \xeb \x17 \x00 \x05 \xe0 \x02 \xef Ij \xd4 ! \x00 \x00 \x00 \x00 IEND \xae B` \x82 '
def test_download_image ( self ) :
self . login ( )
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-04-02 05:34:50 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . IMAGE_DATA ,
content_type = self . content_type ,
session_id = self . session_id ,
)
result = self . http_get (
" /files/download?file_id= %s " % self . file_id ,
session_id = self . session_id ,
)
assert result [ u " redirect " ] == u " /files/preview?file_id= %s "e_filename=False " % self . file_id
2008-04-01 21:54:43 +00:00
def test_download_image_with_preview_true ( self ) :
2008-04-02 05:34:50 +00:00
self . login ( )
2008-04-01 21:54:43 +00:00
2008-04-02 05:34:50 +00:00
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-04-02 05:34:50 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . IMAGE_DATA ,
content_type = self . content_type ,
session_id = self . session_id ,
)
result = self . http_get (
" /files/download?file_id= %s &preview=true " % self . file_id ,
session_id = self . session_id ,
)
assert result [ u " redirect " ] == u " /files/preview?file_id= %s "e_filename=False " % self . file_id
def test_download_image_with_preview_true_and_quote_filename_true ( self ) :
self . login ( )
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-04-02 05:34:50 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . IMAGE_DATA ,
content_type = self . content_type ,
session_id = self . session_id ,
)
2008-04-01 21:54:43 +00:00
2008-04-02 05:34:50 +00:00
result = self . http_get (
" /files/download?file_id= %s &preview=true"e_filename=True " % self . file_id ,
session_id = self . session_id ,
)
assert result [ u " redirect " ] == u " /files/preview?file_id= %s "e_filename=True " % self . file_id
def test_download_image_with_preview_false ( self ) :
self . test_download ( file_data = self . IMAGE_DATA , preview = False )
2008-04-01 21:54:43 +00:00
def test_download_non_image_with_preview_true ( self ) :
2008-04-02 05:34:50 +00:00
self . test_download ( preview = True )
2008-04-01 21:54:43 +00:00
def test_download_non_image_with_preview_false ( self ) :
2008-04-02 05:34:50 +00:00
self . test_download ( preview = False )
2008-04-01 21:54:43 +00:00
2008-02-23 23:57:05 +00:00
def test_download_without_login ( self ) :
self . login ( )
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-02-23 23:57:05 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . file_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
2008-04-18 19:54:50 +00:00
path = " /files/download?file_id= %s " % self . file_id
result = self . http_get ( path )
2008-02-23 23:57:05 +00:00
2008-04-18 19:54:50 +00:00
headers = result . get ( " headers " )
assert headers
assert headers . get ( " Location " ) == u " http:///login?after_login= %s " % urllib . quote ( path )
2008-02-23 23:57:05 +00:00
def test_download_without_access ( self ) :
self . login ( )
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-02-23 23:57:05 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . file_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
self . login2 ( )
result = self . http_get (
" /files/download?file_id= %s " % self . file_id ,
session_id = self . session_id ,
)
assert u " access " in result [ u " body " ] [ 0 ]
def test_download_with_unknown_file_id ( self ) :
self . login ( )
result = self . http_get (
" /files/download?file_id= %s " % self . file_id ,
session_id = self . session_id ,
)
assert u " access " in result [ u " body " ] [ 0 ]
2008-02-23 08:14:19 +00:00
2008-09-11 07:36:12 +00:00
def test_download_product ( self ) :
access_id = u " wheeaccessid "
item_number = u " 5000 "
transaction_id = u " txn "
self . login ( )
download_access = Download_access . create ( access_id , item_number , transaction_id )
self . database . save ( download_access )
result = self . http_get (
2008-09-11 23:57:51 +00:00
" /files/download_product?access_id= %s " % access_id ,
2008-09-11 07:36:12 +00:00
session_id = self . session_id ,
)
headers = result [ u " headers " ]
assert headers
assert headers [ u " Content-Type " ] == u " application/octet-stream "
filename = u " test.exe " . encode ( " utf8 " )
assert headers [ u " Content-Disposition " ] == ' attachment; filename= " %s " ' % filename
gen = result [ u " body " ]
assert isinstance ( gen , types . GeneratorType )
pieces = [ ]
try :
for piece in gen :
pieces . append ( piece )
except AttributeError , exc :
if u " session_storage " not in str ( exc ) :
raise exc
file_data = " " . join ( pieces )
assert file_data == self . file_data
2009-03-18 22:11:46 +00:00
def test_download_product_with_nginx ( self ) :
cherrypy . root . files . _Files__web_server = u " nginx "
access_id = u " wheeaccessid "
item_number = u " 5000 "
transaction_id = u " txn "
self . login ( )
download_access = Download_access . create ( access_id , item_number , transaction_id )
self . database . save ( download_access )
result = self . http_get (
" /files/download_product?access_id= %s " % access_id ,
session_id = self . session_id ,
)
headers = result [ u " headers " ]
assert headers
assert headers [ u " Content-Type " ] == u " application/octet-stream "
filename = u " test.exe " . encode ( " utf8 " )
assert headers [ u " Content-Disposition " ] == ' attachment; filename= " %s " ' % filename
assert headers [ u " X-Accel-Redirect " ] == u " /download_product/test.exe "
gen = result [ u " body " ]
assert isinstance ( gen , types . GeneratorType )
pieces = [ ]
try :
for piece in gen :
pieces . append ( piece )
except AttributeError , exc :
if u " session_storage " not in str ( exc ) :
raise exc
file_data = " " . join ( pieces )
assert file_data == u " "
2008-09-11 07:36:12 +00:00
def test_download_product_without_login ( self ) :
access_id = u " wheeaccessid "
item_number = u " 5000 "
transaction_id = u " txn "
download_access = Download_access . create ( access_id , item_number , transaction_id )
self . database . save ( download_access )
result = self . http_get (
2008-09-11 23:57:51 +00:00
" /files/download_product?access_id= %s " % access_id ,
2008-09-11 07:36:12 +00:00
)
headers = result [ u " headers " ]
assert headers
assert headers [ u " Content-Type " ] == u " application/octet-stream "
filename = u " test.exe " . encode ( " utf8 " )
assert headers [ u " Content-Disposition " ] == ' attachment; filename= " %s " ' % filename
gen = result [ u " body " ]
assert isinstance ( gen , types . GeneratorType )
pieces = [ ]
try :
for piece in gen :
pieces . append ( piece )
except AttributeError , exc :
if u " session_storage " not in str ( exc ) :
raise exc
file_data = " " . join ( pieces )
assert file_data == self . file_data
def test_download_product_unknown_access_id ( self ) :
access_id = u " wheeaccessid "
item_number = u " 5000 "
transaction_id = u " txn "
self . login ( )
download_access = Download_access . create ( access_id , item_number , transaction_id )
self . database . save ( download_access )
result = self . http_get (
2008-09-11 23:57:51 +00:00
" /files/download_product?access_id= %s " % u " unknown_id " ,
2008-09-11 07:36:12 +00:00
session_id = self . session_id ,
)
assert u " access " in result [ u " body " ] [ 0 ]
headers = result [ u " headers " ]
assert headers
assert headers [ u " Content-Type " ] == u " text/html "
assert not headers . get ( u " Content-Disposition " )
def test_download_product_missing_file ( self ) :
access_id = u " wheeaccessid "
item_number = u " 5000 "
transaction_id = u " txn "
self . settings [ u " global " ] [ u " luminotes.download_products " ] [ 0 ] [ u " filename " ] = u " notthere.exe "
self . login ( )
download_access = Download_access . create ( access_id , item_number , transaction_id )
self . database . save ( download_access )
result = self . http_get (
2008-09-11 23:57:51 +00:00
" /files/download_product?access_id= %s " % access_id ,
2008-09-11 07:36:12 +00:00
session_id = self . session_id ,
)
assert u " access " in result [ u " body " ] [ 0 ]
headers = result [ u " headers " ]
assert headers
assert headers [ u " Content-Type " ] == u " text/html "
assert not headers . get ( u " Content-Disposition " )
2008-04-01 21:54:43 +00:00
def test_preview ( self ) :
2008-04-02 05:34:50 +00:00
self . login ( )
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-04-02 05:34:50 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . IMAGE_DATA ,
content_type = self . content_type ,
session_id = self . session_id ,
)
2008-04-01 21:54:43 +00:00
2008-04-02 05:34:50 +00:00
result = self . http_get (
" /files/preview?file_id= %s " % self . file_id ,
session_id = self . session_id ,
)
assert result [ u " file_id " ] == self . file_id
assert result [ u " filename " ] == self . filename
assert result [ u " quote_filename " ] == False
2008-04-01 21:54:43 +00:00
2008-09-17 22:38:54 +00:00
def test_preview_with_unicode_filename ( self ) :
self . login ( )
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-09-17 22:38:54 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . unicode_filename ,
file_data = self . IMAGE_DATA ,
content_type = self . content_type ,
session_id = self . session_id ,
)
result = self . http_get (
" /files/preview?file_id= %s " % self . file_id ,
session_id = self . session_id ,
)
assert result [ u " file_id " ] == self . file_id
assert result [ u " filename " ] == self . unicode_filename
assert result [ u " quote_filename " ] == False
2008-04-01 21:54:43 +00:00
def test_preview_with_quote_filename_true ( self ) :
2008-04-02 05:34:50 +00:00
self . login ( )
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-04-02 05:34:50 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . IMAGE_DATA ,
content_type = self . content_type ,
session_id = self . session_id ,
)
result = self . http_get (
" /files/preview?file_id= %s "e_filename=true " % self . file_id ,
session_id = self . session_id ,
)
assert result [ u " file_id " ] == self . file_id
assert result [ u " filename " ] == self . filename
assert result [ u " quote_filename " ] == True
2008-04-01 21:54:43 +00:00
def test_preview_with_quote_filename_false ( self ) :
2008-04-02 05:34:50 +00:00
self . login ( )
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-04-02 05:34:50 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . IMAGE_DATA ,
content_type = self . content_type ,
session_id = self . session_id ,
)
result = self . http_get (
" /files/preview?file_id= %s "e_filename=false " % self . file_id ,
session_id = self . session_id ,
)
assert result [ u " file_id " ] == self . file_id
assert result [ u " filename " ] == self . filename
assert result [ u " quote_filename " ] == False
2008-04-01 21:54:43 +00:00
def test_preview_without_login ( self ) :
2008-04-02 05:34:50 +00:00
self . login ( )
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-04-02 05:34:50 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . file_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
2008-04-18 19:54:50 +00:00
path = " /files/preview?file_id= %s " % self . file_id
result = self . http_get ( path )
2008-04-02 05:34:50 +00:00
2008-04-18 19:54:50 +00:00
headers = result . get ( " headers " )
assert headers
assert headers . get ( " Location " ) == u " http:///login?after_login= %s " % urllib . quote ( path )
2008-04-01 21:54:43 +00:00
def test_preview_without_access ( self ) :
2008-04-02 05:34:50 +00:00
self . login ( )
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-04-02 05:34:50 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . file_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
self . login2 ( )
result = self . http_get (
" /files/preview?file_id= %s " % self . file_id ,
session_id = self . session_id ,
)
assert u " access " in result [ u " error " ]
2008-04-01 21:54:43 +00:00
def test_preview_with_unknown_file_id ( self ) :
2008-04-02 05:34:50 +00:00
self . login ( )
result = self . http_get (
" /files/preview?file_id= %s " % self . file_id ,
session_id = self . session_id ,
)
assert u " access " in result [ u " error " ]
2008-04-01 21:54:43 +00:00
def test_thumbnail ( self ) :
2008-04-02 05:34:50 +00:00
self . login ( )
# make the test image big enough to require scaling down
image = Image . open ( StringIO ( self . IMAGE_DATA ) )
image = image . transform ( ( 250 , 250 ) , Image . QUAD , range ( 8 ) )
image_data = StringIO ( )
image . save ( image_data , " PNG " )
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-04-02 05:34:50 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = image_data . getvalue ( ) ,
content_type = self . content_type ,
session_id = self . session_id ,
)
result = self . http_get (
" /files/thumbnail?file_id= %s " % self . file_id ,
session_id = self . session_id ,
)
headers = result [ u " headers " ]
assert headers
assert headers [ u " Content-Type " ] == self . content_type
assert u " Content-Disposition " not in headers
file_data = " " . join ( result [ u " body " ] )
image = Image . open ( StringIO ( file_data ) )
assert image
assert image . size == ( 125 , 125 )
def test_thumbnail_without_scaling ( self ) :
self . login ( )
# make the test image small enough so that no scaling is performed
image = Image . open ( StringIO ( self . IMAGE_DATA ) )
image = image . transform ( ( 100 , 100 ) , Image . QUAD , range ( 8 ) )
image_data = StringIO ( )
image . save ( image_data , " PNG " )
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-04-02 05:34:50 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = image_data . getvalue ( ) ,
content_type = self . content_type ,
session_id = self . session_id ,
)
result = self . http_get (
" /files/thumbnail?file_id= %s " % self . file_id ,
session_id = self . session_id ,
)
headers = result [ u " headers " ]
assert headers
assert headers [ u " Content-Type " ] == self . content_type
assert u " Content-Disposition " not in headers
file_data = " " . join ( result [ u " body " ] )
image = Image . open ( StringIO ( file_data ) )
assert image
assert image . size == ( 100 , 100 )
def test_thumbnail_different_dimensions ( self ) :
self . login ( )
# make the test image small enough so that no scaling is performed
image = Image . open ( StringIO ( self . IMAGE_DATA ) )
image = image . transform ( ( 250 , 100 ) , Image . QUAD , range ( 8 ) )
image_data = StringIO ( )
image . save ( image_data , " PNG " )
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-04-02 05:34:50 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = image_data . getvalue ( ) ,
content_type = self . content_type ,
session_id = self . session_id ,
)
result = self . http_get (
" /files/thumbnail?file_id= %s " % self . file_id ,
session_id = self . session_id ,
)
headers = result [ u " headers " ]
assert headers
assert headers [ u " Content-Type " ] == self . content_type
assert u " Content-Disposition " not in headers
file_data = " " . join ( result [ u " body " ] )
image = Image . open ( StringIO ( file_data ) )
assert image
assert image . size == ( 125 , 50 )
2008-04-01 21:54:43 +00:00
2008-06-19 00:14:07 +00:00
def test_thumbnail_with_max_size ( self ) :
self . login ( )
# make the test image big enough to require scaling down
image = Image . open ( StringIO ( self . IMAGE_DATA ) )
image = image . transform ( ( 250 , 250 ) , Image . QUAD , range ( 8 ) )
image_data = StringIO ( )
image . save ( image_data , " PNG " )
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-06-19 00:14:07 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = image_data . getvalue ( ) ,
content_type = self . content_type ,
session_id = self . session_id ,
)
max_size = 225
result = self . http_get (
" /files/thumbnail?file_id= %s &max_size= %s " % ( self . file_id , max_size ) ,
session_id = self . session_id ,
)
headers = result [ u " headers " ]
assert headers
assert headers [ u " Content-Type " ] == self . content_type
assert u " Content-Disposition " not in headers
file_data = " " . join ( result [ u " body " ] )
image = Image . open ( StringIO ( file_data ) )
assert image
assert image . size == ( max_size , max_size )
def test_thumbnail_with_max_size_without_scaling ( self ) :
self . login ( )
# make the test image big enough to require scaling down
image = Image . open ( StringIO ( self . IMAGE_DATA ) )
image = image . transform ( ( 250 , 250 ) , Image . QUAD , range ( 8 ) )
image_data = StringIO ( )
image . save ( image_data , " PNG " )
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-06-19 00:14:07 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = image_data . getvalue ( ) ,
content_type = self . content_type ,
session_id = self . session_id ,
)
max_size = 300
result = self . http_get (
" /files/thumbnail?file_id= %s &max_size= %s " % ( self . file_id , max_size ) ,
session_id = self . session_id ,
)
headers = result [ u " headers " ]
assert headers
assert headers [ u " Content-Type " ] == self . content_type
assert u " Content-Disposition " not in headers
file_data = " " . join ( result [ u " body " ] )
image = Image . open ( StringIO ( file_data ) )
assert image
assert image . size == ( 250 , 250 )
2008-04-01 21:54:43 +00:00
def test_thumbnail_with_non_image ( self ) :
2008-04-02 05:34:50 +00:00
self . login ( )
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-04-02 05:34:50 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . file_data , # not a valid image
content_type = self . content_type ,
session_id = self . session_id ,
)
result = self . http_get (
" /files/thumbnail?file_id= %s " % self . file_id ,
session_id = self . session_id ,
)
headers = result [ u " headers " ]
assert headers
assert headers [ u " Content-Type " ] == self . content_type
assert u " Content-Disposition " not in headers
# should get the default thumbnail image
file_data = " " . join ( result [ u " body " ] )
image = Image . open ( StringIO ( file_data ) )
assert image
assert image . size [ 0 ] < = 125
assert image . size [ 1 ] < = 125
2008-04-01 21:54:43 +00:00
def test_thumbnail_without_login ( self ) :
2008-04-02 05:34:50 +00:00
self . login ( )
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-04-02 05:34:50 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . file_data , # not a valid image
content_type = self . content_type ,
session_id = self . session_id ,
)
2008-04-18 19:54:50 +00:00
path = " /files/thumbnail?file_id= %s " % self . file_id
result = self . http_get ( path )
2008-04-02 05:34:50 +00:00
2008-04-18 19:54:50 +00:00
headers = result . get ( " headers " )
assert headers
assert headers . get ( " Location " ) == u " http:///login?after_login= %s " % urllib . quote ( path )
2008-04-01 21:54:43 +00:00
2008-06-19 00:14:07 +00:00
def test_thumbnail_with_invalid_max_size ( self ) :
self . login ( )
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-06-19 00:14:07 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . file_data , # not a valid image
content_type = self . content_type ,
session_id = self . session_id ,
)
result = self . http_get (
" /files/thumbnail?file_id= %s &max_size=0 " % self . file_id ,
session_id = self . session_id ,
)
assert u " max size " in result [ u " body " ] [ 0 ]
2008-04-01 21:54:43 +00:00
def test_thumbnail_without_access ( self ) :
2008-04-02 05:34:50 +00:00
self . login ( )
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-04-02 05:34:50 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . file_data , # not a valid image
content_type = self . content_type ,
session_id = self . session_id ,
)
self . login2 ( )
result = self . http_get (
" /files/thumbnail?file_id= %s " % self . file_id ,
session_id = self . session_id ,
)
assert u " access " in result [ u " body " ] [ 0 ]
2008-04-01 21:54:43 +00:00
def test_thumbnail_with_unknown_file_id ( self ) :
2008-04-02 05:34:50 +00:00
self . login ( )
result = self . http_get (
" /files/thumbnail?file_id= %s " % self . file_id ,
session_id = self . session_id ,
)
assert u " access " in result [ u " body " ] [ 0 ]
2008-04-01 21:54:43 +00:00
def test_image ( self ) :
2008-04-02 05:34:50 +00:00
self . login ( )
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-04-02 05:34:50 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . IMAGE_DATA ,
content_type = self . content_type ,
session_id = self . session_id ,
)
result = self . http_get (
" /files/image?file_id= %s " % self . file_id ,
session_id = self . session_id ,
)
headers = result [ u " headers " ]
assert headers
assert headers [ u " Content-Type " ] == self . content_type
assert u " Content-Disposition " not in headers
assert " " . join ( result [ u " body " ] ) == self . IMAGE_DATA
2008-04-01 21:54:43 +00:00
2009-03-18 22:11:46 +00:00
def test_image_with_nginx ( self ) :
cherrypy . root . files . _Files__web_server = u " nginx "
self . login ( )
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2009-03-18 22:11:46 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . IMAGE_DATA ,
content_type = self . content_type ,
session_id = self . session_id ,
)
result = self . http_get (
" /files/image?file_id= %s " % self . file_id ,
session_id = self . session_id ,
)
headers = result [ u " headers " ]
assert headers
assert headers [ u " Content-Type " ] == self . content_type
assert u " Content-Disposition " not in headers
assert headers [ u " X-Accel-Redirect " ] == u " /download/ %s " % self . file_id
assert " " . join ( result [ u " body " ] ) == u " "
2008-04-01 21:54:43 +00:00
def test_image_with_non_image ( self ) :
2008-04-02 05:34:50 +00:00
self . login ( )
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-04-02 05:34:50 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . file_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
result = self . http_get (
" /files/image?file_id= %s " % self . file_id ,
session_id = self . session_id ,
)
headers = result [ u " headers " ]
assert headers
assert headers [ u " Content-Type " ] == self . content_type
assert u " Content-Disposition " not in headers
assert " " . join ( result [ u " body " ] ) == self . file_data
2008-04-01 21:54:43 +00:00
def test_image_without_login ( self ) :
2008-04-02 05:34:50 +00:00
self . login ( )
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-04-02 05:34:50 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . IMAGE_DATA ,
content_type = self . content_type ,
session_id = self . session_id ,
)
2008-04-18 19:54:50 +00:00
path = " /files/image?file_id= %s " % self . file_id
result = self . http_get ( path )
2008-04-02 05:34:50 +00:00
2008-04-18 19:54:50 +00:00
headers = result . get ( " headers " )
assert headers
assert headers . get ( " Location " ) == u " http:///login?after_login= %s " % urllib . quote ( path )
2008-04-01 21:54:43 +00:00
def test_image_without_access ( self ) :
2008-04-02 05:34:50 +00:00
self . login ( )
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-04-02 05:34:50 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . IMAGE_DATA ,
content_type = self . content_type ,
session_id = self . session_id ,
)
self . login2 ( )
result = self . http_get (
" /files/image?file_id= %s " % self . file_id ,
session_id = self . session_id ,
)
assert u " access " in result [ u " body " ] [ 0 ]
2008-04-01 21:54:43 +00:00
def test_image_with_unknown_file_id ( self ) :
2008-04-02 05:34:50 +00:00
self . login ( )
result = self . http_get (
" /files/image?file_id= %s " % self . file_id ,
session_id = self . session_id ,
)
assert u " access " in result [ u " body " ] [ 0 ]
2008-04-01 21:54:43 +00:00
2009-03-20 07:57:14 +00:00
def test_upload_id ( self ) :
2008-02-23 08:14:19 +00:00
self . login ( )
2008-02-06 01:34:12 +00:00
result = self . http_get (
2009-03-20 07:57:14 +00:00
" /files/upload_id?notebook_id= %s ¬e_id= %s " % ( self . notebook . object_id , self . note . object_id ) ,
2008-02-23 08:14:19 +00:00
session_id = self . session_id ,
2008-02-06 01:34:12 +00:00
)
2008-02-23 08:14:19 +00:00
assert result . get ( u " file_id " )
2009-03-20 07:57:14 +00:00
def test_upload_id_without_login ( self ) :
path = " /files/upload_id?notebook_id= %s ¬e_id= %s " % ( self . notebook . object_id , self . note . object_id )
2008-04-18 19:54:50 +00:00
result = self . http_get ( path )
2008-02-23 08:14:19 +00:00
2008-04-18 19:54:50 +00:00
headers = result . get ( " headers " )
assert headers
assert headers . get ( " Location " ) == u " http:///login?after_login= %s " % urllib . quote ( path )
2008-02-06 01:34:12 +00:00
2009-03-20 07:57:14 +00:00
def test_upload_id_own_notes ( self ) :
2008-11-04 20:17:06 +00:00
self . login ( )
self . database . execute ( self . user . sql_update_access (
self . notebook . object_id , read_write = Notebook . READ_WRITE_FOR_OWN_NOTES , owner = False ,
) )
result = self . http_get (
2009-03-20 07:57:14 +00:00
" /files/upload_id?notebook_id= %s ¬e_id= %s " % ( self . notebook . object_id , self . note . object_id ) ,
2008-11-04 20:17:06 +00:00
session_id = self . session_id ,
)
assert u " access " in result [ u " error " ]
2008-03-18 20:40:39 +00:00
def test_upload ( self , filename = None ) :
2008-02-06 01:34:12 +00:00
self . login ( )
2008-08-19 21:22:40 +00:00
orig_storage_bytes = self . user . storage_bytes
2008-02-06 01:34:12 +00:00
result = self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-02-06 01:34:12 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
2008-03-18 20:40:39 +00:00
filename = filename or self . filename ,
2008-02-06 01:34:12 +00:00
file_data = self . file_data ,
2008-02-23 08:14:19 +00:00
content_type = self . content_type ,
2008-02-06 01:34:12 +00:00
session_id = self . session_id ,
)
2008-02-23 22:17:02 +00:00
assert u " body " not in result
2008-02-23 08:14:19 +00:00
assert u " script " not in result
2008-02-06 01:34:12 +00:00
2008-02-23 08:14:19 +00:00
# assert that the file metadata was actually stored in the database
2008-02-23 08:59:39 +00:00
db_file = self . database . load ( File , self . file_id )
2008-02-23 08:14:19 +00:00
assert db_file
assert db_file . notebook_id == self . notebook . object_id
assert db_file . note_id == self . note . object_id
2008-03-18 20:40:39 +00:00
assert db_file . filename == filename or self . filename
2008-02-23 08:14:19 +00:00
assert db_file . size_bytes == len ( self . file_data )
assert db_file . content_type == self . content_type
2008-02-06 01:34:12 +00:00
2008-02-23 08:14:19 +00:00
# assert that the file data was actually stored
2008-02-23 08:59:39 +00:00
assert Upload_file . open_file ( self . file_id ) . read ( ) == self . file_data
2008-02-06 01:34:12 +00:00
2008-02-23 22:17:02 +00:00
# assert that storage bytes increased
user = self . database . load ( User , self . user . object_id )
assert user . storage_bytes > orig_storage_bytes
2008-03-18 20:40:39 +00:00
def test_upload_with_unicode_filename ( self ) :
self . test_upload ( self . unicode_filename )
2008-11-04 20:17:06 +00:00
def test_upload_own_notes ( self ) :
self . login ( )
self . database . execute ( self . user . sql_update_access (
self . notebook . object_id , read_write = Notebook . READ_WRITE_FOR_OWN_NOTES , owner = False ,
) )
result = self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-11-04 20:17:06 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . file_data ,
content_type = self . content_type ,
)
assert u " access " in result . get ( u " script " )
db_file = self . database . load ( File , self . file_id )
assert db_file is None
assert not Upload_file . exists ( self . file_id )
2008-02-18 20:08:07 +00:00
def test_upload_without_login ( self ) :
2008-02-06 01:34:12 +00:00
result = self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-02-06 01:34:12 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . file_data ,
2008-02-23 08:59:39 +00:00
content_type = self . content_type ,
2008-02-06 01:34:12 +00:00
)
2008-02-23 08:59:39 +00:00
assert u " access " in result . get ( u " script " )
2008-02-06 01:34:12 +00:00
2008-02-23 22:17:02 +00:00
db_file = self . database . load ( File , self . file_id )
assert db_file is None
assert not Upload_file . exists ( self . file_id )
2008-02-18 20:08:07 +00:00
def test_upload_without_access ( self ) :
2008-02-06 01:34:12 +00:00
self . login2 ( )
result = self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-02-06 01:34:12 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . file_data ,
2008-02-23 08:59:39 +00:00
content_type = self . content_type ,
session_id = self . session_id
2008-02-06 01:34:12 +00:00
)
2008-02-23 08:59:39 +00:00
assert u " access " in result . get ( u " script " )
2008-02-06 01:34:12 +00:00
2008-02-23 22:17:02 +00:00
db_file = self . database . load ( File , self . file_id )
assert db_file is None
assert not Upload_file . exists ( self . file_id )
2008-02-06 01:34:12 +00:00
2008-02-23 22:17:02 +00:00
def test_upload_without_file_id ( self ) :
2008-02-06 01:34:12 +00:00
self . login ( )
result = self . http_upload (
" /files/upload " ,
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
2008-02-23 22:17:02 +00:00
filename = self . filename ,
2008-02-06 01:34:12 +00:00
file_data = self . file_data ,
2008-02-23 22:17:02 +00:00
content_type = self . content_type ,
session_id = self . session_id
2008-02-06 01:34:12 +00:00
)
2008-02-23 22:17:02 +00:00
assert u " error " in result [ u " body " ] [ 0 ]
db_file = self . database . load ( File , self . file_id )
assert db_file is None
assert not Upload_file . exists ( self . file_id )
2008-02-06 01:34:12 +00:00
2008-02-23 22:17:02 +00:00
def test_upload_unnamed ( self ) :
2008-02-06 01:34:12 +00:00
self . login ( )
result = self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-02-06 01:34:12 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
2008-02-23 22:17:02 +00:00
filename = " " ,
file_data = self . file_data ,
content_type = self . content_type ,
session_id = self . session_id
2008-02-06 01:34:12 +00:00
)
2008-02-23 22:17:02 +00:00
assert " select a file " in result [ " script " ]
db_file = self . database . load ( File , self . file_id )
assert db_file is None
assert not Upload_file . exists ( self . file_id )
2008-02-06 01:34:12 +00:00
2008-02-18 20:08:07 +00:00
def test_upload_invalid_content_length ( self ) :
self . login ( )
result = self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-02-18 20:08:07 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . file_data ,
2008-02-23 22:17:02 +00:00
content_type = self . content_type ,
2008-02-18 20:08:07 +00:00
headers = [ ( " Content-Length " , " -10 " ) ] ,
2008-02-23 22:17:02 +00:00
session_id = self . session_id
2008-02-18 20:08:07 +00:00
)
2008-02-23 22:17:02 +00:00
assert u " error " in result [ u " body " ] [ 0 ]
db_file = self . database . load ( File , self . file_id )
assert db_file is None
assert not Upload_file . exists ( self . file_id )
2008-02-18 20:08:07 +00:00
def test_upload_cancel ( self ) :
self . login ( )
result = self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-02-18 20:08:07 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . file_data ,
2008-02-23 22:17:02 +00:00
content_type = self . content_type ,
2008-02-18 20:08:07 +00:00
simulate_cancel = True ,
2008-02-23 22:17:02 +00:00
session_id = self . session_id
2008-02-18 20:08:07 +00:00
)
2009-03-20 07:57:14 +00:00
assert " cancel_due_to_error " in result . get ( u " script " )
2008-02-06 01:34:12 +00:00
2008-02-23 22:17:02 +00:00
db_file = self . database . load ( File , self . file_id )
assert db_file is None
assert not Upload_file . exists ( self . file_id )
2008-02-23 08:14:19 +00:00
2008-02-23 22:17:02 +00:00
def test_upload_over_quota ( self ) :
large_file_data = self . file_data * 5
2008-02-23 08:14:19 +00:00
self . login ( )
result = self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-02-23 08:14:19 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
2008-02-23 22:17:02 +00:00
file_data = large_file_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
assert " quota " in result [ " script " ]
db_file = self . database . load ( File , self . file_id )
assert db_file is None
assert not Upload_file . exists ( self . file_id )
2008-08-20 22:08:08 +00:00
def test_upload_no_quota ( self ) :
large_file_data = self . file_data * 5
self . settings [ u " global " ] [ u " luminotes.rate_plans " ] [ 0 ] [ u " storage_quota_bytes " ] = None
self . login ( )
orig_storage_bytes = self . user . storage_bytes
result = self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-08-20 22:08:08 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = large_file_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
assert u " body " not in result
assert u " script " not in result
# assert that the file metadata was actually stored in the database
db_file = self . database . load ( File , self . file_id )
assert db_file
assert db_file . notebook_id == self . notebook . object_id
assert db_file . note_id == self . note . object_id
assert db_file . filename == self . filename
assert db_file . size_bytes == len ( large_file_data )
assert db_file . content_type == self . content_type
# assert that the file data was actually stored
assert Upload_file . open_file ( self . file_id ) . read ( ) == large_file_data
# assert that storage bytes increased
user = self . database . load ( User , self . user . object_id )
assert user . storage_bytes > orig_storage_bytes
2008-02-23 22:17:02 +00:00
def test_progress ( self ) :
self . database . execute ( self . user2 . sql_save_notebook ( self . notebook . object_id , read_write = True , owner = False ) )
self . database . execute ( self . user2 . sql_save_notebook ( self . notebook . trash_id , read_write = True , owner = False ) )
self . login2 ( )
self . database . save ( File ( object_id = self . file_id ) )
# start a file uploading in a separate thread
def upload ( ) :
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-02-23 22:17:02 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . file_data * 1000 ,
content_type = self . content_type ,
session_id = self . session_id ,
)
2008-02-23 23:57:05 +00:00
self . upload_thread = Thread ( target = upload )
2008-02-23 22:17:02 +00:00
result = self . http_get (
2009-03-20 07:57:14 +00:00
" /files/progress?X-Progress-ID= %s " % self . file_id ,
2008-02-23 08:14:19 +00:00
session_id = self . session_id ,
)
2009-03-20 07:57:14 +00:00
assert result . get ( u " state " ) == u " starting "
2008-02-23 08:14:19 +00:00
2009-03-20 07:57:14 +00:00
self . upload_thread . start ( )
2008-02-23 08:14:19 +00:00
2009-03-20 07:57:14 +00:00
received = 0
size = 0
previous_received = 0
# report on that file's upload progress
while True :
result = self . http_get (
" /files/progress?X-Progress-ID= %s " % self . file_id ,
session_id = self . session_id ,
)
state = result . get ( u " state " )
assert state != " error "
if state == " starting " : continue
if state == " done " : break
2008-02-23 08:14:19 +00:00
2009-03-20 07:57:14 +00:00
if state == " uploading " :
received = result . get ( u " received " )
size = result . get ( u " size " )
assert received
assert size
assert received < size
assert received > = previous_received
previous_received = received
2008-02-23 22:17:02 +00:00
def test_progress_without_login ( self ) :
self . database . execute ( self . user2 . sql_save_notebook ( self . notebook . object_id , read_write = True , owner = False ) )
self . database . execute ( self . user2 . sql_save_notebook ( self . notebook . trash_id , read_write = True , owner = False ) )
self . login2 ( ) # this login is for the upload, not the call to progress
self . database . save ( File ( object_id = self . file_id ) )
# start a file uploading in a separate thread
def upload ( ) :
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-02-23 22:17:02 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . file_data * 1000 ,
content_type = self . content_type ,
session_id = self . session_id ,
)
2008-02-23 23:57:05 +00:00
self . upload_thread = Thread ( target = upload )
self . upload_thread . start ( )
2008-02-23 22:17:02 +00:00
# report on that file's upload progress
2009-03-20 07:57:14 +00:00
while True :
path = " /files/progress?X-Progress-ID= %s " % self . file_id
result = self . http_get ( path )
2008-02-23 22:17:02 +00:00
2009-03-20 07:57:14 +00:00
if result . get ( u " state " ) != u " starting " :
break
assert result . get ( u " state " ) == u " error "
assert result . get ( u " status " ) == httplib . FORBIDDEN
2008-02-23 22:17:02 +00:00
def test_progress_for_completed_upload ( self ) :
self . login ( )
self . database . save ( File ( object_id = self . file_id ) )
# upload a file completely
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-02-23 22:17:02 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . file_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
# report on that completed file's upload progress
result = self . http_get (
2009-03-20 07:57:14 +00:00
" /files/progress?X-Progress-ID= %s " % self . file_id ,
2008-02-23 22:17:02 +00:00
session_id = self . session_id ,
)
2009-03-20 07:57:14 +00:00
assert result . get ( u " state " ) == u " done "
2008-02-23 22:17:02 +00:00
def test_progress_with_unknown_file_id ( self ) :
self . login ( )
result = self . http_get (
2009-03-20 07:57:14 +00:00
" /files/progress?X-Progress-ID= %s " % self . file_id ,
2008-02-23 22:17:02 +00:00
session_id = self . session_id ,
)
2009-03-20 07:57:14 +00:00
assert result . get ( u " state " ) == u " error "
assert result . get ( u " status " ) == httplib . NOT_FOUND
2008-02-23 08:14:19 +00:00
2008-09-08 22:11:49 +00:00
def test_progress_no_quota ( self ) :
self . settings [ u " global " ] [ u " luminotes.rate_plans " ] [ 1 ] [ u " storage_quota_bytes " ] = None
self . test_progress ( )
2008-02-23 08:14:19 +00:00
def test_stats ( self ) :
2008-02-23 22:17:02 +00:00
self . login ( )
2008-08-19 21:22:40 +00:00
orig_storage_bytes = self . user . storage_bytes
2008-02-23 22:17:02 +00:00
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-02-23 22:17:02 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . file_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
result = self . http_get (
" /files/stats?file_id= %s " % self . file_id ,
session_id = self . session_id ,
)
assert result [ u " filename " ] == self . filename
assert result [ u " size_bytes " ] == len ( self . file_data )
user = self . database . load ( User , self . user . object_id )
assert result [ u " storage_bytes " ] == user . storage_bytes
assert user . storage_bytes > orig_storage_bytes
2008-11-04 20:17:06 +00:00
def test_stats_own_notes ( self ) :
self . login ( )
self . database . execute ( self . user . sql_update_access (
self . notebook . object_id , read_write = Notebook . READ_WRITE_FOR_OWN_NOTES , owner = False ,
) )
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-11-04 20:17:06 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . file_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
result = self . http_get (
" /files/stats?file_id= %s " % self . file_id ,
session_id = self . session_id ,
)
assert u " access " in result [ " error " ]
2008-02-23 22:17:02 +00:00
def test_stats_without_login ( self ) :
self . login ( ) # this login is for the upload, not the call to stats
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-02-23 22:17:02 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . file_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
2008-04-18 19:54:50 +00:00
path = " /files/stats?file_id= %s " % self . file_id
result = self . http_get ( path )
2008-02-23 22:17:02 +00:00
2008-04-18 19:54:50 +00:00
headers = result . get ( " headers " )
assert headers
assert headers . get ( " Location " ) == u " http:///login?after_login= %s " % urllib . quote ( path )
2008-02-23 22:17:02 +00:00
def test_stats_without_access ( self ) :
self . login ( )
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-02-23 22:17:02 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . file_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
self . login2 ( ) # now login as a different user
result = self . http_get (
" /files/stats?file_id= %s " % self . file_id ,
session_id = self . session_id ,
)
assert u " access " in result [ u " error " ]
def test_stats_with_unknown_file_id ( self ) :
self . login ( )
result = self . http_get (
" /files/stats?file_id= %s " % self . file_id ,
session_id = self . session_id ,
)
assert u " access " in result [ u " error " ]
2008-02-23 08:14:19 +00:00
def test_delete ( self ) :
2008-02-23 22:17:02 +00:00
self . login ( )
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-02-23 22:17:02 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . file_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
2008-08-19 21:22:40 +00:00
orig_storage_bytes = self . user . storage_bytes
2008-02-23 22:17:02 +00:00
result = self . http_post (
" /files/delete " ,
dict (
file_id = self . file_id ,
) ,
session_id = self . session_id ,
)
db_file = self . database . load ( File , self . file_id )
assert db_file is None
assert not Upload_file . exists ( self . file_id )
user = self . database . load ( User , self . user . object_id )
assert result [ u " storage_bytes " ] == user . storage_bytes
assert user . storage_bytes != orig_storage_bytes
2008-11-04 20:17:06 +00:00
def test_delete_own_notes ( self ) :
self . login ( )
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-11-04 20:17:06 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . file_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
self . database . execute ( self . user . sql_update_access (
self . notebook . object_id , read_write = Notebook . READ_WRITE_FOR_OWN_NOTES , owner = False ,
) )
result = self . http_post (
" /files/delete " ,
dict (
file_id = self . file_id ,
) ,
session_id = self . session_id ,
)
assert u " access " in result [ " error " ]
2008-02-23 22:17:02 +00:00
def test_delete_without_login ( self ) :
self . login ( ) # this login is for the upload, not the call to delete
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-02-23 22:17:02 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . file_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
result = self . http_post (
" /files/delete " ,
dict (
file_id = self . file_id ,
) ,
)
assert u " access " in result [ u " error " ]
def test_delete_without_access ( self ) :
self . login ( )
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-02-23 22:17:02 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . file_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
self . login2 ( ) # now login as a different user
result = self . http_post (
" /files/delete " ,
dict (
file_id = self . file_id ,
) ,
session_id = self . session_id ,
)
assert u " access " in result [ u " error " ]
def test_delete_with_unknown_file_id ( self ) :
self . login ( )
result = self . http_post (
" /files/delete " ,
dict (
file_id = self . file_id ,
) ,
session_id = self . session_id ,
)
assert u " access " in result [ u " error " ]
2008-02-23 08:14:19 +00:00
def test_rename ( self ) :
2008-02-23 22:59:36 +00:00
self . login ( )
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-02-23 22:59:36 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . file_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
result = self . http_post (
" /files/rename " ,
dict (
file_id = self . file_id ,
filename = self . new_filename ,
) ,
session_id = self . session_id ,
)
assert u " error " not in result
assert u " body " not in result
db_file = self . database . load ( File , self . file_id )
assert db_file
assert db_file . filename == self . new_filename
assert Upload_file . exists ( self . file_id )
2008-11-04 20:17:06 +00:00
def test_rename_own_notes ( self ) :
self . login ( )
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-11-04 20:17:06 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . file_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
self . database . execute ( self . user . sql_update_access (
self . notebook . object_id , read_write = Notebook . READ_WRITE_FOR_OWN_NOTES , owner = False ,
) )
result = self . http_post (
" /files/rename " ,
dict (
file_id = self . file_id ,
filename = self . new_filename ,
) ,
session_id = self . session_id ,
)
assert u " access " in result [ " error " ]
2008-02-23 22:59:36 +00:00
def test_rename_with_weird_filename ( self ) :
self . login ( )
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-02-23 22:59:36 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . file_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
result = self . http_post (
" /files/rename " ,
dict (
file_id = self . file_id ,
filename = self . weird_filename ,
) ,
session_id = self . session_id ,
)
assert u " error " not in result
assert u " body " not in result
db_file = self . database . load ( File , self . file_id )
assert db_file
assert db_file . filename == self . weird_filename
assert Upload_file . exists ( self . file_id )
def test_rename_without_login ( self ) :
self . login ( )
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-02-23 22:59:36 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . file_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
result = self . http_post (
" /files/rename " ,
dict (
file_id = self . file_id ,
filename = self . new_filename ,
) ,
)
assert u " access " in result [ u " error " ]
db_file = self . database . load ( File , self . file_id )
assert db_file
assert db_file . filename == self . filename
assert Upload_file . exists ( self . file_id )
def test_rename_without_access ( self ) :
self . login ( )
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-02-23 22:59:36 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . file_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
self . login2 ( )
result = self . http_post (
" /files/rename " ,
dict (
file_id = self . file_id ,
filename = self . new_filename ,
) ,
session_id = self . session_id ,
)
assert u " access " in result [ u " error " ]
db_file = self . database . load ( File , self . file_id )
assert db_file
assert db_file . filename == self . filename
assert Upload_file . exists ( self . file_id )
def test_rename_with_unknown_file_id ( self ) :
self . login ( )
result = self . http_post (
" /files/rename " ,
dict (
file_id = self . file_id ,
filename = self . new_filename ,
) ,
session_id = self . session_id ,
)
assert u " access " in result [ u " error " ]
2008-02-23 08:14:19 +00:00
2008-08-08 23:39:53 +00:00
def test_parse_csv ( self ) :
self . login ( )
2008-08-09 08:04:06 +00:00
csv_data = ' " label 1 " , " label 2 " , " label 3 " \n 5, " blah and stuff " ,3.3 \n " 8 " , " whee " , " hmm \n foo " \n 3,4,5 '
2008-08-08 23:39:53 +00:00
expected_rows = [
[ " label 1 " , " label 2 " , " label 3 " ] ,
[ " 5 " , " blah and stuff " , " 3.3 " ] ,
[ " 8 " , " whee " , " hmm \n foo " ] ,
2008-08-09 08:04:06 +00:00
[ " 3 " , " 4 " , " 5 " ] ,
2008-08-08 23:39:53 +00:00
]
2008-08-09 09:13:52 +00:00
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-08-08 23:39:53 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = csv_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
parser = cherrypy . root . files . parse_csv ( self . file_id )
for ( index , row ) in enumerate ( parser ) :
assert row == expected_rows [ index ]
assert index == len ( expected_rows ) - 1
@raises ( Parse_error )
def test_parse_csv_empty ( self ) :
self . login ( )
csv_data = " "
2008-08-09 09:13:52 +00:00
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-08-08 23:39:53 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = csv_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
parser = cherrypy . root . files . parse_csv ( self . file_id )
parser . next ( )
@raises ( Parse_error )
def test_parse_csv_invalid_text ( self ) :
self . login ( )
csv_data = ' " See, Vera? Dress yourself up, you get taken out somewhere fun. -- Jayne '
2008-08-09 09:13:52 +00:00
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-08-08 23:39:53 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = csv_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
parser = cherrypy . root . files . parse_csv ( self . file_id )
parser . next ( )
@raises ( Parse_error )
def test_parse_csv_invalid_binary ( self ) :
self . login ( )
csv_data = self . file_data + " \x00 "
2008-08-09 09:13:52 +00:00
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-08-08 23:39:53 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = csv_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
parser = cherrypy . root . files . parse_csv ( self . file_id )
parser . next ( )
def test_parse_csv_embedded_quotes ( self ) :
self . login ( )
2008-08-09 08:04:06 +00:00
csv_data = ' " label 1 " , " label 2 " , " label 3 " \n 5, " blah " " and " " stuff " ,3.3 \n " 8 " , " whee " , " hmm \n foo " \n 3,4,5 '
2008-08-08 23:39:53 +00:00
expected_rows = [
[ " label 1 " , " label 2 " , " label 3 " ] ,
[ " 5 " , ' blah " and " stuff ' , " 3.3 " ] ,
[ " 8 " , " whee " , " hmm \n foo " ] ,
2008-08-09 08:04:06 +00:00
[ " 3 " , " 4 " , " 5 " ] ,
2008-08-08 23:39:53 +00:00
]
2008-08-09 09:13:52 +00:00
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-08-08 23:39:53 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = csv_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
parser = cherrypy . root . files . parse_csv ( self . file_id )
for ( index , row ) in enumerate ( parser ) :
assert row == expected_rows [ index ]
assert index == len ( expected_rows ) - 1
@raises ( Parse_error )
def test_parse_csv_different_row_element_counts ( self ) :
self . login ( )
2008-08-09 08:04:06 +00:00
csv_data = ' " label 1 " , " label 2 " , " label 3 " \n 5, " blah and stuff " \n " 8 " , " whee " , " hmm \n foo " ,4.4 \n 3,4,5 '
2008-08-08 23:39:53 +00:00
2008-08-09 09:13:52 +00:00
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-08-08 23:39:53 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = csv_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
parser = cherrypy . root . files . parse_csv ( self . file_id )
for row in parser :
pass
def test_parse_csv_empty_rows ( self ) :
self . login ( )
2008-08-09 08:04:06 +00:00
csv_data = ' " label 1 " , " label 2 " , " label 3 " \n \n 5, " blah and stuff " ,3.3 \n " 8 " , " whee " , " hmm \n foo " \n \n 3,4,5 '
2008-08-08 23:39:53 +00:00
expected_rows = [
[ " label 1 " , " label 2 " , " label 3 " ] ,
[ " 5 " , " blah and stuff " , " 3.3 " ] ,
[ " 8 " , " whee " , " hmm \n foo " ] ,
2008-08-09 08:04:06 +00:00
[ " 3 " , " 4 " , " 5 " ] ,
2008-08-08 23:39:53 +00:00
]
2008-08-09 09:13:52 +00:00
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-08-08 23:39:53 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = csv_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
parser = cherrypy . root . files . parse_csv ( self . file_id )
for ( index , row ) in enumerate ( parser ) :
assert row == expected_rows [ index ]
assert index == len ( expected_rows ) - 1
@raises ( Parse_error )
def test_parse_csv_unknown_file_id ( self ) :
parser = cherrypy . root . files . parse_csv ( u " unknownfileid " )
for row in parser :
pass
def test_parse_csv_without_header ( self ) :
self . login ( )
2008-08-09 08:04:06 +00:00
csv_data = ' 5, " blah and stuff " ,3.3 \n " 8 " , " whee " , " hmm \n foo " \n 3,4,5 '
2008-08-08 23:39:53 +00:00
expected_rows = [
[ " 5 " , " blah and stuff " , " 3.3 " ] ,
[ " 8 " , " whee " , " hmm \n foo " ] ,
2008-08-09 08:04:06 +00:00
[ " 3 " , " 4 " , " 5 " ] ,
2008-08-08 23:39:53 +00:00
]
2008-08-09 09:13:52 +00:00
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-08-08 23:39:53 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = csv_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
parser = cherrypy . root . files . parse_csv ( self . file_id )
for ( index , row ) in enumerate ( parser ) :
assert row == expected_rows [ index ]
assert index == len ( expected_rows ) - 1
def test_parse_csv_skip_header ( self ) :
self . login ( )
2008-08-09 08:04:06 +00:00
csv_data = ' " label 1 " , " label 2 " , " label 3 " \n 5, " blah and stuff " ,3.3 \n " 8 " , " whee " , " hmm \n foo " \n 3,4,5 '
2008-08-08 23:39:53 +00:00
expected_rows = [
[ " 5 " , " blah and stuff " , " 3.3 " ] ,
[ " 8 " , " whee " , " hmm \n foo " ] ,
2008-08-09 08:04:06 +00:00
[ " 3 " , " 4 " , " 5 " ] ,
2008-08-08 23:39:53 +00:00
]
2008-08-09 09:13:52 +00:00
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-08-08 23:39:53 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = csv_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
parser = cherrypy . root . files . parse_csv ( self . file_id , skip_header = True )
for ( index , row ) in enumerate ( parser ) :
assert row == expected_rows [ index ]
assert index == len ( expected_rows ) - 1
def test_parse_csv_skip_header_without_header ( self ) :
self . login ( )
2008-08-09 08:04:06 +00:00
csv_data = ' 5, " blah and stuff " ,3.3 \n " 8 " , " whee " , " hmm \n foo " \n 3,4,5 '
2008-08-08 23:39:53 +00:00
expected_rows = [
[ " 5 " , " blah and stuff " , " 3.3 " ] ,
[ " 8 " , " whee " , " hmm \n foo " ] ,
2008-08-09 08:04:06 +00:00
[ " 3 " , " 4 " , " 5 " ] ,
2008-08-08 23:39:53 +00:00
]
2008-08-09 09:13:52 +00:00
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-08-08 23:39:53 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = csv_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
parser = cherrypy . root . files . parse_csv ( self . file_id , skip_header = True )
for ( index , row ) in enumerate ( parser ) :
assert row == expected_rows [ index ]
assert index == len ( expected_rows ) - 1
def test_csv_head ( self ) :
2008-08-09 09:13:52 +00:00
self . login ( )
csv_data = ' " label 1 " , " label 2 " , " label 3 " \n 5, " blah and stuff " ,3.3 \n " 8 " , " whee " , " hmm \n foo " \n 3,4,5 \n 6,7,8 \n " yay " ,9,10 '
expected_rows = [
[ " label 1 " , " label 2 " , " label 3 " ] ,
[ " 5 " , " blah and stuff " , " 3.3 " ] ,
[ " 8 " , " whee " , " hmm \n foo " ] ,
[ " 3 " , " 4 " , " 5 " ] ,
]
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-08-09 09:13:52 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = csv_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
result = self . http_get (
" /files/csv_head?file_id= %s " % self . file_id ,
session_id = self . session_id ,
)
assert result [ u " file_id " ] == self . file_id
for ( index , row ) in enumerate ( result [ u " rows " ] ) :
assert row == expected_rows [ index ]
assert index == len ( expected_rows ) - 1
2008-11-04 20:17:06 +00:00
def test_csv_head_own_notes ( self ) :
self . login ( )
csv_data = ' " label 1 " , " label 2 " , " label 3 " \n 5, " blah and stuff " ,3.3 \n " 8 " , " whee " , " hmm \n foo " \n 3,4,5 \n 6,7,8 \n " yay " ,9,10 '
expected_rows = [
[ " label 1 " , " label 2 " , " label 3 " ] ,
[ " 5 " , " blah and stuff " , " 3.3 " ] ,
[ " 8 " , " whee " , " hmm \n foo " ] ,
[ " 3 " , " 4 " , " 5 " ] ,
]
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-11-04 20:17:06 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = csv_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
self . database . execute ( self . user . sql_update_access (
self . notebook . object_id , read_write = Notebook . READ_WRITE_FOR_OWN_NOTES , owner = False ,
) )
result = self . http_get (
" /files/csv_head?file_id= %s " % self . file_id ,
session_id = self . session_id ,
)
assert u " access " in result [ " error " ]
2008-08-09 09:13:52 +00:00
def test_csv_head_without_login ( self ) :
self . login ( )
csv_data = ' " label 1 " , " label 2 " , " label 3 " \n 5, " blah and stuff " ,3.3 \n " 8 " , " whee " , " hmm \n foo " \n 3,4,5 \n 6,7,8 \n " yay " ,9,10 '
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-08-09 09:13:52 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = csv_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
path = " /files/csv_head?file_id= %s " % self . file_id
result = self . http_get ( path )
headers = result . get ( " headers " )
assert headers
assert headers . get ( " Location " ) == u " http:///login?after_login= %s " % urllib . quote ( path )
def test_csv_head_without_access ( self ) :
self . login ( )
csv_data = ' " label 1 " , " label 2 " , " label 3 " \n 5, " blah and stuff " ,3.3 \n " 8 " , " whee " , " hmm \n foo " \n 3,4,5 \n 6,7,8 \n " yay " ,9,10 '
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-08-09 09:13:52 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = csv_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
self . login2 ( )
result = self . http_get (
" /files/csv_head?file_id= %s " % self . file_id ,
session_id = self . session_id ,
)
assert u " access " in result [ " error " ]
def test_csv_head_unknown_file_id ( self ) :
self . login ( )
result = self . http_get (
" /files/csv_head?file_id=unknownfileid " ,
session_id = self . session_id ,
)
assert u " access " in result [ " error " ]
def test_csv_head_few_rows ( self ) :
self . login ( )
csv_data = ' " label 1 " , " label 2 " , " label 3 " \n 5, " blah and stuff " ,3.3 '
expected_rows = [
[ " label 1 " , " label 2 " , " label 3 " ] ,
[ " 5 " , " blah and stuff " , " 3.3 " ] ,
]
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-08-09 09:13:52 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = csv_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
result = self . http_get (
" /files/csv_head?file_id= %s " % self . file_id ,
session_id = self . session_id ,
)
assert result [ u " file_id " ] == self . file_id
for ( index , row ) in enumerate ( result [ u " rows " ] ) :
assert row == expected_rows [ index ]
assert index == len ( expected_rows ) - 1
def test_csv_head_large_elements ( self ) :
self . login ( )
csv_data = ' " label 1 " , " label 2 " , " label 3 " \n 5, " Yes. Yes, this is a fertile land, and we will thrive. We will rule over all this land, and we will call it... This Land. " ,3.3 \n " 8 " , " whee " , " hmm \n foo " \n 3,4,5 \n 6,7,8 \n " yay " ,9,10 '
expected_rows = [
[ " label 1 " , " label 2 " , " label 3 " ] ,
[ " 5 " , " Yes. Yes, this is a fertile la ... " , " 3.3 " ] ,
[ " 8 " , " whee " , " hmm \n foo " ] ,
[ " 3 " , " 4 " , " 5 " ] ,
]
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-08-09 09:13:52 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = csv_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
result = self . http_get (
" /files/csv_head?file_id= %s " % self . file_id ,
session_id = self . session_id ,
)
assert result [ u " file_id " ] == self . file_id
for ( index , row ) in enumerate ( result [ u " rows " ] ) :
assert row == expected_rows [ index ]
assert index == len ( expected_rows ) - 1
def test_csv_head_many_elements_per_row ( self ) :
self . login ( )
row0 = ' 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22 '
row1 = ' a,b,c,d,e,f,g,h,i,i,j,k,l,m,n,o,p,q,r,s,t,u '
csv_data = ' %s \n %s \n ' % ( row0 , row1 )
expected_rows = [
row0 . split ( ' , ' ) [ : 20 ] ,
row1 . split ( ' , ' ) [ : 20 ] ,
]
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-08-09 09:13:52 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = csv_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
result = self . http_get (
" /files/csv_head?file_id= %s " % self . file_id ,
session_id = self . session_id ,
)
assert result [ u " file_id " ] == self . file_id
for ( index , row ) in enumerate ( result [ u " rows " ] ) :
assert row == expected_rows [ index ]
assert index == len ( expected_rows ) - 1
2008-08-08 23:39:53 +00:00
2008-02-23 08:14:19 +00:00
def test_purge_unused ( self ) :
2008-02-23 23:16:39 +00:00
self . login ( )
2008-08-19 21:22:40 +00:00
result = self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-02-23 23:16:39 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . file_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
2008-08-19 21:22:40 +00:00
db_file = self . database . load ( File , self . file_id )
assert db_file
2008-02-23 23:16:39 +00:00
# the file is not linked to from the note's contents, so this should delete it
cherrypy . root . files . purge_unused ( self . note )
db_file = self . database . load ( File , self . file_id )
assert db_file is None
assert not Upload_file . exists ( self . file_id )
def test_purge_unused_empty_link ( self ) :
self . login ( )
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-02-23 23:16:39 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . file_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
self . note . contents = ' <a href= " /files/download?file_id= %s " ></a> ' % self . file_id
self . database . save ( self . note )
# the file is linked to from the note's contents but the link title is empty, so this should
# delete it
cherrypy . root . files . purge_unused ( self . note )
db_file = self . database . load ( File , self . file_id )
assert db_file is None
assert not Upload_file . exists ( self . file_id )
2008-03-27 03:48:13 +00:00
def test_purge_unused_empty_link_with_quote_filename ( self ) :
self . login ( )
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-03-27 03:48:13 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . file_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
self . note . contents = ' <a href= " /files/download?file_id= %s "e_filename=true " ></a> ' % self . file_id
self . database . save ( self . note )
# the file is linked to from the note's contents but the link title is empty, so this should
# delete it
cherrypy . root . files . purge_unused ( self . note )
db_file = self . database . load ( File , self . file_id )
assert db_file is None
assert not Upload_file . exists ( self . file_id )
2008-02-23 23:16:39 +00:00
def test_purge_unused_keep_file ( self ) :
self . login ( )
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-02-23 23:16:39 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . file_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
self . note . contents = ' <a href= " /files/download?file_id= %s " >file link</a> ' % self . file_id
self . database . save ( self . note )
2008-03-27 02:50:16 +00:00
# the file is linked to from the note's contents, so this should not delete it
cherrypy . root . files . purge_unused ( self . note )
db_file = self . database . load ( File , self . file_id )
2008-06-13 22:55:19 +00:00
assert db_file
assert db_file . object_id == self . file_id
assert db_file . filename == self . filename
assert Upload_file . exists ( self . file_id )
2008-11-06 20:30:00 +00:00
def test_purge_unused_keep_file_with_nofollow ( self ) :
self . login ( )
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-11-06 20:30:00 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . file_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
self . note . contents = ' <a href= " /files/download?file_id= %s " rel= " nofollow " >file link</a> ' % self . file_id
self . database . save ( self . note )
# the file is linked to from the note's contents, so this should not delete it
cherrypy . root . files . purge_unused ( self . note )
db_file = self . database . load ( File , self . file_id )
assert db_file
assert db_file . object_id == self . file_id
assert db_file . filename == self . filename
assert Upload_file . exists ( self . file_id )
2008-06-13 22:55:19 +00:00
def test_purge_unused_keep_image_file ( self ) :
self . login ( )
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-06-13 22:55:19 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . file_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
self . note . contents = ' <a href= " /files/download?file_id= %s " ><img src= " /blah " ></a> ' % self . file_id
self . database . save ( self . note )
# the image file is linked to from the note's contents, so this should not delete it
cherrypy . root . files . purge_unused ( self . note )
db_file = self . database . load ( File , self . file_id )
2008-03-27 02:50:16 +00:00
assert db_file
assert db_file . object_id == self . file_id
assert db_file . filename == self . filename
assert Upload_file . exists ( self . file_id )
def test_purge_unused_keep_file_with_quote_filename ( self ) :
self . login ( )
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-03-27 02:50:16 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . file_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
self . note . contents = ' <a href= " /files/download?file_id= %s "e_filename=false " >file link</a> ' % self . file_id
self . database . save ( self . note )
2008-02-23 23:16:39 +00:00
# the file is linked to from the note's contents, so this should not delete it
cherrypy . root . files . purge_unused ( self . note )
db_file = self . database . load ( File , self . file_id )
assert db_file
assert db_file . object_id == self . file_id
assert db_file . filename == self . filename
assert Upload_file . exists ( self . file_id )
def test_purge_unused_all_links ( self ) :
self . login ( )
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-02-23 23:16:39 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . file_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
self . note . contents = ' <a href= " /files/download?file_id= %s " >file link</a> ' % self . file_id
self . database . save ( self . note )
# the file is linked to from the note's contents, but because of the purge_all_links flag, it
# should be deleted anyway
cherrypy . root . files . purge_unused ( self . note , purge_all_links = True )
db_file = self . database . load ( File , self . file_id )
assert db_file is None
assert not Upload_file . exists ( self . file_id )
2008-03-27 03:48:13 +00:00
def test_purge_unused_all_links_with_quote_filename ( self ) :
self . login ( )
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-03-27 03:48:13 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . file_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
self . note . contents = ' <a href= " /files/download?file_id= %s "e_filename=true " >file link</a> ' % self . file_id
self . database . save ( self . note )
# the file is linked to from the note's contents, but because of the purge_all_links flag, it
# should be deleted anyway
cherrypy . root . files . purge_unused ( self . note , purge_all_links = True )
db_file = self . database . load ( File , self . file_id )
assert db_file is None
assert not Upload_file . exists ( self . file_id )
2008-02-23 23:16:39 +00:00
def test_purge_unused_multiple_files ( self ) :
self . login ( )
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-02-23 23:16:39 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . file_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
self . note . contents = ' <a href= " /files/download?file_id= %s " >file link</a> ' % self . file_id
self . database . save ( self . note )
other_file_id = u " 23 "
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % other_file_id ,
2008-02-23 23:16:39 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = u " otherfile.png " ,
file_data = u " whee " ,
content_type = self . content_type ,
2008-03-27 03:48:13 +00:00
session_id = self . session_id ,
)
# one file is linked from the note's contents but the other is not. the file that is not linked
# should be deleted
cherrypy . root . files . purge_unused ( self . note )
db_file = self . database . load ( File , self . file_id )
assert db_file
assert db_file . object_id == self . file_id
assert db_file . filename == self . filename
assert Upload_file . exists ( self . file_id )
other_db_file = self . database . load ( File , other_file_id )
assert other_db_file is None
assert not Upload_file . exists ( other_file_id )
2008-06-16 23:22:50 +00:00
def test_purge_unused_multiple_files ( self ) :
self . login ( )
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-06-16 23:22:50 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . file_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
self . note . contents = ' <a href= " /files/download?file_id= %s " >file link</a> ' % self . file_id
self . database . save ( self . note )
other_file_id = u " 23 "
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % other_file_id ,
2008-06-16 23:22:50 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = u " otherfile.png " ,
file_data = u " whee " ,
content_type = self . content_type ,
session_id = self . session_id ,
)
# one file is linked from the note's contents but the other is not. the file that is not linked
# should be deleted
cherrypy . root . files . purge_unused ( self . note )
db_file = self . database . load ( File , self . file_id )
assert db_file
assert db_file . object_id == self . file_id
assert db_file . filename == self . filename
assert Upload_file . exists ( self . file_id )
other_db_file = self . database . load ( File , other_file_id )
assert other_db_file is None
assert not Upload_file . exists ( other_file_id )
2008-03-27 03:48:13 +00:00
def test_purge_unused_multiple_files_with_quote_filename ( self ) :
self . login ( )
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-03-27 03:48:13 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . file_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
self . note . contents = ' <a href= " /files/download?file_id= %s "e_filename=false " >file link</a> ' % self . file_id
self . database . save ( self . note )
other_file_id = u " 23 "
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % other_file_id ,
2008-03-27 03:48:13 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = u " otherfile.png " ,
file_data = u " whee " ,
content_type = self . content_type ,
2008-02-23 23:16:39 +00:00
session_id = self . session_id ,
)
# one file is linked from the note's contents but the other is not. the file that is not linked
# should be deleted
cherrypy . root . files . purge_unused ( self . note )
db_file = self . database . load ( File , self . file_id )
assert db_file
assert db_file . object_id == self . file_id
assert db_file . filename == self . filename
assert Upload_file . exists ( self . file_id )
other_db_file = self . database . load ( File , other_file_id )
assert other_db_file is None
assert not Upload_file . exists ( other_file_id )
2008-02-06 01:34:12 +00:00
2008-06-16 23:22:50 +00:00
def test_purge_unused_multiple_image_files ( self ) :
self . login ( )
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % self . file_id ,
2008-06-16 23:22:50 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = self . filename ,
file_data = self . file_data ,
content_type = self . content_type ,
session_id = self . session_id ,
)
self . note . contents = ' <a href= " /files/download?file_id= %s " ><img src= " /blah " ></a> ' % self . file_id
self . database . save ( self . note )
other_file_id = u " 23 "
self . http_upload (
2009-03-20 07:57:14 +00:00
" /files/upload?X-Progress-ID= %s " % other_file_id ,
2008-06-16 23:22:50 +00:00
dict (
notebook_id = self . notebook . object_id ,
note_id = self . note . object_id ,
) ,
filename = u " otherfile.png " ,
file_data = u " whee " ,
content_type = self . content_type ,
session_id = self . session_id ,
)
# one images file is linked from the note's contents but the other is not. the file that is not
# linked should be deleted
cherrypy . root . files . purge_unused ( self . note )
db_file = self . database . load ( File , self . file_id )
assert db_file
assert db_file . object_id == self . file_id
assert db_file . filename == self . filename
assert Upload_file . exists ( self . file_id )
other_db_file = self . database . load ( File , other_file_id )
assert other_db_file is None
assert not Upload_file . exists ( other_file_id )
2008-02-06 01:34:12 +00:00
def login ( self ) :
result = self . http_post ( " /users/login " , dict (
username = self . username ,
password = self . password ,
login_button = u " login " ,
) )
self . session_id = result [ u " session_id " ]
def login2 ( self ) :
result = self . http_post ( " /users/login " , dict (
username = self . username2 ,
password = self . password2 ,
login_button = u " login " ,
) )
self . session_id = result [ u " session_id " ]