Skip to content

Commit

Permalink
#449 Fix qpc source add/edit to enforce single hosts in the case of a…
Browse files Browse the repository at this point in the history
… range of hosts

- Updated the serializer to make sure that a range of hosts has not been entered for both Vcenter and Satellite source types
- Updated the tests for sources
  • Loading branch information
abaiken committed Jan 19, 2018
1 parent 0e6b253 commit 215f735
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
20 changes: 20 additions & 0 deletions quipucords/api/source/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ def create(self, validated_data):
'hosts': [_(messages.VC_ONE_HOST)]
}
raise ValidationError(error)
elif hosts_data and '[' in hosts_data[0]['host_range']:
error = {
'hosts': [_(messages.VC_ONE_HOST)]
}
raise ValidationError(error)
if credentials and len(credentials) > 1:
error = {
'credentials': [_(messages.VC_ONE_CRED)]
Expand All @@ -174,6 +179,11 @@ def create(self, validated_data):
'hosts': [_(messages.SAT_ONE_HOST)]
}
raise ValidationError(error)
elif hosts_data and '[' in hosts_data[0]['host_range']:
error = {
'hosts': [_(messages.VC_ONE_HOST)]
}
raise ValidationError(error)
if credentials and len(credentials) > 1:
error = {
'credentials': [_(messages.SAT_ONE_CRED)]
Expand Down Expand Up @@ -239,6 +249,11 @@ def update(self, instance, validated_data):
'hosts': [_(messages.VC_ONE_HOST)]
}
raise ValidationError(error)
elif hosts_data and '[' in hosts_data[0]['host_range']:
error = {
'hosts': [_(messages.VC_ONE_HOST)]
}
raise ValidationError(error)
if credentials and len(credentials) > 1:
error = {
'credentials': [_(messages.VC_ONE_CRED)]
Expand All @@ -258,6 +273,11 @@ def update(self, instance, validated_data):
'hosts': [_(messages.SAT_ONE_HOST)]
}
raise ValidationError(error)
elif hosts_data and '[' in hosts_data[0]['host_range']:
error = {
'hosts': [_(messages.VC_ONE_HOST)]
}
raise ValidationError(error)
if credentials and len(credentials) > 1:
error = {
'credentials': [_(messages.SAT_ONE_CRED)]
Expand Down
62 changes: 62 additions & 0 deletions quipucords/api/source/tests_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,14 @@ def test_create_vc_with_hosts(self):
'hosts': ['1.2.3.4', '1.2.3.5'],
'credentials': [self.vc_cred_for_upload]})

def test_create_vc_with_host_range(self):
"""A vcenter source must not have multiple hosts."""
self.create_expect_400(
{'name': 'source1',
'source_type': Source.VCENTER_SOURCE_TYPE,
'hosts': ['1.2.3.4/5'],
'credentials': [self.vc_cred_for_upload]})

def test_create_sat_with_hosts(self):
"""A satellite source must not have multiple hosts."""
self.create_expect_400(
Expand All @@ -333,6 +341,14 @@ def test_create_sat_with_hosts(self):
'hosts': ['1.2.3.4', '1.2.3.5'],
'credentials': [self.sat_cred_for_upload]})

def test_create_sat_with_host_range(self):
"""A vcenter source must not have multiple hosts."""
self.create_expect_400(
{'name': 'source1',
'source_type': Source.SATELLITE_SOURCE_TYPE,
'hosts': ['1.2.3.4/5'],
'credentials': [self.sat_cred_for_upload]})

def test_create_req_type(self):
"""A vcenter source must have an type."""
self.create_expect_400(
Expand Down Expand Up @@ -520,6 +536,52 @@ def test_update_empty_hosts(self):
self.assertEqual(json_rsp['hosts'][0],
messages.SOURCE_HOSTS_CANNOT_BE_EMPTY)

def test_update_vc_range_hosts(self):
"""Fail update due to empty host array."""
initial = self.create_expect_201({
'name': 'source',
'source_type': Source.VCENTER_SOURCE_TYPE,
'hosts': ['1.2.3.4'],
'port': '22',
'credentials': [self.vc_cred_for_upload]})

data = {'name': 'source',
'hosts': ['1.2.3.4/5'],
'port': 22,
'credentials': [self.vc_cred_for_upload]}
url = reverse('source-detail', args=(initial['id'],))
response = self.client.put(url,
json.dumps(data),
content_type='application/json',
format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
json_rsp = response.json()
self.assertEqual(json_rsp['hosts'][0],
messages.VC_ONE_HOST)

def test_update_sat_range_hosts(self):
"""Fail update due to empty host array."""
initial = self.create_expect_201({
'name': 'source',
'source_type': Source.SATELLITE_SOURCE_TYPE,
'hosts': ['1.2.3.4'],
'port': '22',
'credentials': [self.sat_cred_for_upload]})

data = {'name': 'source',
'hosts': ['1.2.3.4/5'],
'port': 22,
'credentials': [self.sat_cred_for_upload]}
url = reverse('source-detail', args=(initial['id'],))
response = self.client.put(url,
json.dumps(data),
content_type='application/json',
format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
json_rsp = response.json()
self.assertEqual(json_rsp['hosts'][0],
messages.VC_ONE_HOST)

def test_update_type_passed(self):
"""Fail update due to type passed."""
initial = self.create_expect_201({
Expand Down

0 comments on commit 215f735

Please sign in to comment.