165 lines
7.7 KiB
Python
165 lines
7.7 KiB
Python
from socket import timeout as SocketTimeout
|
|
from unittest.mock import MagicMock, patch
|
|
from urllib.error import HTTPError, URLError
|
|
|
|
from django.contrib.auth import get_user_model
|
|
from django.test import SimpleTestCase, TestCase
|
|
from django.urls import reverse
|
|
|
|
from notifications.services import NotificationError, send_notification, send_ntfy_notification
|
|
|
|
from .models import UserSettings
|
|
|
|
|
|
|
|
class NtfyNotificationServiceTests(SimpleTestCase):
|
|
@patch("notifications.services.urlopen")
|
|
def test_posts_a_title_and_message_to_the_topic_url(self, mock_urlopen):
|
|
response = MagicMock()
|
|
response.getcode.return_value = 200
|
|
mock_urlopen.return_value = response
|
|
|
|
self.assertTrue(send_ntfy_notification("https://ntfy.example.com/private-topic", "Fireworks Detector", "Test message"))
|
|
|
|
request = mock_urlopen.call_args.args[0]
|
|
self.assertEqual(request.get_method(), "POST")
|
|
self.assertEqual(request.data, b"Test message")
|
|
self.assertEqual(request.get_header("Title"), "Fireworks Detector")
|
|
|
|
@patch("notifications.services.urlopen", side_effect=HTTPError("https://ntfy.example.com/topic", 503, "Unavailable", None, None))
|
|
def test_non_success_http_response_is_a_safe_failure(self, mock_urlopen):
|
|
with self.assertRaisesMessage(NotificationError, "HTTP 503"):
|
|
send_ntfy_notification("https://ntfy.example.com/topic", "Title", "Message")
|
|
|
|
@patch("notifications.services.urlopen", side_effect=URLError(SocketTimeout()))
|
|
def test_timeout_is_a_safe_failure(self, mock_urlopen):
|
|
with self.assertRaisesMessage(NotificationError, "timed out"):
|
|
send_ntfy_notification("https://ntfy.example.com/topic", "Title", "Message")
|
|
|
|
|
|
class UserSettingsNtfyTests(TestCase):
|
|
url = "https://ntfy.example.com/private-topic"
|
|
|
|
def setUp(self):
|
|
self.user = get_user_model().objects.create_user(username="first-user", password="secret")
|
|
self.other_user = get_user_model().objects.create_user(username="other-user", password="secret")
|
|
self.client.force_login(self.user)
|
|
|
|
def test_test_endpoint_requires_login(self):
|
|
self.client.logout()
|
|
|
|
response = self.client.post(reverse("ntfy_test_notification"), {"ntfy_url": self.url})
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
@patch("members.views.send_ntfy_notification")
|
|
def test_invalid_url_is_rejected_without_sending(self, mock_send):
|
|
response = self.client.post(reverse("ntfy_test_notification"), {"ntfy_url": "not a URL"})
|
|
|
|
self.assertEqual(response.status_code, 400)
|
|
self.assertContains(response, "valid HTTP or HTTPS", status_code=400)
|
|
mock_send.assert_not_called()
|
|
|
|
@patch("members.views.send_ntfy_notification")
|
|
def test_successful_test_returns_inline_success_and_marks_current_url_in_session(self, mock_send):
|
|
response = self.client.post(reverse("ntfy_test_notification"), {"ntfy_url": self.url})
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
self.assertContains(response, "Test notification sent successfully")
|
|
mock_send.assert_called_once_with(
|
|
self.url,
|
|
title="Fireworks Detector",
|
|
message="ntfy notifications are configured correctly.",
|
|
)
|
|
|
|
@patch("members.views.send_ntfy_notification", side_effect=NotificationError("Connection to the ntfy server timed out."))
|
|
def test_delivery_failure_returns_an_inline_safe_error(self, mock_send):
|
|
response = self.client.post(reverse("ntfy_test_notification"), {"ntfy_url": self.url})
|
|
|
|
self.assertEqual(response.status_code, 502)
|
|
self.assertContains(response, "timed out", status_code=502)
|
|
|
|
def test_user_cannot_save_an_untested_url(self):
|
|
response = self.client.post(reverse("user_settings"), {"ntfy_url": self.url, "ntfy_enabled": "on"})
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
self.assertContains(response, "Test this URL successfully")
|
|
settings = UserSettings.objects.get(user=self.user)
|
|
self.assertFalse(settings.ntfy_enabled)
|
|
self.assertEqual(settings.ntfy_url, "")
|
|
|
|
@patch("members.views.send_ntfy_notification")
|
|
def test_successfully_tested_url_can_be_saved(self, mock_send):
|
|
self.client.post(reverse("ntfy_test_notification"), {"ntfy_url": self.url})
|
|
|
|
response = self.client.post(reverse("user_settings"), {"ntfy_url": self.url, "ntfy_enabled": "on"})
|
|
|
|
self.assertRedirects(response, reverse("user_settings"))
|
|
settings = UserSettings.objects.get(user=self.user)
|
|
self.assertEqual(settings.ntfy_url, self.url)
|
|
self.assertTrue(settings.ntfy_enabled)
|
|
self.assertIsNotNone(settings.ntfy_last_tested_at)
|
|
|
|
@patch("members.views.send_ntfy_notification")
|
|
def test_changing_the_url_requires_another_successful_test(self, mock_send):
|
|
self.client.post(reverse("ntfy_test_notification"), {"ntfy_url": self.url})
|
|
self.client.post(reverse("user_settings"), {"ntfy_url": self.url, "ntfy_enabled": "on"})
|
|
|
|
changed_url = "https://ntfy.example.com/another-topic"
|
|
response = self.client.post(reverse("user_settings"), {"ntfy_url": changed_url, "ntfy_enabled": "on"})
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
self.assertContains(response, "Test this URL successfully")
|
|
self.assertEqual(UserSettings.objects.get(user=self.user).ntfy_url, self.url)
|
|
|
|
@patch("members.views.send_ntfy_notification")
|
|
def test_one_users_successful_test_cannot_authorize_another_users_settings(self, mock_send):
|
|
self.client.post(reverse("ntfy_test_notification"), {"ntfy_url": self.url})
|
|
self.client.force_login(self.other_user)
|
|
|
|
response = self.client.post(reverse("user_settings"), {"ntfy_url": self.url, "ntfy_enabled": "on"})
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
self.assertContains(response, "Test this URL successfully")
|
|
settings = UserSettings.objects.get(user=self.other_user)
|
|
self.assertFalse(settings.ntfy_enabled)
|
|
|
|
@patch("notifications.services.send_ntfy_notification")
|
|
def test_reusable_sender_only_delivers_for_enabled_user_settings(self, mock_send):
|
|
self.assertFalse(send_notification(self.user, "Title", "Message"))
|
|
UserSettings.objects.create(user=self.user, ntfy_url=self.url, ntfy_enabled=True)
|
|
|
|
self.assertTrue(send_notification(self.user, "Title", "Message"))
|
|
mock_send.assert_called_once_with(self.url, "Title", "Message")
|
|
|
|
|
|
class FrontendAuthenticationTests(TestCase):
|
|
def setUp(self):
|
|
self.user = get_user_model().objects.create_user(username="profile-user", password="secret")
|
|
|
|
def test_frontend_login_page_is_available(self):
|
|
response = self.client.get(reverse("login"))
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
self.assertContains(response, "Sign in")
|
|
self.assertContains(response, "csrfmiddlewaretoken")
|
|
|
|
def test_frontend_login_authenticates_and_returns_home(self):
|
|
response = self.client.post(reverse("login"), {"username": "profile-user", "password": "secret"})
|
|
|
|
self.assertRedirects(response, "/")
|
|
|
|
def test_protected_settings_redirects_to_frontend_login(self):
|
|
response = self.client.get(reverse("user_settings"))
|
|
|
|
self.assertRedirects(response, "/login/?next=/settings/")
|
|
|
|
def test_profile_circle_is_visible_only_for_authenticated_users(self):
|
|
anonymous_response = self.client.get(reverse("hello"))
|
|
self.assertNotContains(anonymous_response, "class=\"profile-circle\"")
|
|
|
|
self.client.force_login(self.user)
|
|
authenticated_response = self.client.get(reverse("hello"))
|
|
self.assertContains(authenticated_response, "class=\"profile-circle\"")
|
|
self.assertContains(authenticated_response, reverse("user_settings"))
|