Skip to content

Commit e689938

Browse files
committed
Merge pull request arachnys#148 from bonniejools/master
Update tests and stop inactive users being alerted
2 parents 3ed5f36 + ee1952c commit e689938

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed

cabot/cabotapp/alert.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232

3333
def send_alert(service, duty_officers=None):
34-
users = service.users_to_notify.all()
34+
users = service.users_to_notify.filter(is_active=True)
3535
if service.email_alert:
3636
send_email_alert(service, users, duty_officers)
3737
if service.hipchat_alert:

cabot/cabotapp/tests/tests_basic.py

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
from rest_framework.reverse import reverse as api_reverse
1313
from cabot.cabotapp.models import (
1414
GraphiteStatusCheck, JenkinsStatusCheck,
15-
HttpStatusCheck, ICMPStatusCheck, Service, Instance, StatusCheckResult)
15+
HttpStatusCheck, ICMPStatusCheck, Service, Instance, StatusCheckResult,
16+
UserProfile)
1617
from cabot.cabotapp.views import StatusCheckReportForm
18+
from cabot.cabotapp.alert import (send_hipchat_alert, send_alert)
1719
from mock import Mock, patch
1820
from twilio import rest
1921
from django.utils import timezone
@@ -411,7 +413,8 @@ def setUp(self):
411413
'sms_alert': False,
412414
'telephone_alert': False,
413415
'hackpad_id': None,
414-
'id': 1
416+
'id': 1,
417+
'url': u''
415418
},
416419
],
417420
'instance': [
@@ -528,7 +531,8 @@ def setUp(self):
528531
'sms_alert': False,
529532
'telephone_alert': False,
530533
'hackpad_id': None,
531-
'id': 2
534+
'id': 2,
535+
'url': u'',
532536
},
533537
],
534538
'instance': [
@@ -744,3 +748,59 @@ def test_negative_sort(self):
744748
[item['name'] for item in response.data],
745749
self.expected_sort_names[::-1]
746750
)
751+
752+
753+
class TestAlerts(LocalTestCase):
754+
def setUp(self):
755+
super(TestAlerts, self).setUp()
756+
757+
self.user_profile = UserProfile.objects.create(
758+
user = self.user,
759+
hipchat_alias = "test_user_hipchat_alias",)
760+
self.user_profile.save()
761+
762+
self.service.users_to_notify.add(self.user)
763+
self.service.update_status()
764+
765+
def test_users_to_notify(self):
766+
self.assertEqual(self.service.users_to_notify.all().count(), 1)
767+
self.assertEqual(self.service.users_to_notify.get(pk=1).username, self.user.username)
768+
769+
@patch('cabot.cabotapp.models.send_alert')
770+
def test_alert(self, fake_send_alert):
771+
self.service.alert()
772+
self.assertEqual(fake_send_alert.call_count, 1)
773+
fake_send_alert.assert_called_with(self.service, duty_officers=[])
774+
775+
@patch('cabot.cabotapp.alert._send_hipchat_alert')
776+
def test_inactive_users(self, fake_hipchat_alert):
777+
self.user.is_active = True
778+
self.user.save()
779+
self.service.alert()
780+
fake_hipchat_alert.assert_called_with(u'Service Service is back to normal: http://localhost/service/1/. @test_user_hipchat_alias', color='green', sender='Cabot/Service')
781+
782+
self.user.is_active = False
783+
self.user.save()
784+
self.service.alert()
785+
fake_hipchat_alert.assert_called_with(u'Service Service is back to normal: http://localhost/service/1/. ', color='green', sender='Cabot/Service')
786+
787+
788+
@patch('cabot.cabotapp.alert._send_hipchat_alert')
789+
def test_normal_alert(self, fake_hipchat_alert):
790+
791+
self.service.overall_status = Service.PASSING_STATUS
792+
self.service.old_overall_status = Service.ERROR_STATUS
793+
self.service.save()
794+
795+
self.service.alert()
796+
fake_hipchat_alert.assert_called_with(u'Service Service is back to normal: http://localhost/service/1/. @test_user_hipchat_alias', color='green', sender='Cabot/Service')
797+
798+
@patch('cabot.cabotapp.alert._send_hipchat_alert')
799+
def test_failure_alert(self, fake_hipchat_alert):
800+
# Most recent failed
801+
self.service.overall_status = Service.CALCULATED_FAILING_STATUS
802+
self.service.old_overall_status = Service.PASSING_STATUS
803+
self.service.save()
804+
805+
self.service.alert()
806+
fake_hipchat_alert.assert_called_with(u'Service Service reporting failing status: http://localhost/service/1/. Checks failing: @test_user_hipchat_alias', color='red', sender='Cabot/Service')

0 commit comments

Comments
 (0)