add tests for handling multple alert objects returned
@@ -1643,3 +1643,73 @@ class TestAlertPermissions(TacticalTestCase):
for url in unauthorized_urls:
self.check_authorized(method, url)
+
+
+def test_handling_multiple_availability_alerts_returned(self):
+ agent = baker.make_recipe("agents.agent")
+ alerts = baker.make(
+ "alerts.Alerts",
+ alert_type="availability",
+ agent=agent,
+ resolved=False,
+ _quantity=3,
+ )
+
+ alert = Alert.create_or_return_availability_alert(agent, skip_create=True)
+
+ # make sure last alert is returned
+ self.assertEqual(alert, alerts[-1])
+
+ # make sure only 1 alert is not resolved
+ self.assertEqual(
+ Alert.objects.filter(
+ alert_type="availability", agent=agent, resolved=False
+ ).count(),
+ 1,
+ )
+
+
+def test_handling_multiple_check_alerts_returned(self):
+ agent = baker.make_recipe("agents.agent")
+ check = baker.make_recipe("checks.diskspace_check", agent=agent)
+ alerts = baker.make(
+ "alerts.Alerts",
+ alert_type="check",
+ assigned_check=check,
+ agent=agent,
+ resolved=False,
+ _quantity=3,
+ )
+
+ alert = Alert.create_or_return_check_alert(check, skip_create=True)
+
+ # make sure last alert is returned
+ self.assertEqual(alert, alerts[-1])
+
+ # make sure only 1 alert is not resolved
+ self.assertEqual(
+ Alert.objects.filter(alert_type="check", agent=agent, resolved=False).count(), 1
+ )
+
+
+def test_handling_multiple_task_alerts_returned(self):
+ agent = baker.make_recipe("agents.agent")
+ task = baker.make_recipe("autotasks.AutomatedTask", agent=agent)
+ alerts = baker.make(
+ "alerts.Alerts",
+ alert_type="check",
+ assigned_task=task,
+ agent=agent,
+ resolved=False,
+ _quantity=3,
+ )
+
+ alert = Alert.create_or_return_task_alert(task, skip_create=True)
+
+ # make sure last alert is returned
+ self.assertEqual(alert, alerts[-1])
+
+ # make sure only 1 alert is not resolved
+ self.assertEqual(
+ Alert.objects.filter(alert_type="task", agent=agent, resolved=False).count(), 1
+ )
