Beispiel #1
0
    def test_post(self):
        token = TokenFactory()
        flavor = AlertFlavorFactory(name='Foo', slug='fooflavor')
        flavor.allowed_tokens.add(token)

        data = {
            'severity': 5,
            'summary': 'test alert',
            'description':
            ('All we ever see of stars are their old photographs.'),
            'flavor': flavor.slug,
            'emitter_name': 'testemitter',
            'emitter_version': 0
        }

        resp = self.client.post(reverse('alerts-api'),
                                data=json.dumps(data),
                                content_type='application/json',
                                HTTP_AUTHORIZATION='token ' + token.token)

        eq_(resp.status_code, 201)
        alert = Alert.objects.latest('id')
        eq_(json.loads(resp.content), {'detail': {'id': alert.id}})
        eq_(alert.flavor.slug, flavor.slug)
        eq_(alert.severity, data['severity'])
        eq_(alert.summary, data['summary'])
        eq_(alert.emitter_name, data['emitter_name'])
        eq_(alert.emitter_version, data['emitter_version'])
Beispiel #2
0
    def test_bad_args(self):
        token = TokenFactory()
        flavor = AlertFlavorFactory(name='Foo', slug='fooflavor')
        flavor.allowed_tokens.add(token)

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

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

        assert resp.status_code == 400
        assert (
            json.loads(resp.content) ==
            {
                'detail': {
                    'non_field_errors': ['"foo" is not a valid argument.']
                }
            }
        )
Beispiel #3
0
    def test_post_invalid_start_time(self):
        token = TokenFactory()
        flavor = AlertFlavorFactory(name='Foo', slug='fooflavor')
        flavor.allowed_tokens.add(token)

        data = {
            'severity': 5,
            'summary': 'test alert',
            'description':
            ('All we ever see of stars are their old photographs.'),
            'flavor': flavor.slug,
            'emitter_name': 'testemitter',
            'emitter_version': 0,
            'start_time': '2015'
        }

        resp = self.client.post(reverse('alerts-api'),
                                data=json.dumps(data),
                                content_type='application/json',
                                HTTP_AUTHORIZATION='token ' + token.token)
        eq_(resp.status_code, 400)
        eq_(
            json.loads(resp.content), {
                u'detail': {
                    u'start_time': [
                        u'Datetime has wrong format. Use one of these formats '
                        u'instead: '
                        u'YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z]'
                    ]
                }
            })
Beispiel #4
0
    def test_post_invalid_start_time(self):
        token = TokenFactory()
        flavor = AlertFlavorFactory(name='Foo', slug='fooflavor')
        flavor.allowed_tokens.add(token)

        data = {
            'severity': 5,
            'summary': 'test alert',
            'description': (
                'All we ever see of stars are their old photographs.'
            ),
            'flavor': flavor.slug,
            'emitter_name': 'testemitter',
            'emitter_version': 0,
            'start_time': '2015'
        }

        resp = self.client.post(
            reverse('alerts-api'),
            data=json.dumps(data),
            content_type='application/json',
            HTTP_AUTHORIZATION='token ' + token.token
        )
        assert resp.status_code == 400
        content = json.loads(resp.content)
        assert (
            content['detail']['start_time'][0]
            .startswith(u'Datetime has wrong format.')
        )
Beispiel #5
0
    def test_post_start_time_timezone_change(self):
        token = TokenFactory()
        flavor = AlertFlavorFactory(name='Foo', slug='fooflavor')
        flavor.allowed_tokens.add(token)

        data = {
            'severity': 5,
            'summary': 'test alert',
            'description':
            ('All we ever see of stars are their old photographs.'),
            'flavor': flavor.slug,
            'emitter_name': 'testemitter',
            'emitter_version': 0,
            'start_time': '2015-03-02T16:22:00-0600',
            'end_time': '2015-03-02T17:23:00-0600'
        }

        resp = self.client.post(reverse('alerts-api'),
                                data=json.dumps(data),
                                content_type='application/json',
                                HTTP_AUTHORIZATION='token ' + token.token)

        eq_(resp.status_code, 201)
        alert = Alert.objects.latest('id')
        eq_(json.loads(resp.content), {'detail': {'id': alert.id}})
        eq_(alert.start_time, datetime.datetime(2015, 3, 2, 14, 22, 0))
        eq_(alert.end_time, datetime.datetime(2015, 3, 2, 15, 23, 0))
