Exemplo n.º 1
0
    def test_max(self):
        token = TokenFactory()
        flavor = AlertFlavorFactory(name='Foo', slug='fooflavor')
        flavor.allowed_tokens.add(token)

        alert1 = AlertFactory(summary=u'alert 1', flavor=flavor)
        # We backdate the created so we can verify we're getting the
        # right order of alerts.
        alert1.created = datetime.datetime.now() - datetime.timedelta(days=5)
        alert1.save()

        AlertFactory(summary=u'alert 2', flavor=flavor)

        qs = {
            'flavors': flavor.slug,
            'max': 1
        }
        resp = self.client.get(
            reverse('alerts-api') + '?' + urllib.urlencode(qs),
            HTTP_AUTHORIZATION='token ' + token.token
        )

        assert resp.status_code == 200
        assert (
            json.loads(resp.content) ==
            {
                u'count': 1,
                u'total': 2,
                u'alerts': [
                    {
                        u'id': WHATEVER,
                        u'summary': u'alert 2',
                        u'description': u'the account balance is at $5.',
                        u'flavor': flavor.slug,
                        u'emitter_version': 0,
                        u'emitter_name': u'balance-checker',
                        u'start_time': None,
                        u'end_time': None,
                        u'created': WHATEVER,
                        u'severity': 0,
                        u'links': []
                    }
                ]
            }
        )
Exemplo n.º 2
0
    def test_created(self):
        token = TokenFactory()
        flavor = AlertFlavorFactory(name='Foo', slug='fooflavor')
        flavor.allowed_tokens.add(token)

        today = datetime.datetime.now()
        yesterday = today - datetime.timedelta(days=1)
        daybeforeyesterday = yesterday - datetime.timedelta(days=1)

        alert1 = AlertFactory(summary=u'alert 1', flavor=flavor)
        alert1.created = yesterday
        alert1.save()

        alert2 = AlertFactory(summary=u'alert 2', flavor=flavor)
        alert2.created = daybeforeyesterday
        alert2.save()

        def test_scenario(created_start, created_end, expected):
            qs = {
                'flavors': flavor.slug,
            }
            if created_start:
                qs['created_start'] = created_start
            if created_end:
                qs['created_end'] = created_end

            resp = self.client.get(
                reverse('alerts-api') + '?' + urllib.urlencode(qs),
                HTTP_AUTHORIZATION='token ' + token.token
            )

            assert resp.status_code == 200
            data = json.loads(resp.content)
            assert (
                sorted([alert['summary'] for alert in data['alerts']]) ==
                sorted(expected)
            )

        # Start yesterday at 00:00 yields alert1.
        test_scenario(
            created_start=yesterday.strftime('%Y-%m-%dT00:00'),
            created_end=None,
            expected=[alert1.summary]
        )

        # Start today at 00:00 yields nothing.
        test_scenario(
            created_start=today.strftime('%Y-%m-%dT00:00'),
            created_end=None,
            expected=[]
        )

        # End today at 23:59 yields both.
        test_scenario(
            created_start=None,
            created_end=today.strftime('%Y-%m-%dT23:59'),
            expected=[alert1.summary, alert2.summary]
        )

        # End day before yesterday at 00:00 yields alert2.
        test_scenario(
            created_start=None,
            created_end=daybeforeyesterday.strftime('%Y-%m-%dT23:59'),
            expected=[alert2.summary]
        )

        # Start daybeforeyesterday at 00:00 and end today at 23:59 yields
        # both.
        test_scenario(
            created_start=daybeforeyesterday.strftime('%Y-%m-%dT00:00'),
            created_end=today.strftime('%Y-%m-%dT23:59'),
            expected=[alert1.summary, alert2.summary]
        )