66 lines
3.2 KiB
Python
66 lines
3.2 KiB
Python
import sqlite3
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
from django.core.exceptions import ValidationError
|
|
from django.db import IntegrityError, transaction
|
|
from django.test import SimpleTestCase
|
|
|
|
from filament import services
|
|
from filament.models import InventoryItem, Variant
|
|
|
|
from .base import CatalogTestCase
|
|
|
|
|
|
class SafetyTests(CatalogTestCase):
|
|
def test_stale_active_variant_cannot_be_added_after_deactivation(self):
|
|
Variant.objects.filter(pk=self.spool.pk).update(is_active=False)
|
|
with self.assertRaises(ValidationError):
|
|
services.add_quantity(InventoryItem, self.alice, self.spool, 1)
|
|
self.assertFalse(InventoryItem.objects.exists())
|
|
|
|
def test_quantities_are_bounded_and_overflow_is_atomic(self):
|
|
for qty in (0, -1, 10000):
|
|
with self.assertRaises(ValidationError):
|
|
services.add_quantity(InventoryItem, self.alice, self.spool, qty)
|
|
services.add_quantity(InventoryItem, self.alice, self.spool, 9999)
|
|
with self.assertRaises(ValidationError):
|
|
services.add_quantity(InventoryItem, self.alice, self.spool, 1)
|
|
self.assertEqual(InventoryItem.objects.get().quantity, 9999)
|
|
with self.assertRaises(IntegrityError), transaction.atomic():
|
|
InventoryItem.objects.create(owner=self.bob, variant=self.spool, quantity=0)
|
|
with self.assertRaises(IntegrityError), transaction.atomic():
|
|
InventoryItem.objects.create(owner=self.alice, variant=self.spool, quantity=1)
|
|
|
|
def test_private_pages_are_not_cacheable_and_have_csp(self):
|
|
self.client.force_login(self.alice)
|
|
response = self.client.get("/bestand/")
|
|
self.assertIn("no-store", response.headers.get("Cache-Control", ""))
|
|
self.assertIn("default-src 'self'", response.headers.get("Content-Security-Policy", ""))
|
|
self.assertEqual(response.headers["X-Frame-Options"], "DENY")
|
|
|
|
def test_health_check_returns_no_user_or_database_details(self):
|
|
response = self.client.get("/healthz/")
|
|
self.assertEqual(response.status_code, 200)
|
|
self.assertEqual(response.json(), {"status": "ok"})
|
|
|
|
|
|
class BackupTests(SimpleTestCase):
|
|
def test_online_backup_is_consistent_private_and_never_overwrites(self):
|
|
self.assertTrue(callable(getattr(services, "backup_database", None)), "SQLite-Backup fehlt")
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
source = Path(directory) / "live.sqlite3"
|
|
target = Path(directory) / "snapshot.sqlite3"
|
|
with sqlite3.connect(source) as db:
|
|
db.execute("PRAGMA journal_mode=WAL")
|
|
db.execute("CREATE TABLE example (quantity INTEGER)")
|
|
db.execute("INSERT INTO example VALUES (3)")
|
|
db.commit()
|
|
services.backup_database(source, target)
|
|
with sqlite3.connect(target) as db:
|
|
self.assertEqual(db.execute("SELECT quantity FROM example").fetchone()[0], 3)
|
|
self.assertEqual(db.execute("PRAGMA integrity_check").fetchone()[0], "ok")
|
|
self.assertEqual(target.stat().st_mode & 0o777, 0o600)
|
|
with self.assertRaises(FileExistsError):
|
|
services.backup_database(source, target)
|