Exemplo n.º 1
0
class TestAPI(BaseTestCase):
    def setUp(self):
        super(TestAPI, self).setUp()
        self.sp = SchedulePolicy(name=random_string(), repeat_times=1)
        self.sp.save()
        self.service = Service(name=random_string(), policy=self.sp)
        self.service.save()
        self.token = Token(key="testtoken")
        self.token.save()
        self.servicetoken = ServiceTokens(name="testbinding",
                                          service_id=self.service,
                                          token_id=self.token)
        self.servicetoken.save()
        self.service2 = Service(name=random_string(), policy=self.sp)
        self.service2.save()
        self.token2 = Token(key="testtoken2")
        self.token2.save()
        self.servicetoken2 = ServiceTokens(name="testbinding",
                                           service_id=self.service2,
                                           token_id=self.token2)
        self.servicetoken2.save()

    def tearDown(self):
        super(TestAPI, self).tearDown()
        try:
            self.servicetoken.delete()
            self.servicetoken2.delete()
            self.token2.delete()
            self.token.delete()
            self.service2.delete()
            self.service.delete()
            self.sp.delete()
        except:
            pass

    def test_create_event(self):
        try:
            client = APIClient()
            response = client.post(
                '/api/create_event',
                data={
                    "incident_key": "testing",
                    "service_key": self.token.key,
                    "event_type": "trigger",
                    "description": "test",
                    "details": "test"
                },
            )
            self.assertEqual(201, response.status_code)
            new_instance = Incident.objects.get(incident_key='testing')
            self.assertEqual("testing", new_instance.incident_key)
            self.assertEqual(Incident.TRIGGER, new_instance.event_type)
            self.assertEqual(self.service, new_instance.service_key)
        finally:
            new_instance.delete()

    def test_create_event_fails_with_invalid_key(self):
        try:
            client = APIClient()
            response = client.post(
                '/api/create_event',
                data={
                    "incident_key": "testing",
                    "service_key": "invalid",
                    "event_type": "trigger",
                    "description": "test",
                    "details": "test"
                },
            )
            self.assertEqual(403, response.status_code)
        finally:
            pass

    def inject_incident(self):
        incident = Incident()
        incident.service_key = self.service
        incident.incident_key = "testing"
        incident.description = "test"
        incident.details = "test"
        incident.occurred_at = timezone.now()
        incident.save()

    def test_create_event_different_service(self):
        self.inject_incident()
        try:
            client = APIClient()
            response = client.post(
                '/api/create_event',
                data={
                    "incident_key": "testing",
                    "service_key": self.token2.key,
                    "event_type": "trigger",
                    "description": "test",
                    "details": "test"
                },
            )
            self.assertEqual(201, response.status_code)
            incidents = Incident.objects.all()
            self.assertEqual(2, incidents.count())
        finally:
            for incident in incidents:
                incident.delete()

    def test_incident_recovery(self):
        self.inject_incident()
        try:
            client = APIClient()
            response = client.post(
                '/api/create_event',
                data={
                    "incident_key": "testing",
                    "service_key": self.token.key,
                    "event_type": "resolve",
                    "description": "test",
                    "details": "test"
                },
            )
            self.assertEqual(201, response.status_code)
            updated = Incident.objects.get(incident_key='testing')
            self.assertEqual(Incident.RESOLVE, updated.event_type)
        finally:
            updated.delete()

    def test_incident_acknowledge(self):
        self.inject_incident()
        try:
            client = APIClient()
            response = client.post(
                '/api/create_event',
                data={
                    "incident_key": "testing",
                    "service_key": self.token.key,
                    "event_type": "acknowledge",
                    "description": "test",
                    "details": "test"
                },
            )
            self.assertEqual(201, response.status_code)
            updated = Incident.objects.get(incident_key='testing')
            self.assertEqual(Incident.ACKNOWLEDGE, updated.event_type)
        finally:
            updated.delete()
