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 class BookmarkSharedViewTestCase(TestCase, BookmarkFactoryMixin): def setUp(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, 'data-is-bookmark-item', 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() self.assertContains(response, 'data-is-user-option', count=len(users)) for user in users: self.assertInHTML(f''' ''', html) def assertInvisibleUserOptions(self, response, users: List[User]): html = response.content.decode() for user in users: self.assertInHTML(f''' ''', html, count=0) def test_should_list_shared_bookmarks_from_all_users_that_have_sharing_enabled(self): 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')) self.assertContains(response, '