Include utility modules, shared channels, prompts, read-only skills, wardrobe memory and regression tests. Externalize legacy credentials and exclude private runtime state.
54 lines
2.3 KiB
Python
54 lines
2.3 KiB
Python
"""Credential assignments stay configurable without importing live services."""
|
|
import ast
|
|
import os
|
|
from pathlib import Path
|
|
import secrets
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
MAPPINGS = (
|
|
('bot-utility/for_admin_komari_agent.py', 'KOMARI_PASSWORD', 'KOMARI_PASSWORD'),
|
|
('bot-utility/pixiv.py', 'CF_PASSWORD', 'PIXIV_CF_PASSWORD'),
|
|
('mail-bridge/mailWeb.py', 'HMAIL_ADMIN_PASSWORD', 'HMAIL_ADMIN_PASSWORD'),
|
|
('mail-bridge/mailWeb.py', 'REGISTER_AUTH_CODE', 'MAIL_REGISTER_AUTH_CODE'),
|
|
('mail-bridge/mailWeb.py', 'app.secret_key', 'MAIL_FLASK_SECRET_KEY'),
|
|
)
|
|
|
|
|
|
def expression(relative, target):
|
|
tree = ast.parse((ROOT / relative).read_text(encoding='utf-8'))
|
|
for node in tree.body:
|
|
if isinstance(node, ast.Assign) and any(ast.unparse(t) == target for t in node.targets):
|
|
return ast.Expression(node.value)
|
|
raise AssertionError('Credential assignment missing: ' + target)
|
|
|
|
|
|
class EnvironmentCredentialTests(unittest.TestCase):
|
|
def test_each_credential_comes_from_its_environment_variable(self):
|
|
for path, target, env in MAPPINGS:
|
|
with self.subTest(target=target), patch.dict(os.environ, {env: 'temporary-unit-test-value'}):
|
|
value = eval(compile(expression(path, target), '<credential-assignment>', 'eval'),
|
|
{'os': os, 'secrets': secrets})
|
|
self.assertEqual(value, 'temporary-unit-test-value')
|
|
|
|
def test_missing_passwords_default_empty_not_embedded_credentials(self):
|
|
for path, target, _ in MAPPINGS[:-1]:
|
|
with self.subTest(target=target), patch.dict(os.environ, {}, clear=True):
|
|
value = eval(compile(expression(path, target), '<credential-assignment>', 'eval'),
|
|
{'os': os, 'secrets': secrets})
|
|
self.assertEqual(value, '')
|
|
|
|
def test_unconfigured_flask_key_is_ephemeral_not_shared(self):
|
|
path, target, _ = MAPPINGS[-1]
|
|
with patch.dict(os.environ, {}, clear=True):
|
|
code = compile(expression(path, target), '<credential-assignment>', 'eval')
|
|
one = eval(code, {'os': os, 'secrets': secrets})
|
|
two = eval(code, {'os': os, 'secrets': secrets})
|
|
self.assertTrue(one)
|
|
self.assertNotEqual(one, two)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|