Files
pages/pages-cli/test_pages.py
T
Anya Tokugawa d5d21b7841
Build and publish container images / images (push) Successful in 9m34s
Release container images / release (push) Successful in 36s
Refactor architecture into eight components and consolidate reliability work
Extract Grammaratur, Dregarnuhr, Geduldh, Mestionora, Ordoschnellic, Schutzaria, Kunstzeal and Seheweit behind compatible application contracts.

Include accumulated editor resilience, navigation and theme UX, remote CLI, storage/schema and DR groundwork, tests and design/operations documentation.

Verified: uncached full Go suite; go vet ./...; race tests for all eight components; 11 targeted Chromium tests; Node suites; 19 Python CLI tests; reproducible editor build. No push, release tag or deployment in this checkpoint.
2026-11-01 15:23:31 +09:00

265 lines
15 KiB
Python

import contextlib
from email.message import Message
import hashlib
import io
import json
import os
from pathlib import Path
import runpy
import stat
import sys
import tempfile
import threading
import time
import unittest
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from unittest.mock import patch
m = runpy.run_path(str(Path(__file__).with_name('pages')))
Client, Failure, Profile = m['Client'], m['Failure'], m['Profile']
@contextlib.contextmanager
def server(callback):
class Handler(BaseHTTPRequestHandler):
def do_GET(self): callback(self)
def do_POST(self): callback(self)
def log_message(self, *args): pass
instance = ThreadingHTTPServer(('127.0.0.1', 0), Handler)
thread = threading.Thread(target=instance.serve_forever, daemon=True)
thread.start()
try: yield 'http://127.0.0.1:%d' % instance.server_port
finally: instance.shutdown(); instance.server_close(); thread.join()
def reply(h, status, body, mime='application/json'):
raw = json.dumps(body).encode() if isinstance(body, dict) else body
h.send_response(status); h.send_header('Content-Type', mime); h.end_headers(); h.wfile.write(raw)
class RemoteTests(unittest.TestCase):
def configure(self, root, *args):
with contextlib.redirect_stdout(io.StringIO()) as output:
m['configure'](list(args), root)
return output.getvalue()
def test_configuration_defaults_and_switch(self):
with tempfile.TemporaryDirectory() as root:
self.configure(root,'--origin','https://one.example','--username','anya')
self.configure(root,'set','--profile','work','--origin','https://two.example','--username','worker','--lang','ja')
self.configure(root,'use','work')
settings=json.loads(self.configure(root,'show'))
self.assertEqual(settings['default_profile'],'work')
self.assertEqual(settings['profiles']['default']['lang'],'und')
self.assertEqual(settings['profiles']['work']['lang'],'ja')
self.configure(root,'--lang','en-US')
settings=json.loads(self.configure(root,'show'))
self.assertEqual(settings['profiles']['work']['lang'],'en-US')
with m['Configuration'](root).locked() as cfg:
self.assertEqual(stat.S_IMODE(cfg.path.stat().st_mode),0o600)
with self.assertRaises(Failure): self.configure(root,'use','missing')
with self.assertRaises(Failure): self.configure(root,'--lang','ja_JP')
def test_active_credentials_cannot_be_retargeted_or_shown(self):
with tempfile.TemporaryDirectory() as root:
self.configure(root,'--origin','https://one.example','--username','anya')
with Profile('default',root).locked() as profile:
profile.data={'origin':'https://one.example','username':'anya','token':'private-token'}; profile.save()
for args in [('--origin','https://other.example'),('--username','other')]:
with self.assertRaises(Failure): self.configure(root,*args)
self.assertNotIn('private-token',self.configure(root,'show'))
self.assertNotIn('private-token',self.configure(root,'list'))
self.configure(root,'--lang','ja')
with Profile('default',root).locked():
with self.assertRaises(Failure): self.configure(root,'--lang','en')
with Profile('default',root).locked() as profile:
profile.data.pop('token'); profile.save()
self.configure(root,'--origin','https://other.example')
def test_main_uses_saved_defaults_and_rejects_manual_retarget(self):
with tempfile.TemporaryDirectory() as root, patch.dict(os.environ,{'XDG_CONFIG_HOME':root}):
self.configure(root,'set','--profile','work','--origin','https://work.example','--username','worker','--lang','ja')
with Profile('work',root).locked() as profile:
profile.data={'origin':'https://work.example','username':'worker','token':'session-token','expires':time.time()+100,'cwd':'/notes'}; profile.save()
# Patch the globals used by main rather than running a real request.
globals=m['main'].__globals__
with patch.dict(globals,{'execute':lambda client,profile,args: self.assertEqual((client.base,client.lang,args),('https://work.example','ja',['ls']))}):
m['main'](['ls'])
with patch.dict(globals,{'execute':lambda client,profile,args: self.assertEqual(client.lang,'en')}):
m['main'](['--lang','en','ls'])
with m['Configuration'](root).locked() as cfg:
cfg.data['profiles']['work']['origin']='https://evil.example'; cfg.save()
with self.assertRaises(Failure): m['main'](['ls'])
def test_bad_configuration_is_not_printed(self):
with tempfile.TemporaryDirectory() as root:
with m['Configuration'](root).locked() as cfg:
cfg.data={'version':1,'default_profile':'default','profiles':{'default':{'origin':'https://example.test','token':'secret'}}}; cfg.save()
with self.assertRaises(Failure): self.configure(root,'show')
for invalid in [None,42,[],{}]:
with self.assertRaises(Failure): m['profile_name'](invalid)
with self.assertRaises(Failure): m['language'](invalid)
with self.assertRaises(Failure): m['origin'](invalid)
def test_origin_and_paths(self):
for value in ['http://example.com', 'https://user:pass@example.com', 'https://example.com/a', 'https://example.com?token=x']:
with self.assertRaises(Failure): m['origin'](value)
self.assertEqual(m['address']('../日本語 #?%', '/docs/deep'), 'path:/docs/日本語 #?%')
self.assertEqual(m['semantic']('../../..', '/'), '/')
for value in ['https://evil.test', 'asset:x:y', 'rev:bad', '/bad\x1b[31m']:
with self.assertRaises(Failure): m['address'](value)
self.assertNotIn('\x1b', m['safe_text']('\x1b]52;clipboard\x07'))
self.assertNotIn('\u202e', m['safe_text']('abc\u202e'))
def test_redirect_never_receives_bearer(self):
seen = []
with server(lambda h: (seen.append(h.headers.get('Authorization')), reply(h, 200, {}))) as sink:
def redirect(h):
h.send_response(302); h.send_header('Location', sink); h.end_headers()
with server(redirect) as base:
with self.assertRaises(Failure) as error: Client(base, 'secret').json('/test')
self.assertEqual(error.exception.status, 302)
self.assertEqual(seen, [])
def test_protocol_failures_and_limits(self):
for status, body, mime in [(200,b'<html>login</html>','text/html'), (200,b'not-json','application/json'), (403,b'private internal error','text/plain')]:
with server(lambda h: reply(h,status,body,mime)) as base:
with self.assertRaises(Failure) as error: Client(base).json('/test')
self.assertNotIn('private internal', str(error.exception))
with server(lambda h: reply(h,200,b'x'*100)) as base:
with self.assertRaises(Failure): Client(base).request('/large',limit=10)
def test_password_totp_contract(self):
requests = []
def handle(h):
requests.append((h.path, h.rfile.read(int(h.headers['Content-Length'])).decode()))
if h.path.endswith('/login'): reply(h,401,{'mfa_required':True,'challenge_token':'short-lived'})
else: reply(h,200,{'access_token':'a'*32,'token_type':'Bearer','expires_in':3600})
with server(handle) as base:
client = Client(base)
client.login('someone','secret',lambda prompt:'123456')
self.assertEqual(client.token,'a'*32)
self.assertIn('challenge_token=short-lived',requests[1][1])
self.assertNotIn('password',requests[1][1])
def test_private_profile_lock_and_symlinks(self):
with tempfile.TemporaryDirectory() as root:
with Profile('work',root).locked() as profile:
profile.data={'token':'secret'}; profile.save()
self.assertEqual(stat.S_IMODE(profile.path.stat().st_mode),0o600)
self.assertEqual(stat.S_IMODE(profile.root.stat().st_mode),0o700)
with self.assertRaises(Failure):
with Profile('work',root).locked(): pass
with Profile('work',root).locked() as loaded: self.assertEqual(loaded.data['token'],'secret')
profile.path.chmod(0o644)
with self.assertRaises(Failure):
with Profile('work',root).locked(): pass
profile.path.unlink(); profile.path.symlink_to(Path(root)/'missing')
with self.assertRaises(Failure):
with Profile('work',root).locked(): pass
def test_cas_and_authoring_contract(self):
state={'object_uuid':'object','head_revision_uuid':'base','principal_uuid':'person','lang':'ja',
'content_type':'text/html','filename':'a.html','body':'old'}
client=Client('https://example.test')
calls=[]
def api(route, **kwargs):
calls.append((route,kwargs['data']))
return {'html':'<p>new</p>'} if route.endswith('/markdown') else {'revision_uuid':'new'}
with patch.object(client,'json',side_effect=api): client.save(state,'new',True)
self.assertTrue(calls[0][1]['authoring']); self.assertFalse(calls[0][1]['projection'])
self.assertEqual(calls[1][1]['base_revision_uuid'],'base')
self.assertEqual(calls[1][1]['editor_principal_uuid'],'person')
self.assertEqual(calls[1][1]['lang'],'ja')
with patch.object(client,'json',side_effect=Failure('conflict',409)):
with self.assertRaises(Failure): client.save(state,'new')
def test_download_private_and_no_clobber(self):
headers=Message(); headers['X-PagesWiki-Revision-UUID']='rev'
with tempfile.TemporaryDirectory() as root:
profile=Profile('test',root)
client=Client('https://example.test')
file=str(Path(root)/'download')
with patch.object(client,'raw',return_value=(b'data',headers)), contextlib.redirect_stdout(io.StringIO()):
m['execute'](client,profile,['get','/path',file])
self.assertEqual(stat.S_IMODE(Path(file).stat().st_mode),0o600)
with self.assertRaises(FileExistsError): m['execute'](client,profile,['get','/path',file])
def test_editor_conflict_retains_private_recovery(self):
state={'object_uuid':'object','head_revision_uuid':'base','principal_uuid':'person','lang':'und',
'content_type':'text/plain','filename':'x.txt','body':'old','markdown':'old'}
client=Client('https://example.test','secret-token')
with tempfile.TemporaryDirectory() as root:
directory=Path(root)/'working'; directory.mkdir(mode=0o700)
def edit(args, **kwargs):
Path(args[-1]).write_text('my edit')
class Result: returncode=0
return Result()
with patch('tempfile.mkdtemp',return_value=str(directory)), patch('subprocess.run',side_effect=edit), patch.object(client,'snapshot',return_value=state), patch.object(client,'save',side_effect=Failure('conflict',409)), contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(io.StringIO()):
with self.assertRaises(Failure): m['editor'](client,'path:/x','auto')
self.assertEqual((directory/'document.txt').read_text(),'my edit')
recovery=(directory/'recovery.json').read_text()
self.assertNotIn('secret-token',recovery)
self.assertIn('base',recovery)
self.assertEqual(stat.S_IMODE((directory/'recovery.json').stat().st_mode),0o600)
def integration():
fixture=json.load(sys.stdin)
# Exercise the public entry point with persisted defaults, not only Client.
with tempfile.TemporaryDirectory() as root, patch.dict(os.environ,{'XDG_CONFIG_HOME':root}), contextlib.redirect_stdout(io.StringIO()) as output:
m['main'](['configure','--origin',fixture['origin'],'--username','admin'])
with patch('getpass.getpass',return_value='a long test password'): m['main'](['login'])
with Profile('default',root).locked() as session: session_token=session.data['token']
m['main'](['ls'])
m['main'](['status'])
def local_editor(args, **kwargs):
file=Path(args[-1]); file.write_text(file.read_text()+'\nConfigured CLI edit\n')
class Result: returncode=0
return Result()
with patch('subprocess.run',side_effect=local_editor): m['main'](['edit','/editor-fixture'])
check=Client(fixture['origin'],session_token)
assert 'Configured CLI edit' in check.snapshot('path:/editor-fixture')['body']
m['main'](['logout'])
assert session_token not in output.getvalue()
with Profile('default',root).locked() as session: assert 'token' not in session.data
client=Client(fixture['origin'])
client.login('admin','a long test password')
addr='path:/editor-fixture'
state=client.snapshot(addr)
assert state['lang']=='und' and state['principal_uuid']
missing=Client(fixture['origin'], client.token, 'ja')
try: missing.snapshot(addr)
except Failure: pass
else: raise AssertionError('missing language silently fell back')
assert client.listing('/')['entries']
assert client.save(state,'# Remote\n\n{{children}}\n',True)['revision_uuid'] != state['head_revision_uuid']
latest=client.snapshot(addr)
assert '{{children}}' in latest['markdown'] and '<h1' in latest['body']
try: client.save(state,'stale edit')
except Failure as e: assert e.status==409
else: raise AssertionError('stale save accepted')
raw,headers=client.raw(addr)
assert hashlib.sha256(raw).hexdigest()==headers['X-PagesWiki-Document-SHA256']
assert client.raw('rev:'+latest['revision_uuid'])[0]==raw
try: client.snapshot('rev:'+latest['revision_uuid'])
except Failure: pass
else: raise AssertionError('immutable edit accepted')
with tempfile.TemporaryDirectory() as root:
profile=Profile('integration',root)
profile.data={'cwd':'/'}
file=Path(root)/'edit.txt'; file.write_text('local replacement')
with contextlib.redirect_stdout(io.StringIO()):
m['execute'](client,profile,['put',str(file),addr,'--base',latest['head_revision_uuid']])
assert client.snapshot(addr)['body']=='local replacement'
client.json('/_/auth/logout',method='POST')
try: client.snapshot(addr)
except Failure as e: assert e.status in (401,403,303)
else: raise AssertionError('revoked session accepted')
print('Remote login/list/read/authoring/CAS/put/logout integration passed')
if __name__=='__main__':
if sys.argv[1:] == ['--integration']: integration()
else: unittest.main()