Skip to content

Commit

Permalink
Security scans API improvements (allegro#2932)
Browse files Browse the repository at this point in the history
* allow for empty vulnerabilities (thus empty external_vulnerabilities too)
* allow for duplicates on external_vulnerabilities
  • Loading branch information
mkurek authored and ar4s committed Jan 10, 2017
1 parent 11eb826 commit 9bcc78c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/ralph/security/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ def to_internal_value(self, data):

# external_id to local_id
if 'external_vulnerabilities' in data:
external_ids = data.get('external_vulnerabilities', [])
external_ids = set(data.get('external_vulnerabilities', []))
converted = Vulnerability.objects.filter(
external_vulnerability_id__in=external_ids)
if len(converted) != len(external_ids):
unknown = set(external_ids) - set(
unknown = external_ids - set(
[str(v.external_vulnerability_id) for v in converted]
)
msg = "Unknow external_vulnerabilities: {}".format(
Expand Down
19 changes: 19 additions & 0 deletions src/ralph/security/migrations/0003_auto_20170110_1352.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('security', '0002_auto_20160307_1138'),
]

operations = [
migrations.AlterField(
model_name='securityscan',
name='vulnerabilities',
field=models.ManyToManyField(blank=True, to='security.Vulnerability'),
),
]
2 changes: 1 addition & 1 deletion src/ralph/security/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ class SecurityScan(
details_url = models.URLField(max_length=255, blank=True)
rescan_url = models.URLField(blank=True, verbose_name=_('Rescan url'))
base_object = models.ForeignKey(BaseObject)
vulnerabilities = models.ManyToManyField(Vulnerability)
vulnerabilities = models.ManyToManyField(Vulnerability, blank=True)
20 changes: 16 additions & 4 deletions src/ralph/security/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,24 @@ def test_create_scan_by_external_id_works(self):
self.vulnerability.id, security_scan.vulnerabilities.get().id
)

def test_create_scan_raise_error_when_both_vulnerabilities_empty(self):
self.data['external_vulnerabilities'] = []
def test_create_scan_by_duplicated_external_id_works(self):
self.data['external_vulnerabilities'] = [
self.vulnerability.external_vulnerability_id,
self.vulnerability.external_vulnerability_id
]
response = self.client.post(
reverse('securityscan-list'), self.data, format='json'
)
security_scan = SecurityScan.objects.get(pk=response.data['id'])
self.assertEqual(security_scan.vulnerabilities.count(), 1)
self.assertEqual(
response.data,
{'vulnerabilities': ['This list may not be empty.']},
self.vulnerability.id, security_scan.vulnerabilities.get().id
)

def test_create_scan_works_when_both_vulnerabilities_empty(self):
self.data['external_vulnerabilities'] = []
response = self.client.post(
reverse('securityscan-list'), self.data, format='json'
)
security_scan = SecurityScan.objects.get(pk=response.data['id'])
self.assertEqual(security_scan.vulnerabilities.count(), 0)

0 comments on commit 9bcc78c

Please sign in to comment.