Skip to content

Commit

Permalink
[chore] Add extra tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhanus3133 committed Jun 18, 2024
1 parent 01182c4 commit 483df00
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
4 changes: 2 additions & 2 deletions openwisp_notifications/templates/emails/batch_email.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ Summary of {{ notifications_count }} Notifications from {{ site_name }}
- {{ notification.message }}
URL: {{ notification.url }}
Timestamp: {{ notification.timestamp|date:"F j, Y, g:i a" }}
{% if show_notification_message %}
Description: {{ notification.rendered_description }}
{% if show_notification_description %}
Description: {{ notification.rendered_description }}
{% endif %}
{% endfor %}

Expand Down
39 changes: 29 additions & 10 deletions openwisp_notifications/tests/test_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from django.apps.registry import apps
from django.contrib.auth import get_user_model
from django.contrib.contenttypes.models import ContentType
from django.contrib.sites.models import Site
from django.core import mail
from django.core.cache import cache
from django.core.exceptions import ImproperlyConfigured
Expand Down Expand Up @@ -946,24 +945,44 @@ def test_notification_for_unverified_email(self):
self.assertEqual(len(mail.outbox), 0)

@patch('openwisp_notifications.tasks.batch_email_notification.apply_async')
def test_batch_email_notification(self, mock_send_email):
current_site = Site.objects.get_current()
notify.send(**self.notification_options)
notify.send(**self.notification_options)
notify.send(**self.notification_options)
def test_batch_email_notification_with_descriptions(self, mock_send_email):
for _ in range(5):
notify.send(recipient=self.admin, **self.notification_options)

# Check if only one mail is sent
# Check if only one mail is sent initially
self.assertEqual(len(mail.outbox), 1)

# Call the task
tasks.batch_email_notification(self.admin.id)

# Check if the rest of the notifications are sent in a batch
self.assertEqual(len(mail.outbox), 2)
self.assertEqual(
mail.outbox[1].subject,
f'Summary of 2 Notifications from {current_site.name}',
self.assertIn('Summary of 4 Notifications', mail.outbox[1].subject)
self.assertNotIn('View all Notifications', mail.outbox[1].body)
self.assertIn('Test Notification', mail.outbox[1].body)

@patch('openwisp_notifications.tasks.batch_email_notification.apply_async')
def test_batch_email_notification_with_call_to_action(self, mock_send_email):
self.notification_options.update(
{
'message': 'Notification title',
'type': 'default',
}
)
for _ in range(11):
notify.send(recipient=self.admin, **self.notification_options)

# Check if only one mail is sent initially
self.assertEqual(len(mail.outbox), 1)

# Call the task
tasks.batch_email_notification(self.admin.id)

# Check if the rest of the notifications are sent in a batch
self.assertEqual(len(mail.outbox), 2)
self.assertIn('Summary of 10 Notifications', mail.outbox[1].subject)
self.assertIn('View all Notifications', mail.outbox[1].body)
self.assertNotIn('Test Notification', mail.outbox[1].body)

def test_that_the_notification_is_only_sent_once_to_the_user(self):
first_org = self._create_org()
Expand Down

0 comments on commit 483df00

Please sign in to comment.