Files
Project-SDE-WP-Suite/tests/okta_sync_ui_check.py
Matt Mabrey 763ab8d98d T13.5: verify an auto-disable reads clearly in the admin console (D18)
No UI change. The existing Activity log card already renders a sync disable
legibly: system:okta_sync / user deprovisioned by sync / user / <name> /
okta_status: SUSPENDED|MISSING. tests/okta_sync_ui_check.py runs the real sync
on a throwaway database and reads the real page at 1440px and 390px (28/28):
rows, search, filter, no sideways scroll, no JS errors, and the disabled
account refused on its next request. Does not depend on CR-019's usage card.
2026-10-16 11:36:38 -07:00

154 lines
7.5 KiB
Python

#!/usr/bin/env python3
"""Does an auto-disable read clearly in the admin console? - D18 / T13.5.
T13.5 builds no new UI. It confirms that an account the Okta sync disabled shows
up legibly in the surface admins already use (the Activity log card on
admin.html), without special-casing, and that the disabled account is refused on
its very next request. This runs the REAL sync against a throwaway database,
then reads the real page in a headless browser at 1440px and 390px.
It does not rely on CR-019's Activity & usage card: only the Activity log card
(AuditLog) is read.
Boots its own throwaway SQLite + uvicorn + headless browser; run it alone.
Screenshots go to a folder printed at the end (WP_SHOTS_DIR overrides it).
Exit 0 all passed, 1 a failure, 2 could not run.
"""
import json
import os
import re
import sys
import tempfile
import time
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import cdp # noqa: E402
from browser_check import seed, start_server, chk, _PASS, _FAIL # noqa: E402
from qa_gate_check import api # noqa: E402
def ascii_(v, n=300):
return re.sub(r"\s+", " ", str(v)).encode("ascii", "replace").decode()[:n]
ROWS_JS = """JSON.stringify([...document.querySelectorAll('#audit-admin tbody tr')].map(
tr => [...tr.children].map(td => td.textContent.trim())))"""
def main():
exe = cdp.find_browser()
if not exe:
print("no headless-capable browser found; set WP_BROWSER.")
return 2
tmpdir = tempfile.mkdtemp(prefix="wpsuite-syncui-")
shots = os.environ.get("WP_SHOTS_DIR") or os.path.join(tmpdir, "shots")
db_path = os.path.join(tmpdir, "check.db")
server = browser = None
try:
tok = seed(db_path)
print("\n1. run the real sync against the throwaway database")
from server.db import SessionLocal
from server import okta_sync
from server.okta_sync import OktaUser
okta = lambda: [OktaUser("root", "ACTIVE"), OktaUser("sue", "ACTIVE"),
OktaUser("pat", "SUSPENDED"), OktaUser("mix", "ACTIVE"),
OktaUser("sam", "ACTIVE"), OktaUser("legacy", "ACTIVE")] # bob is missing
with SessionLocal() as db:
res = okta_sync.run_sync(db, fetch=okta)
chk("the sync disabled pat (SUSPENDED) and bob (missing)",
res.ok and sorted(res.disabled) == ["bob", "pat"], ascii_((res.ok, res.error, res.disabled)))
port = cdp.free_port()
base = "http://127.0.0.1:%d" % port
server = start_server(port, db_path)
if not server:
print("the server did not start")
return 2
print("\n2. a disabled account is refused on its very next request")
st, _ = api(base, "/api/auth/me", tok["pat"])
chk("pat (auto-disabled) is refused", st in (401, 403), st)
st, _ = api(base, "/api/auth/me", tok["bob"])
chk("bob (auto-disabled) is refused", st in (401, 403), st)
st, _ = api(base, "/api/auth/me", tok["mix"])
chk("mix (still active in Okta) still works", st == 200, st)
print("\n3. the audit rows through the API")
st, audit = api(base, "/api/audit?entity_type=user&limit=50", tok["root"])
mine = [e for e in (audit or []) if e.get("actor") == "system:okta_sync"]
chk("an admin gets both rows", st == 200 and len(mine) == 2, ascii_((st, len(mine))))
chk("each row names the action, the user, and the Okta status",
{(e["summary"], e["detail"].get("okta_status")) for e in mine} == {("pat", "SUSPENDED"), ("bob", "MISSING")},
ascii_([(e["summary"], e["detail"]) for e in mine]))
st, audit_sue = api(base, "/api/audit?entity_type=user&limit=50", tok["sue"])
chk("a project super user does not see them (they belong to no project)",
st == 200 and not [e for e in audit_sue if e.get("actor") == "system:okta_sync"])
print("\n4. the admin console, as an admin")
browser = cdp.Browser(exe)
page = browser.page()
for width, label in ((1440, "1440"), (390, "390")):
page.clear_cookies()
page.set_cookie("wp_session", tok["root"])
page.viewport(width, 900, mobile=(width < 500))
page.goto(base + "/admin.html")
time.sleep(2.0)
page.eval("void loadAudit()")
time.sleep(1.2)
rows = json.loads(page.eval(ROWS_JS) or "[]")
sync_rows = [r for r in rows if "system:okta_sync" in r]
chk("[%s] the Activity log lists both auto-disables" % label, len(sync_rows) == 2,
ascii_(rows[:4]))
if sync_rows:
r = next((x for x in sync_rows if "pat" in x), sync_rows[0])
# columns: When, Who, Action, Type, Item, Detail
chk("[%s] Who reads 'system:okta_sync', not a person" % label, r[1] == "system:okta_sync", r)
chk("[%s] Action reads 'user deprovisioned by sync'" % label,
r[2] == "user deprovisioned by sync", r)
chk("[%s] Type and Item name the account" % label, r[3] == "user" and r[4] == "pat", r)
chk("[%s] Detail gives the Okta status found" % label,
"SUSPENDED" in r[5] and "okta" in r[5].lower(), r)
miss = next((x for x in sync_rows if "bob" in x), None)
chk("[%s] a person missing from Okta reads 'MISSING'" % label,
bool(miss) and "MISSING" in miss[5], miss)
# filter + search work on it with no special-casing
page.eval("document.getElementById('audit-search').value='okta_sync'; renderAudit();")
rows = json.loads(page.eval(ROWS_JS) or "[]")
chk("[%s] searching 'okta_sync' finds exactly the sync rows" % label,
len(rows) == 2 and all("system:okta_sync" in r for r in rows), ascii_(rows))
page.eval("document.getElementById('audit-search').value='';"
"document.getElementById('audit-type').value='user'; renderAudit();")
rows = json.loads(page.eval(ROWS_JS) or "[]")
chk("[%s] the 'User accounts' filter keeps them" % label,
len([r for r in rows if "system:okta_sync" in r]) == 2)
page.eval("document.getElementById('audit-type').value=''; renderAudit();")
over = page.eval("document.documentElement.scrollWidth - document.documentElement.clientWidth")
chk("[%s] no sideways scroll on the page" % label, (over or 0) <= 0, over)
card = page.eval("(()=>{const e=document.getElementById('audit-admin');"
"e.scrollIntoView();return e.getBoundingClientRect().width>0})()")
chk("[%s] the card is on screen" % label, bool(card))
time.sleep(0.4)
page.screenshot(os.path.join(shots, "admin-activity-log-%s.png" % label), full_page=False)
errs = [e for e in page.js_errors() if "beforeunload" not in e]
chk("no JavaScript errors", not errs, ascii_(errs[:3]))
print("\n screenshots: " + shots)
finally:
if browser:
browser.close()
if server:
server.terminate()
print("\n" + "-" * 54)
print("%d/%d checks passed." % (len(_PASS), len(_PASS) + len(_FAIL)))
for f in _FAIL:
print(" - " + f)
return 1 if _FAIL else 0
if __name__ == "__main__":
sys.exit(main())