diff --git a/swarm-controller/src/forge/docs_grant.rs b/swarm-controller/src/forge/docs_grant.rs index 63bfa3d91..3e79b0812 100644 --- a/swarm-controller/src/forge/docs_grant.rs +++ b/swarm-controller/src/forge/docs_grant.rs @@ -260,6 +260,15 @@ mod tests { "/api/v1/repos/internal/docs/collaborators/{user}/permission", axum::routing::get(permission), ) + // An agent's own config repo is served by the same stand-in. + .route( + "/api/v1/repos/agent-configs/alice/collaborators/{user}", + axum::routing::get(check).put(add), + ) + .route( + "/api/v1/repos/agent-configs/alice/collaborators/{user}/permission", + axum::routing::get(permission), + ) .with_state(Arc::clone(&docs)); let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); let url = url::Url::parse(&format!("http://{}/", listener.local_addr().unwrap())).unwrap(); @@ -439,4 +448,37 @@ mod tests { assert!(puts(&docs).is_empty(), "{:?}", puts(&docs)); assert_eq!(docs.lock().unwrap().collaborators["alice"], "write"); } + + /// An agent's grant on its own config repo is **read**: the `AddRepoMember` + /// node's work, not write. + #[tokio::test] + async fn the_config_repo_grant_adds_the_agent_as_a_read_collaborator() { + let (client, docs) = stub_forge(&[], false).await; + + client.add_repo_member("alice", "alice").await.unwrap(); + + let sent = puts(&docs); + assert_eq!(sent.len(), 1, "{sent:?}"); + let (user, body) = sent[0].split_once(' ').unwrap(); + assert_eq!(user, "alice"); + let body: serde_json::Value = serde_json::from_str(body).unwrap(); + assert_eq!(body["permission"], "read"); + } + + /// A write grant an operator made on the agent's config repo survives the + /// node running again: the node reads the permission first and sends + /// nothing. This is the condition the change was approved on. + #[tokio::test] + async fn the_config_repo_grant_leaves_a_write_an_operator_made_alone() { + let (client, docs) = serve(Docs { + collaborators: BTreeMap::from([("alice".to_owned(), "write".to_owned())]), + ..Docs::default() + }) + .await; + + client.add_repo_member("alice", "alice").await.unwrap(); + + assert!(puts(&docs).is_empty(), "{:?}", puts(&docs)); + assert_eq!(docs.lock().unwrap().collaborators["alice"], "write"); + } }