feat: tenant-aware netbox-inventory assets
Assets without a tenant take the tenant of the device, module, inventory item or rack they are assigned to, both in clean() and before save. The plugin's assign, reassign and "create hardware from asset" views save assets without a tenant field and failed with mandatory tenancy before. Asset forms are also pre-filled from an assigned module or inventory item. Existing tenants are never changed. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
4dd01b82e3
commit
cf1cafb632
5 files changed
+134
-1
No files matched your search
@@ -48,3 +48,44 @@ class TenantValidationTest(SimpleTestCase):
|
||||
@patch("netbox_utilities.tenant_validation.tenant_required", return_value=False)
|
||||
def test_allows_missing_tenant_when_disabled(self, _tenant_required):
|
||||
validate_tenant_assignment(TenantModel())
|
||||
|
||||
|
||||
class FakeAssetMeta(FakeMeta):
|
||||
app_label = "netbox_inventory"
|
||||
model_name = "asset"
|
||||
|
||||
def get_fields(self):
|
||||
return [SimpleNamespace(name=name) for name in ("tenant", "device", "module", "inventoryitem", "rack")]
|
||||
|
||||
|
||||
class FakeAsset(TenantModel):
|
||||
_meta = FakeAssetMeta()
|
||||
|
||||
def __init__(self, tenant_id=None, device=None):
|
||||
super().__init__(tenant_id)
|
||||
self.device, self.module, self.inventoryitem, self.rack = device, None, None, None
|
||||
|
||||
|
||||
class FakeDevice:
|
||||
_meta = SimpleNamespace(get_fields=lambda: [SimpleNamespace(name="tenant")])
|
||||
|
||||
def __init__(self, tenant_id):
|
||||
self.tenant_id = tenant_id
|
||||
|
||||
|
||||
class InventoryAssetTenantTest(SimpleTestCase):
|
||||
@patch("netbox_utilities.tenant_validation.tenant_required", return_value=True)
|
||||
def test_reassigned_asset_takes_device_tenant_before_save(self, _tenant_required):
|
||||
from netbox_utilities.tenant_validation import _enforce_before_save
|
||||
|
||||
asset = FakeAsset(device=FakeDevice(42))
|
||||
_enforce_before_save(FakeAsset, asset)
|
||||
|
||||
self.assertEqual(asset.tenant_id, 42)
|
||||
|
||||
@patch("netbox_utilities.tenant_validation.tenant_required", return_value=True)
|
||||
def test_unassigned_asset_without_tenant_is_still_rejected(self, _tenant_required):
|
||||
from netbox_utilities.tenant_validation import _enforce_before_save
|
||||
|
||||
with self.assertRaises(ValidationError):
|
||||
_enforce_before_save(FakeAsset, FakeAsset())
|
||||
Reference in new issue
Block a user