import urllib.parse from typing import List from django.contrib.auth.models import User from django.test import TestCase from django.urls import reverse from bookmarks.models import Bookmark, BookmarkSearch, Tag, UserProfile from bookmarks.tests.helpers import ( BookmarkFactoryMixin, HtmlTestMixin, collapse_whitespace, ) class BookmarkArchivedViewTestCase(TestCase, BookmarkFactoryMixin, HtmlTestMixin): def setUp(self) -> None: user = self.get_or_create_test_user() self.client.force_login(user) def assertVisibleBookmarks( self, response, bookmarks: List[Bookmark], link_target: str = "_blank" ): soup = self.make_soup(response.content.decode()) bookmark_list = soup.select_one( f'ul.bookmark-list[data-bookmarks-total="{len(bookmarks)}"]' ) self.assertIsNotNone(bookmark_list) bookmark_items = bookmark_list.select("li[ld-bookmark-item]") self.assertEqual(len(bookmark_items), len(bookmarks)) for bookmark in bookmarks: bookmark_item = bookmark_list.select_one( f'li[ld-bookmark-item] a[href="{bookmark.url}"][target="{link_target}"]' ) self.assertIsNotNone(bookmark_item) def assertInvisibleBookmarks( self, response, bookmarks: List[Bookmark], link_target: str = "_blank" ): soup = self.make_soup(response.content.decode()) for bookmark in bookmarks: bookmark_item = soup.select_one( f'li[ld-bookmark-item] a[href="{bookmark.url}"][target="{link_target}"]' ) self.assertIsNone(bookmark_item) def assertVisibleTags(self, response, tags: List[Tag]): soup = self.make_soup(response.content.decode()) tag_cloud = soup.select_one("div.tag-cloud") self.assertIsNotNone(tag_cloud) tag_items = tag_cloud.select("a[data-is-tag-item]") self.assertEqual(len(tag_items), len(tags)) tag_item_names = [tag_item.text.strip() for tag_item in tag_items] for tag in tags: self.assertTrue(tag.name in tag_item_names) def assertInvisibleTags(self, response, tags: List[Tag]): soup = self.make_soup(response.content.decode()) tag_items = soup.select("a[data-is-tag-item]") tag_item_names = [tag_item.text.strip() for tag_item in tag_items] for tag in tags: self.assertFalse(tag.name in tag_item_names) def assertSelectedTags(self, response, tags: List[Tag]): soup = self.make_soup(response.content.decode()) selected_tags = soup.select_one("p.selected-tags") self.assertIsNotNone(selected_tags) tag_list = selected_tags.select("a") self.assertEqual(len(tag_list), len(tags)) for tag in tags: self.assertTrue( tag.name in selected_tags.text, msg=f"Selected tags do not contain: {tag.name}", ) def assertEditLink(self, response, url): html = response.content.decode() self.assertInHTML( f""" Edit """, html, ) def assertBulkActionForm(self, response, url: str): html = collapse_whitespace(response.content.decode()) needle = collapse_whitespace( f"""