|
|
@ -182,3 +182,59 @@ class CommunicationNotificationsFlowTests(TestCase): |
|
|
# Student should not receive a notification for their own message |
|
|
# Student should not receive a notification for their own message |
|
|
self.assertFalse(Notification.objects.filter(user=self.student).exists()) |
|
|
self.assertFalse(Notification.objects.filter(user=self.student).exists()) |
|
|
self.assertFalse(mock_send.called) |
|
|
self.assertFalse(mock_send.called) |
|
|
|
|
|
|
|
|
|
|
|
@patch('apps.account.notification_service.send_notification') |
|
|
|
|
|
def test_webhook_unauthorized(self, mock_send): |
|
|
|
|
|
from django.urls import reverse |
|
|
|
|
|
url = reverse('chat-notify-message') |
|
|
|
|
|
response = self.client.post(url, {'message_id': 1}, HTTP_X_INTERNAL_SECRET='wrong-token') |
|
|
|
|
|
self.assertEqual(response.status_code, 403) |
|
|
|
|
|
|
|
|
|
|
|
@patch('apps.account.notification_service.send_notification') |
|
|
|
|
|
def test_webhook_authorized_success(self, mock_send): |
|
|
|
|
|
from django.urls import reverse |
|
|
|
|
|
from django.conf import settings |
|
|
|
|
|
from django.db.models.signals import post_save |
|
|
|
|
|
from apps.chat.signals import notify_on_new_chat_message |
|
|
|
|
|
|
|
|
|
|
|
# Create a private chat room |
|
|
|
|
|
room = RoomMessage.objects.create( |
|
|
|
|
|
name="Private chat with teacher", |
|
|
|
|
|
initiator=self.student, |
|
|
|
|
|
recipient=self.teacher, |
|
|
|
|
|
room_type=RoomMessage.RoomTypeChoices.PRIVATE |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
# Temporarily disconnect the signal to avoid trigger on save, so we only test webhook execution |
|
|
|
|
|
post_save.disconnect(notify_on_new_chat_message, sender=ChatMessage) |
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
|
|
|
# Teacher sends a message |
|
|
|
|
|
msg = ChatMessage.objects.create( |
|
|
|
|
|
room=room, |
|
|
|
|
|
sender=self.teacher, |
|
|
|
|
|
content="How are you?", |
|
|
|
|
|
content_type=ChatMessage.ChatTypeChoices.TEXT |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
# Clear notifications created (if any) |
|
|
|
|
|
Notification.objects.all().delete() |
|
|
|
|
|
mock_send.reset_mock() |
|
|
|
|
|
|
|
|
|
|
|
url = reverse('chat-notify-message') |
|
|
|
|
|
secret = getattr(settings, 'INTERNAL_WEBHOOK_SECRET', 'super-secret-key-12345') |
|
|
|
|
|
response = self.client.post( |
|
|
|
|
|
url, |
|
|
|
|
|
{'message_id': msg.id}, |
|
|
|
|
|
HTTP_X_INTERNAL_SECRET=secret |
|
|
|
|
|
) |
|
|
|
|
|
self.assertEqual(response.status_code, 200) |
|
|
|
|
|
|
|
|
|
|
|
# Verify notification was sent |
|
|
|
|
|
notif = Notification.objects.filter(user=self.student).first() |
|
|
|
|
|
self.assertIsNotNone(notif) |
|
|
|
|
|
self.assertIn("How are you?", notif.message) |
|
|
|
|
|
self.assertTrue(mock_send.called) |
|
|
|
|
|
finally: |
|
|
|
|
|
# Reconnect signal |
|
|
|
|
|
post_save.connect(notify_on_new_chat_message, sender=ChatMessage) |