def test_file_path(self): # absolute path self.assertEqual(__file__, file_path(__file__)) self.assertEqual(__file__, file_path(__file__, filter_ext=None)) # means "no filter" too self.assertEqual(__file__, file_path(__file__, filter_ext=('.py',))) # directory target is ok self.assertEqual(os.path.dirname(__file__), file_path(os.path.join(__file__, '..'))) # relative path relpath = os.path.join(*(__file__.split(os.sep)[-3:])) # 'base/tests/test_misc.py' self.assertEqual(__file__, file_path(relpath)) self.assertEqual(__file__, file_path(relpath, filter_ext=('.py',))) # leading 'addons/' is ignored if present self.assertTrue(file_path("addons/web/__init__.py")) relpath = os.path.join('addons', relpath) # 'addons/base/tests/test_misc.py' self.assertEqual(__file__, file_path(relpath)) # files in root_path are allowed self.assertTrue(file_path('tools/misc.py')) # errors when outside addons_paths self.assertCannotAccess('/doesnt/exist') self.assertCannotAccess('/tmp') self.assertCannotAccess('../../../../../../../../../tmp') self.assertCannotAccess(os.path.join(__file__, '../../../../../')) # data_dir is forbidden self.assertCannotAccess(config['data_dir']) # errors for illegal extensions self.assertCannotAccess(__file__, ValueError, filter_ext=('.png',)) # file doesnt exist but has wrong extension self.assertCannotAccess(__file__.replace('.py', '.foo'), ValueError, filter_ext=('.png',))
def get_fonts(self, fontname=None): """This route will return a list of base64 encoded fonts. Those fonts will be proposed to the user when creating a signature using mode 'auto'. :return: base64 encoded fonts :rtype: list """ supported_exts = ('.ttf', '.otf', '.woff', '.woff2') fonts = [] fonts_directory = file_path( os.path.join('web', 'static', 'fonts', 'sign')) if fontname: font_path = os.path.join(fonts_directory, fontname) with file_open(font_path, 'rb', filter_ext=supported_exts) as font_file: font = base64.b64encode(font_file.read()) fonts.append(font) else: font_filenames = sorted([ fn for fn in os.listdir(fonts_directory) if fn.endswith(supported_exts) ]) for filename in font_filenames: font_file = file_open(os.path.join(fonts_directory, filename), 'rb', filter_ext=supported_exts) font = base64.b64encode(font_file.read()) fonts.append(font) return fonts
def test_misc3_is_static_file(self): uri = 'test_http/static/src/img/gizeh.png' path = file_path(uri) # Valid URLs self.assertEqual(root.get_static_file(f'/{uri}'), path, "Valid file") self.assertEqual( root.get_static_file(f'odoo.com/{uri}', host='odoo.com'), path, "Valid file with valid host") self.assertEqual( root.get_static_file(f'http://odoo.com/{uri}', host='odoo.com'), path, "Valid file with valid host") # Invalid URLs self.assertIsNone(root.get_static_file('/test_http/i-dont-exist'), "File doesn't exist") self.assertIsNone(root.get_static_file('/test_http/__manifest__.py'), "File is not static") self.assertIsNone(root.get_static_file(f'odoo.com/{uri}'), "No host allowed") self.assertIsNone(root.get_static_file(f'http://odoo.com/{uri}'), "No host allowed")
def assertCannotAccess(self, path, ExceptionType=FileNotFoundError, filter_ext=None): with self.assertRaises(ExceptionType): file_path(path, filter_ext=filter_ext)