Skip to content

Commit

Permalink
[chores] Added check for OPENWISP_MONITORING_WIFI_CLIENTS_CHECK_SNOOZ…
Browse files Browse the repository at this point in the history
…E_SCHEDULE
  • Loading branch information
pandafy committed Jan 21, 2025
1 parent 9044e5b commit f7ecaac
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
1 change: 1 addition & 0 deletions openwisp_monitoring/check/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.utils.translation import gettext_lazy as _
from swapper import load_model

from openwisp_monitoring.check import checks # noqa
from openwisp_monitoring.check import settings as app_settings


Expand Down
89 changes: 88 additions & 1 deletion openwisp_monitoring/check/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from unittest.mock import patch

from django.core import management
from django.test import TransactionTestCase
from django.core.checks import Error
from django.test import TestCase, TransactionTestCase
from swapper import load_model

from ...device.tests import TestDeviceMonitoringMixin
from .. import settings as app_settings
from ..checks import check_wifi_clients_snooze_schedule
from ..classes import Ping
from ..settings import CHECK_CLASSES
from ..tasks import perform_check
Expand Down Expand Up @@ -38,3 +41,87 @@ def test_perform_check_task_resiliency(self, mock):
check = Check(name='Test check')
perform_check.delay(check.pk)
mock.assert_called_with(f'The check with uuid {check.pk} has been deleted')


class TestCheckWifiClientsSnoozeSchedule(TestCase):
def setUp(self):
self.setting_name = 'OPENWISP_MONITORING_WIFI_CLIENTS_CHECK_SNOOZE_SCHEDULE'

@patch.object(app_settings, 'WIFI_CLIENTS_CHECK_SNOOZE_SCHEDULE', 'invalid_format')
def test_invalid_schedule_format(self):
errors = check_wifi_clients_snooze_schedule(None)
expected_error = Error(
'Invalid schedule format',
hint='Schedule must be a list of date-time ranges',
obj=self.setting_name,
)
self.assertIn(expected_error, errors)

@patch.object(
app_settings, 'WIFI_CLIENTS_CHECK_SNOOZE_SCHEDULE', [('invalid_entry',)]
)
def test_invalid_schedule_entry_format(self):
errors = check_wifi_clients_snooze_schedule(None)
expected_error = Error(
'Invalid schedule entry format: (\'invalid_entry\',)',
hint='Each schedule entry must be a pair of start and end times',
obj=self.setting_name,
)
self.assertIn(expected_error, errors)

@patch.object(
app_settings, 'WIFI_CLIENTS_CHECK_SNOOZE_SCHEDULE', [('invalid_time', '12:00')]
)
def test_invalid_date_time_format(self):
errors = check_wifi_clients_snooze_schedule(None)
expected_error = Error(
'Invalid date-time format: (\'invalid_time\', \'12:00\')',
hint='Use format "MM-DD HH:MM", "MM-DD", or "HH:MM"',
obj=self.setting_name,
)
self.assertIn(expected_error, errors)

@patch.object(app_settings, 'WIFI_CLIENTS_CHECK_SNOOZE_SCHEDULE', [(11, 12)])
def test_invalid_time_format(self):
errors = check_wifi_clients_snooze_schedule(None)
expected_error = Error(
'Invalid time format: (11, 12)',
hint='Use format "MM-DD HH:MM", "MM-DD", or "HH:MM"',
obj=self.setting_name,
)
self.assertIn(expected_error, errors)

@patch.object(
app_settings, 'WIFI_CLIENTS_CHECK_SNOOZE_SCHEDULE', [('12:00', '01-01')]
)
def test_inconsistent_format(self):
errors = check_wifi_clients_snooze_schedule(None)
expected_error = Error(
'Inconsistent format: (\'12:00\', \'01-01\')',
hint='Both start and end must be in the same format (either both time or both date)',
obj=self.setting_name,
)
self.assertIn(expected_error, errors)

@patch.object(
app_settings, 'WIFI_CLIENTS_CHECK_SNOOZE_SCHEDULE', [('12:00', '13:00')]
)
def test_valid_time_format(self):
errors = check_wifi_clients_snooze_schedule(None)
self.assertEqual(errors, [])

@patch.object(
app_settings, 'WIFI_CLIENTS_CHECK_SNOOZE_SCHEDULE', [('01-01', '01-02')]
)
def test_valid_date_format(self):
errors = check_wifi_clients_snooze_schedule(None)
self.assertEqual(errors, [])

@patch.object(
app_settings,
'WIFI_CLIENTS_CHECK_SNOOZE_SCHEDULE',
[('01-01 12:00', '01-01 13:00')],
)
def test_valid_date_time_format(self):
errors = check_wifi_clients_snooze_schedule(None)
self.assertEqual(errors, [])

0 comments on commit f7ecaac

Please sign in to comment.