def test_burn_after_read(self): # Upload a random file _file = osjoin(self.testdir, 'test_file') test_md5 = write_random_file(_file) rv = self.app.post('/', data={ 'file': (open(_file, 'r'), 'test_pastefile_random.file'), 'burn': 'True', }) # Try to get the file but can't acquire the lock. Shouldn't not send the file. with mock.patch('pastefile.controller.JsonDB._lock', mock.Mock(return_value=False)): rv = self.app.get('/%s' % test_md5, headers={'User-Agent': 'curl'}) self.assertEquals("Can't lock db for burning file", rv.get_data()) # Try to get the file with the lock acquired. Should send the file. rv = self.app.get('/%s' % test_md5, headers={'User-Agent': 'curl'}) gotten_file = osjoin(self.testdir, 'gotten_test_file') gotten_test_md5 = write_file(filename=gotten_file, content=rv.get_data()) self.assertEquals(test_md5, gotten_test_md5) # Try to get the file a second time, shouldn't work and return a 404 since it is 'burned'. rv = self.app.get('/%s' % test_md5, headers={'User-Agent': 'curl'}) self.assertEquals(rv.status, '404 NOT FOUND')
def test_display_feature(self): flaskr.app.config['DISPLAY_FOR'] = ['firefox'] # Upload a txt file and random file binary_file = osjoin(self.testdir, 'binary_file') binary_md5 = write_file(binary_file, '') self.app.post('/', data={ 'file': (open(binary_file, 'r'), 'test_pastefile.binary'), }) txt_file = osjoin(self.testdir, 'txt_file') txt_md5 = write_file(txt_file, 'foobar') self.app.post('/', data={ 'file': (open(txt_file, 'r'), 'test_pastefile.txt'), }) # Get the file without display mode cause curl useragent # The header Content-Disposition with attachment is only added # when a file will not be displayed rv = self.app.get("/%s" % (binary_md5), headers={'User-Agent': 'curl'}) self.assertEquals(rv.headers['Content-Disposition'], 'attachment; filename=test_pastefile.binary') rv = self.app.get("/%s" % (txt_md5), headers={'User-Agent': 'curl'}) self.assertEquals(rv.headers['Content-Disposition'], 'attachment; filename=test_pastefile.txt') # do the same with foo user agent. display should be enabled # Header Content-Disposition should not have attachment # Content-type should contain the good mem type rv = self.app.get("/%s" % (binary_md5), headers={'User-Agent': 'firefox'}) # Type for empty file self.assertEquals(rv.headers['Content-Type'], 'inode/x-empty') self.assertFalse('Content-Disposition' in rv.headers) rv = self.app.get("/%s" % (txt_md5), headers={'User-Agent': 'firefox'}) self.assertEquals(rv.headers['Content-Type'], 'text/plain; charset=utf-8') self.assertFalse('Content-Disposition' in rv.headers)
def test_upload_and_retrieve(self): # Upload a random file _file = osjoin(self.testdir, 'test_file') test_md5 = write_random_file(_file) rv = self.app.post('/', data={ 'file': (open(_file, 'r'), 'test_pastefile_random.file'), }) self.assertEquals(rv.get_data(), "http://localhost/%s\n" % (test_md5)) self.assertEquals(rv.status, '200 OK') # Get the file rv = self.app.get("/%s" % (test_md5), headers={'User-Agent': 'curl'}) gotten_file = osjoin(self.testdir, 'gotten_test_file') gotten_test_md5 = write_file(filename=gotten_file, content=rv.get_data()) self.assertEquals(test_md5, gotten_test_md5) self.assertEquals(rv.status, '200 OK') self.assertEquals(rv.headers['Content-Disposition'], 'attachment; filename=test_pastefile_random.file') # Try to re upload the same file. Should return same url rv = self.app.post('/', data={ 'file': (open(_file, 'r'), 'test_pastefile_random.file'), }) self.assertEquals(rv.get_data(), "http://localhost/%s\n" % (test_md5)) # Try to upload a second file with the same filename. Both file should still available _file_bis = osjoin(self.testdir, 'test_file') test_md5_bis = write_random_file(_file_bis) rv = self.app.post('/', data={ 'file': (open(_file_bis, 'r'), 'test_pastefile_random.file'), }) self.assertEquals(rv.get_data(), "http://localhost/%s\n" % (test_md5_bis)) db_content = json.load(open(flaskr.app.config['FILE_LIST'])) md5s = sorted([md5 for md5 in db_content.keys()]) self.assertEquals(sorted([test_md5, test_md5_bis]), md5s) # can't lock the database, post should work for an existing file (using last test file) with mock.patch('pastefile.controller.JsonDB._lock', mock.Mock(return_value=False)): # Take file from last test rv = self.app.post('/', data={ 'file': (open(_file_bis, 'r'), 'test_pastefile_random.file'), }) self.assertEquals(rv.get_data(), "http://localhost/%s\n" % (test_md5_bis)) # can't lock the database, get should work (using last test file) with mock.patch('pastefile.controller.JsonDB._lock', mock.Mock(return_value=False)): # Take file from last test rv = self.app.get("/%s" % (test_md5_bis), headers={'User-Agent': 'curl'}) gotten_file = osjoin(self.testdir, 'gotten_test_file') gotten_test_md5 = write_file(filename=gotten_file, content=rv.get_data()) self.assertEquals(test_md5_bis, gotten_test_md5) self.assertEquals(rv.status, '200 OK') # can't lock the database, post should NOT work for new file with mock.patch('pastefile.controller.JsonDB._lock', mock.Mock(return_value=False)): _file = osjoin(self.testdir, 'test_file') test_md5 = write_random_file(_file) rv = self.app.post('/', data={ 'file': (open(_file, 'r'), 'test_pastefile_random.file'), }) self.assertTrue('Unable to upload the file' in rv.get_data())
def test_burn_after_read(self): # Upload a random file _file = osjoin(self.testdir, 'test_file') test_md5 = write_random_file(_file) rv = self.app.post('/', data={ 'file': (open(_file, 'r'), 'test_pastefile_random.file'), 'burn': 'True', }) # Check if the flag burn is set on the file rv = self.app.get('/%s/infos' % test_md5, headers={'User-Agent': 'curl'}) rv_json = json.loads(rv.get_data()) self.assertEquals(rv_json['burn_after_read'], 'True') # Try to get the file but can't acquire the lock. # Shouldn't not send the file. with mock.patch('pastefile.controller.JsonDB._lock', mock.Mock(return_value=False)): rv = self.app.get('/%s' % test_md5, headers={'User-Agent': 'curl'}) self.assertEquals("Can't lock db for burning file", rv.get_data()) # Try to get the file with the lock acquired. Should send the file. rv = self.app.get('/%s' % test_md5, headers={'User-Agent': 'curl'}) gotten_file = osjoin(self.testdir, 'gotten_test_file') gotten_test_md5 = write_file(filename=gotten_file, content=rv.get_data()) self.assertEquals(test_md5, gotten_test_md5) # Try to get the file a second time, shouldn't work and return # a 404 since it is 'burned'. rv = self.app.get('/%s' % test_md5, headers={'User-Agent': 'curl'}) self.assertEquals(rv.status, '404 NOT FOUND') # This should file should have a burned flag rv = self.app.get('/%s/infos' % test_md5, headers={'User-Agent': 'curl'}) rv_json = json.loads(rv.get_data()) self.assertEquals(rv_json['burn_after_read'], 'Burned') # Upload the file again, it should be unburned rv = self.app.post('/', data={ 'file': (open(_file, 'r'), 'test_pastefile_random.file'), 'burn': 'True', }) rv = self.app.get('/%s/infos' % test_md5, headers={'User-Agent': 'curl'}) rv_json = json.loads(rv.get_data()) self.assertEquals(rv_json['burn_after_read'], 'True') # Upload the file again to disable burn rv = self.app.post('/', data={ 'file': (open(_file, 'r'), 'test_pastefile_random.file'), 'burn': 'False', }) rv = self.app.get('/%s/infos' % test_md5, headers={'User-Agent': 'curl'}) rv_json = json.loads(rv.get_data()) self.assertEquals(rv_json['burn_after_read'], 'False')