Beispiel #6
0
    def test_post_with_link(self):
        token = TokenFactory()
        flavor = AlertFlavorFactory(name='Foo', slug='fooflavor')
        flavor.allowed_tokens.add(token)

        data = {
            'severity': 5,
            'summary': 'test alert',
            'description':
            ('All we ever see of stars are their old photographs.'),
            'flavor': flavor.slug,
            'emitter_name': 'testemitter',
            'emitter_version': 0,
            'links': [{
                'name': 'link',
                'url': 'http://example.com/'
            }]
        }

        resp = self.client.post(reverse('alerts-api'),
                                data=json.dumps(data),
                                content_type='application/json',
                                HTTP_AUTHORIZATION='token ' + token.token)

        eq_(resp.status_code, 201)
        alert = Alert.objects.latest('id')
        eq_(json.loads(resp.content), {'detail': {'id': alert.id}})

        links = Link.objects.filter(alert=alert)
        eq_(len(links), 1)
        eq_(links[0].name, 'link')
        eq_(links[0].url, 'http://example.com/')
Beispiel #7
0
    def test_not_all_permitted(self):
        token = TokenFactory()
        flavor = AlertFlavorFactory(name='Foo', slug='fooflavor')
        flavor2 = AlertFlavorFactory(name='Bar', slug='barflavor')
        flavor.allowed_tokens.add(token)

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

        assert resp.status_code == 403
        assert (
            json.loads(resp.content) ==
            {'detail': 'You do not have permission to perform this action.'}
        )

        # Reverse the order of flavors to make sure that also works
        qs = {
            'flavors': flavor2.slug + ',' + flavor.slug
        }
        resp = self.client.get(
            reverse('alerts-api') + '?' + urllib.urlencode(qs),
            HTTP_AUTHORIZATION='token ' + token.token
        )

        assert resp.status_code == 403
        assert (
            json.loads(resp.content) ==
            {'detail': 'You do not have permission to perform this action.'}
        )
Beispiel #8
0
    def test_get_one_flavor(self):
        token = TokenFactory()
        flavor = AlertFlavorFactory(name='Foo', slug='fooflavor')
        flavor.allowed_tokens.add(token)

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

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

        eq_(resp.status_code, 200)
        eq_(
            json.loads(resp.content), {
                u'count':
                3,
                u'total':
                3,
                u'alerts': [{
                    u'id': WHATEVER,
                    u'summary': u'alert 1',
                    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': []
                }, {
                    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': []
                }, {
                    u'id': WHATEVER,
                    u'summary': u'alert 3',
                    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': []
                }]
            })
Beispiel #9
0
    def test_post_date_roundtrip(self):
        """Test we can POST a date and then GET the same date back"""
        token = TokenFactory()
        flavor = AlertFlavorFactory(name='Foo', slug='fooflavor')
        flavor.allowed_tokens.add(token)

        start_time = '2015-03-02T16:22:00Z'

        data = {
            'severity': 5,
            'summary': 'test alert',
            'description': ('One if by land.'),
            'flavor': flavor.slug,
            'emitter_name': 'testemitter',
            'emitter_version': 0,
            'start_time': start_time
        }

        resp = self.client.post(reverse('alerts-api'),
                                data=json.dumps(data),
                                content_type='application/json',
                                HTTP_AUTHORIZATION='token ' + token.token)

        eq_(resp.status_code, 201)
        alert = Alert.objects.latest('id')
        eq_(json.loads(resp.content), {'detail': {'id': alert.id}})
        eq_(alert.start_time, datetime.datetime(2015, 3, 2, 8, 22, 0))

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

        eq_(resp.status_code, 200)
        eq_(
            json.loads(resp.content), {
                u'count':
                1,
                u'total':
                1,
                u'alerts': [{
                    u'id': WHATEVER,
                    u'summary': u'test alert',
                    u'description': u'One if by land.',
                    u'flavor': flavor.slug,
                    u'emitter_version': 0,
                    u'emitter_name': u'testemitter',
                    u'start_time': start_time,
                    u'end_time': None,
                    u'created': WHATEVER,
                    u'severity': 5,
                    u'links': [],
                }]
            })