Exemplo n.º 2
0
class TestGetEscalation(BaseTestCase):

    def setUp(self):
        super(TestGetEscalation, self).setUp()
        self.sp = SchedulePolicy(name=random_string(), repeat_times=1)
        self.sp.save()
        self.service = Service(name=random_string(), policy=self.sp)
        self.service.save()
        self.cal = Calendar(
            name=random_string(),
            slug=random_string(),
        )
        self.cal.save()
        self.spr = SchedulePolicyRule(
            schedule_policy=self.sp,
            position=0,
            schedule=self.cal,
            escalate_after=1,
        )
        self.spr.save()

    def tearDown(self):
        super(TestGetEscalation, self).tearDown()
        try:
            self.spr.delete()
            self.cal.delete()
            self.service.delete()
            self.sp.delete()
        except:
            pass

    def test_get_escalation_works_with_no_recurrence(self):
        event = Event(
            start = timezone.now() - timedelta(days=1),
            end = timezone.now() + timedelta(days=1),
            title = '{username},{username}'.format(username=self.username),
            calendar = self.cal,
        )
        event.save()
        try:
            events = get_escalation_for_service(self.service)
            self.assertEqual(2, len(events))
        finally:
            event.delete()

    def test_get_escalation_fails_with_no_recurrence_after_event_end(self):
        event = Event(
            start = timezone.now() - timedelta(days=2),
            end = timezone.now() - timedelta(days=1),
            title = '{username},{username}'.format(username=self.username),
            calendar = self.cal,
        )
        event.save()
        try:
            events = get_escalation_for_service(self.service)
            self.assertEqual(0, len(events))
        finally:
            event.delete()

    def test_get_escalation_empty_when_recurrance_is_not_now(self):
        rule = Rule(
            name=random_string(),
            description=random_string(),
            frequency='WEEKLY',
        )
        rule.save()
        # Active yesterday, and 1 week from now, but not today
        event = Event(
            start = timezone.now() - timedelta(days=2),
            end = timezone.now() - timedelta(days=1),
            title = '{username},{username}'.format(username=self.username),
            calendar = self.cal,
            rule = rule,
        )
        event.save()
        events = get_escalation_for_service(self.service)
        self.assertEqual(0, len(events))

    def test_get_escalation_works_when_recurrance_is_now(self):
        rule = Rule(
            name=random_string(),
            description=random_string(),
            frequency='WEEKLY',
        )
        rule.save()
        # Active last week at this time, recurring now
        event = Event(
            start = timezone.now() - timedelta(days=7, hours=5),
            end = timezone.now() + timedelta(hours=4) - timedelta(days=7),
            title = '{username},{username}'.format(username=self.username),
            calendar = self.cal,
            rule = rule,
        )
        event.save()
        events = get_escalation_for_service(self.service)
        self.assertEqual(2, len(events))

    def test_get_escalation_returns_empty_for_muted_services(self):
        event = Event(
            start = timezone.now() - timedelta(days=1),
            end = timezone.now() + timedelta(days=1),
            title = '{username},{username}'.format(username=self.username),
            calendar = self.cal,
        )
        event.save()
        self.service.notifications_disabled = True
        try:
            events = get_escalation_for_service(self.service)
            self.assertEqual(0, len(events))
        finally:
            event.delete()
Exemplo n.º 3
0
class TestGetEscalation(BaseTestCase):
    def setUp(self):
        super(TestGetEscalation, self).setUp()
        self.sp = SchedulePolicy(name=random_string(), repeat_times=1)
        self.sp.save()
        self.service = Service(name=random_string(), policy=self.sp)
        self.service.save()
        self.cal = Calendar(
            name=random_string(),
            slug=random_string(),
        )
        self.cal.save()
        self.spr = SchedulePolicyRule(
            schedule_policy=self.sp,
            position=0,
            schedule=self.cal,
            escalate_after=1,
        )
        self.spr.save()

    def tearDown(self):
        super(TestGetEscalation, self).tearDown()
        try:
            self.spr.delete()
            self.cal.delete()
            self.service.delete()
            self.sp.delete()
        except:
            pass

    def test_get_escalation_works_with_no_recurrence(self):
        event = Event(
            start=timezone.now() - timedelta(days=1),
            end=timezone.now() + timedelta(days=1),
            title='{username},{username}'.format(username=self.username),
            calendar=self.cal,
        )
        event.save()
        try:
            events = get_escalation_for_service(self.service)
            self.assertEqual(2, len(events))
        finally:
            event.delete()

    def test_get_escalation_fails_with_no_recurrence_after_event_end(self):
        event = Event(
            start=timezone.now() - timedelta(days=2),
            end=timezone.now() - timedelta(days=1),
            title='{username},{username}'.format(username=self.username),
            calendar=self.cal,
        )
        event.save()
        try:
            events = get_escalation_for_service(self.service)
            self.assertEqual(0, len(events))
        finally:
            event.delete()

    def test_get_escalation_empty_when_recurrance_is_not_now(self):
        rule = Rule(
            name=random_string(),
            description=random_string(),
            frequency='WEEKLY',
        )
        rule.save()
        # Active yesterday, and 1 week from now, but not today
        event = Event(
            start=timezone.now() - timedelta(days=2),
            end=timezone.now() - timedelta(days=1),
            title='{username},{username}'.format(username=self.username),
            calendar=self.cal,
            rule=rule,
        )
        event.save()
        events = get_escalation_for_service(self.service)
        self.assertEqual(0, len(events))

    def test_get_escalation_works_when_recurrance_is_now(self):
        rule = Rule(
            name=random_string(),
            description=random_string(),
            frequency='WEEKLY',
        )
        rule.save()
        # Active last week at this time, recurring now
        event = Event(
            start=timezone.now() - timedelta(days=7, hours=5),
            end=timezone.now() + timedelta(hours=4) - timedelta(days=7),
            title='{username},{username}'.format(username=self.username),
            calendar=self.cal,
            rule=rule,
        )
        event.save()
        events = get_escalation_for_service(self.service)
        self.assertEqual(2, len(events))

    def test_get_escalation_returns_empty_for_muted_services(self):
        event = Event(
            start=timezone.now() - timedelta(days=1),
            end=timezone.now() + timedelta(days=1),
            title='{username},{username}'.format(username=self.username),
            calendar=self.cal,
        )
        event.save()
        self.service.notifications_disabled = True
        try:
            events = get_escalation_for_service(self.service)
            self.assertEqual(0, len(events))
        finally:
            event.delete()
