Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

6.3 KB
Raw Blame
use super::*;
use macro_db_migrator::MACRO_DB_MIGRATIONS;
use sqlx::PgPool;

async fn insert_email_link(pool: &PgPool) -> Uuid {
    let link_id = Uuid::now_v7();
    let email_address = format!("sso-grant-{link_id}@example.com");
    sqlx::query!(
        r#"
        INSERT INTO email_links (
            id, macro_id, fusionauth_user_id, email_address, provider
        )
        VALUES ($1, $2, $2, $3, 'GMAIL')
        "#,
        link_id,
        "macro|[email protected]",
        email_address,
    )
    .execute(pool)
    .await
    .unwrap();
    link_id
}

async fn body_json(response: Response) -> serde_json::Value {
    let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
        .await
        .unwrap();
    serde_json::from_slice(&bytes).unwrap()
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn zero_grant_is_unrecorded(pool: PgPool) {
    let link_id = insert_email_link(&pool).await;
    sqlx::query!(
        "INSERT INTO email_link_google_scopes (link_id) VALUES ($1)",
        link_id,
    )
    .execute(&pool)
    .await
    .unwrap();

    assert!(has_unrecorded_google_grant(&pool, link_id).await);
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn missing_side_table_state_is_unrecorded(pool: PgPool) {
    let link_id = insert_email_link(&pool).await;

    assert!(has_unrecorded_google_grant(&pool, link_id).await);
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn recorded_side_table_grant_is_not_unrecorded(pool: PgPool) {
    let link_id = insert_email_link(&pool).await;
    sqlx::query!(
        "INSERT INTO email_link_google_scopes (link_id, grant_version) VALUES ($1, 1)",
        link_id,
    )
    .execute(&pool)
    .await
    .unwrap();

    assert!(!has_unrecorded_google_grant(&pool, link_id).await);
}

#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn missing_email_link_is_not_unrecorded(pool: PgPool) {
    assert!(!has_unrecorded_google_grant(&pool, Uuid::now_v7()).await);
}

#[tokio::test]
async fn no_gmail_grant_is_a_coded_400() {
    let response = InitError::NoGmailGrant.into_response();
    assert_eq!(response.status(), StatusCode::BAD_REQUEST);
    let body = body_json(response).await;
    assert_eq!(body["code"], NO_GMAIL_GRANT_CODE);
}

#[tokio::test]
async fn already_initialized_is_a_coded_400() {
    let response = InitError::AlreadyInitialized.into_response();
    assert_eq!(response.status(), StatusCode::BAD_REQUEST);
    let body = body_json(response).await;
    assert_eq!(body["code"], ALREADY_INITIALIZED_CODE);
}

#[test]
fn unusable_provider_grants_classify_as_no_gmail_grant() {
    for error in [EmailApiError::AuthRequired, EmailApiError::Forbidden] {
        assert!(matches!(
            classify_provider_init_error(error),
            InitError::NoGmailGrant
        ));
    }
}

#[test]
fn rate_limiting_classifies_as_http_429() {
    let error = classify_provider_init_error(EmailApiError::RateLimited {
        retry_after: None,
        origin: email_api_client::domain::models::RateLimitOrigin::Provider { concurrency: false },
    });
    assert_eq!(error.status_code(), StatusCode::TOO_MANY_REQUESTS);
}

#[test]
fn a_rate_limited_init_tells_the_client_when_to_retry() {
    let response = InitError::ProviderError(EmailApiError::RateLimited {
        retry_after: Some(std::time::Duration::from_millis(1_200)),
        origin: email_api_client::domain::models::RateLimitOrigin::Local,
    })
    .into_response();

    assert_eq!(response.status(), StatusCode::TOO_MANY_REQUESTS);
    assert_eq!(
        response
            .headers()
            .get(axum::http::header::RETRY_AFTER)
            .and_then(|value| value.to_str().ok()),
        Some("2")
    );
}

#[tokio::test]
async fn provider_diagnostics_are_not_returned_to_clients() {
    let response = InitError::ProviderError(EmailApiError::Permanent {
        message: "sanitized provider body".to_string(),
    })
    .into_response();
    let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
        .await
        .unwrap();
    assert_eq!(bytes, "Email provider operation failed");
}

#[test]
fn only_shared_inbox_conflict_keeps_the_in_progress_link() {
    // SharedInboxConflict is held open for the force_share retry, so its row must be kept;
    // every other terminal failure must clean up the row so it stops counting toward the
    // /link/gmail start cap. The delete itself is covered by macro_db_client's own tests.
    assert!(
        !should_clean_up_in_progress_link(&InitError::SharedInboxConflict {
            email_address: "[email protected]".to_string(),
            existing_owner_email: "[email protected]".to_string(),
            existing_link_id: Uuid::new_v4(),
        }),
        "SharedInboxConflict must keep the row for the force_share retry"
    );

    for err in [
        InitError::AlreadyInitialized,
        InitError::NoGmailGrant,
        InitError::EnqueueError,
        InitError::BadRequest("bad".to_string()),
        InitError::ProviderError(EmailApiError::AuthRequired),
    ] {
        assert!(
            should_clean_up_in_progress_link(&err),
            "terminal failure {err:?} should clean up the in_progress row"
        );
    }
}

/// The grant alone cannot say whether the user wanted calendar: consent
/// requests carry `include_granted_scopes=true`, so Google re-issues the
/// calendar scopes of an earlier grant on a plain Gmail reconnect. Only what
/// the request asked for decides.
#[test]
fn calendar_intent_follows_the_consent_request_not_the_grant() {
    let calendar_scopes = calendar_events::domain::models::GOOGLE_CALENDAR_SCOPES
        .map(str::to_owned)
        .to_vec();
    let gmail_scope = "https://www.googleapis.com/auth/gmail.modify".to_owned();
    let in_progress = |requested: Vec<String>| InProgressUserLink {
        email_provider: "GMAIL".into(),
        macro_user_id: Uuid::now_v7(),
        linked_email: None,
        requested_google_scopes: requested,
        granted_google_scopes: [vec![gmail_scope.clone()], calendar_scopes.clone()].concat(),
    };

    assert!(matches!(
        CompletedGoogleGrant::from_in_progress(&in_progress(vec![gmail_scope.clone()])).intent,
        CalendarGrantIntent::Incidental
    ));
    assert!(matches!(
        CompletedGoogleGrant::from_in_progress(&in_progress(
            [vec![gmail_scope.clone()], calendar_scopes.clone()].concat()
        ))
        .intent,
        CalendarGrantIntent::CalendarRequested
    ));
}