latest changes before manual submissions redesign
This commit is contained in:
+98
-1
@@ -4,13 +4,14 @@ from urllib.error import HTTPError, URLError
|
||||
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.conf import settings
|
||||
from django.core.management import call_command
|
||||
from django.test import SimpleTestCase, TestCase
|
||||
from django.urls import reverse
|
||||
from django.utils import translation
|
||||
|
||||
from notifications.services import NotificationError, send_notification, send_ntfy_notification
|
||||
|
||||
from .models import UserSettings
|
||||
from .models import PendingNotificationBatch, UserSettings
|
||||
|
||||
|
||||
|
||||
@@ -90,6 +91,58 @@ class UserSettingsNtfyTests(TestCase):
|
||||
self.assertFalse(settings.ntfy_enabled)
|
||||
self.assertEqual(settings.ntfy_url, "")
|
||||
|
||||
def test_unchecked_url_can_be_saved_without_testing_then_requires_test_to_enable(self):
|
||||
response = self.client.post(reverse("user_settings"), {"ntfy_url": self.url})
|
||||
self.assertRedirects(response, reverse("user_settings"))
|
||||
saved = UserSettings.objects.get(user=self.user)
|
||||
self.assertEqual(saved.ntfy_url, self.url)
|
||||
self.assertFalse(saved.ntfy_enabled)
|
||||
self.assertIsNone(saved.ntfy_last_tested_at)
|
||||
response = self.client.post(reverse("user_settings"), {"ntfy_url": self.url, "ntfy_enabled": "on"})
|
||||
self.assertContains(response, "Test deze URL")
|
||||
|
||||
@patch("notifications.services.send_ntfy_notification")
|
||||
def test_pending_arrivals_are_grouped_by_minute_and_source(self, mock_send):
|
||||
from datetime import timedelta
|
||||
from django.utils import timezone
|
||||
from fireworks_detect.models import PendingEvent
|
||||
|
||||
UserSettings.objects.create(user=self.user, ntfy_url=self.url, ntfy_enabled=True)
|
||||
PendingEvent.objects.create(original_filename="manual", classifier_response={}, source_type=PendingEvent.SourceType.MANUAL)
|
||||
PendingEvent.objects.create(original_filename="sensor", classifier_response={})
|
||||
PendingEvent.objects.create(original_filename="sensor2", classifier_response={})
|
||||
batch = PendingNotificationBatch.objects.get(user=self.user)
|
||||
self.assertEqual((batch.manual_count, batch.sensor_count), (1, 2))
|
||||
call_command("send_pending_notifications")
|
||||
mock_send.assert_not_called()
|
||||
PendingNotificationBatch.objects.filter(pk=batch.pk).update(minute=timezone.now() - timedelta(minutes=2))
|
||||
call_command("send_pending_notifications")
|
||||
mock_send.assert_called_once()
|
||||
self.assertIn("1 handmatig", mock_send.call_args.args[2])
|
||||
self.assertIn("2 sensor", mock_send.call_args.args[2])
|
||||
self.assertFalse(PendingNotificationBatch.objects.exists())
|
||||
PendingEvent.objects.create(original_filename="next", classifier_response={})
|
||||
self.assertEqual(PendingNotificationBatch.objects.get(user=self.user).sensor_count, 1)
|
||||
|
||||
@patch("notifications.services.send_ntfy_notification", side_effect=NotificationError("Could not connect"))
|
||||
def test_failed_summary_is_kept_for_retry(self, mock_send):
|
||||
from datetime import timedelta
|
||||
from django.utils import timezone
|
||||
from fireworks_detect.models import PendingEvent
|
||||
|
||||
UserSettings.objects.create(user=self.user, ntfy_url=self.url, ntfy_enabled=True)
|
||||
PendingEvent.objects.create(original_filename="manual", classifier_response={}, source_type=PendingEvent.SourceType.MANUAL)
|
||||
PendingNotificationBatch.objects.update(minute=timezone.now() - timedelta(minutes=2))
|
||||
call_command("send_pending_notifications")
|
||||
self.assertEqual(PendingNotificationBatch.objects.get().manual_count, 1)
|
||||
|
||||
def test_disabled_user_does_not_accumulate_pending_summaries(self):
|
||||
from fireworks_detect.models import PendingEvent
|
||||
|
||||
UserSettings.objects.create(user=self.user, ntfy_url=self.url, ntfy_enabled=False)
|
||||
PendingEvent.objects.create(original_filename="manual", classifier_response={}, source_type=PendingEvent.SourceType.MANUAL)
|
||||
self.assertFalse(PendingNotificationBatch.objects.exists())
|
||||
|
||||
@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})
|
||||
@@ -145,13 +198,54 @@ class FrontendAuthenticationTests(TestCase):
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertContains(response, "Sign in")
|
||||
self.assertContains(response, reverse("register"))
|
||||
self.assertContains(response, "csrfmiddlewaretoken")
|
||||
|
||||
def test_registration_creates_minimal_account_and_signs_in(self):
|
||||
response = self.client.post(reverse("register"), {
|
||||
"username": "new-user",
|
||||
"password1": "long-unique-password-5842",
|
||||
"password2": "long-unique-password-5842",
|
||||
})
|
||||
|
||||
self.assertRedirects(response, "/")
|
||||
user = get_user_model().objects.get(username="new-user")
|
||||
self.assertEqual(user.email, "")
|
||||
self.assertEqual(str(self.client.session["_auth_user_id"]), str(user.pk))
|
||||
|
||||
def test_registration_preserves_safe_login_destination(self):
|
||||
login_page = self.client.get(reverse("login"), {"next": "/settings/"})
|
||||
self.assertContains(login_page, "/register/?next=/settings/")
|
||||
|
||||
response = self.client.post(reverse("register"), {
|
||||
"username": "another-new-user",
|
||||
"password1": "long-unique-password-5842",
|
||||
"password2": "long-unique-password-5842",
|
||||
"next": "/settings/",
|
||||
})
|
||||
self.assertRedirects(response, "/settings/")
|
||||
|
||||
def test_registration_rejects_external_destination(self):
|
||||
response = self.client.post(reverse("register"), {
|
||||
"username": "safe-user",
|
||||
"password1": "long-unique-password-5842",
|
||||
"password2": "long-unique-password-5842",
|
||||
"next": "https://example.com/steal",
|
||||
})
|
||||
self.assertRedirects(response, "/")
|
||||
|
||||
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_frontend_logout_uses_post_and_returns_home(self):
|
||||
self.client.force_login(self.user)
|
||||
self.assertEqual(self.client.get(reverse("logout")).status_code, 405)
|
||||
response = self.client.post(reverse("logout"))
|
||||
self.assertRedirects(response, "/")
|
||||
self.assertNotIn("_auth_user_id", self.client.session)
|
||||
|
||||
def test_protected_settings_redirects_to_frontend_login(self):
|
||||
response = self.client.get(reverse("user_settings"))
|
||||
|
||||
@@ -160,11 +254,14 @@ class FrontendAuthenticationTests(TestCase):
|
||||
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.assertContains(anonymous_response, f'href="{reverse("login")}"')
|
||||
|
||||
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"))
|
||||
self.assertContains(authenticated_response, f'action="{reverse("logout")}"')
|
||||
self.assertNotContains(authenticated_response, f'href="{reverse("login")}"')
|
||||
|
||||
|
||||
class LanguageSettingsTests(TestCase):
|
||||
|
||||
Reference in New Issue
Block a user