47 lines
1.8 KiB
Python
47 lines
1.8 KiB
Python
import pytest
|
|
|
|
from netbox_services.models import Service, ServiceTemplate
|
|
from netbox_services.signals import TemplateLockedError
|
|
|
|
|
|
@pytest.fixture
|
|
def template_with_service(db, tenant):
|
|
st = ServiceTemplate.objects.create(name="locked-tmpl", status="active", tree=[])
|
|
Service.objects.create(name="locked-svc", service_template=st, tenant=tenant)
|
|
return st
|
|
|
|
|
|
@pytest.fixture
|
|
def tenant(db):
|
|
from tenancy.models import Tenant
|
|
return Tenant.objects.create(name="lock-tenant", slug="lock-tenant")
|
|
|
|
|
|
@pytest.mark.django_db
|
|
class TestTemplateLock:
|
|
def test_cannot_edit_name_when_locked(self, template_with_service):
|
|
template_with_service.name = "renamed"
|
|
with pytest.raises(TemplateLockedError):
|
|
template_with_service.save()
|
|
|
|
def test_cannot_edit_tree_when_locked(self, template_with_service):
|
|
template_with_service.tree = [{"name": "x", "object_type": "dcim.device", "quantity": 1, "selector": {"type": "netbox_filter", "filters": {}}}]
|
|
with pytest.raises(TemplateLockedError):
|
|
template_with_service.save()
|
|
|
|
def test_cannot_delete_when_locked(self, template_with_service):
|
|
from django.db.models import ProtectedError
|
|
with pytest.raises((ProtectedError, TemplateLockedError)):
|
|
template_with_service.delete()
|
|
|
|
def test_can_edit_when_unlocked(self, db):
|
|
st = ServiceTemplate.objects.create(name="unlocked", status="draft", tree=[])
|
|
st.name = "renamed-unlocked"
|
|
st.save()
|
|
assert ServiceTemplate.objects.get(pk=st.pk).name == "renamed-unlocked"
|
|
|
|
def test_can_delete_when_unlocked(self, db):
|
|
st = ServiceTemplate.objects.create(name="deletable", tree=[])
|
|
st.delete()
|
|
assert not ServiceTemplate.objects.filter(pk=st.pk).exists()
|