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, Tag, UserProfile from bookmarks.tests.helpers import BookmarkFactoryMixin, collapse_whitespace class BookmarkSharedViewTestCase(TestCase, BookmarkFactoryMixin): def authenticate(self) -> None: user = self.get_or_create_test_user() self.client.force_login(user) def assertBookmarkCount(self, html: str, bookmark: Bookmark, count: int, link_target: str = '_blank'): self.assertInHTML( f'{bookmark.resolved_title}', html, count=count ) def assertVisibleBookmarks(self, response, bookmarks: List[Bookmark], link_target: str = '_blank'): html = response.content.decode() self.assertContains(response, '
  • ', count=len(bookmarks)) for bookmark in bookmarks: self.assertBookmarkCount(html, bookmark, 1, link_target) def assertInvisibleBookmarks(self, response, bookmarks: List[Bookmark], link_target: str = '_blank'): html = response.content.decode() for bookmark in bookmarks: self.assertBookmarkCount(html, bookmark, 0, link_target) def assertVisibleTags(self, response, tags: [Tag]): self.assertContains(response, 'data-is-tag-item', count=len(tags)) for tag in tags: self.assertContains(response, tag.name) def assertInvisibleTags(self, response, tags: [Tag]): for tag in tags: self.assertNotContains(response, tag.name) def assertVisibleUserOptions(self, response, users: List[User]): html = response.content.decode() user_options = [ '' ] for user in users: user_options.append(f'') user_select_html = f''' ''' self.assertInHTML(user_select_html, html) def assertEditLink(self, response, url): html = response.content.decode() self.assertInHTML(f''' Edit ''', html) def test_should_list_shared_bookmarks_from_all_users_that_have_sharing_enabled(self): self.authenticate() user1 = self.setup_user(enable_sharing=True) user2 = self.setup_user(enable_sharing=True) user3 = self.setup_user(enable_sharing=True) user4 = self.setup_user(enable_sharing=False) visible_bookmarks = [ self.setup_bookmark(shared=True, user=user1), self.setup_bookmark(shared=True, user=user2), self.setup_bookmark(shared=True, user=user3), ] invisible_bookmarks = [ self.setup_bookmark(shared=False, user=user1), self.setup_bookmark(shared=False, user=user2), self.setup_bookmark(shared=False, user=user3), self.setup_bookmark(shared=True, user=user4), ] response = self.client.get(reverse('bookmarks:shared')) html = collapse_whitespace(response.content.decode()) # Should render list self.assertIn('