BlueGit

projects-timfalken-com verhalenboek

Files

verhalenboek / web / tests / startup.py

startup.py

feat: add a wiki with categories, chapters and internal links
timfa project_87242033_bot_b167a9be22027ad5a4b431d0ad8426c6 · pushed by timfa · 5 weeks ago
2556ae87 History
startup.py 3.0 KiB Download Raw
1#!/usr/bin/env python3
2"""Eerste start en een tweede start bewaren bestaande dagen en configuratie."""
3from pathlib import Path
4import shutil
5import sqlite3
6import subprocess
7import tempfile
8
9WEB = Path(__file__).resolve().parents[1]
10ARTIFACTS = WEB / 'tests/artifacts'
11ARTIFACTS.mkdir(exist_ok=True)
12with tempfile.TemporaryDirectory(prefix='startup-', dir=ARTIFACTS) as tmp:
13 root = Path(tmp)
14 shutil.copytree(WEB / 'lib', root / 'lib')
15 (root / 'data').mkdir()
16
17 def startup():
18 result = subprocess.run(
19 ['php', '-r', 'require $argv[1]; db();', str(root / 'lib/bootstrap.php')],
20 capture_output=True, text=True, check=True,
21 )
22 assert not result.stderr, result.stderr
23 log = root / 'data/php-errors.log'
24 assert not log.exists() or not log.read_text(), 'Startup emitted PHP errors'
25
26 startup()
27 config = root / 'data/config.php'
28 assert config.is_file(), 'Eerste start maakt config.php'
29 original = config.read_text()
30 assert all(f"'{key}' => ''" in original for key in ['base_url', 'client_id', 'client_secret'])
31 assert __import__('re').search(r"'key_secret' => '[a-f0-9]{64}'", original), original
32 config.write_text(original.replace("'client_id' => ''", "'client_id' => 'projects.timfalken.com'") + '\n// servercommentaar\n')
33 marked = config.read_text()
34 con = sqlite3.connect(root / 'data/verhalenboek.sqlite')
35 con.execute("INSERT INTO users (id, name, role, theme, font, created) VALUES ('auth:1','tim','admin','system','serif',1)")
36 con.execute("INSERT INTO days (date, title, created, updated) VALUES ('2026-01-01','Bewaard',1,1)")
37 con.execute("INSERT INTO stories (day_id, position, title, created, updated) VALUES (1,1,'Een',1,1)")
38 con.execute("INSERT INTO paragraphs (story_id, position, body) VALUES (1,1,'Blijft staan.')")
39 con.commit()
40 con.execute('DROP TABLE wiki_pages')
41 con.execute('DROP TABLE wiki_categories')
42 con.commit()
43 startup()
44 assert config.read_text() == marked, 'Bestaande config.php blijft byte voor byte staan'
45 tables = [name for (name,) in con.execute("SELECT name FROM sqlite_master WHERE type='table'")]
46 assert 'wiki_categories' in tables and 'wiki_pages' in tables, tables
47 row = con.execute('SELECT date, title FROM days').fetchone()
48 assert row == ('2026-01-01', 'Bewaard'), row
49 assert con.execute('SELECT body FROM paragraphs').fetchone()[0] == 'Blijft staan.'
50 assert 'email' not in [name for _, name, *_ in con.execute('PRAGMA table_info(users)')]
51 con.close()
52 import re
53 stripped = re.sub(r"\n 'key_secret' => '[a-f0-9]{64}',", '', marked, count=1)
54 assert "'key_secret' =>" not in stripped
55 config.write_text(stripped)
56 startup()
57 restored = config.read_text()
58 assert 'servercommentaar' in restored and "'client_id' => 'projects.timfalken.com'" in restored
59 assert re.search(r"'key_secret' => '[a-f0-9]{64}'", restored), restored
60print('Startup: fresh database, preserved day and preserved config passed.')