def import_bookmarks(): """ Import bookmarks from file. """ browser = request.form.get('browser') bookmarks = request.files.get('file') status = 302 # only edit if all the fields are ok if all([item is not None for item in (browser, bookmarks)]): with NamedTemporaryFile() as temp: temp.write(bookmarks.read()) Importer(browser).import_from(temp.name) status = 301 response = Response( status=status, headers={ 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Headers': '*', 'Access-Control-Allow-Methods': '*', 'Location': '/' }, mimetype='application/json' ) return response
def test_import_chrome(self): ''' Test importing bookmark tree into internal DB. ''' from frostmark import db_base from frostmark import user_data from frostmark.common import fetch_bookmark_tree, print_bookmark_tree from frostmark.importer import Importer urls = [ 'https://www.youtube.com/', 'https://www.google.com/', 'http://portableapps.com/', 'https://www.google.com/maps' ] expected_calls = [ f'[F] 0\tROOT', f'|-- [F] 1\t<no title>', f'|-- [F] 2\tPanel so záložkami', f'| |-- [F] 5\tfolder', f'| | |-- [F] 6\tnestedfolder', f'| | | +-- [B] 3\tYouTube {urls[0]}', f'| | |-- [F] 7\temptyfolder', f'| | +-- [B] 2\tGoogle {urls[1]}', f'| +-- [B] 1\tPortableApps.com {urls[2]}', f'|-- [F] 3\tOstatné', f'| +-- [B] 4\tMapy Google {urls[3]}', f'+-- [F] 4\tZáložky v mobile' ] folder = dirname(abspath(user_data.__file__)) self.assertNotIn(db_base.DB_NAME, listdir(folder)) # import bookmarks from sample firefox database Importer('chrome').import_from(join( dirname(abspath(__file__)), 'sample_chrome.json' )) self.assertIn(db_base.DB_NAME, listdir(folder)) # fetch bookmarks from internal db and test the console output with patch('builtins.print') as output: print_bookmark_tree(fetch_bookmark_tree()) for idx, item in enumerate(output.call_args_list): args, _ = item self.assertEqual(args[0], expected_calls[idx]) self.assertEqual(len(output.call_args_list), len(expected_calls)) # remove internal DB remove(join(folder, db_base.DB_NAME))
def test_pull_firefox(self): ''' Test fetching Firefox profiles from places.sqlite. ''' from anytree import Node from frostmark.common import traverse from frostmark.models import Folder, Bookmark from frostmark.importer import Importer from frostmark.importer.firefox import FirefoxImporter sample = join( dirname(abspath(__file__)), 'sample_firefox.sqlite' ) # pylint: disable=protected-access session = Importer._path_session(sample, FirefoxImporter.BASE) tree = FirefoxImporter.assemble_import_tree(session) flat_tree = traverse(tree) self.assertIsInstance(tree, Node) self.maxDiff = None # pylint: disable=invalid-name self.assertEqual([ (item.folder_name, item.node_type) if item.node_type == Folder else (item.title, item.node_type) for item in flat_tree ], [ ('<no title>', Folder), ('Bookmarks Menu', Folder), ('Mozilla Firefox', Folder), ('Help and Tutorials', Bookmark), ('Customize Firefox', Bookmark), ('Get Involved', Bookmark), ('About Us', Bookmark), ('PortableApps.com', Bookmark), ('Recently Bookmarked', Bookmark), ('Recent Tags', Bookmark), ('Bookmarks Toolbar', Folder), ('Latest Headlines', Folder), ('Getting Started', Bookmark), ('Most Visited', Bookmark), ('Tags', Folder), ('Unsorted Bookmarks', Folder) ])
*args, **kwargs, func=lambda *args, **kwargs: print_all_profiles())) PARSER.console_parser.add_argument( '-i', '--import-bookmarks', help='import bookmarks from a browser profile', required=False, nargs='+', metavar=('BROWSER', 'PROFILE'), # pylint: disable=unnecessary-lambda action=lambda *args, **kwargs: ExecuteAction( *args, **kwargs, func=lambda *args, **kwargs: [ Importer(kwargs['arg_values'][0]).import_from(val) for val in kwargs['arg_values'][1:] ])) PARSER.console_parser.add_argument( '-x', '--export-bookmarks', help='export bookmarks to an HTML format', required=False, nargs=1, metavar=('PATH', ), # pylint: disable=unnecessary-lambda action=lambda *args, **kwargs: ExecuteAction( *args, **kwargs,
def test_import_firefox(self): ''' Test importing bookmark tree into internal DB. ''' from frostmark import db_base from frostmark import user_data from frostmark.common import fetch_bookmark_tree, print_bookmark_tree from frostmark.importer import Importer urls = [ 'http://www.mozilla.com/en-US/firefox/help/', 'http://www.mozilla.com/en-US/firefox/customize/', 'http://www.mozilla.com/en-US/firefox/community/', 'http://www.mozilla.com/en-US/about/', 'http://portableapps.com/', ( 'place:folder=BOOKMARKS_MENU&folder=UNFILED_BOOKMARKS' '&folder=TOOLBAR&queryType=1&sort=12&maxResults=10' '&excludeQueries=1' ), 'place:type=6&sort=14&maxResults=10', 'http://www.mozilla.com/en-US/firefox/central/', 'place:sort=8&maxResults=10' ] expected_calls = [ f'[F] 0\tROOT', f'+-- [F] 1\t<no title>', f' |-- [F] 2\tBookmarks Menu', f' | |-- [F] 6\tMozilla Firefox', f' | | |-- [B] 2\tHelp and Tutorials {urls[0]}', f' | | |-- [B] 3\tCustomize Firefox {urls[1]}', f' | | |-- [B] 4\tGet Involved {urls[2]}', f' | | +-- [B] 5\tAbout Us {urls[3]}', f' | |-- [B] 6\tPortableApps.com {urls[4]}', f' | |-- [B] 8\tRecently Bookmarked {urls[5]}', f' | +-- [B] 9\tRecent Tags {urls[6]}', f' |-- [F] 3\tBookmarks Toolbar', f' | |-- [F] 7\tLatest Headlines', f' | |-- [B] 1\tGetting Started {urls[7]}', f' | +-- [B] 7\tMost Visited {urls[8]}', f' |-- [F] 4\tTags', f' +-- [F] 5\tUnsorted Bookmarks' ] folder = dirname(abspath(user_data.__file__)) self.assertNotIn(db_base.DB_NAME, listdir(folder)) # import bookmarks from sample firefox database Importer('firefox').import_from(join( dirname(abspath(__file__)), 'sample_firefox.sqlite' )) self.assertIn(db_base.DB_NAME, listdir(folder)) # fetch bookmarks from internal db and test the console output with patch('builtins.print') as output: print_bookmark_tree(fetch_bookmark_tree()) for idx, item in enumerate(output.call_args_list): args, _ = item self.assertEqual(args[0], expected_calls[idx]) self.assertEqual(len(output.call_args_list), len(expected_calls)) # remove internal DB remove(join(folder, db_base.DB_NAME))
def test_import_opera(self): ''' Test importing bookmark tree into internal DB. ''' from frostmark import db_base from frostmark import user_data from frostmark.common import fetch_bookmark_tree, print_bookmark_tree from frostmark.importer import Importer urls = [ 'http://portableapps.com/', 'http://www.opera.com/follow', 'http://www.booking.com/', 'http://en.wikipedia.org/', 'http://www.opera.com/sk/follow', 'http://www.zoznam.sk/', 'http://www.facebook.com/', 'http://www.amazon.com/', 'http://www.ebay.com/', 'http://www.yahoo.com/', 'http://www.twitter.com/', 'http://www.youtube.com/', 'http://www.mall.sk/', 'http://www.heureka.sk/', 'http://www.topky.sk/', 'http://www.amazon.de/', 'http://www.azet.sk/' ] expected_calls = [ f'[F] 0\tROOT', f'|-- [F] 1\t<no title>', f'|-- [F] 2\tBookmarks bar', f'| +-- [B] 1\tPortableApps.com {urls[0]}', f'|-- [F] 3\tShared Bookmarks', f'|-- [F] 4\tSpeed Dial', f'| +-- [F] 10\tV7', f'| |-- [F] 11\t', f'| | |-- [B] 8\tLet\'s connect {urls[1]}', f'| | |-- [B] 9\tBooking.com: Cheap Hotels {urls[2]}', f'| | +-- [B] 10\tWikipedia {urls[3]}', f'| |-- [F] 12\t', f'| | |-- [B] 16\tSpojte sa s nami {urls[4]}', f'| | +-- [B] 17\tZoznam {urls[5]}', f'| |-- [B] 2\tFacebook {urls[6]}', f'| |-- [B] 3\tAmazon.com {urls[7]}', f'| |-- [B] 4\teBay {urls[8]}', f'| |-- [B] 5\tYahoo! {urls[9]}', f'| |-- [B] 6\tTwitter {urls[10]}', f'| |-- [B] 7\tYouTube {urls[11]}', f'| |-- [B] 11\tMall {urls[12]}', f'| |-- [B] 12\tHeureka {urls[13]}', f'| |-- [B] 13\tTopky {urls[14]}', f'| |-- [B] 14\tAmazon.de {urls[15]}', f'| +-- [B] 15\tAzet {urls[16]}', f'|-- [F] 5\tTrash', f'|-- [F] 6\tUnsorted Bookmarks', f'|-- [F] 7\tMy Folders', f'|-- [F] 8\tOther bookmarks', f'+-- [F] 9\tMobile bookmarks' ] folder = dirname(abspath(user_data.__file__)) self.assertNotIn(db_base.DB_NAME, listdir(folder)) # import bookmarks from sample firefox database Importer('opera').import_from(join( dirname(abspath(__file__)), 'sample_opera.json' )) self.assertIn(db_base.DB_NAME, listdir(folder)) # fetch bookmarks from internal db and test the console output with patch('builtins.print') as output: print_bookmark_tree(fetch_bookmark_tree()) for idx, item in enumerate(output.call_args_list): args, _ = item self.assertEqual(args[0], expected_calls[idx]) self.assertEqual(len(output.call_args_list), len(expected_calls)) # remove internal DB remove(join(folder, db_base.DB_NAME))
def test_export_prepare(self): ''' Test preparint an export HTML file from internal DB. ''' from frostmark import db_base from frostmark import user_data from frostmark.importer import Importer from frostmark.exporter import Exporter expected = ( '<!DOCTYPE NETSCAPE-Bookmark-file-1>\n' '<!-- This is an automatically generated file.\n' ' It will be read and overwritten.\n' ' DO NOT EDIT!\n' '-->\n' '<META\n' ' HTTP-EQUIV="Content-Type"\n' ' CONTENT="text/html; charset=UTF-8"\n' '/>\n' '<TITLE>Bookmarks</TITLE>\n' '<H1>Bookmarks</H1>\n' '<DL><p>\n' ' <DT><H3><no title></H3>\n' ' <DL><p>\n' ' <DT><H3>Bookmarks Menu</H3>\n' ' <DL><p>\n' ' <DT><H3>Mozilla Firefox</H3>\n' ' <DL><p>\n' ' <DT><A HREF="http://www.mozilla.com/en-US/firefox' '/help/" ICON="">Help and Tutorials</A>\n' ' <DT><A HREF="http://www.mozilla.com/en-US/firefox' '/customize/" ICON="">Customize Firefox</A>\n' ' <DT><A HREF="http://www.mozilla.com/en-US/firefox' '/community/" ICON="">Get Involved</A>\n' ' <DT><A HREF="http://www.mozilla.com/en-US/about/"' ' ICON="">About Us</A>\n' ' </DL><p>\n' ' <HR>\n' ' <DT><A HREF="http://portableapps.com/" ICON="">' 'PortableApps.com</A>\n' ' <DT><A HREF="place:folder=BOOKMARKS_MENU&folder=' 'UNFILED_BOOKMARKS&folder=TOOLBAR&queryType=1&sort=12&maxResults=' '10&excludeQueries=1" ICON="">Recently Bookmarked</A>\n' ' <DT><A HREF="place:type=6&sort=14&maxResults=10" ' 'ICON="">Recent Tags</A>\n' ' </DL><p>\n' ' <DT><H3>Bookmarks Toolbar</H3>\n' ' <DL><p>\n' ' <DT><H3>Latest Headlines</H3>\n' ' <DL><p>\n' ' </DL><p>\n' ' <DT><A HREF="http://www.mozilla.com/en-US/firefox/' 'central/" ICON="">Getting Started</A>\n' ' <DT><A HREF="place:sort=8&maxResults=10" ICON="">' 'Most Visited</A>\n' ' </DL><p>\n' ' <DT><H3>Tags</H3>\n' ' <DL><p>\n' ' </DL><p>\n' ' <DT><H3>Unsorted Bookmarks</H3>\n' ' <DL><p>\n' ' </DL><p>\n' ' </DL><p>\n' '</DL><p>\n') folder = dirname(abspath(user_data.__file__)) self.assertNotIn(db_base.DB_NAME, listdir(folder)) # import bookmarks from sample firefox database Importer('firefox').import_from( join(dirname(abspath(__file__)), 'sample_firefox.sqlite')) self.assertIn(db_base.DB_NAME, listdir(folder)) # compare results self.assertEqual(Exporter.prepare_export(), expected) # remove internal DB remove(join(folder, db_base.DB_NAME))
def test_export_firefox(self): ''' Test exporting HTML file from internal DB. ''' from frostmark import db_base from frostmark import user_data from frostmark.importer import Importer from frostmark.exporter import Exporter expected = ( '<!DOCTYPE NETSCAPE-Bookmark-file-1>\n' '<!-- This is an automatically generated file.\n' ' It will be read and overwritten.\n' ' DO NOT EDIT!\n' '-->\n' '<META\n' ' HTTP-EQUIV="Content-Type"\n' ' CONTENT="text/html; charset=UTF-8"\n' '/>\n' '<TITLE>Bookmarks</TITLE>\n' '<H1>Bookmarks</H1>\n' '<DL><p>\n' ' <DT><H3><no title></H3>\n' ' <DL><p>\n' ' <DT><H3>Bookmarks Menu</H3>\n' ' <DL><p>\n' ' <DT><H3>Mozilla Firefox</H3>\n' ' <DL><p>\n' ' <DT><A HREF="http://www.mozilla.com/en-US/firefox' '/help/" ICON="">Help and Tutorials</A>\n' ' <DT><A HREF="http://www.mozilla.com/en-US/firefox' '/customize/" ICON="">Customize Firefox</A>\n' ' <DT><A HREF="http://www.mozilla.com/en-US/firefox' '/community/" ICON="">Get Involved</A>\n' ' <DT><A HREF="http://www.mozilla.com/en-US/about/"' ' ICON="">About Us</A>\n' ' </DL><p>\n' ' <HR>\n' ' <DT><A HREF="http://portableapps.com/" ICON="">' 'PortableApps.com</A>\n' ' <DT><A HREF="place:folder=BOOKMARKS_MENU&folder=' 'UNFILED_BOOKMARKS&folder=TOOLBAR&queryType=1&sort=12&maxResults=' '10&excludeQueries=1" ICON="">Recently Bookmarked</A>\n' ' <DT><A HREF="place:type=6&sort=14&maxResults=10" ' 'ICON="">Recent Tags</A>\n' ' </DL><p>\n' ' <DT><H3>Bookmarks Toolbar</H3>\n' ' <DL><p>\n' ' <DT><H3>Latest Headlines</H3>\n' ' <DL><p>\n' ' </DL><p>\n' ' <DT><A HREF="http://www.mozilla.com/en-US/firefox/' 'central/" ICON="">Getting Started</A>\n' ' <DT><A HREF="place:sort=8&maxResults=10" ICON="">' 'Most Visited</A>\n' ' </DL><p>\n' ' <DT><H3>Tags</H3>\n' ' <DL><p>\n' ' </DL><p>\n' ' <DT><H3>Unsorted Bookmarks</H3>\n' ' <DL><p>\n' ' </DL><p>\n' ' </DL><p>\n' '</DL><p>\n') folder = dirname(abspath(user_data.__file__)) self.assertNotIn(db_base.DB_NAME, listdir(folder)) # import bookmarks from sample firefox database Importer('firefox').import_from( join(dirname(abspath(__file__)), 'sample_firefox.sqlite')) self.assertIn(db_base.DB_NAME, listdir(folder)) # file-like object to catch HTML output bytesout = BytesIO() # mock context manager to return BytesIO mocked_file = MagicMock(__enter__=MagicMock(return_value=bytesout)) # mock builtins.open() to return mocked_file mocked_open = MagicMock(return_value=mocked_file) with patch('builtins.open', new=mocked_open) as output: # export HTML to "out.html" file Exporter.export_to(join(folder, 'out.html')) # "file" was created and written to output.assert_called_once_with(join(folder, 'out.html'), 'rb') # rewind and compare bytesout.seek(0) self.assertEqual(bytesout.read().decode('utf-8'), expected) # remove internal DB remove(join(folder, db_base.DB_NAME))