Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
//! HTTP + real PostgreSQL contract checks; only identity and external calendar are controlled.
use super::*;
use crate::outbound::postgres::PostgresRepository;
use chrono::{DateTime, Duration, Utc};
use macro_authorization::{InternalIdentityClaims, MacroAuthorizationError};
use model_user::UserContext;
use rootcause::Report;
use std::sync::atomic::{AtomicBool, Ordering};
/// CI clones every test database from a `template1` that already holds the macrodb
/// schema (`just setup_test_template`). These tests build their own schema, so they
/// start from an empty `public` schema.
const EMPTY_PUBLIC_SCHEMA: &str = "DROP SCHEMA public CASCADE; CREATE SCHEMA public;";
#[derive(Clone)]
struct Identity;
impl MacroAuthorizationService for Identity {
async fn authorize(&self, jwt: &str) -> Result<UserContext, Report<MacroAuthorizationError>> {
if !matches!(jwt, "host" | "member" | "stranger") {
return Err(Report::new(MacroAuthorizationError::InvalidCredentials));
}
Ok(UserContext {
user_id: format!("macro|{jwt}@example.test"),
fusion_user_id: jwt.into(),
..Default::default()
})
}
async fn authorize_identity(
&self,
jwt: &str,
) -> Result<macro_authorization::ValidatedCredential, Report<MacroAuthorizationError>> {
self.authorize(jwt)
.await
.map(macro_authorization::ValidatedCredential::session)
}
async fn authorize_internal(
&self,
_: &str,
_: InternalIdentityClaims,
) -> Result<Option<UserContext>, Report<MacroAuthorizationError>> {
Err(Report::new(MacroAuthorizationError::InvalidCredentials))
}
}
struct People;
impl Directory for People {
async fn user_teams(&self, _: &str) -> Result<Vec<Uuid>, Error> {
Ok(vec![])
}
async fn members(&self, _: Uuid) -> Result<Vec<TeamMember>, Error> {
Ok(vec![
TeamMember {
user_id: "macro|[email protected]".into(),
admin: true,
},
TeamMember {
user_id: "macro|[email protected]".into(),
admin: false,
},
])
}
}
struct Calendar(AtomicBool);
impl Calendars for Calendar {
async fn creation_calendar(&self, _: &str) -> Result<Uuid, Error> {
Ok(Uuid::nil())
}
async fn busy(
&self,
_: &[String],
_: DateTime<Utc>,
_: DateTime<Utc>,
_: Option<Uuid>,
) -> Result<Vec<BusyRange>, Error> {
Ok(vec![])
}
async fn create(&self, r: &BookingRecord, _: &EventType) -> Result<(Uuid, String), Error> {
if self.0.swap(false, Ordering::SeqCst) {
return Err(Error::CalendarUnavailable);
}
Ok((r.booking.id, "Test location".into()))
}
async fn cancel(&self, _: &BookingRecord) -> Result<(), Error> {
Ok(())
}
async fn reschedule(&self, _: &BookingRecord) -> Result<(), Error> {
Ok(())
}
}
#[sqlx::test(migrations = false)]
async fn http_booking_receipt_recovery_reschedule_cancel_and_team_auth(pool: sqlx::PgPool) {
sqlx::raw_sql(EMPTY_PUBLIC_SCHEMA)
.execute(&pool)
.await
.unwrap();
sqlx::raw_sql("CREATE TABLE \"User\" (id text PRIMARY KEY); CREATE TABLE team (id uuid PRIMARY KEY); INSERT INTO \"User\" VALUES ('macro|[email protected]');").execute(&pool).await.unwrap();
sqlx::raw_sql(include_str!(
"../../../../macro_db_client/migrations/20260918164303_calendar_scheduling.sql"
))
.execute(&pool)
.await
.unwrap();
sqlx::raw_sql(include_str!(
"../../../../macro_db_client/migrations/20260918175703_scheduling_recovery.sql"
))
.execute(&pool)
.await
.unwrap();
let service = Arc::new(Service::new(
PostgresRepository::new(pool.clone()),
Calendar(AtomicBool::new(true)),
People,
));
let app: Router = router(RouterState::new(
service.clone(),
MacroAuthorizationState::new(Arc::new(Identity)),
));
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let base = format!("http://{}", listener.local_addr().unwrap());
let task = tokio::spawn(async move {
axum::serve(listener, app).await.unwrap();
});
let client = reqwest::Client::new();
assert_eq!(
client
.get(format!("{base}/settings"))
.send()
.await
.unwrap()
.status(),
401
);
let response: serde_json::Value = client
.get(format!("{base}/settings"))
.bearer_auth("host")
.send()
.await
.unwrap()
.json()
.await
.unwrap();
let id: Uuid = serde_json::from_value(response["profile"]["id"].clone()).unwrap();
let schedule = Uuid::now_v7();
let event = Uuid::now_v7();
let profile = serde_json::json!({
"id":id,"name":"Integration host","description":"","revision":0,"defaultScheduleId":schedule,
"schedules":[{"id":schedule,"name":"Work","timeZone":"UTC","overrides":[],"weekly":(0..7).map(|day| serde_json::json!({"day":day,"windows":[{"start":"09:00","end":"17:00"}]})).collect::<Vec<_>>() }],
"eventTypes":[{"id":event,"title":"Integration meeting","slug":"meeting","description":"","durationMinutes":30,"location":"","googleMeet":false,"enabled":true,"scheduleId":schedule,"mode":"individual","hosts":["macro|[email protected]"],"beforeMinutes":0,"afterMinutes":0,"noticeMinutes":0,"horizonDays":30,"intervalMinutes":30,"dailyLimit":null,"requiresConfirmation":false,"questions":[]}]
});
let saved = client
.put(format!("{base}/settings"))
.bearer_auth("host")
.json(&profile)
.send()
.await
.unwrap();
assert_eq!(saved.status(), 200);
assert_eq!(
client
.put(format!("{base}/settings"))
.bearer_auth("stranger")
.json(&profile)
.send()
.await
.unwrap()
.status(),
403
);
let day = (Utc::now() + Duration::days(2)).date_naive();
let slots: serde_json::Value = client
.get(format!(
"{base}/public/{id}/{event}/slots?date={day}&time_zone=UTC"
))
.send()
.await
.unwrap()
.json()
.await
.unwrap();
let start = slots["slots"][0]["startsAt"].as_str().unwrap();
let request = serde_json::json!({"startsAt":start,"name":"Test Guest","email":"[email protected]","timeZone":"UTC","answers":{},"requestId":Uuid::now_v7()});
let booked = client
.post(format!("{base}/public/{id}/{event}/book"))
.json(&request)
.send()
.await
.unwrap();
assert_eq!(
booked.status(),
200,
"durable reservations return their receipt even on provider failure"
);
assert_eq!(booked.headers()["cache-control"], "private, no-store");
let receipt: serde_json::Value = booked.json().await.unwrap();
assert_eq!(receipt["booking"]["status"], "failed");
let booking = Uuid::parse_str(receipt["booking"]["id"].as_str().unwrap()).unwrap();
let token = receipt["token"].as_str().unwrap();
assert_eq!(
client
.get(format!("{base}/public/bookings/{booking}"))
.bearer_auth(Uuid::new_v4())
.send()
.await
.unwrap()
.status(),
404
);
let retry: serde_json::Value = client
.post(format!("{base}/public/{id}/{event}/book"))
.json(&request)
.send()
.await
.unwrap()
.json()
.await
.unwrap();
assert_eq!(retry["booking"]["id"], receipt["booking"]["id"]);
sqlx::query!(
"UPDATE scheduling_booking SET recovery_at = now() - interval '1 second' WHERE id = $1",
booking
)
.execute(&pool)
.await
.unwrap();
assert!(service.recover_once().await.unwrap());
let confirmed: serde_json::Value = client
.get(format!("{base}/public/bookings/{booking}"))
.bearer_auth(token)
.send()
.await
.unwrap()
.json()
.await
.unwrap();
assert_eq!(confirmed["booking"]["status"], "confirmed");
let next = slots["slots"][2]["startsAt"].as_str().unwrap();
let moved: serde_json::Value = client
.post(format!("{base}/public/bookings/{booking}/reschedule"))
.json(&serde_json::json!({"token":token,"startsAt":next}))
.send()
.await
.unwrap()
.json()
.await
.unwrap();
assert_eq!(moved["booking"]["rescheduleCount"], 1);
let cancelled: serde_json::Value = client
.post(format!("{base}/public/bookings/{booking}/cancel"))
.json(&serde_json::json!({"token":token}))
.send()
.await
.unwrap()
.json()
.await
.unwrap();
assert_eq!(cancelled["booking"]["status"], "cancelled");
let team = Uuid::now_v7();
assert_eq!(
client
.get(format!("{base}/settings?teamId={team}"))
.bearer_auth("stranger")
.send()
.await
.unwrap()
.status(),
403
);
assert_eq!(
client
.get(format!("{base}/settings?teamId={team}"))
.bearer_auth("member")
.send()
.await
.unwrap()
.status(),
200
);
task.abort();
}
#[sqlx::test(migrations = false)]
async fn http_booking_links_require_confirmation_and_revision(pool: sqlx::PgPool) {
sqlx::raw_sql(EMPTY_PUBLIC_SCHEMA)
.execute(&pool)
.await
.unwrap();
sqlx::raw_sql("CREATE TABLE \"User\" (id text PRIMARY KEY); CREATE TABLE team (id uuid PRIMARY KEY); INSERT INTO \"User\" VALUES ('macro|[email protected]');").execute(&pool).await.unwrap();
sqlx::raw_sql(include_str!(
"../../../../macro_db_client/migrations/20260918164303_calendar_scheduling.sql"
))
.execute(&pool)
.await
.unwrap();
sqlx::raw_sql(include_str!(
"../../../../macro_db_client/migrations/20260918175703_scheduling_recovery.sql"
))
.execute(&pool)
.await
.unwrap();
let app: Router = router(RouterState::new(
Arc::new(Service::new(
PostgresRepository::new(pool.clone()),
Calendar(AtomicBool::new(false)),
People,
)),
MacroAuthorizationState::new(Arc::new(Identity)),
));
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let base = format!("http://{}", listener.local_addr().unwrap());
let task = tokio::spawn(async move {
axum::serve(listener, app).await.unwrap();
});
let client = reqwest::Client::new();
let draft = serde_json::json!({
"event": {"title":"Intro","slug":"intro","description":"","durationMinutes":30,"location":"Office","googleMeet":false,"enabled":true,"mode":"individual","hosts":["macro|[email protected]"],"beforeMinutes":0,"afterMinutes":0,"noticeMinutes":60,"horizonDays":30,"intervalMinutes":30,"dailyLimit":null,"requiresConfirmation":false,"questions":[]},
"schedule": {"name":"Working hours","timeZone":"UTC","weekly":(0..7).map(|day| serde_json::json!({"day":day,"windows":[{"start":"09:00","end":"17:00"}]})).collect::<Vec<_>>(),"overrides":[]}
});
let empty: serde_json::Value = client
.get(format!("{base}/links"))
.bearer_auth("host")
.send()
.await
.unwrap()
.json()
.await
.unwrap();
assert_eq!(empty["links"], serde_json::json!([]));
assert_eq!(empty["userId"], "macro|[email protected]");
let unconfirmed = client
.post(format!("{base}/links"))
.bearer_auth("host")
.json(&serde_json::json!({"draft": draft, "userConfirmation": " \n"}))
.send()
.await
.unwrap();
assert_eq!(unconfirmed.status(), 400);
let created = client
.post(format!("{base}/links"))
.bearer_auth("host")
.json(&serde_json::json!({"draft": draft, "userConfirmation": "Yes, create it."}))
.send()
.await
.unwrap();
assert_eq!(created.status(), 200);
let created: serde_json::Value = created.json().await.unwrap();
assert_eq!(created["draft"], draft);
let link = created["eventTypeId"].as_str().unwrap().to_owned();
let revision = created["revision"].as_i64().unwrap();
let listed: serde_json::Value = client
.get(format!("{base}/links?query=intro"))
.bearer_auth("host")
.send()
.await
.unwrap()
.json()
.await
.unwrap();
assert_eq!(listed["links"][0]["eventTypeId"], link.as_str());
assert_eq!(listed["revision"], revision);
let mut edited = draft.clone();
edited["event"]["title"] = serde_json::json!("Intro call");
let saved = client
.put(format!("{base}/links/{link}"))
.bearer_auth("host")
.json(&serde_json::json!({"expectedRevision": revision, "draft": edited, "userConfirmation": "Yes, rename it."}))
.send()
.await
.unwrap();
assert_eq!(saved.status(), 200);
let saved: serde_json::Value = saved.json().await.unwrap();
assert_eq!(saved["draft"]["event"]["title"], "Intro call");
assert!(saved["revision"].as_i64().unwrap() > revision);
let mut stale = draft.clone();
stale["event"]["title"] = serde_json::json!("Stale title");
assert_eq!(
client
.put(format!("{base}/links/{link}"))
.bearer_auth("host")
.json(&serde_json::json!({"expectedRevision": revision, "draft": stale, "userConfirmation": "Yes."}))
.send()
.await
.unwrap()
.status(),
409
);
let team = Uuid::now_v7();
assert_eq!(
client
.get(format!("{base}/links?teamId={team}"))
.bearer_auth("stranger")
.send()
.await
.unwrap()
.status(),
403
);
task.abort();
}