Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

fix(restore-points): json one-door model reads; modernize last-domain restore test

Juan Denisffec0ca3adc8
diff --git a/backend/app/models/restore_point.py b/backend/app/models/restore_point.py
index 56074dae..9ee30d7e 100644
--- a/backend/app/models/restore_point.py
+++ b/backend/app/models/restore_point.py
@@ -1,12 +1,11 @@
"""Immutable checkpoints for panel-managed configuration surfaces."""
-import json
-
from app import db
+from app.models.json_column_mixin import JsonColumnMixin
from app.models.mixins import SerializableMixin, TimestampMixin, uuid_pk
-class RestorePoint(TimestampMixin, SerializableMixin, db.Model):
+class RestorePoint(JsonColumnMixin, TimestampMixin, SerializableMixin, db.Model):
"""One point on a scope's linear configuration timeline."""
__tablename__ = 'restore_points'
@@ -42,17 +41,10 @@ class RestorePoint(TimestampMixin, SerializableMixin, db.Model):
__serialize_exclude__ = ('payload_json', 'coverage_json')
def get_payload(self):
- try:
- return json.loads(self.payload_json)
- except (TypeError, ValueError):
- return {}
+ return self._json_read('payload_json', expect=dict)
def get_coverage(self):
- try:
- value = json.loads(self.coverage_json)
- except (TypeError, ValueError):
- return []
- return value if isinstance(value, list) else []
+ return self._json_read('coverage_json', default=[], expect=list)
def serialize_extra(self):
return {
diff --git a/backend/tests/test_recycle_bin.py b/backend/tests/test_recycle_bin.py
index 2cfd64cb..aa90a111 100644
--- a/backend/tests/test_recycle_bin.py
+++ b/backend/tests/test_recycle_bin.py
@@ -246,18 +246,23 @@ def test_restoring_the_last_domain_re_enables_the_site(app, app_with_domain, mon
When the deleted domain was the app's LAST one, delete_domain ran
disable_site() + delete_site() — the sites-enabled symlink is gone, not just
- the server_name. A bare NginxService.create_site rewrites the file and
- leaves it UNSERVED. Restore has to go through SiteDomainService.write_app_vhost,
- which calls enable_site as well.
+ the server_name. Restore has to go back through the enabling door:
+ write_app_vhost → create_site → write_vhost, whose default enable=True
+ re-links sites-enabled. A write that opted out of enabling would leave
+ the site UNSERVED.
"""
from app.services import domain_restore
from app.services.nginx_service import NginxService
seen = {'create': 0, 'enable': 0}
- monkeypatch.setattr(NginxService, 'create_site',
- classmethod(lambda cls, **kw: seen.__setitem__('create', seen['create'] + 1) or {'success': True}))
- monkeypatch.setattr(NginxService, 'enable_site',
- classmethod(lambda cls, name: seen.__setitem__('enable', seen['enable'] + 1) or {'success': True}))
+
+ def fake_write_vhost(cls, name, content, *, enable=True):
+ seen['create'] += 1
+ if enable:
+ seen['enable'] += 1
+ return {'success': True, 'path': f'/etc/nginx/sites-available/{name}'}
+
+ monkeypatch.setattr(NginxService, 'write_vhost', classmethod(fake_write_vhost))
assert domain_restore is not None # hook module must be importable