Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3032f34269 | ||
|
|
c1234973be |
5 changed files with 1068 additions and 0 deletions
|
|
@ -2819,6 +2819,83 @@ mod tests {
|
||||||
(Client { api, url }, seen)
|
(Client { api, url }, seen)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A stand-in forge on a loopback port answering each request by
|
||||||
|
/// `route(method, path, body)` with a status and JSON body, and
|
||||||
|
/// recording every request like [`stub_forge`]. For the tests that need
|
||||||
|
/// a different answer per path.
|
||||||
|
pub(super) async fn routed_forge(
|
||||||
|
route: impl Fn(&str, &str, &serde_json::Value) -> (u16, String) + Send + Sync + 'static,
|
||||||
|
) -> (Client, Seen) {
|
||||||
|
use axum::http::{Method, StatusCode, Uri, header::CONTENT_TYPE};
|
||||||
|
|
||||||
|
let seen = Seen::default();
|
||||||
|
let recorded = seen.clone();
|
||||||
|
let route = std::sync::Arc::new(route);
|
||||||
|
let app = axum::Router::new().fallback(
|
||||||
|
move |method: Method, uri: Uri, body: axum::body::Bytes| {
|
||||||
|
let (recorded, route) = (recorded.clone(), std::sync::Arc::clone(&route));
|
||||||
|
async move {
|
||||||
|
let json = serde_json::from_slice(&body).unwrap_or_default();
|
||||||
|
let (status, answer) = route(method.as_str(), uri.path(), &json);
|
||||||
|
recorded.lock().unwrap().push((
|
||||||
|
method.to_string(),
|
||||||
|
uri.path().to_owned(),
|
||||||
|
json,
|
||||||
|
));
|
||||||
|
let status = StatusCode::from_u16(status).unwrap();
|
||||||
|
// A list answer carries its length, which the forge client
|
||||||
|
// reads to know it has seen every page.
|
||||||
|
let total = serde_json::from_str::<Vec<serde_json::Value>>(&answer)
|
||||||
|
.map_or(0, |list| list.len());
|
||||||
|
(
|
||||||
|
status,
|
||||||
|
[
|
||||||
|
(CONTENT_TYPE, "application/json".to_owned()),
|
||||||
|
(
|
||||||
|
axum::http::HeaderName::from_static("x-total-count"),
|
||||||
|
total.to_string(),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
answer,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
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();
|
||||||
|
tokio::spawn(async move { axum::serve(listener, app).await });
|
||||||
|
let api = crate::forge::api::tests::fixed(url.clone(), "stub-token");
|
||||||
|
(Client { api, url }, seen)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The requests in `seen` that are not reads, as `METHOD path`.
|
||||||
|
pub(super) fn writes(seen: &Seen) -> Vec<String> {
|
||||||
|
seen.lock()
|
||||||
|
.unwrap()
|
||||||
|
.iter()
|
||||||
|
.filter(|(method, _, _)| method != "GET")
|
||||||
|
.map(|(method, path, _)| format!("{method} {path}"))
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The body of the first `method path` request in `seen`, with unset
|
||||||
|
/// (`null`) fields dropped: what the forge acts on.
|
||||||
|
pub(super) fn sent(seen: &Seen, method: &str, path: &str) -> Option<serde_json::Value> {
|
||||||
|
let body = seen
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.iter()
|
||||||
|
.find(|(m, p, _)| m == method && p == path)
|
||||||
|
.map(|(_, _, body)| body.clone())?;
|
||||||
|
Some(serde_json::Value::Object(
|
||||||
|
body.as_object()?
|
||||||
|
.iter()
|
||||||
|
.filter(|(_, v)| !v.is_null())
|
||||||
|
.map(|(k, v)| (k.clone(), v.clone()))
|
||||||
|
.collect(),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
/// The `PATCH` [`Client::ensure_agent_user`] sent, if any.
|
/// The `PATCH` [`Client::ensure_agent_user`] sent, if any.
|
||||||
pub(super) fn lockdown_patch(seen: &Seen, agent: &str) -> Option<serde_json::Value> {
|
pub(super) fn lockdown_patch(seen: &Seen, agent: &str) -> Option<serde_json::Value> {
|
||||||
let path = format!("/api/v1/admin/users/{agent}");
|
let path = format!("/api/v1/admin/users/{agent}");
|
||||||
|
|
|
||||||
|
|
@ -309,4 +309,49 @@ mod tests {
|
||||||
assert_eq!(body["max_repo_creation"], 0);
|
assert_eq!(body["max_repo_creation"], 0);
|
||||||
assert_eq!(body["allow_create_organization"], false);
|
assert_eq!(body["allow_create_organization"], false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The account is created, its email checked, and it is locked out of
|
||||||
|
/// creating repos and organizations, in that order.
|
||||||
|
#[tokio::test]
|
||||||
|
async fn the_account_is_created_and_locked_out_of_creating_repos() {
|
||||||
|
use super::super::tests::{lockdown_patch, stub_forge, writes};
|
||||||
|
|
||||||
|
let (client, seen) = stub_forge(
|
||||||
|
(201, "{}"),
|
||||||
|
(200, r#"{"email":"builder@services.hyperhive.local"}"#),
|
||||||
|
(200, "{}"),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
client.ensure_builder_user().await.unwrap();
|
||||||
|
let lock = lockdown_patch(&seen, LOGIN).expect("lockdown sent");
|
||||||
|
assert_eq!(lock["max_repo_creation"], 0);
|
||||||
|
assert_eq!(lock["allow_create_organization"], false);
|
||||||
|
assert_eq!(lock["login_name"], LOGIN);
|
||||||
|
assert_eq!(
|
||||||
|
writes(&seen),
|
||||||
|
[
|
||||||
|
"POST /api/v1/admin/users".to_owned(),
|
||||||
|
format!("PATCH /api/v1/admin/users/{LOGIN}")
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn a_refused_create_or_lockdown_fails_the_job() {
|
||||||
|
use super::super::tests::stub_forge;
|
||||||
|
|
||||||
|
let email = (200, r#"{"email":"builder@services.hyperhive.local"}"#);
|
||||||
|
let (client, seen) = stub_forge((500, r#"{"message":"x"}"#), email, (200, "{}")).await;
|
||||||
|
client
|
||||||
|
.ensure_builder_user()
|
||||||
|
.await
|
||||||
|
.expect_err("create refused");
|
||||||
|
assert_eq!(seen.lock().unwrap().len(), 1, "nothing is sent after it");
|
||||||
|
|
||||||
|
let (client, _) = stub_forge((201, "{}"), email, (500, r#"{"message":"x"}"#)).await;
|
||||||
|
client
|
||||||
|
.ensure_builder_user()
|
||||||
|
.await
|
||||||
|
.expect_err("a lockdown that failed is not a built account");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -494,4 +494,273 @@ mod tests {
|
||||||
assert_eq!(body["max_repo_creation"], 0);
|
assert_eq!(body["max_repo_creation"], 0);
|
||||||
assert_eq!(body["allow_create_organization"], false);
|
assert_eq!(body["allow_create_organization"], false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The forge side, against a stand-in forge that answers by path.
|
||||||
|
use super::super::tests::{Seen as Requests, routed_forge, sent, writes};
|
||||||
|
use serde_json::{Value, json};
|
||||||
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||||
|
|
||||||
|
fn team_json(id: i64, permission: &str, units: &[&str]) -> Value {
|
||||||
|
json!({
|
||||||
|
"id": id,
|
||||||
|
"name": TEAM,
|
||||||
|
"description": TEAM_DESCRIPTION,
|
||||||
|
"permission": permission,
|
||||||
|
"includes_all_repositories": true,
|
||||||
|
"can_create_org_repo": false,
|
||||||
|
"units": units,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A forge listing `orgs` and giving each the teams in `teams`, every
|
||||||
|
/// request not named by `rest` a 201 for a POST and 200 otherwise.
|
||||||
|
async fn forge_of(
|
||||||
|
orgs: &[&str],
|
||||||
|
teams: Vec<(&'static str, u16, Value)>,
|
||||||
|
rest: Vec<(String, u16, Value)>,
|
||||||
|
) -> (Client, Requests) {
|
||||||
|
let orgs: Vec<Value> = orgs.iter().map(|o| json!({"username": o})).collect();
|
||||||
|
let teams: Vec<(String, u16, Value)> = teams
|
||||||
|
.into_iter()
|
||||||
|
.map(|(k, s, b)| (k.to_owned(), s, b))
|
||||||
|
.collect();
|
||||||
|
routed_forge(move |method, path, _| {
|
||||||
|
let key = format!("{method} {path}");
|
||||||
|
if key == "GET /api/v1/admin/orgs" {
|
||||||
|
return (200, json!(orgs).to_string());
|
||||||
|
}
|
||||||
|
if let Some((_, s, b)) = teams.iter().chain(rest.iter()).find(|(k, _, _)| *k == key) {
|
||||||
|
return (*s, b.to_string());
|
||||||
|
}
|
||||||
|
match method {
|
||||||
|
"GET" => (404, r#"{"message":"no"}"#.to_owned()),
|
||||||
|
"POST" => (201, "{}".to_owned()),
|
||||||
|
_ => (200, "{}".to_owned()),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn the_account_is_created_and_locked_out_of_creating_repos() {
|
||||||
|
let (client, seen) = forge_of(
|
||||||
|
&[],
|
||||||
|
vec![],
|
||||||
|
vec![at(
|
||||||
|
"GET /api/v1/users/ci-metrics",
|
||||||
|
200,
|
||||||
|
json!({"email": EMAIL}),
|
||||||
|
)],
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
client.ensure_ci_metrics_user().await.unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
writes(&seen),
|
||||||
|
[
|
||||||
|
"POST /api/v1/admin/users".to_owned(),
|
||||||
|
"PATCH /api/v1/admin/users/ci-metrics".to_owned()
|
||||||
|
]
|
||||||
|
);
|
||||||
|
let lock = sent(&seen, "PATCH", "/api/v1/admin/users/ci-metrics").unwrap();
|
||||||
|
assert_eq!(lock["max_repo_creation"], 0);
|
||||||
|
assert_eq!(lock["allow_create_organization"], false);
|
||||||
|
assert_eq!(lock["login_name"], LOGIN);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn a_refused_create_stops_before_the_account_is_read_or_locked() {
|
||||||
|
let (client, seen) = forge_of(
|
||||||
|
&[],
|
||||||
|
vec![],
|
||||||
|
vec![
|
||||||
|
at("POST /api/v1/admin/users", 500, json!({"message": "x"})),
|
||||||
|
at("GET /api/v1/users/ci-metrics", 200, json!({"email": EMAIL})),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
client.ensure_ci_metrics_user().await.expect_err("refused");
|
||||||
|
assert_eq!(writes(&seen), ["POST /api/v1/admin/users"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn a_team_that_matches_is_left_and_one_with_the_wrong_grant_is_put_back() {
|
||||||
|
let path = "GET /api/v1/orgs/o/teams";
|
||||||
|
let (client, seen) = forge_of(
|
||||||
|
&[],
|
||||||
|
vec![(path, 200, json!([team_json(3, "read", &TEAM_UNITS)]))],
|
||||||
|
vec![],
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
assert_eq!(client.ensure_ci_metrics_team("o").await.unwrap(), 3);
|
||||||
|
assert!(writes(&seen).is_empty());
|
||||||
|
|
||||||
|
let wide = team_json(3, "write", &["repo.actions", "repo.code"]);
|
||||||
|
let (client, seen) = forge_of(&[], vec![(path, 200, json!([wide]))], vec![]).await;
|
||||||
|
assert_eq!(client.ensure_ci_metrics_team("o").await.unwrap(), 3);
|
||||||
|
assert_eq!(writes(&seen), ["PATCH /api/v1/teams/3"]);
|
||||||
|
let edit = sent(&seen, "PATCH", "/api/v1/teams/3").unwrap();
|
||||||
|
assert_eq!(edit["permission"], "read");
|
||||||
|
assert_eq!(edit["units"], json!(TEAM_UNITS));
|
||||||
|
assert_eq!(edit["includes_all_repositories"], true);
|
||||||
|
assert_eq!(edit["can_create_org_repo"], false);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn a_missing_team_is_created_read_only_on_actions_and_a_missing_id_fails() {
|
||||||
|
let list = ("GET /api/v1/orgs/o/teams", 200, json!([]));
|
||||||
|
let (client, seen) = forge_of(
|
||||||
|
&[],
|
||||||
|
vec![list.clone()],
|
||||||
|
vec![at("POST /api/v1/orgs/o/teams", 201, json!({"id": 11}))],
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
assert_eq!(client.ensure_ci_metrics_team("o").await.unwrap(), 11);
|
||||||
|
let team = sent(&seen, "POST", "/api/v1/orgs/o/teams").unwrap();
|
||||||
|
assert_eq!(team["name"], TEAM);
|
||||||
|
assert_eq!(team["permission"], "read");
|
||||||
|
assert_eq!(team["units"], json!(TEAM_UNITS));
|
||||||
|
assert_eq!(team["includes_all_repositories"], true);
|
||||||
|
assert_eq!(team["can_create_org_repo"], false);
|
||||||
|
|
||||||
|
let (client, _) = forge_of(&[], vec![list], vec![]).await;
|
||||||
|
client
|
||||||
|
.ensure_ci_metrics_team("o")
|
||||||
|
.await
|
||||||
|
.expect_err("created, but no id came back");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn an_unreadable_team_list_is_an_error_and_never_a_create() {
|
||||||
|
let (client, seen) = forge_of(
|
||||||
|
&[],
|
||||||
|
vec![("GET /api/v1/orgs/o/teams", 500, json!({"message": "x"}))],
|
||||||
|
vec![],
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
client
|
||||||
|
.ensure_ci_metrics_team("o")
|
||||||
|
.await
|
||||||
|
.expect_err("unknown");
|
||||||
|
assert!(writes(&seen).is_empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn a_duplicate_create_is_found_by_a_re_read_and_a_refusal_with_no_team_fails() {
|
||||||
|
let reads = Arc::new(AtomicUsize::new(0));
|
||||||
|
let counted = Arc::clone(&reads);
|
||||||
|
let (client, _) = routed_forge(move |method, path, _| match (method, path) {
|
||||||
|
("GET", "/api/v1/orgs/o/teams") if counted.fetch_add(1, Ordering::SeqCst) == 0 => {
|
||||||
|
(200, "[]".to_owned())
|
||||||
|
}
|
||||||
|
("GET", "/api/v1/orgs/o/teams") => {
|
||||||
|
(200, json!([team_json(4, "read", &TEAM_UNITS)]).to_string())
|
||||||
|
}
|
||||||
|
("POST", _) => (409, r#"{"message":"exists"}"#.to_owned()),
|
||||||
|
_ => (200, "{}".to_owned()),
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
assert_eq!(client.ensure_ci_metrics_team("o").await.unwrap(), 4);
|
||||||
|
|
||||||
|
let (client, _) = forge_of(
|
||||||
|
&[],
|
||||||
|
vec![("GET /api/v1/orgs/o/teams", 200, json!([]))],
|
||||||
|
vec![at(
|
||||||
|
"POST /api/v1/orgs/o/teams",
|
||||||
|
409,
|
||||||
|
json!({"message": "x"}),
|
||||||
|
)],
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
client
|
||||||
|
.ensure_ci_metrics_team("o")
|
||||||
|
.await
|
||||||
|
.expect_err("not found again");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn every_page_of_organisations_is_read() {
|
||||||
|
let asked = Arc::new(AtomicUsize::new(0));
|
||||||
|
let counted = Arc::clone(&asked);
|
||||||
|
let (client, _) = routed_forge(move |_, path, _| {
|
||||||
|
assert_eq!(path, "/api/v1/admin/orgs");
|
||||||
|
let page = counted.fetch_add(1, Ordering::SeqCst);
|
||||||
|
let n = if page == 0 { ORG_PAGE as usize } else { 1 };
|
||||||
|
let orgs: Vec<Value> = (0..n)
|
||||||
|
.map(|i| json!({"username": format!("org-{page}-{i}")}))
|
||||||
|
.collect();
|
||||||
|
(200, json!(orgs).to_string())
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
let names = client.all_orgs().await.unwrap();
|
||||||
|
assert_eq!(names.len(), ORG_PAGE as usize + 1);
|
||||||
|
assert_eq!(asked.load(Ordering::SeqCst), 2);
|
||||||
|
assert_eq!(names.last().unwrap(), "org-1-0");
|
||||||
|
}
|
||||||
|
|
||||||
|
fn at(key: &str, status: u16, body: Value) -> (String, u16, Value) {
|
||||||
|
(key.to_owned(), status, body)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn member(id: i64) -> (String, u16, Value) {
|
||||||
|
at(
|
||||||
|
&format!("GET /api/v1/teams/{id}/members/ci-metrics"),
|
||||||
|
200,
|
||||||
|
json!({}),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn the_grants_are_complete_only_when_every_org_has_the_team_with_the_account_in_it() {
|
||||||
|
let teams = |ok: bool| {
|
||||||
|
vec![
|
||||||
|
(
|
||||||
|
"GET /api/v1/orgs/a/teams",
|
||||||
|
200,
|
||||||
|
json!([team_json(1, "read", &TEAM_UNITS)]),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"GET /api/v1/orgs/b/teams",
|
||||||
|
200,
|
||||||
|
if ok {
|
||||||
|
json!([team_json(2, "read", &TEAM_UNITS)])
|
||||||
|
} else {
|
||||||
|
json!([])
|
||||||
|
},
|
||||||
|
),
|
||||||
|
]
|
||||||
|
};
|
||||||
|
let members = vec![member(1), member(2)];
|
||||||
|
let (client, _) = forge_of(&["a", "b"], teams(true), members.clone()).await;
|
||||||
|
assert_eq!(client.observe_ci_metrics_grants().await, Some(true));
|
||||||
|
|
||||||
|
// A team missing in one org.
|
||||||
|
let (client, _) = forge_of(&["a", "b"], teams(false), members.clone()).await;
|
||||||
|
assert_eq!(client.observe_ci_metrics_grants().await, Some(false));
|
||||||
|
|
||||||
|
// The account missing from a team (404), against a failed read (500).
|
||||||
|
let (client, _) = forge_of(&["a", "b"], teams(true), vec![member(1)]).await;
|
||||||
|
assert_eq!(client.observe_ci_metrics_grants().await, Some(false));
|
||||||
|
let (client, _) = forge_of(
|
||||||
|
&["a", "b"],
|
||||||
|
teams(true),
|
||||||
|
vec![
|
||||||
|
member(1),
|
||||||
|
at(
|
||||||
|
"GET /api/v1/teams/2/members/ci-metrics",
|
||||||
|
500,
|
||||||
|
json!({"message": "x"}),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
assert_eq!(client.observe_ci_metrics_grants().await, None);
|
||||||
|
|
||||||
|
// An unreadable team list, and an unreadable org list.
|
||||||
|
let mut unreadable = teams(true);
|
||||||
|
unreadable[1] = ("GET /api/v1/orgs/b/teams", 500, json!({"message": "x"}));
|
||||||
|
let (client, _) = forge_of(&["a", "b"], unreadable, members).await;
|
||||||
|
assert_eq!(client.observe_ci_metrics_grants().await, None);
|
||||||
|
let (client, _) = routed_forge(|_, _, _| (500, r#"{"message":"x"}"#.to_owned())).await;
|
||||||
|
assert_eq!(client.observe_ci_metrics_grants().await, None);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -695,4 +695,179 @@ mod tests {
|
||||||
some_repos.includes_all_repositories = Some(false);
|
some_repos.includes_all_repositories = Some(false);
|
||||||
assert!(!hives_team_matches(&some_repos));
|
assert!(!hives_team_matches(&some_repos));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The forge side, against a stand-in forge that answers by path.
|
||||||
|
use super::super::tests::{routed_forge, sent, writes};
|
||||||
|
use serde_json::{Value, json};
|
||||||
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||||
|
|
||||||
|
const TEAMS: &str = "/api/v1/orgs/agent-configs/teams";
|
||||||
|
|
||||||
|
fn hives_team_json(permission: &str, units: &[&str]) -> Value {
|
||||||
|
json!({
|
||||||
|
"id": 9,
|
||||||
|
"name": HIVES_TEAM,
|
||||||
|
"description": HIVES_TEAM_DESCRIPTION,
|
||||||
|
"permission": permission,
|
||||||
|
"includes_all_repositories": true,
|
||||||
|
"can_create_org_repo": false,
|
||||||
|
"units": units,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A forge answering the team list with `teams` and every other request
|
||||||
|
/// by `rest`, defaulting to 201 for a POST and 200 otherwise.
|
||||||
|
async fn teams_forge(
|
||||||
|
teams: (u16, Value),
|
||||||
|
rest: Vec<(&'static str, u16, Value)>,
|
||||||
|
) -> (Client, super::super::tests::Seen) {
|
||||||
|
routed_forge(move |method, path, _| {
|
||||||
|
let key = format!("{method} {path}");
|
||||||
|
if key == format!("GET {TEAMS}") {
|
||||||
|
return (teams.0, teams.1.to_string());
|
||||||
|
}
|
||||||
|
if let Some((_, status, body)) = rest.iter().find(|(k, _, _)| *k == key) {
|
||||||
|
return (*status, body.to_string());
|
||||||
|
}
|
||||||
|
(if method == "POST" { 201 } else { 200 }, "{}".to_owned())
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn a_hives_team_that_matches_is_used_as_it_is() {
|
||||||
|
let ok = hives_team_json("read", &HIVES_TEAM_UNITS);
|
||||||
|
let (client, seen) = teams_forge((200, json!([ok])), vec![]).await;
|
||||||
|
assert_eq!(client.ensure_hives_team().await.unwrap(), 9);
|
||||||
|
assert!(writes(&seen).is_empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn a_hives_team_with_a_wider_grant_is_put_back_to_read_on_code_and_pulls() {
|
||||||
|
let wide = hives_team_json("write", &["repo.code", "repo.pulls", "repo.issues"]);
|
||||||
|
let (client, seen) = teams_forge((200, json!([wide])), vec![]).await;
|
||||||
|
assert_eq!(client.ensure_hives_team().await.unwrap(), 9);
|
||||||
|
assert_eq!(writes(&seen), ["PATCH /api/v1/teams/9"]);
|
||||||
|
let edit = sent(&seen, "PATCH", "/api/v1/teams/9").unwrap();
|
||||||
|
assert_eq!(edit["permission"], "read");
|
||||||
|
assert_eq!(edit["units"], json!(HIVES_TEAM_UNITS));
|
||||||
|
assert_eq!(edit["includes_all_repositories"], true);
|
||||||
|
assert_eq!(edit["can_create_org_repo"], false);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn a_missing_hives_team_is_created_read_only_and_its_id_returned() {
|
||||||
|
let (client, seen) = teams_forge(
|
||||||
|
(200, json!([])),
|
||||||
|
vec![(
|
||||||
|
"POST /api/v1/orgs/agent-configs/teams",
|
||||||
|
201,
|
||||||
|
json!({"id": 5}),
|
||||||
|
)],
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
assert_eq!(client.ensure_hives_team().await.unwrap(), 5);
|
||||||
|
let team = sent(&seen, "POST", TEAMS).unwrap();
|
||||||
|
assert_eq!(team["name"], HIVES_TEAM);
|
||||||
|
assert_eq!(team["permission"], "read");
|
||||||
|
assert_eq!(team["units"], json!(HIVES_TEAM_UNITS));
|
||||||
|
assert_eq!(team["includes_all_repositories"], true);
|
||||||
|
assert_eq!(team["can_create_org_repo"], false);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn a_created_team_with_no_id_and_an_unreadable_team_list_both_fail() {
|
||||||
|
let (client, _) = teams_forge((200, json!([])), vec![]).await;
|
||||||
|
client
|
||||||
|
.ensure_hives_team()
|
||||||
|
.await
|
||||||
|
.expect_err("a team the forge gave no id for is no team");
|
||||||
|
let (client, seen) = teams_forge((500, json!({"message": "x"})), vec![]).await;
|
||||||
|
client
|
||||||
|
.ensure_hives_team()
|
||||||
|
.await
|
||||||
|
.expect_err("an unreadable list is never answered with a create");
|
||||||
|
assert!(writes(&seen).is_empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn a_duplicate_create_finds_the_team_by_a_re_read() {
|
||||||
|
let reads = Arc::new(AtomicUsize::new(0));
|
||||||
|
let counted = Arc::clone(&reads);
|
||||||
|
let (client, _) = routed_forge(move |method, path, _| match (method, path) {
|
||||||
|
("GET", TEAMS) if counted.fetch_add(1, Ordering::SeqCst) == 0 => (200, "[]".to_owned()),
|
||||||
|
("GET", TEAMS) => (
|
||||||
|
200,
|
||||||
|
json!([hives_team_json("read", &HIVES_TEAM_UNITS)]).to_string(),
|
||||||
|
),
|
||||||
|
("POST", TEAMS) => (409, r#"{"message":"exists"}"#.to_owned()),
|
||||||
|
_ => (200, "{}".to_owned()),
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
assert_eq!(client.ensure_hives_team().await.unwrap(), 9);
|
||||||
|
assert_eq!(reads.load(Ordering::SeqCst), 2);
|
||||||
|
|
||||||
|
// Control: the same refusal with the team still absent is an error.
|
||||||
|
let (client, _) = teams_forge(
|
||||||
|
(200, json!([])),
|
||||||
|
vec![(
|
||||||
|
"POST /api/v1/orgs/agent-configs/teams",
|
||||||
|
409,
|
||||||
|
json!({"message": "x"}),
|
||||||
|
)],
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
client.ensure_hives_team().await.expect_err("nothing found");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn a_hive_account_is_created_and_locked_out_of_creating_repos() {
|
||||||
|
let email = hive_email("alpha");
|
||||||
|
let login = forge::hive_login("alpha");
|
||||||
|
let user = format!("/api/v1/admin/users/{login}");
|
||||||
|
let body = json!({"email": email}).to_string();
|
||||||
|
let (client, seen) = routed_forge(move |method, path, _| match method {
|
||||||
|
"GET" if path == format!("/api/v1/users/{}", forge::hive_login("alpha")) => {
|
||||||
|
(200, body.clone())
|
||||||
|
}
|
||||||
|
"POST" => (201, "{}".to_owned()),
|
||||||
|
_ => (200, "{}".to_owned()),
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
client.ensure_hive_user("alpha", &login).await.unwrap();
|
||||||
|
let lock = sent(&seen, "PATCH", &user).expect("lockdown sent");
|
||||||
|
assert_eq!(lock["max_repo_creation"], 0);
|
||||||
|
assert_eq!(lock["allow_create_organization"], false);
|
||||||
|
assert_eq!(lock["login_name"], login);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn a_failed_account_create_stops_before_the_lockdown() {
|
||||||
|
let (client, seen) = routed_forge(|method, _, _| match method {
|
||||||
|
"POST" => (500, r#"{"message":"x"}"#.to_owned()),
|
||||||
|
_ => (200, "{}".to_owned()),
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
client
|
||||||
|
.ensure_hive_user("alpha", &forge::hive_login("alpha"))
|
||||||
|
.await
|
||||||
|
.expect_err("the forge refused the create");
|
||||||
|
assert!(writes(&seen).iter().all(|w| w.starts_with("POST")));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn a_hive_missing_from_the_roster_is_never_queued_and_its_token_is_not_touched() {
|
||||||
|
let stale = seen(Observed::Decided(Decision::Conflict), Some(false));
|
||||||
|
let mint = seen(
|
||||||
|
Observed::Decided(Decision::Mint(MintReason::NotStored)),
|
||||||
|
Some(true),
|
||||||
|
);
|
||||||
|
// Only the hives the pass was given are judged; the roster is the whole truth.
|
||||||
|
assert_eq!(
|
||||||
|
plan(TeamSeen::Matches(1), &[("kept".to_owned(), mint)]),
|
||||||
|
["kept"]
|
||||||
|
);
|
||||||
|
assert!(plan(TeamSeen::Absent, &[]).is_empty());
|
||||||
|
assert!(plan(TeamSeen::Matches(1), &[("removed".to_owned(), stale)]).is_empty());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1499,4 +1499,506 @@ mod tests {
|
||||||
assert!(team_matches(&team(&reversed)));
|
assert!(team_matches(&team(&reversed)));
|
||||||
assert!(!team_matches(&team(&OPERATORS_TEAM_UNITS[1..])));
|
assert!(!team_matches(&team(&OPERATORS_TEAM_UNITS[1..])));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The write side, against a stand-in forge that answers by path.
|
||||||
|
use super::super::tests::{Seen as Requests, routed_forge, sent, writes};
|
||||||
|
use serde_json::{Value, json};
|
||||||
|
|
||||||
|
const ALL_ORGS: [&str; 4] = [CONFIG_ORG, SHARED_ORG, AGENTS_ORG, "actions"];
|
||||||
|
|
||||||
|
fn operators_team_json(id: i64) -> Value {
|
||||||
|
json!({
|
||||||
|
"id": id,
|
||||||
|
"name": OPERATORS_TEAM,
|
||||||
|
"description": OPERATORS_TEAM_DESCRIPTION,
|
||||||
|
"permission": "write",
|
||||||
|
"includes_all_repositories": true,
|
||||||
|
"can_create_org_repo": false,
|
||||||
|
"units": OPERATORS_TEAM_UNITS,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn config_rule_json() -> Value {
|
||||||
|
json!({
|
||||||
|
"enable_merge_whitelist": true,
|
||||||
|
"merge_whitelist_teams": [OPERATORS_TEAM],
|
||||||
|
"merge_whitelist_usernames": [],
|
||||||
|
"enable_approvals_whitelist": true,
|
||||||
|
"approvals_whitelist_teams": [OPERATORS_TEAM],
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A forge holding [`converged`]'s objects plus one config repo `atlas`
|
||||||
|
/// with its merge gate and an archived one, each answer replaced by the
|
||||||
|
/// matching entry of `overrides` (`"METHOD path"` → status, body). Any
|
||||||
|
/// other write is a 200 and any other read a 404.
|
||||||
|
async fn world(overrides: Vec<(&'static str, u16, Value)>) -> (Client, Requests) {
|
||||||
|
let mut answers: BTreeMap<String, (u16, String)> = BTreeMap::new();
|
||||||
|
let mut put = |key: String, status: u16, body: Value| {
|
||||||
|
answers.insert(key, (status, body.to_string()));
|
||||||
|
};
|
||||||
|
for org in ALL_ORGS {
|
||||||
|
put(
|
||||||
|
format!("GET /api/v1/orgs/{org}"),
|
||||||
|
200,
|
||||||
|
json!({"username": org}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
for (org, id) in [(AGENTS_ORG, 1), (CONFIG_ORG, 2)] {
|
||||||
|
put(
|
||||||
|
format!("GET /api/v1/orgs/{org}/teams"),
|
||||||
|
200,
|
||||||
|
json!([operators_team_json(id)]),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
put(
|
||||||
|
format!("GET /api/v1/repos/{SHARED_ORG}/{SHARED_DOCS_REPO}"),
|
||||||
|
200,
|
||||||
|
json!({"private": true, "empty": true}),
|
||||||
|
);
|
||||||
|
put(
|
||||||
|
format!("GET /api/v1/repos/{KNOWLEDGE_ORG}/{KNOWLEDGE_REPO}"),
|
||||||
|
200,
|
||||||
|
json!({"private": false, "empty": false}),
|
||||||
|
);
|
||||||
|
put(
|
||||||
|
"GET /api/v1/repos/actions/checkout".to_owned(),
|
||||||
|
200,
|
||||||
|
json!({"mirror": true, "mirror_interval": MIRROR_INTERVAL}),
|
||||||
|
);
|
||||||
|
put(
|
||||||
|
format!("GET /api/v1/orgs/{CONFIG_ORG}/repos"),
|
||||||
|
200,
|
||||||
|
json!([{"name": "atlas"}, {"name": "gone", "archived": true}]),
|
||||||
|
);
|
||||||
|
put(
|
||||||
|
format!("GET /api/v1/repos/{CONFIG_ORG}/atlas/branch_protections/main"),
|
||||||
|
200,
|
||||||
|
config_rule_json(),
|
||||||
|
);
|
||||||
|
for (key, status, body) in overrides {
|
||||||
|
put(key.to_owned(), status, body);
|
||||||
|
}
|
||||||
|
routed_forge(move |method, path, _| {
|
||||||
|
answers
|
||||||
|
.get(&format!("{method} {path}"))
|
||||||
|
.cloned()
|
||||||
|
.unwrap_or_else(|| match method {
|
||||||
|
"GET" => (404, r#"{"message":"not found"}"#.to_owned()),
|
||||||
|
"POST" => (201, "{}".to_owned()),
|
||||||
|
_ => (200, "{}".to_owned()),
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// [`desired`] without the avatar, which would read a file.
|
||||||
|
fn desired_without_avatar() -> Desired {
|
||||||
|
Desired::new(
|
||||||
|
vec![mirror("actions", "checkout")],
|
||||||
|
None,
|
||||||
|
PathBuf::from("/state"),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn no(path: &'static str) -> (&'static str, u16, Value) {
|
||||||
|
(path, 404, json!({"message": "no"}))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn a_converged_forge_gets_no_write_and_no_failure() {
|
||||||
|
let (client, seen) = world(vec![]).await;
|
||||||
|
let outcome = client.reconcile(&desired_without_avatar()).await;
|
||||||
|
assert_eq!(
|
||||||
|
outcome,
|
||||||
|
PassOutcome {
|
||||||
|
applied: 0,
|
||||||
|
failed: 0
|
||||||
|
}
|
||||||
|
);
|
||||||
|
assert!(writes(&seen).is_empty(), "{:?}", writes(&seen));
|
||||||
|
// Control: the pass read the objects it was judging.
|
||||||
|
assert!(
|
||||||
|
seen.lock()
|
||||||
|
.unwrap()
|
||||||
|
.iter()
|
||||||
|
.any(|(_, p, _)| p == "/api/v1/orgs/agent-configs/teams")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn a_team_with_the_wrong_permission_is_put_back_to_write_on_its_units() {
|
||||||
|
let mut wrong = operators_team_json(2);
|
||||||
|
wrong["permission"] = json!("admin");
|
||||||
|
let (client, seen) = world(vec![(
|
||||||
|
"GET /api/v1/orgs/agent-configs/teams",
|
||||||
|
200,
|
||||||
|
json!([wrong]),
|
||||||
|
)])
|
||||||
|
.await;
|
||||||
|
let outcome = client.reconcile(&desired_without_avatar()).await;
|
||||||
|
assert_eq!(
|
||||||
|
outcome,
|
||||||
|
PassOutcome {
|
||||||
|
applied: 1,
|
||||||
|
failed: 0
|
||||||
|
}
|
||||||
|
);
|
||||||
|
assert_eq!(writes(&seen), ["PATCH /api/v1/teams/2"]);
|
||||||
|
let edit = sent(&seen, "PATCH", "/api/v1/teams/2").unwrap();
|
||||||
|
assert_eq!(edit["permission"], "write");
|
||||||
|
assert_eq!(edit["includes_all_repositories"], true);
|
||||||
|
assert_eq!(edit["can_create_org_repo"], false);
|
||||||
|
assert_eq!(edit["units"], json!(OPERATORS_TEAM_UNITS));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn a_missing_team_is_created_as_a_write_team_on_every_repo() {
|
||||||
|
let (client, seen) = world(vec![("GET /api/v1/orgs/agents/teams", 200, json!([]))]).await;
|
||||||
|
let outcome = client.reconcile(&desired_without_avatar()).await;
|
||||||
|
assert_eq!(
|
||||||
|
outcome,
|
||||||
|
PassOutcome {
|
||||||
|
applied: 1,
|
||||||
|
failed: 0
|
||||||
|
}
|
||||||
|
);
|
||||||
|
assert_eq!(writes(&seen), ["POST /api/v1/orgs/agents/teams"]);
|
||||||
|
let team = sent(&seen, "POST", "/api/v1/orgs/agents/teams").unwrap();
|
||||||
|
assert_eq!(team["name"], OPERATORS_TEAM);
|
||||||
|
assert_eq!(team["permission"], "write");
|
||||||
|
assert_eq!(team["includes_all_repositories"], true);
|
||||||
|
assert_eq!(team["can_create_org_repo"], false);
|
||||||
|
assert_eq!(team["units"], json!(OPERATORS_TEAM_UNITS));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn a_duplicate_team_is_not_an_error_but_a_422_without_the_team_is() {
|
||||||
|
let (client, _) = world(vec![
|
||||||
|
("GET /api/v1/orgs/agents/teams", 200, json!([])),
|
||||||
|
(
|
||||||
|
"POST /api/v1/orgs/agents/teams",
|
||||||
|
409,
|
||||||
|
json!({"message": "exists"}),
|
||||||
|
),
|
||||||
|
])
|
||||||
|
.await;
|
||||||
|
client
|
||||||
|
.create_operators_team(AGENTS_ORG)
|
||||||
|
.await
|
||||||
|
.expect("409 is done");
|
||||||
|
|
||||||
|
let invalid = (
|
||||||
|
"POST /api/v1/orgs/agents/teams",
|
||||||
|
422,
|
||||||
|
json!({"message": "validation failed: team already exists"}),
|
||||||
|
);
|
||||||
|
let (client, _) = world(vec![invalid.clone()]).await;
|
||||||
|
client
|
||||||
|
.create_operators_team(AGENTS_ORG)
|
||||||
|
.await
|
||||||
|
.expect("422 with the team present");
|
||||||
|
let (client, _) = world(vec![
|
||||||
|
("GET /api/v1/orgs/agents/teams", 200, json!([])),
|
||||||
|
invalid,
|
||||||
|
])
|
||||||
|
.await;
|
||||||
|
client
|
||||||
|
.create_operators_team(AGENTS_ORG)
|
||||||
|
.await
|
||||||
|
.expect_err("422 and no such team is a real validation error");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn an_absent_org_is_created_and_a_failed_create_skips_what_needs_it() {
|
||||||
|
let (client, seen) = world(vec![no("GET /api/v1/orgs/actions")]).await;
|
||||||
|
let outcome = client.reconcile(&desired_without_avatar()).await;
|
||||||
|
assert_eq!(
|
||||||
|
outcome,
|
||||||
|
PassOutcome {
|
||||||
|
applied: 2,
|
||||||
|
failed: 0
|
||||||
|
}
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
writes(&seen),
|
||||||
|
["POST /api/v1/orgs", "POST /api/v1/repos/migrate"]
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
sent(&seen, "POST", "/api/v1/orgs").unwrap()["username"],
|
||||||
|
"actions"
|
||||||
|
);
|
||||||
|
|
||||||
|
// The org create fails, so the mirror is not created into it.
|
||||||
|
let (client, seen) = world(vec![
|
||||||
|
no("GET /api/v1/orgs/actions"),
|
||||||
|
no("GET /api/v1/repos/actions/checkout"),
|
||||||
|
("POST /api/v1/orgs", 500, json!({"message": "boom"})),
|
||||||
|
])
|
||||||
|
.await;
|
||||||
|
let outcome = client.reconcile(&desired_without_avatar()).await;
|
||||||
|
assert_eq!(
|
||||||
|
outcome,
|
||||||
|
PassOutcome {
|
||||||
|
applied: 0,
|
||||||
|
failed: 2
|
||||||
|
}
|
||||||
|
);
|
||||||
|
assert_eq!(writes(&seen), ["POST /api/v1/orgs"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn create_org_folds_a_race_but_not_a_failure() {
|
||||||
|
let (client, _) = world(vec![("POST /api/v1/orgs", 409, json!({"message": "x"}))]).await;
|
||||||
|
client.create_org("actions").await.expect("409 is a race");
|
||||||
|
let (client, _) = world(vec![("POST /api/v1/orgs", 500, json!({"message": "x"}))]).await;
|
||||||
|
client
|
||||||
|
.create_org("actions")
|
||||||
|
.await
|
||||||
|
.expect_err("500 surfaces");
|
||||||
|
let (client, _) = world(vec![(
|
||||||
|
"POST /api/v1/orgs",
|
||||||
|
422,
|
||||||
|
json!({"message": "validation failed: exists"}),
|
||||||
|
)])
|
||||||
|
.await;
|
||||||
|
client
|
||||||
|
.create_org("actions")
|
||||||
|
.await
|
||||||
|
.expect("422 and the org is there");
|
||||||
|
client
|
||||||
|
.create_org("nope")
|
||||||
|
.await
|
||||||
|
.expect_err("422 and the org is still absent");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn a_config_repo_without_the_operators_gate_gets_exactly_the_gate() {
|
||||||
|
let path = "/api/v1/repos/agent-configs/atlas/branch_protections/main";
|
||||||
|
let (client, seen) = world(vec![(
|
||||||
|
"GET /api/v1/repos/agent-configs/atlas/branch_protections/main",
|
||||||
|
200,
|
||||||
|
json!({"enable_merge_whitelist": true, "merge_whitelist_usernames": ["mallory"]}),
|
||||||
|
)])
|
||||||
|
.await;
|
||||||
|
let outcome = client.reconcile(&desired_without_avatar()).await;
|
||||||
|
assert_eq!(
|
||||||
|
outcome,
|
||||||
|
PassOutcome {
|
||||||
|
applied: 1,
|
||||||
|
failed: 0
|
||||||
|
}
|
||||||
|
);
|
||||||
|
assert_eq!(writes(&seen), [format!("PATCH {path}")]);
|
||||||
|
assert_eq!(
|
||||||
|
sent(&seen, "PATCH", path).unwrap(),
|
||||||
|
json!({
|
||||||
|
"approvals_whitelist_teams": [OPERATORS_TEAM],
|
||||||
|
"enable_approvals_whitelist": true,
|
||||||
|
"enable_merge_whitelist": true,
|
||||||
|
"merge_whitelist_teams": [OPERATORS_TEAM],
|
||||||
|
"merge_whitelist_usernames": [],
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn a_repo_with_no_rule_is_left_and_an_archived_repos_drifted_rule_is_not_touched() {
|
||||||
|
let drifted = json!({"enable_merge_whitelist": false});
|
||||||
|
let (client, seen) = world(vec![
|
||||||
|
no("GET /api/v1/repos/agent-configs/atlas/branch_protections/main"),
|
||||||
|
(
|
||||||
|
"GET /api/v1/repos/agent-configs/gone/branch_protections/main",
|
||||||
|
200,
|
||||||
|
drifted.clone(),
|
||||||
|
),
|
||||||
|
])
|
||||||
|
.await;
|
||||||
|
client.reconcile(&desired_without_avatar()).await;
|
||||||
|
assert!(writes(&seen).is_empty(), "{:?}", writes(&seen));
|
||||||
|
|
||||||
|
// Control: the same drift on the live repo is converged.
|
||||||
|
let (client, seen) = world(vec![(
|
||||||
|
"GET /api/v1/repos/agent-configs/atlas/branch_protections/main",
|
||||||
|
200,
|
||||||
|
drifted,
|
||||||
|
)])
|
||||||
|
.await;
|
||||||
|
client.reconcile(&desired_without_avatar()).await;
|
||||||
|
assert_eq!(
|
||||||
|
writes(&seen),
|
||||||
|
["PATCH /api/v1/repos/agent-configs/atlas/branch_protections/main"]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn an_unreadable_object_is_a_failure_and_never_a_write() {
|
||||||
|
let (client, seen) = world(vec![(
|
||||||
|
"GET /api/v1/orgs/agent-configs/teams",
|
||||||
|
500,
|
||||||
|
json!({"message": "boom"}),
|
||||||
|
)])
|
||||||
|
.await;
|
||||||
|
let outcome = client.reconcile(&desired_without_avatar()).await;
|
||||||
|
assert_eq!(
|
||||||
|
outcome,
|
||||||
|
PassOutcome {
|
||||||
|
applied: 0,
|
||||||
|
failed: 1
|
||||||
|
}
|
||||||
|
);
|
||||||
|
assert!(writes(&seen).is_empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn a_private_knowledge_repo_is_set_public() {
|
||||||
|
let (client, seen) = world(vec![(
|
||||||
|
"GET /api/v1/repos/internal/knowledge",
|
||||||
|
200,
|
||||||
|
json!({"private": true, "empty": false}),
|
||||||
|
)])
|
||||||
|
.await;
|
||||||
|
client.reconcile(&desired_without_avatar()).await;
|
||||||
|
assert_eq!(writes(&seen), ["PATCH /api/v1/repos/internal/knowledge"]);
|
||||||
|
assert_eq!(
|
||||||
|
sent(&seen, "PATCH", "/api/v1/repos/internal/knowledge").unwrap(),
|
||||||
|
json!({"private": false})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn a_mirror_with_another_interval_is_patched_to_the_interval_alone() {
|
||||||
|
let (client, seen) = world(vec![(
|
||||||
|
"GET /api/v1/repos/actions/checkout",
|
||||||
|
200,
|
||||||
|
json!({"mirror": true, "mirror_interval": "0s"}),
|
||||||
|
)])
|
||||||
|
.await;
|
||||||
|
client.reconcile(&desired_without_avatar()).await;
|
||||||
|
assert_eq!(
|
||||||
|
sent(&seen, "PATCH", "/api/v1/repos/actions/checkout").unwrap(),
|
||||||
|
json!({"mirror_interval": MIRROR_INTERVAL})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn an_absent_repo_is_created_with_its_privacy_and_an_empty_public_one_is_seeded() {
|
||||||
|
let (client, seen) = world(vec![
|
||||||
|
no("GET /api/v1/repos/internal/docs"),
|
||||||
|
no("GET /api/v1/repos/internal/knowledge"),
|
||||||
|
])
|
||||||
|
.await;
|
||||||
|
client.reconcile(&desired_without_avatar()).await;
|
||||||
|
let created: Vec<(String, bool)> = seen
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.iter()
|
||||||
|
.filter(|(m, p, _)| m == "POST" && p == "/api/v1/orgs/internal/repos")
|
||||||
|
.map(|(_, _, b)| {
|
||||||
|
(
|
||||||
|
b["name"].as_str().unwrap().to_owned(),
|
||||||
|
b["private"].as_bool().unwrap(),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
assert_eq!(
|
||||||
|
created,
|
||||||
|
[("docs".to_owned(), true), ("knowledge".to_owned(), false)]
|
||||||
|
);
|
||||||
|
|
||||||
|
let (client, seen) = world(vec![(
|
||||||
|
"GET /api/v1/repos/internal/knowledge",
|
||||||
|
200,
|
||||||
|
json!({"private": false, "empty": true}),
|
||||||
|
)])
|
||||||
|
.await;
|
||||||
|
client.reconcile(&desired_without_avatar()).await;
|
||||||
|
let readme = sent(&seen, "POST", "/api/v1/repos/internal/knowledge/contents").unwrap();
|
||||||
|
assert_eq!(readme["files"][0]["path"], "README.md");
|
||||||
|
assert_eq!(readme["files"][0]["operation"], "create");
|
||||||
|
// Control: a repo that already has content is not seeded.
|
||||||
|
let (client, seen) = world(vec![]).await;
|
||||||
|
client.reconcile(&desired_without_avatar()).await;
|
||||||
|
assert!(sent(&seen, "POST", "/api/v1/repos/internal/knowledge/contents").is_none());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn a_mirror_is_created_public_and_periodic_and_only_a_409_is_a_race() {
|
||||||
|
let (client, seen) = world(vec![]).await;
|
||||||
|
client
|
||||||
|
.create_mirror("actions", "checkout", "https://example.invalid/checkout")
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
let body = sent(&seen, "POST", "/api/v1/repos/migrate").unwrap();
|
||||||
|
assert_eq!(body["mirror"], true);
|
||||||
|
assert_eq!(body["private"], false);
|
||||||
|
assert_eq!(body["mirror_interval"], MIRROR_INTERVAL);
|
||||||
|
assert_eq!(body["repo_owner"], "actions");
|
||||||
|
assert_eq!(body["clone_addr"], "https://example.invalid/checkout");
|
||||||
|
|
||||||
|
let (client, _) = world(vec![(
|
||||||
|
"POST /api/v1/repos/migrate",
|
||||||
|
409,
|
||||||
|
json!({"message": "x"}),
|
||||||
|
)])
|
||||||
|
.await;
|
||||||
|
client
|
||||||
|
.create_mirror("actions", "checkout", "u")
|
||||||
|
.await
|
||||||
|
.expect("409 is a race");
|
||||||
|
let (client, _) = world(vec![(
|
||||||
|
"POST /api/v1/repos/migrate",
|
||||||
|
422,
|
||||||
|
json!({"message": "x"}),
|
||||||
|
)])
|
||||||
|
.await;
|
||||||
|
client
|
||||||
|
.create_mirror("actions", "checkout", "u")
|
||||||
|
.await
|
||||||
|
.expect_err("a 422 is a bad clone address, not a duplicate");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn the_avatar_is_uploaded_once_and_marked() {
|
||||||
|
let dir = tempfile::tempdir().unwrap();
|
||||||
|
let png = dir.path().join("a.png");
|
||||||
|
std::fs::write(&png, b"fixed bytes").unwrap();
|
||||||
|
let desired = Desired::new(vec![], Some(png), dir.path().to_owned());
|
||||||
|
let (client, seen) = world(vec![(
|
||||||
|
"POST /api/v1/orgs/agent-configs/avatar",
|
||||||
|
204,
|
||||||
|
json!({}),
|
||||||
|
)])
|
||||||
|
.await;
|
||||||
|
client.reconcile(&desired).await;
|
||||||
|
assert!(sent(&seen, "POST", "/api/v1/orgs/agent-configs/avatar").is_some());
|
||||||
|
assert!(desired.avatar_marker().exists());
|
||||||
|
let (client, seen) = world(vec![]).await;
|
||||||
|
client.reconcile(&desired).await;
|
||||||
|
assert!(writes(&seen).is_empty(), "the marker stops a second upload");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn the_merge_gate_prerequisites_fail_while_the_team_cannot_be_created() {
|
||||||
|
let (client, _) = world(vec![]).await;
|
||||||
|
client
|
||||||
|
.ensure_merge_gate_prerequisites()
|
||||||
|
.await
|
||||||
|
.expect("converged");
|
||||||
|
let (client, seen) = world(vec![
|
||||||
|
("GET /api/v1/orgs/agent-configs/teams", 200, json!([])),
|
||||||
|
(
|
||||||
|
"POST /api/v1/orgs/agent-configs/teams",
|
||||||
|
500,
|
||||||
|
json!({"message": "x"}),
|
||||||
|
),
|
||||||
|
])
|
||||||
|
.await;
|
||||||
|
let err = client
|
||||||
|
.ensure_merge_gate_prerequisites()
|
||||||
|
.await
|
||||||
|
.expect_err("no team");
|
||||||
|
assert!(format!("{err:#}").contains("could not be ensured"));
|
||||||
|
assert_eq!(writes(&seen), ["POST /api/v1/orgs/agent-configs/teams"]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue