Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: aphp/Cohort360-Back-end
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 150199bf05e9fedaceddfe9710b994d2d3929834
Choose a base ref
..
head repository: aphp/Cohort360-Back-end
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 53a4dacd375128f535cca13e819381787bd52c03
Choose a head ref
Showing with 64 additions and 1 deletion.
  1. +2 −1 admin_cohort/services/maintenance.py
  2. +62 −0 admin_cohort/tests/test_maintenance.py
3 changes: 2 additions & 1 deletion admin_cohort/services/maintenance.py
Original file line number Diff line number Diff line change
@@ -78,8 +78,9 @@ def send_maintenance_notification(maintenance_info: WSMaintenanceInfo, force_act
end_time = dateutil.parser.parse(maintenance_info.maintenance_end)
maintenance_info.active = force_active_state if force_active_state is not None else start_time < now < end_time
logging.info(f"Sending maintenance notification: {maintenance_info}")
current_maintenances = MaintenancePhase.objects.filter(start_datetime__lte=now, end_datetime__gte=now).order_by('-end_datetime').all()
current_active_maintenances = [cur for cur in
MaintenancePhase.objects.filter(start_datetime__lte=now, end_datetime__gte=now).order_by('-end_datetime').all()
current_maintenances
if cur.id != maintenance_info.id]
if maintenance_info.active or not current_active_maintenances:
WebsocketManager.send_to_client("__all__", WSMaintenance(type=WebSocketMessageType.MAINTENANCE, info=maintenance_info))
62 changes: 62 additions & 0 deletions admin_cohort/tests/test_maintenance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from datetime import timedelta
from unittest.mock import patch, MagicMock

from django.test import TestCase
from django.utils import timezone

from admin_cohort.services.maintenance import MaintenanceService, WSMaintenanceInfo


class TestMaintenanceService(TestCase):
@patch('admin_cohort.services.maintenance.maintenance_phase_to_info')
@patch('admin_cohort.services.maintenance.MaintenanceService.send_maintenance_notification')
def test_send_deleted_maintenance_notification_within_time_range(self, mock_send_notification, mock_phase_to_info):
now = timezone.now()
maintenance_info = MagicMock()
maintenance_info.start_datetime = now - timedelta(minutes=5)
maintenance_info.end_datetime = now + timedelta(minutes=5)
mock_phase_to_info.return_value = WSMaintenanceInfo(id=1, maintenance_start=str(maintenance_info.start_datetime),
maintenance_end=str(maintenance_info.end_datetime), active=True, type='test',
subject='test', message='test')

MaintenanceService.send_deleted_maintenance_notification(maintenance_info)
mock_send_notification.assert_called_once()

@patch('admin_cohort.services.maintenance.maintenance_phase_to_info')
@patch('admin_cohort.services.maintenance.MaintenanceService.send_maintenance_notification')
def test_send_deleted_maintenance_notification_outside_time_range(self, mock_send_notification, mock_phase_to_info):
now = timezone.now()
maintenance_info = MagicMock()
maintenance_info.start_datetime = now - timedelta(minutes=10)
maintenance_info.end_datetime = now - timedelta(minutes=5)

MaintenanceService.send_deleted_maintenance_notification(maintenance_info)
mock_send_notification.assert_not_called()

@patch('admin_cohort.services.maintenance.dateutil.parser.parse')
@patch('admin_cohort.services.maintenance.MaintenancePhase.objects.filter')
@patch('admin_cohort.services.maintenance.WebsocketManager.send_to_client')
def test_send_maintenance_notification_active(self, mock_send_to_client, mock_filter, mock_parse):
now = timezone.now()
maintenance_info = WSMaintenanceInfo(id=1, maintenance_start=str(now - timedelta(minutes=5)),
maintenance_end=str(now + timedelta(minutes=5)), active=True, type='test',
subject='test', message='test')
mock_parse.side_effect = [now - timedelta(minutes=5), now + timedelta(minutes=5)]
mock_filter.return_value.exclude.return_value = []

MaintenanceService.send_maintenance_notification(maintenance_info)
mock_send_to_client.assert_called_once()

@patch('admin_cohort.services.maintenance.dateutil.parser.parse')
@patch('admin_cohort.services.maintenance.MaintenancePhase.objects.filter')
@patch('admin_cohort.services.maintenance.WebsocketManager.send_to_client')
def test_send_maintenance_notification_inactive_with_current_active(self, mock_send_to_client, mock_filter, mock_parse):
now = timezone.now()
maintenance_info = WSMaintenanceInfo(id=1, maintenance_start=str(now - timedelta(minutes=10)),
maintenance_end=str(now - timedelta(minutes=5)), active=False, type='test',
subject='test', message='test')
mock_parse.side_effect = [now - timedelta(minutes=10), now - timedelta(minutes=5)]
mock_filter.return_value.order_by.return_value.all.return_value = [MagicMock()]

MaintenanceService.send_maintenance_notification(maintenance_info)
mock_send_to_client.assert_not_called()