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 ) assert resp.status_code == 201 alert = Alert.objects.latest('id') assert json.loads(resp.content) == {'detail': {'id': alert.id}} links = Link.objects.filter(alert=alert) assert len(links) == 1 assert links[0].name == 'link' assert links[0].url == 'http://example.com/'
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 ) assert resp.status_code == 400 assert ( json.loads(resp.content) == { u'detail': { u'links': [ u'Missing names or urls in link data. ' u"{u'url': u'http://example.com/'}" ] } } ) assert 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 ) assert resp.status_code == 400 assert ( json.loads(resp.content) == { u'detail': { u'links': [ u'Missing names or urls in link data. ' u"{u'name': u'link'}" ] } } ) assert Alert.objects.filter(summary=data['summary']).count() == 0