Files
clawmates/crates/tc-api/tests/claw_crud.rs
T
Omar SobhandClaude Fable 5 000b9b3a4b P4 core: broker-held app connections + gated, broker-executed Slack posting
- app_connections repo; POST /api/apps/connect (keys/basic): the credential
  goes to the secret broker over its socket and only the encrypted ref lands
  in the row; disconnect endpoint; /api/apps directory merged with live
  connection status; audit rows for connect/disconnect
- Broker protocol: InvokeHttp carries a JSON body
- slack.post tool (SendsExternally -> gated): marked broker_executed — the
  runtime skips its own grant consumption and the BROKER independently
  verifies + consumes the single-use grant, then calls Slack with the bot
  token injected; the runtime never sees the credential
- Config: [broker] socket_path + [slack] base_url; e2e harness spawns the
  real teamclaw-broker daemon and the server hosts an e2e-only /__slack sink
- SlackApp: Connection tab stores the token via the broker; connected state
- Integration test: blocked while pending -> approved -> sink received
  exactly one post with 'Bearer xoxb-test-token' -> grant replay refused
- E2E journey: connect Slack in the panel -> gated post card with preview ->
  sink empty while pending -> approve -> exactly one post, queue clear

133 Rust + 63 frontend tests + 21 Playwright journeys.

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-06-10 05:51:19 -05:00

252 lines
7.7 KiB
Rust

