[ARCH] Media Alpha SSO entry and live tests 4/4
This commit is contained in:
1 parent
ea475d7537
commit
a9d910c743
3 files changed
+55
-2
No files matched your search
@@ -11,7 +11,8 @@
|
||||
## Где
|
||||
|
||||
```text
|
||||
127.0.0.1:9180
|
||||
Внутренний: 127.0.0.1:9180
|
||||
Через SSO: http://192.168.1.10:9161/alpha/
|
||||
/opt/media-alpha/media_alpha.py
|
||||
media-alpha.service
|
||||
```
|
||||
|
||||
@@ -11,6 +11,7 @@ from pathlib import Path
|
||||
JF = "http://192.168.1.103:8096"
|
||||
KEY = Path("/root/.jellyfin-key").read_text().strip()
|
||||
HOST, PORT = "127.0.0.1", 9180
|
||||
ALLOWED = {"maloyakapg@gmail.com"}
|
||||
|
||||
|
||||
def jf(path):
|
||||
@@ -55,7 +56,17 @@ class H(BaseHTTPRequestHandler):
|
||||
self.end_headers()
|
||||
self.wfile.write(data)
|
||||
|
||||
def allowed(self):
|
||||
user = (self.headers.get("X-Audit-User") or "").strip().lower()
|
||||
gateway = self.headers.get("X-Audit-Gateway") == "1"
|
||||
if not gateway and self.client_address[0] in ("127.0.0.1", "::1"):
|
||||
return True
|
||||
return user in ALLOWED
|
||||
|
||||
def do_GET(self):
|
||||
if not self.allowed():
|
||||
self.out(403, {"ok": False, "error": "forbidden"})
|
||||
return
|
||||
u = urllib.parse.urlparse(self.path)
|
||||
if u.path == "/health":
|
||||
try:
|
||||
@@ -98,7 +109,7 @@ a{color:#58a6ff}
|
||||
<div id=msg class=mut></div><div id=out class=grid></div>
|
||||
<script>
|
||||
const e=s=>String(s??'').replace(/[&<>"]/g,c=>({'&':'&','<':'<','>':'>','"':'"'}[c]));
|
||||
async function go(){const q=document.getElementById('q').value.trim();if(!q)return;document.getElementById('msg').textContent='поиск...';const r=await fetch('/v1/media/search?q='+encodeURIComponent(q));const d=await r.json();if(!d.ok){document.getElementById('msg').textContent=d.error||'ошибка';return}document.getElementById('msg').textContent=d.items.length?'':'ничего не найдено';document.getElementById('out').innerHTML=d.items.map(x=>`<div class=card><h3>${e(x.name)}</h3><div class=tag>${e(x.type)} ${e(x.year||'')}</div><div class=mut style="margin-top:6px">${e(x.overview||'')}</div></div>`).join('')}
|
||||
async function go(){const q=document.getElementById('q').value.trim();if(!q)return;document.getElementById('msg').textContent='поиск...';const r=await fetch('v1/media/search?q='+encodeURIComponent(q));const d=await r.json();if(!d.ok){document.getElementById('msg').textContent=d.error||'ошибка';return}document.getElementById('msg').textContent=d.items.length?'':'ничего не найдено';document.getElementById('out').innerHTML=d.items.map(x=>`<div class=card><h3>${e(x.name)}</h3><div class=tag>${e(x.type)} ${e(x.year||'')}</div><div class=mut style="margin-top:6px">${e(x.overview||'')}</div></div>`).join('')}
|
||||
document.getElementById('q').addEventListener('keydown',e=>{if(e.key==='Enter')go()});
|
||||
</script></body></html>"""
|
||||
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Live read-only tests for Media Alpha (local service)."""
|
||||
import json
|
||||
import unittest
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
|
||||
BASE = "http://127.0.0.1:9180"
|
||||
|
||||
|
||||
def get(path, headers=None):
|
||||
req = urllib.request.Request(BASE + path, headers=headers or {})
|
||||
with urllib.request.urlopen(req, timeout=10) as r:
|
||||
return r.status, r.read().decode()
|
||||
|
||||
|
||||
class Alpha(unittest.TestCase):
|
||||
def test_health(self):
|
||||
code, body = get("/health")
|
||||
self.assertEqual(code, 200)
|
||||
self.assertTrue(json.loads(body).get("jellyfin"))
|
||||
|
||||
def test_search_returns_real_items(self):
|
||||
code, body = get("/v1/media/search?q=the")
|
||||
self.assertEqual(code, 200)
|
||||
data = json.loads(body)
|
||||
self.assertTrue(data.get("ok"))
|
||||
self.assertGreater(len(data.get("items") or []), 0)
|
||||
|
||||
def test_other_email_forbidden(self):
|
||||
with self.assertRaises(urllib.error.HTTPError) as ctx:
|
||||
get("/", {"X-Audit-Gateway": "1", "X-Audit-User": "other@example.com"})
|
||||
self.assertEqual(ctx.exception.code, 403)
|
||||
|
||||
def test_roman_allowed(self):
|
||||
code, _ = get("/", {"X-Audit-Gateway": "1", "X-Audit-User": "maloyakapg@gmail.com"})
|
||||
self.assertEqual(code, 200)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main(verbosity=2)
|
||||
Reference in new issue
Block a user