Enable CLI language-aware editing and reuse OIDC authentication
This commit is contained in:
1 parent
f188e7eac1
commit
fd8d0f20d1
3 files changed
+154
-30
No files matched your search
+84
-6
@@ -139,11 +139,14 @@ class RemoteTests(unittest.TestCase):
|
||||
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']['default']['lang'],'auto')
|
||||
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')
|
||||
self.configure(root,'--lang','und')
|
||||
settings=json.loads(self.configure(root,'show'))
|
||||
self.assertEqual(settings['profiles']['work']['lang'],'und')
|
||||
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')
|
||||
@@ -274,6 +277,62 @@ class RemoteTests(unittest.TestCase):
|
||||
self.assertEqual(stat.S_IMODE(Path(file).stat().st_mode),0o600)
|
||||
with self.assertRaises(FileExistsError): m['execute'](client,profile,['get','/path',file])
|
||||
|
||||
def test_auto_snapshot_pins_visible_language_and_head(self):
|
||||
client=Client('https://example.test','token')
|
||||
state={'lang':'ja','head_revision_uuid':'current-ja','content_type':'text/plain','filename':'x.txt','body':'new','markdown':'new','editable':True,'principal_uuid':'owner'}
|
||||
headers={'X-PagesWiki-Object-UUID':'object','X-PagesWiki-Revision-UUID':'visible-ja'}
|
||||
with patch.object(client,'raw',return_value=(b'',headers)),patch.object(client,'json',side_effect=[{'lang':'ja'},state]) as api:
|
||||
result=client.snapshot('path:/x')
|
||||
self.assertEqual(result['lang'],'ja')
|
||||
self.assertEqual(result['head_revision_uuid'],'current-ja')
|
||||
self.assertIn('revision=visible-ja',api.call_args_list[0].args[0])
|
||||
self.assertIn('lang=ja',api.call_args_list[1].args[0])
|
||||
self.assertNotIn('revision=',api.call_args_list[1].args[0])
|
||||
|
||||
def test_fork_uses_empty_base_and_preserves_source(self):
|
||||
client=Client('https://example.test','token','en')
|
||||
target={'lang':'en','head_revision_uuid':'','content_type':'text/html','filename':'','body':'','markdown':'','editable':True,'principal_uuid':'owner'}
|
||||
source=dict(target,lang='ja',body='<h1>Source</h1>',markdown='# Source',revision_uuid='ja-base')
|
||||
headers={'X-PagesWiki-Object-UUID':'object','X-PagesWiki-Revision-UUID':'ja-base'}
|
||||
with patch.object(client,'raw',return_value=(b'',headers)),patch.object(client,'json',side_effect=[target,source]):
|
||||
result=client.snapshot('path:/x')
|
||||
self.assertEqual(result['lang'],'en'); self.assertEqual(result['head_revision_uuid'],'')
|
||||
self.assertEqual(result['source_language'],'ja'); self.assertEqual(result['body'],source['body'])
|
||||
with patch.object(client,'json',return_value={'revision_uuid':'en-new'}) as api:
|
||||
client.save(result,result['body'])
|
||||
self.assertEqual(api.call_args.kwargs['data']['base_revision_uuid'],'')
|
||||
self.assertEqual(api.call_args.kwargs['data']['lang'],'en')
|
||||
for changes in ({'editable':False},{'principal_uuid':'someone-else'}):
|
||||
with patch.object(client,'raw',return_value=(b'',headers)),patch.object(client,'json',side_effect=[dict(target,head_revision_uuid=''),dict(source,**changes)]):
|
||||
with self.assertRaises(Failure): client.snapshot('path:/x')
|
||||
|
||||
def test_editor_unchanged_fork_does_not_save(self):
|
||||
client=Client('https://example.test','token','ja')
|
||||
state={'object_uuid':'object','head_revision_uuid':'','principal_uuid':'owner','lang':'ja',
|
||||
'content_type':'text/plain','filename':'x.txt','body':'source','markdown':'source','new_language':True,'source_language':'und'}
|
||||
with patch.object(client,'snapshot',return_value=state),patch.object(client,'save') as save,patch('subprocess.run') as command,contextlib.redirect_stdout(io.StringIO()):
|
||||
command.return_value.returncode=0
|
||||
m['editor'](client,'path:/x','auto')
|
||||
save.assert_not_called()
|
||||
self.assertFalse(Path(command.call_args.args[0][-1]).exists())
|
||||
|
||||
def test_edit_language_override_does_not_change_profile(self):
|
||||
client=Client('https://example.test','token','und')
|
||||
with tempfile.TemporaryDirectory() as root:
|
||||
profile=Profile('test',root)
|
||||
profile.data={'cwd':'/notes'}
|
||||
def editor_override(editing,addr,mode):
|
||||
self.assertEqual((editing.base,editing.token,editing.lang),(client.base,client.token,'auto'))
|
||||
self.assertEqual((addr,mode),('path:/notes/page','auto'))
|
||||
with patch.dict(m['execute'].__globals__,{'editor':editor_override}):
|
||||
m['execute'](client,profile,['edit','page','--lang','auto'])
|
||||
self.assertEqual(client.lang,'und')
|
||||
self.assertEqual(profile.data,{'cwd':'/notes'})
|
||||
with patch.object(client,'snapshot',return_value={'head_revision_uuid':''}),patch.object(client,'save') as save:
|
||||
with self.assertRaisesRegex(Failure,'use edit --lang'):
|
||||
m['execute'](client,profile,['put','unused','page','--base',''])
|
||||
save.assert_not_called()
|
||||
|
||||
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'}
|
||||
@@ -284,8 +343,9 @@ class RemoteTests(unittest.TestCase):
|
||||
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 patch('tempfile.mkdtemp',return_value=str(directory)), patch('subprocess.run',side_effect=edit), patch.object(client,'snapshot',return_value=state) as snapshot, 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')
|
||||
snapshot.assert_called_with('uuid:object',lang='und')
|
||||
self.assertEqual((directory/'document.txt').read_text(),'my edit')
|
||||
recovery=(directory/'recovery.json').read_text()
|
||||
self.assertNotIn('secret-token',recovery)
|
||||
@@ -342,15 +402,33 @@ def integration():
|
||||
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=Client(fixture['origin'], lang='und')
|
||||
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')
|
||||
fork=missing.snapshot(addr)
|
||||
assert fork['lang']=='ja' and fork['head_revision_uuid']=='' and fork['new_language']
|
||||
assert fork['body']==state['body'] and fork['source_language']=='und'
|
||||
missing.save(fork,'# Japanese draft\n\n{{children}}\n',True)
|
||||
assert missing.snapshot(addr)['lang']=='ja'
|
||||
assert client.snapshot(addr)['head_revision_uuid']==state['head_revision_uuid']
|
||||
try: missing.save(fork,'competing first revision')
|
||||
except Failure as e: assert e.status==409
|
||||
else: raise AssertionError('concurrent language creation overwrote the head')
|
||||
auto=Client(fixture['origin'],client.token)
|
||||
auto_state=auto.snapshot('path:/ja-only')
|
||||
assert auto_state['lang']=='ja' and auto_state['head_revision_uuid']
|
||||
def edit_japanese(args, **kwargs):
|
||||
path=Path(args[-1]); path.write_text(path.read_text()+'\nJapanese edit\n')
|
||||
class Result: returncode=0
|
||||
return Result()
|
||||
with patch('subprocess.run',side_effect=edit_japanese),contextlib.redirect_stdout(io.StringIO()):
|
||||
m['editor'](auto,'path:/ja-only','auto')
|
||||
assert 'Japanese edit' in auto.snapshot('path:/ja-only')['body']
|
||||
pending=Client(fixture['origin'],client.token,'und').snapshot('path:/ja-only')
|
||||
assert pending['head_revision_uuid']==''
|
||||
assert client.listing('/')['entries']
|
||||
assert client.save(state,'# Remote\n\n{{children}}\n',True)['revision_uuid'] != state['head_revision_uuid']
|
||||
latest=client.snapshot(addr)
|
||||
|
||||
Reference in new issue
Block a user