Repository: fossasia/open-event-server

Issue:
Write tests for notification functions 
**Description**
This is a sub issue of #5320
 
Tests are required for notification functions to ensure that they work properly. This increases the test coverage for the code.

**Additional context**
Working on it.




Discussion:
Please only create sub issues when the PR is ready
In send_followup_notif_monthly_fee_payment and send_notif_monthly_fee_payment in helpers notification.py.

There is **no** title attribute in NOTIFS[MONTHLY_PAYMENT_FOLLOWUP_NOTIF] and NOTIFS[MONTHLY_PAYMENT_NOTIF]

![image](https://user-images.githubusercontent.com/39561007/74958189-93049000-542e-11ea-9373-64041c888347.png)
Great. You can go ahead and fix it

Resolving patch:
diff --git a/app/api/helpers/notification.py b/app/api/helpers/notification.py
--- a/app/api/helpers/notification.py
+++ b/app/api/helpers/notification.py
@@ -46,13 +46,12 @@ def send_notification(user, title, message, actions=None):
     :param actions:
     :return:
     """
-    if not current_app.config['TESTING']:
-        notification = Notification(user_id=user.id, title=title, message=message)
-        if not actions:
-            actions = []
-        notification.actions = actions
-        save_to_db(notification, msg="Notification saved")
-        record_activity('notification_event', user=user, title=title, actions=actions)
+    notification = Notification(user_id=user.id, title=title, message=message)
+    if not actions:
+        actions = []
+    notification.actions = actions
+    save_to_db(notification, msg="Notification saved")
+    record_activity('notification_event', user=user, title=title, actions=actions)
 
 
 def send_notif_new_session_organizer(user, event_name, link, session_id):
@@ -107,7 +106,7 @@ def send_notif_after_import(
     if error_text:
         send_notification(
             user=user,
-            title=NOTIFS[EVENT_IMPORT_FAIL]['title'],
+            title=NOTIFS[EVENT_IMPORT_FAIL]['title'].format(event_name=event_name),
             message=NOTIFS[EVENT_IMPORT_FAIL]['message'].format(error_text=error_text),
         )
     elif event_name:
@@ -123,7 +122,7 @@ def send_notif_after_import(
 
 
 def send_notif_after_export(user, event_name, download_url=None, error_text=None):
-    """send notification after event import"""
+    """send notification after event export"""
     if error_text:
         send_notification(
             user=user,
@@ -162,7 +161,7 @@ def send_notif_monthly_fee_payment(
     if not message_settings or message_settings.notification_status == 1:
         actions = get_monthly_payment_notification_actions(event_id, link)
         notification = NOTIFS[MONTHLY_PAYMENT_NOTIF]
-        title = notification['title'].format(date=previous_month, event_name=event_name)
+        title = notification['subject'].format(date=previous_month, event_name=event_name)
         message = notification['message'].format(
             event_name=event_name, date=previous_month, amount=amount, app_name=app_name,
         )
@@ -190,7 +189,7 @@ def send_followup_notif_monthly_fee_payment(
     if not message_settings or message_settings.notification_status == 1:
         actions = get_monthly_payment_follow_up_notification_actions(event_id, link)
         notification = NOTIFS[MONTHLY_PAYMENT_FOLLOWUP_NOTIF]
-        title = notification['title'].format(date=previous_month, event_name=event_name)
+        title = notification['subject'].format(date=previous_month, event_name=event_name)
         message = notification['message'].format(
             event_name=event_name, date=previous_month, amount=amount, app_name=app_name
         )
diff --git a/app/api/helpers/system_notifications.py b/app/api/helpers/system_notifications.py
--- a/app/api/helpers/system_notifications.py
+++ b/app/api/helpers/system_notifications.py
@@ -255,7 +255,7 @@ def get_invite_papers_notification_actions(cfs_link, submit_link):
     },
     EVENT_IMPORT_FAIL: {
         'recipient': 'User',
-        'title': u'Import of event failed',
+        'title': u'Import of event {event_name} failed',
         'message': (u"The following error occurred:<br>" + u"<pre>{error_text}</pre>"),
     },
     MONTHLY_PAYMENT_NOTIF: {
@@ -290,12 +290,12 @@ def get_invite_papers_notification_actions(cfs_link, submit_link):
     },
     TICKET_PURCHASED_ORGANIZER: {
         'recipient': 'Owner, Organizer',
-        'title': u'New ticket purchase for {event_name} : ({invoice_id}) ',
+        'title': u'New ticket purchase for {event_name} : ({invoice_id})',
         'message': (u"The order has been processed successfully."),
     },
     TICKET_RESEND_ORGANIZER: {
         'recipient': 'Owner, Organizer',
-        'title': u'Email resent for {event_name} by {buyer_email} ({invoice_id}) ',
+        'title': u'Email resent for {event_name} by {buyer_email} ({invoice_id})',
         'message': (u"Email has been sent successfully."),
     },
     TICKET_CANCELLED: {


Test patch:
diff --git a/tests/all/integration/api/helpers/test_notification.py b/tests/all/integration/api/helpers/test_notification.py
new file mode 100644
--- /dev/null
+++ b/tests/all/integration/api/helpers/test_notification.py
@@ -0,0 +1,171 @@
+import unittest
+from app.api.helpers.notification import (
+    send_notif_new_session_organizer,
+    send_followup_notif_monthly_fee_payment,
+    send_notif_after_event,
+    send_notif_after_export,
+    send_notif_after_import,
+    send_notif_event_role,
+    send_notif_monthly_fee_payment,
+    send_notif_session_accept_reject,
+    send_notif_ticket_purchase_organizer,
+)
+from app.factories.session import SessionFactory
+from app.factories.user import UserFactory
+from app.models.notification import Notification
+from tests.all.integration.utils import OpenEventTestCase
+
+
+class TestNotificationHelper(OpenEventTestCase):
+    link = 'https://test.link'
+
+    def test_send_notif_new_session_organizer(self):
+        """Method to test new session notification"""
+        with self.app.test_request_context():
+            send_notif_new_session_organizer(
+                UserFactory(), 'Hobo Meet', self.link, SessionFactory().id
+            )
+            notification = Notification.query.first()
+            self.assertEqual(notification.title, 'New session proposal for Hobo Meet')
+            self.assertEqual(
+                notification.message,
+                'The event <strong>Hobo Meet</strong> has received a new session '
+                'proposal.',
+            )
+
+    def test_send_notif_session_accept_reject(self):
+        """Method to test session accept reject notification"""
+        with self.app.test_request_context():
+            send_notif_session_accept_reject(
+                UserFactory(),
+                'Homeless Therapy',
+                'accepted',
+                self.link,
+                SessionFactory().id,
+            )
+            notification = Notification.query.first()
+            self.assertEqual(
+                notification.title, 'Session Homeless Therapy has been accepted'
+            )
+            self.assertEqual(
+                notification.message,
+                'The session <strong>Homeless Therapy</strong> has been '
+                '<strong>accepted</strong> by the Organizer.',
+            )
+
+    def test_send_notif_after_import(self):
+        """Method to test notification after import"""
+        with self.app.test_request_context():
+            send_notif_after_import(
+                UserFactory(),
+                event_name='Tooth Fairy Convention',
+                error_text='TOOTH_NOT_FOUND',
+            )
+            notification = Notification.query.first()
+            self.assertEqual(
+                notification.title, 'Import of event Tooth Fairy Convention failed'
+            )
+            self.assertEqual(
+                notification.message,
+                'The following error occurred:<br><pre>TOOTH_NOT_FOUND</pre>',
+            )
+
+    def test_send_notif_after_export(self):
+        """Method to test notification after export"""
+        with self.app.test_request_context():
+            send_notif_after_export(
+                UserFactory(), 'Elf Gather', self.link, 'SLEIGH_BROKEN'
+            )
+            notification = Notification.query.first()
+            self.assertEqual(notification.title, 'Export of event Elf Gather failed')
+            self.assertEqual(
+                notification.message,
+                'The following error occurred:<br><pre>SLEIGH_BROKEN</pre>',
+            )
+
+    def test_send_notif_monthly_fee_payment(self):
+        """Method to test monthly fee payment notification"""
+        with self.app.test_request_context():
+            send_notif_monthly_fee_payment(
+                UserFactory(), 'Pizza Party', 'October', 563.65, 'Kite', self.link, 1
+            )
+            notification = Notification.query.first()
+            self.assertEqual(
+                notification.title,
+                'October - Monthly service fee invoice for Pizza Party',
+            )
+            self.assertEqual(
+                notification.message,
+                'The total service fee for the ticket sales of Pizza Party in the '
+                'month of October is 563.65.<br/> That payment for the same has to '
+                'be made in two weeks.<br><br><em>Thank you for using Kite.</em>',
+            )
+
+    def test_send_followup_notif_monthly_fee_payment(self):
+        """Method to test follow up monthly fee payment notification"""
+        with self.app.test_request_context():
+            send_followup_notif_monthly_fee_payment(
+                UserFactory(),
+                'Champagne Showers',
+                'November',
+                4532.99,
+                'RedFoo',
+                self.link,
+                1,
+            )
+            notification = Notification.query.first()
+            self.assertEqual(
+                notification.title,
+                'Past Due: November - Monthly service fee invoice for Champagne Showers',
+            )
+            self.assertEqual(
+                notification.message,
+                'The total service fee for the ticket sales of Champagne Showers '
+                'in the month of November is 4532.99.<br/> That payment for the '
+                'same is past the due date.<br><br><em>Thank you for using '
+                'RedFoo.</em>',
+            )
+
+    def test_send_notif_event_role(self):
+        """Method to test event role invite notification"""
+        with self.app.test_request_context():
+            send_notif_event_role(
+                UserFactory(), 'Dinosaur', 'Mass Extinction', self.link, 1
+            )
+            notification = Notification.query.first()
+            self.assertEqual(
+                notification.title, 'Invitation to be Dinosaur at Mass Extinction',
+            )
+            self.assertEqual(
+                notification.message,
+                "You've been invited to be one of the <strong>Dinosaurs</strong> at <strong>Mass Extinction</strong>.",
+            )
+
+    def test_send_notif_after_event(self):
+        """Method to test notification after conclusion"""
+        with self.app.test_request_context():
+            send_notif_after_event(UserFactory(), 'Apocalypse')
+            notification = Notification.query.first()
+            self.assertEqual(notification.title, 'Event Apocalypse completed')
+            self.assertEqual(
+                notification.message,
+                'The event <strong>Apocalypse</strong> has been completed.<br><br>',
+            )
+
+    def test_send_notif_ticket_purchase_organizer(self):
+        """Method to test order invoice notification after purchase"""
+        with self.app.test_request_context():
+            send_notif_ticket_purchase_organizer(
+                UserFactory(), 's53js79zgd', self.link, 'Poodle', 1
+            )
+            notification = Notification.query.first()
+            self.assertEqual(
+                notification.title, 'New ticket purchase for Poodle : (s53js79zgd)'
+            )
+            self.assertEqual(
+                notification.message, 'The order has been processed successfully.'
+            )
+
+
+if __name__ == '__main__':
+    unittest.main()