12
12
from rest_framework .reverse import reverse as api_reverse
13
13
from cabot .cabotapp .models import (
14
14
GraphiteStatusCheck , JenkinsStatusCheck ,
15
- HttpStatusCheck , ICMPStatusCheck , Service , Instance , StatusCheckResult )
15
+ HttpStatusCheck , ICMPStatusCheck , Service , Instance , StatusCheckResult ,
16
+ UserProfile )
16
17
from cabot .cabotapp .views import StatusCheckReportForm
18
+ from cabot .cabotapp .alert import (send_hipchat_alert , send_alert )
17
19
from mock import Mock , patch
18
20
from twilio import rest
19
21
from django .utils import timezone
@@ -411,7 +413,8 @@ def setUp(self):
411
413
'sms_alert' : False ,
412
414
'telephone_alert' : False ,
413
415
'hackpad_id' : None ,
414
- 'id' : 1
416
+ 'id' : 1 ,
417
+ 'url' : u''
415
418
},
416
419
],
417
420
'instance' : [
@@ -528,7 +531,8 @@ def setUp(self):
528
531
'sms_alert' : False ,
529
532
'telephone_alert' : False ,
530
533
'hackpad_id' : None ,
531
- 'id' : 2
534
+ 'id' : 2 ,
535
+ 'url' : u'' ,
532
536
},
533
537
],
534
538
'instance' : [
@@ -744,3 +748,59 @@ def test_negative_sort(self):
744
748
[item ['name' ] for item in response .data ],
745
749
self .expected_sort_names [::- 1 ]
746
750
)
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