Skip to content

Commit

Permalink
[fix] Fixed tests failing due to openwisp-notification 1.0.2
Browse files Browse the repository at this point in the history
Bug:
In openwisp-notification 1.0.2, the operation for creating notification
settings when an organization is created is wrapped in transaction.on_commit.
Tests which relied on this functionality and which inherited from
TestCase class were failing.

Fix:
Moved these tests to test class which inherits from TransactionTestCase.
  • Loading branch information
pandafy authored Jul 4, 2022
1 parent 126ab58 commit 1f6506a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 90 deletions.
4 changes: 2 additions & 2 deletions openwisp_controller/config/tests/test_notifications.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from unittest.mock import patch

from django.apps.registry import apps
from django.test import TestCase
from django.test import TransactionTestCase
from swapper import load_model

from openwisp_controller.config.tests.utils import CreateConfigMixin
Expand All @@ -15,7 +15,7 @@
notification_qs = Notification.objects.all()


class TestNotifications(CreateConfigMixin, TestOrganizationMixin, TestCase):
class TestNotifications(CreateConfigMixin, TestOrganizationMixin, TransactionTestCase):
app_label = 'config'

def setUp(self):
Expand Down
156 changes: 68 additions & 88 deletions openwisp_controller/connection/tests/test_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,6 @@ def _generic_notification_test(


class TestNotifications(CreateConnectionsMixin, BaseTestNotification, TestCase):
def test_connection_working_notification(self):
self.assertEqual(Notification.objects.count(), 0)
device_connection = DeviceConnection.objects.create(
credentials=self.creds, device=self.d, is_working=False
)
device_connection.is_working = True
device_connection.save()
self.assertEqual(Notification.objects.count(), 1)
self._generic_notification_test(
exp_level='info',
exp_type='connection_is_working',
exp_verb='working',
exp_message=(
'(SSH) connection to device <a href="{target_link}">'
'{n.target}</a> is {n.verb}.'
),
exp_email_subject='[example.com] RECOVERY: Connection to device {n.target}',
)

def test_connection_is_working_none(self):
self.assertEqual(Notification.objects.count(), 0)

Expand All @@ -87,48 +68,6 @@ def test_connection_is_working_none(self):
device_connection.save()
self.assertEqual(Notification.objects.count(), 0)

def test_connection_not_working_notification(self):
device_connection = DeviceConnection.objects.create(
credentials=self.creds, device=self.d, is_working=True
)
self.assertEqual(Notification.objects.count(), 0)
device_connection.is_working = False
device_connection.save()
self.assertEqual(Notification.objects.count(), 1)
self._generic_notification_test(
exp_level='error',
exp_type='connection_is_not_working',
exp_verb='not working',
exp_message=(
'(SSH) connection to device <a href="{target_link}">'
'{n.target}</a> is {n.verb}.'
),
exp_email_subject='[example.com] PROBLEM: Connection to device {n.target}',
)

def test_unreachable_after_upgrade_notification(self):
device_connection = DeviceConnection.objects.create(
credentials=self.creds, device=self.d, is_working=True
)
self.assertEqual(Notification.objects.count(), 0)
device_connection.is_working = False
device_connection.failure_reason = (
'Giving up, device not reachable anymore after upgrade'
)
device_connection.save()
self.assertEqual(Notification.objects.count(), 1)
self._generic_notification_test(
exp_level='error',
exp_type='connection_is_not_working',
exp_verb='not working',
exp_message=(
'(SSH) connection to device <a href="{target_link}">'
'{n.target}</a> is {n.verb}. '
'Giving up, device not reachable anymore after upgrade'
),
exp_email_subject='[example.com] PROBLEM: Connection to device {n.target}',
)

def test_default_notification_type_already_unregistered(self):
# Simulates if 'default notification type is already unregistered
# by some other module
Expand All @@ -147,74 +86,115 @@ def test_default_notification_type_already_unregistered(self):
@patch(
'openwisp_controller.connection.apps.ConnectionConfig'
'._ignore_connection_notification_reasons',
['timed out'],
['Unable to connect'],
)
@patch.object(notify, 'send')
def test_connection_is_working_changed_timed_out(self, notify_send, *args):
def test_connection_is_working_changed_unable_to_connect(self, notify_send, *args):
credentials = self._create_credentials_with_key(port=self.ssh_server.port)
self._create_config(device=self.d)
device_conn = self._create_device_connection(
credentials=credentials, device=self.d, is_working=True
)
self.assertEqual(device_conn.is_working, True)
device_conn.failure_reason = (
'[Errno None] Unable to connect to port 5555 on 127.0.0.1'
)
device_conn.is_working = False
device_conn.failure_reason = 'timed out'
device_conn.full_clean()
device_conn.save()
notify_send.assert_not_called()
# Connection recovers, device is reachable again
device_conn.is_working = True
# Connection makes recovery.
device_conn.failure_reason = ''
device_conn.is_working = True
device_conn.full_clean()
device_conn.save()
notify_send.assert_not_called()


class TestNotificationTransaction(
CreateConnectionsMixin, BaseTestNotification, TransactionTestCase
):
def test_connection_working_notification(self):
self.assertEqual(Notification.objects.count(), 0)
device_connection = DeviceConnection.objects.create(
credentials=self.creds, device=self.d, is_working=False
)
device_connection.is_working = True
device_connection.save()
self.assertEqual(Notification.objects.count(), 1)
self._generic_notification_test(
exp_level='info',
exp_type='connection_is_working',
exp_verb='working',
exp_message=(
'(SSH) connection to device <a href="{target_link}">'
'{n.target}</a> is {n.verb}.'
),
exp_email_subject='[example.com] RECOVERY: Connection to device {n.target}',
)

@patch(
'openwisp_controller.connection.apps.ConnectionConfig'
'._ignore_connection_notification_reasons',
['Unable to connect'],
['timed out'],
)
@patch.object(notify, 'send')
def test_connection_is_working_changed_unable_to_connect(self, notify_send, *args):
def test_connection_is_working_changed_timed_out(self, notify_send, *args):
credentials = self._create_credentials_with_key(port=self.ssh_server.port)
self._create_config(device=self.d)
device_conn = self._create_device_connection(
credentials=credentials, device=self.d, is_working=True
)
device_conn.failure_reason = (
'[Errno None] Unable to connect to port 5555 on 127.0.0.1'
)
self.assertEqual(device_conn.is_working, True)
device_conn.is_working = False
device_conn.failure_reason = 'timed out'
device_conn.full_clean()
device_conn.save()
notify_send.assert_not_called()
# Connection makes recovery.
device_conn.failure_reason = ''
# Connection recovers, device is reachable again
device_conn.is_working = True
device_conn.failure_reason = ''
device_conn.full_clean()
device_conn.save()
notify_send.assert_not_called()


class TestNotificationTransaction(
CreateConnectionsMixin, BaseTestNotification, TransactionTestCase
):
def test_unreachable_after_upgrade_notification(self):
failure_reason = 'A failure reason'
def test_connection_not_working_notification(self):
device_connection = DeviceConnection.objects.create(
credentials=self.creds, device=self.d, is_working=True
)
self.assertEqual(Notification.objects.count(), 0)
device_connection.is_working = False
device_connection.failure_reason = failure_reason
device_connection.save()
self.assertEqual(Notification.objects.count(), 1)
notification = Notification.objects.get(type='connection_is_not_working')
self.assertIn(failure_reason, notification.message)
self._generic_notification_test(
exp_level='error',
exp_type='connection_is_not_working',
exp_verb='not working',
exp_message=(
'(SSH) connection to device <a href="{target_link}">'
'{n.target}</a> is {n.verb}.'
),
exp_email_subject='[example.com] PROBLEM: Connection to device {n.target}',
)

device_connection.is_working = True
device_connection.failure_reason = ''
def test_unreachable_after_upgrade_notification(self):
device_connection = DeviceConnection.objects.create(
credentials=self.creds, device=self.d, is_working=True
)
self.assertEqual(Notification.objects.count(), 0)
device_connection.is_working = False
device_connection.failure_reason = (
'Giving up, device not reachable anymore after upgrade'
)
device_connection.save()
self.assertEqual(Notification.objects.count(), 2)
notification = Notification.objects.get(type='connection_is_working')
self.assertNotIn(failure_reason, notification.message)
self.assertEqual(Notification.objects.count(), 1)
self._generic_notification_test(
exp_level='error',
exp_type='connection_is_not_working',
exp_verb='not working',
exp_message=(
'(SSH) connection to device <a href="{target_link}">'
'{n.target}</a> is {n.verb}. '
'Giving up, device not reachable anymore after upgrade'
),
exp_email_subject='[example.com] PROBLEM: Connection to device {n.target}',
)

0 comments on commit 1f6506a

Please sign in to comment.