feat: rename orbits; snapshots and downloads show the orbit's current name

PUT /api/v1/orbits/:id changes only the name (admin, validated, case-insensitively unique, audited).
Snapshot metadata is immutable, so the list and the download file name look the orbit up by id and
fall back to the written name only when the orbit is gone.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-15 15:01:49 +07:00
parent c6dd52c700
commit f52fc1930f
7 changed files with 240 additions and 4 deletions

View File

@@ -1846,3 +1846,144 @@ async fn a_target_name_is_validated_like_a_registration_and_only_administrators_
StatusCode::NO_CONTENT
);
}
// ---- renaming orbits ----
impl Lab {
async fn download_disposition(&self, snapshot: Uuid) -> String {
let request = Request::builder()
.method("GET")
.uri(format!("/api/v1/snapshots/{snapshot}/download"))
.header("x-tessera-admin-token", ADMIN)
.extension(ConnectInfo("127.0.0.1:4000".parse::<SocketAddr>().unwrap()))
.body(Body::empty())
.unwrap();
let response = self.app.clone().oneshot(request).await.unwrap();
assert_eq!(response.status(), StatusCode::OK);
response.headers()[header::CONTENT_DISPOSITION]
.to_str()
.unwrap()
.to_string()
}
}
#[tokio::test]
async fn renaming_an_orbit_renames_its_old_snapshots_everywhere_they_are_shown() {
let lab = fresh().await;
let admin = lab.admin();
let (qa, _) = lab.refresher("qa");
lab.start(&admin, "orbit-rename").await;
let snapshot = lab
.capture(&Lab::production_identity(), false)
.await
.unwrap();
assert!(lab
.download_disposition(snapshot)
.await
.contains("filename=\"prod-db_"));
let uri = format!("/api/v1/orbits/{}", lab.orbit);
assert_eq!(
lab.send(&admin, "PUT", &uri, Some(json!({"name": " broiler "})))
.await
.0,
StatusCode::NO_CONTENT
);
// the snapshot was written under the old name; it is shown under the current one
let (_, listed) = lab.send(&admin, "GET", "/api/v1/snapshots", None).await;
assert_eq!(listed[0]["orbit_name"], "broiler", "{listed}");
assert!(lab
.download_disposition(snapshot)
.await
.contains("filename=\"broiler_"));
let (_, overview) = lab.send(&admin, "GET", "/api/v1/relations", None).await;
assert!(overview["orbits"]
.as_array()
.unwrap()
.iter()
.any(|o| o["name"] == "broiler"));
let (_, mine) = lab
.send(&qa, "GET", "/api/v1/refresh-relations", None)
.await;
assert_eq!(mine[0]["source_name"], "broiler");
// nothing but the name moved: the relation, its confirmation text and the id are the same relation
assert_eq!(overview["relations"][0]["id"], lab.relation);
assert!(
lab.audit_log()
.iter()
.any(|(event, detail)| event == "orbit.renamed"
&& detail.contains(&lab.orbit.to_string()))
);
}
#[tokio::test]
async fn an_orbit_name_is_validated_unique_and_only_administrators_change_it() {
let lab = fresh().await;
let admin = lab.admin();
let uri = format!("/api/v1/orbits/{}", lab.orbit);
for name in ["", " ", &"x".repeat(129), "bad\nname"] {
assert_eq!(
lab.send(&admin, "PUT", &uri, Some(json!({"name": name})))
.await
.0,
StatusCode::BAD_REQUEST,
"{name:?}"
);
}
assert_eq!(
lab.send(
&admin,
"PUT",
&uri,
Some(json!({"name": "ok", "provider": "x"}))
)
.await
.0,
StatusCode::UNPROCESSABLE_ENTITY
);
let unknown = format!("/api/v1/orbits/{}", Uuid::new_v4());
assert_eq!(
lab.send(&admin, "PUT", &unknown, Some(json!({"name": "ok"})))
.await
.0,
StatusCode::NOT_FOUND
);
// another orbit already has that name (compared without regard to case); keeping your own name is fine
lab.state
.db
.upsert_orbit(&crate::db::OrbitRow {
id: Uuid::new_v4(),
name: "Other".into(),
provider: "wormhole".into(),
provider_config: "{}".into(),
schedule: None,
decay_preset: "week-of-dailies".into(),
retention: None,
dry_run: false,
last_status: "idle".into(),
})
.await
.unwrap();
assert_eq!(
lab.send(&admin, "PUT", &uri, Some(json!({"name": "other"})))
.await
.0,
StatusCode::CONFLICT
);
assert_eq!(
lab.send(&admin, "PUT", &uri, Some(json!({"name": "prod-db"})))
.await
.0,
StatusCode::NO_CONTENT
);
let (qa, _) = lab.refresher("qa");
let status = lab
.send(&qa, "PUT", &uri, Some(json!({"name": "mine"})))
.await
.0;
assert!(
status == StatusCode::FORBIDDEN || status == StatusCode::NOT_FOUND,
"{status}"
);
}