Beispiel #10
0
    def test_not_permitted(self):
        token = TokenFactory()
        flavor = AlertFlavorFactory(name='Foo', slug='fooflavor')

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

        eq_(resp.status_code, 403)
        eq_(json.loads(resp.content),
            {'detail': 'You do not have permission to perform this action.'})
Beispiel #11
0
    def test_disabled_flavor(self):
        token = TokenFactory()
        flavor = AlertFlavorFactory(name='Foo',
                                    slug='fooflavor',
                                    enabled=False)
        flavor.allowed_tokens.add(token)

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

        eq_(resp.status_code, 400)
        eq_(json.loads(resp.content),
            {'detail': 'Flavor "fooflavor" is disabled.'})
Beispiel #12
0
    def test_created_invalid(self):
        token = TokenFactory()
        flavor = AlertFlavorFactory(name='Foo', slug='fooflavor')
        flavor.allowed_tokens.add(token)

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

        assert resp.status_code == 400
        data = json.loads(resp.content)
        assert (
            data['detail']['created_start'][0]
            .startswith('Datetime has wrong format')
        )
        assert (
            data['detail']['created_end'][0]
            .startswith('Datetime has wrong format')
        )

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

        assert resp.status_code == 400
        data = json.loads(resp.content)
        assert (
            data['detail'] ==
            {'non_field_errors': [
                u'created_start must occur before created_end.'
            ]}
        )
Beispiel #13
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': []
                    }
                ]
            }
        )
Beispiel #14
0
    def test_fjord_authorization_token(self):
        """Verify auth will use Fjord-Authorization header if Authorization
        isn't there

        """
        token = TokenFactory()
        flavor = AlertFlavorFactory(name='Foo', slug='fooflavor')
        flavor.allowed_tokens.add(token)

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

        eq_(resp.status_code, 200)
        eq_(json.loads(resp.content), {
            u'count': 0,
            u'total': 0,
            u'alerts': []
        })
Beispiel #15
0
    def test_bad_max(self):
        token = TokenFactory()
        flavor = AlertFlavorFactory(name='Foo', slug='fooflavor')
        flavor.allowed_tokens.add(token)

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

        assert resp.status_code == 400
        assert (
            json.loads(resp.content) ==
            {'detail': {'max': ['A valid integer is required.']}}
        )

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

        assert resp.status_code == 400
        assert (
            json.loads(resp.content) ==
            {
                'detail': {
                    'max': [
                        'Ensure this value is greater than or equal to 1.'
                    ]
                }
            }
        )
Beispiel #16
0
    def test_invalid_links(self):
        token = TokenFactory()
        flavor = AlertFlavorFactory(name='Foo', slug='fooflavor')
        flavor.allowed_tokens.add(token)

        # Missing link name
        data = {
            'severity': 5,
            'summary': str(uuid4()),
            'description':
            ('All we ever see of stars are their old photographs.'),
            'flavor': flavor.slug,
            'emitter_name': 'testemitter',
            'emitter_version': 0,
            'links': [{
                'url': 'http://example.com/'
            }]
        }

        resp = self.client.post(reverse('alerts-api'),
                                data=json.dumps(data),
                                content_type='application/json',
                                HTTP_AUTHORIZATION='token ' + token.token)

        eq_(resp.status_code, 400)
        eq_(
            json.loads(resp.content), {
                u'detail': {
                    'links': ('Missing names or urls in link data. '
                              "[{u'url': u'http://example.com/'}]")
                }
            })
        eq_(Alert.objects.filter(summary=data['summary']).count(), 0)

        # Missing link url
        data = {
            'severity': 5,
            'summary': str(uuid4()),
            'description':
            ('All we ever see of stars are their old photographs.'),
            'flavor': flavor.slug,
            'emitter_name': 'testemitter',
            'emitter_version': 0,
            'links': [{
                'name': 'link'
            }]
        }

        resp = self.client.post(reverse('alerts-api'),
                                data=json.dumps(data),
                                content_type='application/json',
                                HTTP_AUTHORIZATION='token ' + token.token)

        eq_(resp.status_code, 400)
        eq_(
            json.loads(resp.content), {
                u'detail': {
                    'links': ('Missing names or urls in link data. '
                              "[{u'name': u'link'}]")
                }
            })
        eq_(Alert.objects.filter(summary=data['summary']).count(), 0)
Beispiel #17
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]
        )