Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

9 downloads

dbbasic-object-server/tests/test_backup_schedule_attention.py
Install on DigitalOcean Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

Daniel Quellhorst Changelog for this batch. 65ef78d 3 hours, 19 minutes ago History
test_backup_schedule_attention.py · 5.1 KB · 125 lines
1"""Backup problems reach the home screen's attention band, loudly and for
2admins only, and the /backups page shows the detail.
3
4The provider is the half that makes somebody read the status: a backup
5that stopped running looks exactly like one that is running until the day
6it is needed.
7"""
8from __future__ import annotations
9
10import importlib.util
11import json
12from pathlib import Path
13from types import SimpleNamespace
14
15import pytest
16
17import object_attention
18import object_backup_schedule as sched
19import object_records
20
21PACKAGE = Path(__file__).resolve().parents[1] / "packages" / "system-dashboard"
22
23
24def _load(name):
25 path = PACKAGE / "objects" / "system" / f"{name}.py"
26 spec = importlib.util.spec_from_file_location(f"_test_{name}", path)
27 module = importlib.util.module_from_spec(spec)
28 spec.loader.exec_module(module)
29 return module
30
31
32@pytest.fixture
33def box(tmp_path, monkeypatch):
34 objects_dir = tmp_path / "objects"
35 data_dir = tmp_path / "data"
36 dest = tmp_path / "offsite"
37 dest.mkdir()
38 (objects_dir / "site").mkdir(parents=True)
39 (objects_dir / "site" / "home.py").write_text("def GET(request):\n return {}\n")
40 (data_dir / "schemas").mkdir(parents=True)
41 (data_dir / "schemas" / "notes.json").write_text(json.dumps(
42 {"name": "notes", "fields": [{"name": "id", "type": "string"}]}))
43 object_records.create_collection_record("notes", {"id": "n1"}, base_dir=data_dir)
44 monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir))
45 monkeypatch.setenv("DBBASIC_OBJECTS_DIR", str(objects_dir))
46 return {"objects": objects_dir, "data": data_dir, "dest": dest}
47
48
49def test_the_provider_reads_zero_while_backups_are_off(box, monkeypatch):
50 monkeypatch.delenv(sched.DESTINATION_ENV, raising=False)
51 assert _load("backup_attention").COUNT({}) == {"count": 0}
52
53
54def test_the_provider_counts_problems_and_names_the_first(box, monkeypatch):
55 monkeypatch.setenv(sched.DESTINATION_ENV, str(box["dest"]))
56 answer = _load("backup_attention").COUNT({})
57 assert answer["count"] >= 2 and "no backup has completed yet" in answer["detail"]
58
59 sched.run_backup(box["data"], objects_dir=box["objects"])
60 assert _load("backup_attention").COUNT({}) == {"count": 0}
61
62 snapshot = sched.list_snapshots(box["dest"])[0]
63 (snapshot / "objects" / "site" / "home.py").write_text("tampered\n")
64 sched.run_restore_test(box["data"], objects_dir=box["objects"])
65 answer = _load("backup_attention").COUNT({})
66 assert answer["count"] == 1 and "restore test" in answer["detail"]
67
68
69def _source():
70 manifest = json.loads((PACKAGE / "dbbasic-package.json").read_text())
71 return next(e for e in manifest["attention"] if e["id"] == "backup_problems")
72
73
74def test_the_declaration_is_urgent_and_names_no_reads():
75 entry = _source()
76 assert entry["severity"] == "urgent"
77 assert entry["object_id"] == "system_backup_attention"
78 assert "reads" not in entry
79
80
81def test_only_an_admin_is_shown_the_count(tmp_path):
82 """No `reads` means admins only (object_attention.provider_shown_to)."""
83 source = {"id": "backup_problems", "reads": ""}
84 member = SimpleNamespace(is_admin=False, signed_in=True,
85 reads_every_row=lambda name: True)
86 admin = SimpleNamespace(is_admin=True, signed_in=True,
87 reads_every_row=lambda name: True)
88 assert object_attention.provider_shown_to(member, source, base_dir=tmp_path) is False
89 assert object_attention.provider_shown_to(admin, source, base_dir=tmp_path) is True
90
91
92def test_the_page_shows_nothing_to_a_non_admin(box, monkeypatch):
93 monkeypatch.setenv(sched.DESTINATION_ENV, str(box["dest"]))
94 page = _load("backups")
95 body = page.GET({"_identity": {"user_id": "u1", "roles": ["member"]}})["body"]
96 assert "needs an admin role" in body and str(box["dest"]) not in body
97 body = page.GET({})["body"]
98 assert "Sign in" in body
99
100
101def test_the_page_says_off_in_words(box, monkeypatch):
102 monkeypatch.delenv(sched.DESTINATION_ENV, raising=False)
103 body = _load("backups").GET({"_identity": {"user_id": "a", "roles": ["admin"]}})["body"]
104 assert "<strong>off</strong>" in body and sched.DESTINATION_ENV in body
105
106
107def test_the_page_shows_a_failure_as_a_failure(box, monkeypatch):
108 monkeypatch.setenv(sched.DESTINATION_ENV, str(box["dest"]))
109 sched.run_backup(box["data"], objects_dir=box["objects"])
110 page = _load("backups")
111 admin = {"_identity": {"user_id": "a", "roles": ["admin"]}}
112 assert "Healthy." in page.GET(admin)["body"]
113 snapshot = sched.list_snapshots(box["dest"])[0]
114 (snapshot / "objects" / "site" / "home.py").write_text("tampered\n")
115 sched.run_restore_test(box["data"], objects_dir=box["objects"])
116 body = page.GET(admin)["body"]
117 assert "Needs attention." in body and "FAILED" in body and "Healthy." not in body
118
119
120def test_the_route_and_execute_rule_are_seeded():
121 routes = (PACKAGE / "seed" / "site_routes.tsv").read_text()
122 assert "route_backups\t/backups\tsystem_backups" in routes
123 rules = json.loads((PACKAGE / "permissions" / "rules.json").read_text())["rules"]
124 assert any(rule.get("object_id") == "system_backups" and rule["actions"] == ["execute"]
125 for rule in rules)