2021-05-14 21:34:53 +00:00
|
|
|
from django.test import TestCase
|
|
|
|
from django.urls import reverse
|
2021-10-03 13:13:45 +00:00
|
|
|
from rest_framework.authtoken.models import Token
|
2021-05-14 21:34:53 +00:00
|
|
|
|
|
|
|
from bookmarks.tests.helpers import BookmarkFactoryMixin
|
2022-07-23 21:20:27 +00:00
|
|
|
from bookmarks.models import FeedToken
|
2021-05-14 21:34:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SettingsIntegrationsViewTestCase(TestCase, BookmarkFactoryMixin):
|
|
|
|
|
|
|
|
def setUp(self) -> None:
|
|
|
|
user = self.get_or_create_test_user()
|
|
|
|
self.client.force_login(user)
|
|
|
|
|
|
|
|
def test_should_render_successfully(self):
|
2024-01-27 10:29:16 +00:00
|
|
|
response = self.client.get(reverse("bookmarks:settings.integrations"))
|
2021-05-14 21:34:53 +00:00
|
|
|
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
def test_should_check_authentication(self):
|
|
|
|
self.client.logout()
|
2024-01-27 10:29:16 +00:00
|
|
|
response = self.client.get(
|
|
|
|
reverse("bookmarks:settings.integrations"), follow=True
|
|
|
|
)
|
2021-05-14 21:34:53 +00:00
|
|
|
|
2024-01-27 10:29:16 +00:00
|
|
|
self.assertRedirects(
|
|
|
|
response,
|
|
|
|
reverse("login") + "?next=" + reverse("bookmarks:settings.integrations"),
|
|
|
|
)
|
2021-10-03 13:13:45 +00:00
|
|
|
|
|
|
|
def test_should_generate_api_token_if_not_exists(self):
|
|
|
|
self.assertEqual(Token.objects.count(), 0)
|
|
|
|
|
2024-01-27 10:29:16 +00:00
|
|
|
self.client.get(reverse("bookmarks:settings.integrations"))
|
2021-10-03 13:13:45 +00:00
|
|
|
|
|
|
|
self.assertEqual(Token.objects.count(), 1)
|
|
|
|
token = Token.objects.first()
|
|
|
|
self.assertEqual(token.user, self.user)
|
|
|
|
|
|
|
|
def test_should_not_generate_api_token_if_exists(self):
|
|
|
|
Token.objects.get_or_create(user=self.user)
|
|
|
|
self.assertEqual(Token.objects.count(), 1)
|
|
|
|
|
2024-01-27 10:29:16 +00:00
|
|
|
self.client.get(reverse("bookmarks:settings.integrations"))
|
2021-10-03 13:13:45 +00:00
|
|
|
|
|
|
|
self.assertEqual(Token.objects.count(), 1)
|
2022-07-23 21:20:27 +00:00
|
|
|
|
|
|
|
def test_should_generate_feed_token_if_not_exists(self):
|
|
|
|
self.assertEqual(FeedToken.objects.count(), 0)
|
|
|
|
|
2024-01-27 10:29:16 +00:00
|
|
|
self.client.get(reverse("bookmarks:settings.integrations"))
|
2022-07-23 21:20:27 +00:00
|
|
|
|
|
|
|
self.assertEqual(FeedToken.objects.count(), 1)
|
|
|
|
token = FeedToken.objects.first()
|
|
|
|
self.assertEqual(token.user, self.user)
|
|
|
|
|
|
|
|
def test_should_not_generate_feed_token_if_exists(self):
|
|
|
|
FeedToken.objects.get_or_create(user=self.user)
|
|
|
|
self.assertEqual(FeedToken.objects.count(), 1)
|
|
|
|
|
2024-01-27 10:29:16 +00:00
|
|
|
self.client.get(reverse("bookmarks:settings.integrations"))
|
2022-07-23 21:20:27 +00:00
|
|
|
|
|
|
|
self.assertEqual(FeedToken.objects.count(), 1)
|
|
|
|
|
|
|
|
def test_should_display_feed_urls(self):
|
2024-01-27 10:29:16 +00:00
|
|
|
response = self.client.get(reverse("bookmarks:settings.integrations"))
|
2022-07-23 21:20:27 +00:00
|
|
|
html = response.content.decode()
|
|
|
|
|
|
|
|
token = FeedToken.objects.first()
|
2024-01-27 10:29:16 +00:00
|
|
|
self.assertInHTML(
|
|
|
|
f'<a href="http://testserver/feeds/{token.key}/all">All bookmarks</a>', html
|
|
|
|
)
|
|
|
|
self.assertInHTML(
|
|
|
|
f'<a href="http://testserver/feeds/{token.key}/unread">Unread bookmarks</a>',
|
|
|
|
html,
|
|
|
|
)
|
2024-03-17 10:55:34 +00:00
|
|
|
self.assertInHTML(
|
|
|
|
f'<a href="http://testserver/feeds/{token.key}/shared">Shared bookmarks</a>',
|
|
|
|
html,
|
|
|
|
)
|
|
|
|
self.assertInHTML(
|
|
|
|
f'<a href="http://testserver/feeds/shared">Public shared bookmarks</a>',
|
|
|
|
html,
|
|
|
|
)
|