from typing import List from django.test import TestCase from bookmarks.models import parse_tag_string from bookmarks.services.parser import NetscapeBookmark from bookmarks.services.parser import parse from bookmarks.tests.helpers import ImportTestMixin, BookmarkHtmlTag class ParserTestCase(TestCase, ImportTestMixin): def assertTagsEqual( self, bookmarks: List[NetscapeBookmark], html_tags: List[BookmarkHtmlTag] ): self.assertEqual(len(bookmarks), len(html_tags)) for bookmark in bookmarks: html_tag = html_tags[bookmarks.index(bookmark)] self.assertEqual(bookmark.href, html_tag.href) self.assertEqual(bookmark.title, html_tag.title) self.assertEqual(bookmark.date_added, html_tag.add_date) self.assertEqual(bookmark.description, html_tag.description) self.assertEqual(bookmark.tag_names, parse_tag_string(html_tag.tags)) self.assertEqual(bookmark.to_read, html_tag.to_read) self.assertEqual(bookmark.private, html_tag.private) def test_parse_bookmarks(self): html_tags = [ BookmarkHtmlTag( href="https://example.com", title="Example title", description="Example description", add_date="1", tags="example-tag", ), BookmarkHtmlTag( href="https://example.com/foo", title="Foo title", description="", add_date="2", tags="", ), BookmarkHtmlTag( href="https://example.com/bar", title="Bar title", description="Bar description", add_date="3", tags="bar-tag, other-tag", ), BookmarkHtmlTag( href="https://example.com/baz", title="Baz title", description="Baz description", add_date="3", to_read=True, ), ] html = self.render_html(html_tags) bookmarks = parse(html) self.assertTagsEqual(bookmarks, html_tags) def test_no_bookmarks(self): html = self.render_html() bookmarks = parse(html) self.assertEqual(bookmarks, []) def test_reset_properties_after_adding_bookmark(self): html_tags = [ BookmarkHtmlTag( href="https://example.com", title="Example title", description="Example description", add_date="1", tags="example-tag", ), BookmarkHtmlTag(href="", title="", description="", add_date="", tags=""), ] html = self.render_html(html_tags) bookmarks = parse(html) self.assertTagsEqual(bookmarks, html_tags) def test_empty_title(self): html_tags = [ BookmarkHtmlTag( href="https://example.com", title="", description="Example description", add_date="1", tags="example-tag", ), ] html = self.render_html( tags_html="""
Example description """ ) bookmarks = parse(html) self.assertTagsEqual(bookmarks, html_tags) def test_with_closing_description_tag(self): html_tags = [ BookmarkHtmlTag( href="https://example.com", title="Example title", description="Example description", add_date="1", tags="example-tag", ), BookmarkHtmlTag( href="https://foo.com", title="Foo title", description="", add_date="2", tags="", ), ] html = self.render_html( tags_html="""
Example title
Example description
Foo title
""" ) bookmarks = parse(html) self.assertTagsEqual(bookmarks, html_tags) def test_description_tag_before_anchor_tag(self): html_tags = [ BookmarkHtmlTag( href="https://example.com", title="Example title", description="Example description", add_date="1", tags="example-tag", ), BookmarkHtmlTag( href="https://foo.com", title="Foo title", description="", add_date="2", tags="", ), ] html = self.render_html( tags_html="""
Example description
Example title
Foo title """ ) bookmarks = parse(html) self.assertTagsEqual(bookmarks, html_tags) def test_with_folders(self): html_tags = [ BookmarkHtmlTag( href="https://example.com", title="Example title", description="Example description", add_date="1", tags="example-tag", ), BookmarkHtmlTag( href="https://foo.com", title="Foo title", description="", add_date="2", tags="", ), ] html = self.render_html( tags_html="""

Folder 1

Example title
Example description

Folder 2

Foo title

""" ) bookmarks = parse(html) self.assertTagsEqual(bookmarks, html_tags) def test_private_flag(self): # is private by default html = self.render_html( tags_html="""

Example title
Example description
""" ) bookmarks = parse(html) self.assertEqual(bookmarks[0].private, True) # explicitly marked as private html = self.render_html( tags_html="""
Example title
Example description
""" ) bookmarks = parse(html) self.assertEqual(bookmarks[0].private, True) # explicitly marked as public html = self.render_html( tags_html="""
Example title
Example description
""" ) bookmarks = parse(html) self.assertEqual(bookmarks[0].private, False) def test_notes(self): # no description, no notes html = self.render_html( tags_html="""
Example title """ ) bookmarks = parse(html) self.assertEqual(bookmarks[0].description, "") self.assertEqual(bookmarks[0].notes, "") # description, no notes html = self.render_html( tags_html="""
Example title
Example description """ ) bookmarks = parse(html) self.assertEqual(bookmarks[0].description, "Example description") self.assertEqual(bookmarks[0].notes, "") # description, notes html = self.render_html( tags_html="""
Example title
Example description[linkding-notes]Example notes[/linkding-notes] """ ) bookmarks = parse(html) self.assertEqual(bookmarks[0].description, "Example description") self.assertEqual(bookmarks[0].notes, "Example notes") # description, notes without closing tag html = self.render_html( tags_html="""
Example title
Example description[linkding-notes]Example notes """ ) bookmarks = parse(html) self.assertEqual(bookmarks[0].description, "Example description") self.assertEqual(bookmarks[0].notes, "Example notes") # no description, notes html = self.render_html( tags_html="""
Example title
[linkding-notes]Example notes[/linkding-notes] """ ) bookmarks = parse(html) self.assertEqual(bookmarks[0].description, "") self.assertEqual(bookmarks[0].notes, "Example notes") # notes reset between bookmarks html = self.render_html( tags_html="""
Example title
[linkding-notes]Example notes[/linkding-notes]
Example title
Example description """ ) bookmarks = parse(html) self.assertEqual(bookmarks[0].description, "") self.assertEqual(bookmarks[0].notes, "Example notes") self.assertEqual(bookmarks[1].description, "Example description") self.assertEqual(bookmarks[1].notes, "") def test_unescape_content(self): html = self.render_html( tags_html="""
<style>: The Style Information element
The <style> HTML element contains style information for a document, or part of a document.[linkding-notes]Interesting notes about the <style> HTML element.[/linkding-notes] """ ) bookmarks = parse(html) self.assertEqual(bookmarks[0].title, "