okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
2ba95cf61
main
1"""The web app uses an edited API key immediately, even when site storage is blocked. 2 3Runs the page's storage and server-access code with Node, without a GPU or server: 4python -m unittest serve.test_web_api_key -v 5""" 6import json 7from pathlib import Path 8import shutil 9import subprocess 10import unittest 11 12APP = Path (__file__ ).with_name ("web" )/ "app.js" 13 14 15@ unittest . skipUnless ( shutil . which ( "node" ), "node is not installed" ) 16class WebApiKey (unittest .TestCase ): 17def run_js (self ,actions ,* ,stored = None ,blocked = False ,quota = False ): 18src = APP .read_text (encoding = "utf-8" ) 19store = src [src .index ("const store =" ):src .index ("// ------------------------------------------------------------------ toasts" )] 20access = src [src .index ("// ------------------------------------------------------------------ server access" ):src .index ("let health =" )] 21js = f""" 22const data = new Map(Object.entries( { json . dumps ( stored or {}) } )); 23const field = {{value: ""}}; 24const notices = []; 25const $ = () => field; 26const toast = (...args) => notices.push(args); 27const localStorage = {{ 28getItem(k) {{ 29if ( { json . dumps ( blocked ) } ) throw new Error("Storage blocked"); 30return data.has(k) ? data.get(k) : null; 31}}, 32setItem(k, v) {{ 33if ( { json . dumps ( blocked or quota ) } ) throw new Error("Storage unavailable"); 34data.set(k, v); 35}}, 36}}; 37function edit(value, event = "input") {{ 38field.value = value; 39const handler = field["on" + event]; 40if (handler) handler(); 41}} 42{ store } 43{ access } 44{ actions } 45""" 46result = subprocess .run (["node" ,"-e" ,js ],capture_output = True ,text = True ,timeout = 30 ) 47self .assertEqual (result .returncode ,0 ,result .stderr ) 48return json .loads (result .stdout ) 49 50def test_input_is_used_and_saved_without_blur (self ): 51got = self .run_js (''' 52edit(" test-key "); 53console.log(JSON.stringify({headers: headers(true), saved: data.get("strata.apikey")})); 54''' ) 55self .assertEqual (got , {"headers" : {"Authorization" :"Bearer test-key" ,"Content-Type" :"application/json" }, 56"saved" :'"test-key"' }) 57 58def test_saved_key_is_restored_on_reload (self ): 59got = self .run_js ('console.log(JSON.stringify({value: field.value, headers: headers()}));' , 60stored = {"strata.apikey" :'"saved-key"' }) 61self .assertEqual (got , {"value" :"saved-key" ,"headers" : {"Authorization" :"Bearer saved-key" }}) 62 63def test_blocked_storage_still_uses_key_in_memory (self ): 64got = self .run_js ('edit("test-key", "change"); console.log(JSON.stringify(headers()));' ,blocked = True ) 65self .assertEqual (got , {"Authorization" :"Bearer test-key" }) 66 67def test_failed_write_does_not_reuse_old_key (self ): 68got = self .run_js ('edit("new-key", "change"); console.log(JSON.stringify(headers()));' , 69stored = {"strata.apikey" :'"old-key"' },quota = True ) 70self .assertEqual (got , {"Authorization" :"Bearer new-key" }) 71 72def test_clearing_key_removes_authorization (self ): 73for quota in (False ,True ): 74with self .subTest (quota = quota ): 75got = self .run_js ('edit(" "); console.log(JSON.stringify(headers()));' , 76stored = {"strata.apikey" :'"old-key"' },quota = quota ) 77self .assertEqual (got , {}) 78 79def test_successful_save_notifies_only_on_change (self ): 80got = self .run_js (''' 81edit("test-key"); 82const before = notices.length; 83edit("test-key", "change"); 84console.log(JSON.stringify({before, kinds: notices.map(n => n[0])})); 85''' ) 86self .assertEqual (got , {"before" :0 ,"kinds" : ["success" ]}) 87 88def test_failed_save_warns_instead_of_claiming_success (self ): 89got = self .run_js ('edit("test-key", "change"); console.log(JSON.stringify(notices.map(n => n[0])));' , 90blocked = True ) 91self .assertEqual (got , ["warn" ]) 92 93def test_server_without_key_keeps_json_header_only (self ): 94got = self .run_js ('console.log(JSON.stringify(headers(true)));' ) 95self .assertEqual (got , {"Content-Type" :"application/json" }) 96 97 98if __name__ == "__main__" : 99unittest .main ()