ext/web P3: RBAC, audit timestamps, FK, gallery, ETag
Complete the remaining network-extension gaps so media sites can gate by role, auto-stamp rows, declare foreign keys, browse uploads, and conditional-get downloads. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
2d51f29199
commit
c6360adccb
13 files changed
+938
-35
No files matched your search
@@ -0,0 +1,23 @@
|
||||
---
|
||||
title: web P3 live — gallery HTML + download ETag
|
||||
import web:ext/web/web.mq.md
|
||||
import plugin:lib/plugin.mq.md
|
||||
import sys:lib/sys.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
*p = > plugin.native_path name="web"*
|
||||
1. `p`
|
||||
> plugin.load path=`p`
|
||||
2. *
|
||||
> sys.exit code=1
|
||||
|
||||
*blob = > web.storage url="file:web-fixtures/data/p3-live-blobs"*
|
||||
> `blob`.put key="uploads/hello.txt" body="etag-body" content_type="text/plain"
|
||||
|
||||
*pg = > web.page title="p3-live"*
|
||||
*app = > web.app page=`pg` port=18131*
|
||||
*app = > `app`.download path="/_media/{*key}" storage=`blob` disposition="inline"*
|
||||
*app = > `app`.gallery path="/gallery" storage=`blob` prefix="uploads/" title="P3 Gallery" download_base="/_media"*
|
||||
> `app`.listen
|
||||
@@ -0,0 +1,106 @@
|
||||
---
|
||||
title: web P3 smoke — audit timestamps, FK, RBAC, gallery (offline)
|
||||
import web:ext/web/web.mq.md
|
||||
import plugin:lib/plugin.mq.md
|
||||
import sys:lib/sys.mq.md
|
||||
import json:lib/json.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
*p = > plugin.native_path name="web"*
|
||||
1. `p`
|
||||
> plugin.load path=`p`
|
||||
2. *
|
||||
> sys.exit code=1
|
||||
|
||||
*store = > web.db url="sqlite:web-fixtures/data/p3-smoke.db"*
|
||||
> `store`.exec sql="DROP TABLE IF EXISTS comments"
|
||||
> `store`.exec sql="DROP TABLE IF EXISTS posts"
|
||||
|
||||
`post_fields` =
|
||||
|
||||
| 字段 | 类型 | 可空 |
|
||||
|------|------|------|
|
||||
| id | int | 否 |
|
||||
| title | text | 否 |
|
||||
| created_at | timestamp | 是 |
|
||||
| updated_at | timestamp | 是 |
|
||||
|
||||
> `store`.init name="posts" fields=`post_fields`
|
||||
|
||||
`comment_fields` =
|
||||
|
||||
| 字段 | 类型 | 可空 | 外键 |
|
||||
|------|------|------|------|
|
||||
| id | int | 否 | |
|
||||
| post_id | int | 否 | posts |
|
||||
| body | text | 否 | |
|
||||
|
||||
> `store`.init name="comments" fields=`comment_fields`
|
||||
|
||||
`rows` =
|
||||
|
||||
| @ | title |
|
||||
|---|-------|
|
||||
| 1 | Hello P3 |
|
||||
|
||||
> `store`.insert table="posts" rows=`rows`
|
||||
|
||||
*got = > `store`.get table="posts" id=1*
|
||||
*ca = got[^created_at]*
|
||||
1. `ca`
|
||||
> print text=audit-insert-ok
|
||||
2. *
|
||||
> print text=audit-insert-fail
|
||||
|
||||
*upd = > json.parse text={"title":"Hello P3b"}*
|
||||
> `store`.update table="posts" id=1 row=`upd`
|
||||
*got2 = > `store`.get table="posts" id=1*
|
||||
*ua2 = got2[^updated_at]*
|
||||
1. `ua2`
|
||||
> print text=audit-update-ok
|
||||
2. *
|
||||
> print text=audit-update-fail
|
||||
|
||||
*fk = > `store`.query sql="SELECT sql FROM sqlite_master WHERE type='table' AND name='comments'"*
|
||||
*fkddl = fk[^rows][^1][^sql]*
|
||||
1. `fkddl`
|
||||
> print text=fk-ok
|
||||
2. *
|
||||
> print text=fk-fail
|
||||
|
||||
`users` =
|
||||
|
||||
| username | password | role |
|
||||
|----------|----------|------|
|
||||
| admin | secret | admin |
|
||||
| alice | secret | author |
|
||||
|
||||
*login = > web_auth_login username="alice" password="secret" users=`users` session_ttl=120*
|
||||
*role = login[^role]*
|
||||
1. `role` == "author"
|
||||
> print text=rbac-login-ok
|
||||
2. *
|
||||
> print text=rbac-login-fail
|
||||
|
||||
*pg = > web.page title="p3"*
|
||||
*app = > web.app page=`pg` port=18130*
|
||||
*app = > `app`.auth users=`users`*
|
||||
*app = > `app`.gate path="/write*" roles="admin,author"*
|
||||
*gates = app[^gates]*
|
||||
*ng = > len value=`gates`*
|
||||
1. `ng` >= 2
|
||||
> print text=rbac-gate-ok
|
||||
2. *
|
||||
> print text=rbac-gate-fail
|
||||
|
||||
*blob = > web.storage url="file:web-fixtures/data/p3-gallery"*
|
||||
> `blob`.put key="uploads/a.txt" body="hi" content_type="text/plain"
|
||||
*app = > `app`.gallery path="/gallery" storage=`blob` prefix="uploads/" title="Media" download_base="/_media"*
|
||||
*gal = app[^gallery_routes]*
|
||||
*g = gal[^/gallery]*
|
||||
1. `g`
|
||||
> print text=gallery-ok
|
||||
2. *
|
||||
> print text=gallery-fail
|
||||
+115
@@ -2113,6 +2113,121 @@ fn ext_web_w7_live() {
|
||||
let _ = child.wait();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ext_web_p3_smoke() {
|
||||
let status = Command::new("cargo")
|
||||
.args(["build", "-p", "marqdo_plugin_web"])
|
||||
.current_dir(env!("CARGO_MANIFEST_DIR"))
|
||||
.status()
|
||||
.expect("build web plugin");
|
||||
assert!(status.success(), "failed to build marqdo_plugin_web");
|
||||
assert_out(
|
||||
"tests/ext/web-p3-smoke.mq.md",
|
||||
"audit-insert-ok
|
||||
audit-update-ok
|
||||
fk-ok
|
||||
rbac-login-ok
|
||||
rbac-gate-ok
|
||||
gallery-ok",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ext_web_p3_live() {
|
||||
let status = Command::new("cargo")
|
||||
.args(["build", "-p", "marqdo_plugin_web"])
|
||||
.current_dir(env!("CARGO_MANIFEST_DIR"))
|
||||
.status()
|
||||
.expect("build web plugin");
|
||||
assert!(status.success(), "failed to build marqdo_plugin_web");
|
||||
|
||||
let script = "tests/ext/web-p3-live-server.mq.md";
|
||||
let bin = env!("CARGO_BIN_EXE_marqdo");
|
||||
let mut child = Command::new(bin)
|
||||
.args(["run", script])
|
||||
.current_dir(env!("CARGO_MANIFEST_DIR"))
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::piped())
|
||||
.spawn()
|
||||
.expect("spawn p3 live server");
|
||||
|
||||
let base = "http://127.0.0.1:18131";
|
||||
let ready = std::time::Instant::now();
|
||||
loop {
|
||||
if ready.elapsed().as_secs() > 10 {
|
||||
let _ = child.kill();
|
||||
panic!("p3 live server did not start in time");
|
||||
}
|
||||
let out = Command::new("curl")
|
||||
.args([
|
||||
"-s",
|
||||
"-o",
|
||||
"/dev/null",
|
||||
"-w",
|
||||
"%{http_code}",
|
||||
&format!("{base}/"),
|
||||
])
|
||||
.output()
|
||||
.expect("curl probe");
|
||||
if String::from_utf8_lossy(&out.stdout) == "200" {
|
||||
break;
|
||||
}
|
||||
std::thread::sleep(std::time::Duration::from_millis(300));
|
||||
}
|
||||
|
||||
let out = Command::new("curl")
|
||||
.args(["-s", &format!("{base}/gallery")])
|
||||
.output()
|
||||
.expect("curl gallery");
|
||||
let gal = String::from_utf8_lossy(&out.stdout);
|
||||
assert!(
|
||||
gal.contains("P3 Gallery") && gal.contains("hello.txt"),
|
||||
"gallery={gal}"
|
||||
);
|
||||
|
||||
let out = Command::new("curl")
|
||||
.args([
|
||||
"-s",
|
||||
"-D",
|
||||
"-",
|
||||
"-o",
|
||||
"/dev/null",
|
||||
&format!("{base}/_media/uploads/hello.txt"),
|
||||
])
|
||||
.output()
|
||||
.expect("curl media");
|
||||
let headers_raw = String::from_utf8_lossy(&out.stdout);
|
||||
let headers = headers_raw.to_ascii_lowercase();
|
||||
assert!(headers.contains("etag:"), "media headers={headers}");
|
||||
let etag = headers_raw
|
||||
.lines()
|
||||
.find(|l| l.to_ascii_lowercase().starts_with("etag:"))
|
||||
.map(|l| l.split_once(':').map(|(_, v)| v.trim().trim_matches('\r').to_string()).unwrap_or_default())
|
||||
.expect("etag line");
|
||||
|
||||
let out = Command::new("curl")
|
||||
.args([
|
||||
"-s",
|
||||
"-o",
|
||||
"/dev/null",
|
||||
"-w",
|
||||
"%{http_code}",
|
||||
"-H",
|
||||
&format!("If-None-Match: {etag}"),
|
||||
&format!("{base}/_media/uploads/hello.txt"),
|
||||
])
|
||||
.output()
|
||||
.expect("curl 304");
|
||||
assert_eq!(
|
||||
String::from_utf8_lossy(&out.stdout),
|
||||
"304",
|
||||
"expected 304 for If-None-Match"
|
||||
);
|
||||
|
||||
let _ = child.kill();
|
||||
let _ = child.wait();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ext_web_ws_broadcast_live() {
|
||||
use futures_util::{SinkExt, StreamExt};
|
||||
|
||||
Reference in new issue
Block a user