use serde_json::{json, Value};
use tc_api::AppState;
use tc_auth::AuthService;
use tc_domain::{Role, User, UserId, Workspace, WorkspaceId};
struct TestServer {
base: String,
client: reqwest::Client,
}
fn test_runtime(pool: sqlx::PgPool) -> tc_runtime::Runtime {
tc_runtime::Runtime::new(
pool,
std::sync::Arc::new(tc_llm::ScriptedProvider::from_toml("").unwrap()),
tc_runtime::RuntimeConfig::basic("scripted", 1024),
)
}
async fn serve(pool: sqlx::PgPool) -> TestServer {
let app = tc_api::router(AppState::new(pool.clone(), test_runtime(pool.clone())));
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
tokio::spawn(async move {
axum::serve(listener, app).await.unwrap();
});
TestServer {
base: format!("http://{addr}"),
client: reqwest::Client::new(),
}
}
async fn seed_user(pool: &sqlx::PgPool, ws: &Workspace, email: &str, role: Role) -> User {
let user = User {
id: UserId::new(),
workspace_id: ws.id,
email: email.into(),
role,
display_name: email.split('@').next().unwrap().into(),
created_at: time::OffsetDateTime::UNIX_EPOCH,
};
tc_db::repo::users::insert(pool, &user).await.unwrap();
AuthService::new(pool.clone())
.set_password(user.id, "pw")
.await
.unwrap();
user
}
async fn seed_workspace(pool: &sqlx::PgPool) -> Workspace {
let ws = Workspace {
id: WorkspaceId::new(),
name: "Acme".into(),
plan: "team".into(),
};
tc_db::repo::workspaces::insert(pool, &ws).await.unwrap();
ws
}
async fn login(server: &TestServer, email: &str) -> String {
let res = server
.client
.post(format!("{}/api/auth/login", server.base))
.json(&json!({"email": email, "password": "pw"}))
.send()
.await
.unwrap();
assert_eq!(res.status(), 200);
res.json::<Value>().await.unwrap()["token"]
.as_str()
.unwrap()
.to_owned()
}
async fn create_claw(server: &TestServer, token: &str, name: &str) -> Value {
let res = server
.client
.post(format!("{}/api/claws", server.base))
.bearer_auth(token)
.json(&json!({"name": name, "job_title": "Research Analyst"}))
.send()
.await
.unwrap();
assert_eq!(res.status(), 201);
res.json().await.unwrap()
}
#[tokio::test]
async fn create_returns_live_agent_with_default_policy_and_audit() {
let pool = tc_testkit::test_pool().await;
let ws = seed_workspace(&pool).await;
let owner = seed_user(&pool, &ws, "[email protected]", Role::Owner).await;
let server = serve(pool.clone()).await;
let token = login(&server, "[email protected]").await;
let claw = create_claw(&server, &token, "Scout").await;
assert_eq!(claw["name"], "Scout");
assert_eq!(claw["status"], "online");
assert_eq!(claw["managed_by"], owner.id.to_string());
let settings: Value = server
.client
.get(format!(
"{}/api/claws/settings/full?clawId={}",
server.base,
claw["id"].as_str().unwrap()
))
.bearer_auth(&token)
.send()
.await
.unwrap()
.json()
.await
.unwrap();
assert_eq!(settings["agent"]["name"], "Scout");
assert_eq!(settings["access_policy"]["humans"]["mode"], "entire_team");
assert_eq!(settings["access_policy"]["agents"]["mode"], "any");
let audited = sqlx::query_scalar::<_, i64>(
"SELECT count(*) FROM audit_log WHERE event_type = 'agent.created'",
)
.fetch_one(&pool)
.await
.unwrap();
assert_eq!(audited, 1);
}
#[tokio::test]
async fn patch_updates_profile_and_system_prompt() {
let pool = tc_testkit::test_pool().await;
let ws = seed_workspace(&pool).await;
seed_user(&pool, &ws, "[email protected]", Role::Owner).await;
let server = serve(pool).await;
let token = login(&server, "[email protected]").await;
let claw = create_claw(&server, &token, "Scout").await;
let id = claw["id"].as_str().unwrap();
let res = server
.client
.patch(format!("{}/api/claws/{id}", server.base))
.bearer_auth(&token)
.json(&json!({
"job_title": "Senior Analyst",
"system_prompt": "Think in bullet points."
}))
.send()
.await
.unwrap();
assert_eq!(res.status(), 200);
let updated: Value = res.json().await.unwrap();
assert_eq!(updated["job_title"], "Senior Analyst");
assert_eq!(updated["system_prompt"], "Think in bullet points.");
// Untouched fields persist.
assert_eq!(updated["name"], "Scout");
}
#[tokio::test]
async fn delete_requires_owner_or_manager() {
let pool = tc_testkit::test_pool().await;
let ws = seed_workspace(&pool).await;
seed_user(&pool, &ws, "[email protected]", Role::Owner).await;
seed_user(&pool, &ws, "[email protected]", Role::Member).await;
let server = serve(pool).await;
let owner_token = login(&server, "[email protected]").await;
let member_token = login(&server, "[email protected]").await;
let claw = create_claw(&server, &owner_token, "Scout").await;
let id = claw["id"].as_str().unwrap();
// A plain member who doesn't manage the claw cannot delete it.
let forbidden = server
.client
.delete(format!("{}/api/claws/{id}", server.base))
.bearer_auth(&member_token)
.send()
.await
.unwrap();
assert_eq!(forbidden.status(), 403);
let deleted = server
.client
.delete(format!("{}/api/claws/{id}", server.base))
.bearer_auth(&owner_token)
.send()
.await
.unwrap();
assert_eq!(deleted.status(), 204);
// Gone from the roster.
let claws: Value = server
.client
.get(format!("{}/api/team/claws", server.base))
.bearer_auth(&owner_token)
.send()
.await
.unwrap()
.json()
.await
.unwrap();
assert_eq!(claws.as_array().unwrap().len(), 0);
}
#[tokio::test]
async fn members_can_create_and_manage_their_own_claws() {
let pool = tc_testkit::test_pool().await;
let ws = seed_workspace(&pool).await;
seed_user(&pool, &ws, "[email protected]", Role::Member).await;
let server = serve(pool).await;
let token = login(&server, "[email protected]").await;
// Builders (plain members) can create claws (§1 personas)...
let claw = create_claw(&server, &token, "Drafter").await;
let id = claw["id"].as_str().unwrap();
// ...and delete the ones they manage.
let deleted = server
.client
.delete(format!("{}/api/claws/{id}", server.base))
.bearer_auth(&token)
.send()
.await
.unwrap();
assert_eq!(deleted.status(), 204);
}
#[tokio::test]
async fn cross_workspace_access_is_not_found() {
let pool = tc_testkit::test_pool().await;
let ws_a = seed_workspace(&pool).await;
let ws_b = seed_workspace(&pool).await;
seed_user(&pool, &ws_a, "[email protected]", Role::Owner).await;
seed_user(&pool, &ws_b, "[email protected]", Role::Owner).await;
let server = serve(pool).await;
let token_a = login(&server, "[email protected]").await;
let token_b = login(&server, "[email protected]").await;
let claw = create_claw(&server, &token_a, "Scout").await;
let id = claw["id"].as_str().unwrap();
// Tenant isolation: the other workspace can't even see it exists.
let other = server
.client
.get(format!(
"{}/api/claws/settings/full?clawId={id}",
server.base
))
.bearer_auth(&token_b)
.send()
.await
.unwrap();
assert_eq!(other.status(), 404);
}