Skip to content

Commit

Permalink
fix errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan1elRoos committed Oct 1, 2024
1 parent 3403609 commit a58eae7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
20 changes: 11 additions & 9 deletions src/nac/management/commands/import_devices_from_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def check_device(self, deviceObject):
f"{deviceObject.get('appl-NAC-Hostname')}")
try:
if deviceObject.get('objectClass') != 'appl-NAC-Device':
raise Exception(
raise ValidationError(
f"Invalid Object-type! EXPECTED: appl-NAC-Device <->"
f" ACTUAL: {deviceObject.get('objectClass')}")
with transaction.atomic():
Expand All @@ -100,22 +100,24 @@ def check_device(self, deviceObject):
deviceRoleProd = DeviceRoleProd.objects.get(
name=deviceObject.get("appl-NAC-DeviceRoleProd"))
except ObjectDoesNotExist:
raise Exception(
"DeviceRoleProd: %s not in Database",
deviceObject.get("appl-NAC-DeviceRoleProd"))
raise ValidationError(
f"DeviceRoleProd: "
f"{deviceObject.get("appl-NAC-DeviceRoleProd")} "
f"not in Database")
try:
deviceRoleInst = DeviceRoleInst.objects.get(
name=deviceObject.get("appl-NAC-DeviceRoleInst"))
except ObjectDoesNotExist:
raise Exception(
"DeviceRoleInst: %s not in Database",
deviceObject.get("appl-NAC-DeviceRoleInst"))
raise ValidationError(
f"DeviceRoleInst: "
f"{deviceObject.get("appl-NAC-DeviceRoleInst")} "
f"not in Database")
if deviceRoleProd not in auth_group.DeviceRoleProd.all():
raise Exception(
raise ValidationError(
f"DeviceRoleProd: {deviceRoleProd} "
f"not in authorization group: {auth_group}")
elif deviceRoleInst not in auth_group.DeviceRoleInst.all():
raise Exception(
raise ValidationError(
f"DeviceRoleInst: {deviceRoleInst} "
f"not in authorization group: {auth_group}")

Expand Down
12 changes: 6 additions & 6 deletions src/tests/test_commands/test_import_devices_from_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ def test_check_device_exceptions(
"appl-NAC-DeviceRoleProd": "dummy",
"appl-NAC-DeviceRoleInst": test_deviceRoleInst}
with pytest.raises(
Exception,
match="('DeviceRoleProd: %s not in Database', 'dummy')"):
ValidationError,
match="('DeviceRoleProd: dummy not in Database')"):
command.check_device(invalid_device)
mock_atomic.assert_called()

Expand All @@ -152,8 +152,8 @@ def test_check_device_exceptions(
"appl-NAC-DeviceRoleProd": test_deviceRoleProd,
"appl-NAC-DeviceRoleInst": "dummy"}
with pytest.raises(
Exception,
match="('DeviceRoleInst: %s not in Database', 'dummy')"):
ValidationError,
match="('DeviceRoleInst: dummy not in Database')"):
command.check_device(invalid_device)
mock_atomic.assert_called()

Expand All @@ -167,7 +167,7 @@ def test_check_device_exceptions(
"appl-NAC-DeviceRoleProd": "dummy",
"appl-NAC-DeviceRoleInst": test_deviceRoleInst}
with pytest.raises(
Exception, match="DeviceRoleProd: dummy "
ValidationError, match="DeviceRoleProd: dummy "
"not in authorization group: test"):
command.check_device(invalid_device)
mock_atomic.assert_called()
Expand All @@ -181,7 +181,7 @@ def test_check_device_exceptions(
"appl-NAC-DeviceRoleProd": test_deviceRoleProd,
"appl-NAC-DeviceRoleInst": "dummy"}
with pytest.raises(
Exception, match="DeviceRoleInst: dummy "
ValidationError, match="DeviceRoleInst: dummy "
"not in authorization group: test"):
command.check_device(invalid_device)
mock_atomic.assert_called()
Expand Down

0 comments on commit a58eae7

Please sign in to comment.