linkding/bookmarks/tests/test_bookmark_edit_view.py

255 lines
8.9 KiB
Python
Raw Normal View History

2022-03-22 01:24:21 +00:00
from django.contrib.auth.models import User
2021-05-14 00:32:19 +00:00
from django.test import TestCase
from django.urls import reverse
from bookmarks.models import build_tag_string
from bookmarks.tests.helpers import BookmarkFactoryMixin
class BookmarkEditViewTestCase(TestCase, BookmarkFactoryMixin):
def setUp(self) -> None:
user = self.get_or_create_test_user()
self.client.force_login(user)
def create_form_data(self, overrides=None):
if overrides is None:
overrides = {}
form_data = {
2024-01-27 10:29:16 +00:00
"url": "http://example.com/edited",
"tag_string": "editedtag1 editedtag2",
"title": "edited title",
"description": "edited description",
"notes": "edited notes",
"unread": False,
"shared": False,
2021-05-14 00:32:19 +00:00
}
return {**form_data, **overrides}
def test_should_edit_bookmark(self):
bookmark = self.setup_bookmark()
2024-01-27 10:29:16 +00:00
form_data = self.create_form_data({"id": bookmark.id})
2021-05-14 00:32:19 +00:00
2024-01-27 10:29:16 +00:00
self.client.post(reverse("bookmarks:edit", args=[bookmark.id]), form_data)
2021-05-14 00:32:19 +00:00
bookmark.refresh_from_db()
self.assertEqual(bookmark.owner, self.user)
2024-01-27 10:29:16 +00:00
self.assertEqual(bookmark.url, form_data["url"])
self.assertEqual(bookmark.title, form_data["title"])
self.assertEqual(bookmark.description, form_data["description"])
self.assertEqual(bookmark.notes, form_data["notes"])
self.assertEqual(bookmark.unread, form_data["unread"])
self.assertEqual(bookmark.shared, form_data["shared"])
2021-05-14 00:32:19 +00:00
self.assertEqual(bookmark.tags.count(), 2)
2024-01-27 10:29:16 +00:00
tags = bookmark.tags.order_by("name").all()
self.assertEqual(tags[0].name, "editedtag1")
self.assertEqual(tags[1].name, "editedtag2")
2021-05-14 00:32:19 +00:00
def test_should_edit_unread_state(self):
bookmark = self.setup_bookmark()
2024-01-27 10:29:16 +00:00
form_data = self.create_form_data({"id": bookmark.id, "unread": True})
self.client.post(reverse("bookmarks:edit", args=[bookmark.id]), form_data)
bookmark.refresh_from_db()
self.assertTrue(bookmark.unread)
2024-01-27 10:29:16 +00:00
form_data = self.create_form_data({"id": bookmark.id, "unread": False})
self.client.post(reverse("bookmarks:edit", args=[bookmark.id]), form_data)
bookmark.refresh_from_db()
self.assertFalse(bookmark.unread)
def test_should_edit_shared_state(self):
bookmark = self.setup_bookmark()
2024-01-27 10:29:16 +00:00
form_data = self.create_form_data({"id": bookmark.id, "shared": True})
self.client.post(reverse("bookmarks:edit", args=[bookmark.id]), form_data)
bookmark.refresh_from_db()
self.assertTrue(bookmark.shared)
2024-01-27 10:29:16 +00:00
form_data = self.create_form_data({"id": bookmark.id, "shared": False})
self.client.post(reverse("bookmarks:edit", args=[bookmark.id]), form_data)
bookmark.refresh_from_db()
self.assertFalse(bookmark.shared)
2021-05-14 00:32:19 +00:00
def test_should_prefill_bookmark_form_fields(self):
tag1 = self.setup_tag()
tag2 = self.setup_tag()
2024-01-27 10:29:16 +00:00
bookmark = self.setup_bookmark(
tags=[tag1, tag2],
title="edited title",
description="edited description",
notes="edited notes",
website_title="website title",
website_description="website description",
)
response = self.client.get(reverse("bookmarks:edit", args=[bookmark.id]))
2021-05-14 00:32:19 +00:00
html = response.content.decode()
2024-01-27 10:29:16 +00:00
self.assertInHTML(
f"""
<input type="text" name="url" value="{bookmark.url}" placeholder=" "
autofocus class="form-input" required id="id_url">
2024-01-27 10:29:16 +00:00
""",
html,
)
2021-05-14 00:32:19 +00:00
2024-01-27 10:29:16 +00:00
tag_string = build_tag_string(bookmark.tag_names, " ")
self.assertInHTML(
f"""
<input ld-tag-autocomplete type="text" name="tag_string" value="{tag_string}"
autocomplete="off" autocapitalize="off" class="form-input" id="id_tag_string">
2024-01-27 10:29:16 +00:00
""",
html,
)
2024-01-27 10:29:16 +00:00
self.assertInHTML(
f"""
<input type="text" name="title" value="{bookmark.title}" maxlength="512" autocomplete="off"
class="form-input" id="id_title">
2024-01-27 10:29:16 +00:00
""",
html,
)
2024-01-27 10:29:16 +00:00
self.assertInHTML(
f"""
<textarea name="description" cols="40" rows="2" class="form-input" id="id_description">
{bookmark.description}
</textarea>
2024-01-27 10:29:16 +00:00
""",
html,
)
2024-01-27 10:29:16 +00:00
self.assertInHTML(
f"""
<textarea name="notes" cols="40" rows="8" class="form-input" id="id_notes">
{bookmark.notes}
</textarea>
2024-01-27 10:29:16 +00:00
""",
html,
)
2024-01-27 10:29:16 +00:00
self.assertInHTML(
f"""
<input type="hidden" name="website_title" id="id_website_title"
value="{bookmark.website_title}">
2024-01-27 10:29:16 +00:00
""",
html,
)
2024-01-27 10:29:16 +00:00
self.assertInHTML(
f"""
<input type="hidden" name="website_description" id="id_website_description"
value="{bookmark.website_description}">
2024-01-27 10:29:16 +00:00
""",
html,
)
2021-05-14 00:32:19 +00:00
2022-03-25 17:29:54 +00:00
def test_should_redirect_to_return_url(self):
bookmark = self.setup_bookmark()
form_data = self.create_form_data()
2024-01-27 10:29:16 +00:00
url = (
reverse("bookmarks:edit", args=[bookmark.id])
+ "?return_url="
+ reverse("bookmarks:close")
)
2022-03-25 17:29:54 +00:00
response = self.client.post(url, form_data)
2024-01-27 10:29:16 +00:00
self.assertRedirects(response, reverse("bookmarks:close"))
2022-03-25 17:29:54 +00:00
def test_should_redirect_to_bookmark_index_by_default(self):
2021-05-14 00:32:19 +00:00
bookmark = self.setup_bookmark()
2022-03-25 17:29:54 +00:00
form_data = self.create_form_data()
2021-05-14 00:32:19 +00:00
2024-01-27 10:29:16 +00:00
response = self.client.post(
reverse("bookmarks:edit", args=[bookmark.id]), form_data
)
2021-05-14 00:32:19 +00:00
2024-01-27 10:29:16 +00:00
self.assertRedirects(response, reverse("bookmarks:index"))
2021-05-14 00:32:19 +00:00
2022-03-25 17:29:54 +00:00
def test_should_not_redirect_to_external_url(self):
2021-05-14 00:32:19 +00:00
bookmark = self.setup_bookmark()
2022-03-27 09:47:45 +00:00
def post_with(return_url, follow=None):
form_data = self.create_form_data()
2024-01-27 10:29:16 +00:00
url = (
reverse("bookmarks:edit", args=[bookmark.id])
+ f"?return_url={return_url}"
)
2022-03-27 09:47:45 +00:00
return self.client.post(url, form_data, follow=follow)
2021-05-14 00:32:19 +00:00
2024-01-27 10:29:16 +00:00
response = post_with("https://example.com")
self.assertRedirects(response, reverse("bookmarks:index"))
response = post_with("//example.com")
self.assertRedirects(response, reverse("bookmarks:index"))
response = post_with("://example.com")
self.assertRedirects(response, reverse("bookmarks:index"))
2022-03-22 01:24:21 +00:00
2024-01-27 10:29:16 +00:00
response = post_with("/foo//example.com", follow=True)
2022-03-27 09:47:45 +00:00
self.assertEqual(response.status_code, 404)
2022-03-22 01:24:21 +00:00
def test_can_only_edit_own_bookmarks(self):
2024-01-27 10:29:16 +00:00
other_user = User.objects.create_user(
"otheruser", "otheruser@example.com", "password123"
)
2022-03-22 01:24:21 +00:00
bookmark = self.setup_bookmark(user=other_user)
2024-01-27 10:29:16 +00:00
form_data = self.create_form_data({"id": bookmark.id})
2022-03-22 01:24:21 +00:00
2024-01-27 10:29:16 +00:00
response = self.client.post(
reverse("bookmarks:edit", args=[bookmark.id]), form_data
)
2022-03-22 01:24:21 +00:00
bookmark.refresh_from_db()
2024-01-27 10:29:16 +00:00
self.assertNotEqual(bookmark.url, form_data["url"])
2022-03-22 01:24:21 +00:00
self.assertEqual(response.status_code, 404)
def test_should_respect_share_profile_setting(self):
bookmark = self.setup_bookmark()
self.user.profile.enable_sharing = False
self.user.profile.save()
2024-01-27 10:29:16 +00:00
response = self.client.get(reverse("bookmarks:edit", args=[bookmark.id]))
html = response.content.decode()
2024-01-27 10:29:16 +00:00
self.assertInHTML(
"""
<label for="id_shared" class="form-checkbox">
<input type="checkbox" name="shared" id="id_shared">
<i class="form-icon"></i>
<span>Share</span>
</label>
2024-01-27 10:29:16 +00:00
""",
html,
count=0,
)
self.user.profile.enable_sharing = True
self.user.profile.save()
2024-01-27 10:29:16 +00:00
response = self.client.get(reverse("bookmarks:edit", args=[bookmark.id]))
html = response.content.decode()
2024-01-27 10:29:16 +00:00
self.assertInHTML(
"""
<label for="id_shared" class="form-checkbox">
<input type="checkbox" name="shared" id="id_shared">
<i class="form-icon"></i>
<span>Share</span>
</label>
2024-01-27 10:29:16 +00:00
""",
html,
count=1,
)
def test_should_hide_notes_if_there_are_no_notes(self):
bookmark = self.setup_bookmark()
2024-01-27 10:29:16 +00:00
response = self.client.get(reverse("bookmarks:edit", args=[bookmark.id]))
self.assertContains(response, '<details class="notes">', count=1)
def test_should_show_notes_if_there_are_notes(self):
2024-01-27 10:29:16 +00:00
bookmark = self.setup_bookmark(notes="test notes")
response = self.client.get(reverse("bookmarks:edit", args=[bookmark.id]))
self.assertContains(response, '<details class="notes" open>', count=1)