Skip to content

Commit

Permalink
Merge pull request #33935 from dimagi/mk/3233-fix-check
Browse files Browse the repository at this point in the history
Fix check for active alerts
  • Loading branch information
mkangia authored Jan 3, 2024
2 parents 3568e86 + 5c8a2b4 commit c842bd8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
29 changes: 28 additions & 1 deletion corehq/apps/domain/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from corehq.apps.accounting.utils import clear_plan_version_cache
from corehq.apps.app_manager.models import Application
from corehq.apps.domain.models import Domain
from corehq.apps.domain.views.settings import ManageDomainAlertsView
from corehq.apps.domain.views.settings import ManageDomainAlertsView, MAX_ACTIVE_ALERTS
from corehq.apps.hqwebapp.models import Alert
from corehq.apps.users.models import WebUser
from corehq.motech.models import ConnectionSettings
Expand Down Expand Up @@ -309,6 +309,33 @@ def test_apply_command_with_other_doamin_alert(self):
self.assertEqual(messages[0].message, 'Alert not found!')
self.assertEqual(response.status_code, 302)

@flag_enabled('CUSTOM_DOMAIN_BANNER_ALERTS')
def test_limiting_active_alerts(self):
new_alerts = [
self._create_alert_for_domain(self.domain_name, 'New Alert 1!', self.username),
self._create_alert_for_domain(self.domain_name, 'New Alert 2!', self.username),
self._create_alert_for_domain(self.domain_name, 'New Alert 3!', self.username),
]
for alert in new_alerts:
alert.active = True
alert.save()

self.assertEqual(
Alert.objects.filter(created_by_domain=self.domain, active=True).count(),
MAX_ACTIVE_ALERTS
)

response = self.client.post(
self.url,
data={
'command': 'activate',
'alert_id': self.domain_alert.id,
},
)

messages = list(get_messages(response.wsgi_request))
self.assertEqual(messages[0].message, 'Alert not updated. Only 3 active alerts allowed.')


class TestDeleteDomainAlertView(TestBaseDomainAlertView):
@classmethod
Expand Down
8 changes: 4 additions & 4 deletions corehq/apps/domain/views/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -638,10 +638,10 @@ def _load_alert(alert_id, domain):

def _apply_update(request, alert):
command = request.POST.get('command')
domain_alerts = Alert.objects.filter(created_by_domain=request.domain)
if command == "activate" and len(domain_alerts) >= MAX_ACTIVE_ALERTS:
messages.error(request, _("Only 3 active alerts allowed!"))
return
if command == "activate":
if Alert.objects.filter(created_by_domain=request.domain, active=True).count() >= MAX_ACTIVE_ALERTS:
messages.error(request, _("Alert not updated. Only 3 active alerts allowed."))
return

if command in ['activate', 'deactivate']:
_update_alert(alert, command)
Expand Down

0 comments on commit c842bd8

Please sign in to comment.