Exemplo n.º 4
0
class TestAPI(BaseTestCase):

    def setUp(self):
        super(TestAPI, self).setUp()
        self.sp = SchedulePolicy(name=random_string(), repeat_times=1)
        self.sp.save()
        self.service = Service(name=random_string(), policy=self.sp)
        self.service.save()
        self.token = Token(key="testtoken")
        self.token.save()
        self.servicetoken = ServiceTokens(name="testbinding", service_id=self.service, token_id=self.token)
        self.servicetoken.save()
        self.service2 = Service(name=random_string(), policy=self.sp)
        self.service2.save()
        self.token2 = Token(key="testtoken2")
        self.token2.save()
        self.servicetoken2 = ServiceTokens(name="testbinding", service_id=self.service2, token_id=self.token2)
        self.servicetoken2.save()

    def tearDown(self):
        super(TestAPI, self).tearDown()
        try:
            self.servicetoken.delete()
            self.servicetoken2.delete()
            self.token2.delete()
            self.token.delete()
            self.service2.delete()
            self.service.delete()
            self.sp.delete()
        except:
            pass

    def test_create_event(self):
        try:
            client = APIClient()
            response = client.post(
                '/api/create_event',
                data = {
                    "incident_key": "testing",
                    "service_key": self.token.key,
                    "event_type": "trigger",
                    "description": "test",
                    "details": "test"
                },
            )
            self.assertEqual(201, response.status_code)
            new_instance = Incident.objects.get(incident_key='testing')
            self.assertEqual("testing", new_instance.incident_key)
            self.assertEqual(Incident.TRIGGER, new_instance.event_type)
            self.assertEqual(self.service, new_instance.service_key)
        finally:
            new_instance.delete()

    def test_create_event_fails_with_invalid_key(self):
        try:
            client = APIClient()
            response = client.post(
                '/api/create_event',
                data = {
                    "incident_key": "testing",
                    "service_key": "invalid",
                    "event_type": "trigger",
                    "description": "test",
                    "details": "test"
                },
            )
            self.assertEqual(403, response.status_code)
        finally:
            pass


    def inject_incident(self):
        incident = Incident()
        incident.service_key = self.service
        incident.incident_key = "testing"
        incident.description = "test"
        incident.details = "test"
        incident.occurred_at = timezone.now()
        incident.save()

    def test_create_event_different_service(self):
        self.inject_incident()
        try:
            client = APIClient()
            response = client.post(
                '/api/create_event',
                data = {
                    "incident_key": "testing",
                    "service_key": self.token2.key,
                    "event_type": "trigger",
                    "description": "test",
                    "details": "test"
                },
            )
            self.assertEqual(201, response.status_code)
            incidents = Incident.objects.all()
            self.assertEqual(2, incidents.count())
        finally:
            for incident in incidents:
                incident.delete()

    def test_incident_recovery(self):
        self.inject_incident()
        try:
            client = APIClient()
            response = client.post(
                '/api/create_event',
                data = {
                    "incident_key": "testing",
                    "service_key": self.token.key,
                    "event_type": "resolve",
                    "description": "test",
                    "details": "test"
                },
            )
            self.assertEqual(201, response.status_code)
            updated = Incident.objects.get(incident_key='testing')
            self.assertEqual(Incident.RESOLVE, updated.event_type)
        finally:
            updated.delete()

    def test_incident_acknowledge(self):
        self.inject_incident()
        try:
            client = APIClient()
            response = client.post(
                '/api/create_event',
                data = {
                    "incident_key": "testing",
                    "service_key": self.token.key,
                    "event_type": "acknowledge",
                    "description": "test",
                    "details": "test"
                },
            )
            self.assertEqual(201, response.status_code)
            updated = Incident.objects.get(incident_key='testing')
            self.assertEqual(Incident.ACKNOWLEDGE, updated.event_type)
        finally:
            updated.delete()