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.

32.2 KB
Raw Blame
use entity_access::domain::models::TeamRole;
use std::sync::{Arc, Mutex};

use axum::{
    Router,
    body::{Body, to_bytes},
    http::{Request, StatusCode, header},
    routing::get,
};
use entity_access::domain::{
    models::{
        AccessError, AccessLevel, BotAccessScope, BotId, CallChannelInfo, EntityAccessAuth,
        EntityAccessReceipt, EntityPermission, EntityType, RequiredPermission, UserTeamInfo,
    },
    ports::EntityAccessService,
};
#[allow(deprecated)]
use macro_authorization::{
    INTERNAL_API_KEY_HEADER, InternalAuthConfig, JwtValidator, LEGACY_DSS_INTERNAL_API_KEY_HEADER,
    MacroAuthorizationError, MacroAuthorizationExtractor, MacroAuthorizationServiceImpl,
    MacroAuthorizationState, UserOrInternal, ValidatedIdentity,
};
use macro_user_id::{
    lowercased::Lowercase,
    user_id::{MacroUserId, MacroUserIdStr},
};
use rootcause::Report;
use tower::ServiceExt;
use uuid::Uuid;

use super::{
    PropertiesRouterState, PropertyTeamExtractor,
    extract::{EditReceiptExtractor, ViewReceiptExtractor},
};
use crate::{
    PropertiesServiceImpl,
    domain::ports::{MockNotificationService, MockPermissionService, MockPropertiesRepo},
};

const DEFAULT_INTERNAL_USER_ID: &str = "macro|[email protected]";
const INTERNAL_API_KEY: &str = "test-internal-key";
const ORGANIZATION_ID: i32 = 42;
const VALID_USER_ID: &str = "macro|[email protected]";

type TestPropertiesService =
    PropertiesServiceImpl<MockPropertiesRepo, MockPermissionService, MockNotificationService>;
type TestAuthorizationService = MacroAuthorizationServiceImpl<FakeJwtValidator>;

#[derive(Clone, Debug, PartialEq)]
enum AccessCall {
    GenerateReceipt {
        user_id: String,
        organization_id: Option<i64>,
        entity_id: String,
        entity_type: EntityType,
    },
    PublicAccess {
        entity_id: String,
        entity_type: EntityType,
        required_level: AccessLevel,
    },
    UserTeam {
        user_id: String,
    },
}

#[derive(Clone, Debug, Default)]
struct FakeEntityAccessService {
    calls: Arc<Mutex<Vec<AccessCall>>>,
    team: Option<UserTeamInfo>,
}

impl FakeEntityAccessService {
    /// A service whose caller belongs to `team_id`, for the endpoints that
    /// require team membership.
    fn on_team(team_id: Uuid) -> Self {
        Self {
            calls: Arc::default(),
            team: Some(UserTeamInfo {
                team_id,
                role: TeamRole::Member,
            }),
        }
    }

    fn calls(&self) -> Vec<AccessCall> {
        self.calls.lock().expect("calls lock poisoned").clone()
    }

    fn record(&self, call: AccessCall) {
        self.calls.lock().expect("calls lock poisoned").push(call);
    }
}

impl EntityAccessService for FakeEntityAccessService {
    async fn generate_entity_access_receipt<T: RequiredPermission>(
        &self,
        user_id: &MacroUserId<Lowercase<'_>>,
        user_org_id: Option<i64>,
        entity_id: &str,
        entity_type: EntityType,
    ) -> Result<EntityAccessReceipt<T>, AccessError> {
        self.record(AccessCall::GenerateReceipt {
            user_id: user_id.as_ref().to_string(),
            organization_id: user_org_id,
            entity_id: entity_id.to_string(),
            entity_type,
        });

        // Entities whose id contains "denied" model the caller lacking access,
        // so receipt-minting fails for them (used by the bulk skip test).
        if entity_id.contains("denied") {
            return Err(AccessError::Unauthorized);
        }

        let user_id = MacroUserIdStr::try_from(user_id.as_ref().to_string())
            .expect("authorized test user id should be valid");
        Ok(EntityAccessReceipt::dangerously_assert_authenticated_user(
            user_id,
            entity_id,
            entity_type,
        ))
    }

    async fn generate_bot_entity_access_receipt<T: RequiredPermission>(
        &self,
        _bot_id: BotId,
        _scope: BotAccessScope,
        _entity_id: &str,
        _entity_type: EntityType,
    ) -> Result<EntityAccessReceipt<T>, AccessError> {
        panic!("unexpected bot receipt request")
    }

    async fn get_access_level(
        &self,
        _user_id: Option<&MacroUserId<Lowercase<'_>>>,
        _entity_id: &str,
        _entity_type: EntityType,
    ) -> Result<Option<AccessLevel>, AccessError> {
        panic!("unexpected access-level request")
    }

    async fn check_access(
        &self,
        _user_id: Option<&MacroUserId<Lowercase<'_>>>,
        _entity_id: &str,
        _entity_type: EntityType,
        _required_level: AccessLevel,
    ) -> Result<AccessLevel, AccessError> {
        panic!("unexpected authenticated access check")
    }

    async fn check_public_access(
        &self,
        entity_id: &str,
        entity_type: EntityType,
        required_level: AccessLevel,
    ) -> Result<AccessLevel, AccessError> {
        self.record(AccessCall::PublicAccess {
            entity_id: entity_id.to_string(),
            entity_type,
            required_level,
        });
        Ok(AccessLevel::View)
    }

    async fn get_entity_permission(
        &self,
        _user_id: Option<&MacroUserId<Lowercase<'_>>>,
        _entity_id: &str,
        _entity_type: EntityType,
        _user_org_id: Option<i64>,
    ) -> Result<EntityPermission, AccessError> {
        panic!("unexpected entity-permission request")
    }

    async fn get_crm_entity_permission_with_team(
        &self,
        _user_id: Option<&MacroUserId<Lowercase<'_>>>,
        _entity_id: &str,
        _entity_type: EntityType,
    ) -> Result<(EntityPermission, Uuid, TeamRole), AccessError> {
        panic!("unexpected CRM permission request")
    }

    async fn get_users_by_entity(
        &self,
        _entity_id: &str,
        _entity_type: EntityType,
    ) -> Result<Vec<MacroUserIdStr<'static>>, AccessError> {
        panic!("unexpected entity-users request")
    }

    async fn get_call_channel(
        &self,
        _call_id: &Uuid,
    ) -> Result<Option<CallChannelInfo>, AccessError> {
        panic!("unexpected call-channel request")
    }

    async fn get_call_channel_by_channel_id(
        &self,
        _channel_id: &Uuid,
    ) -> Result<Option<CallChannelInfo>, AccessError> {
        panic!("unexpected channel-call request")
    }

    async fn get_user_team(
        &self,
        user_id: &MacroUserId<Lowercase<'_>>,
    ) -> Result<Option<UserTeamInfo>, AccessError> {
        self.record(AccessCall::UserTeam {
            user_id: user_id.as_ref().to_string(),
        });
        Ok(self.team)
    }
}

#[derive(Clone, Copy)]
struct FakeJwtValidator;

impl JwtValidator for FakeJwtValidator {
    fn validate_credential(
        &self,
        jwt: &str,
    ) -> Result<macro_authorization::ValidatedCredential, Report<MacroAuthorizationError>> {
        match jwt {
            "valid" => Ok(macro_authorization::ValidatedCredential {
                origin: macro_authorization::CredentialOrigin::Session,
                identity: ValidatedIdentity {
                    agent_id: None,
                    agent_via: None,
                    user_id: VALID_USER_ID.to_string(),
                    fusion_user_id: "fusion-valid-user".to_string(),
                    organization_id: Some(ORGANIZATION_ID),
                    permissions: None,
                },
            }),
            "expired" => Err(Report::new(MacroAuthorizationError::CredentialsExpired)),
            _ => Err(Report::new(MacroAuthorizationError::InvalidCredentials)),
        }
    }
}

fn no_op_properties_service() -> TestPropertiesService {
    PropertiesServiceImpl::new(
        MockPropertiesRepo::new(),
        None::<MockPermissionService>,
        None::<MockNotificationService>,
    )
}

fn authorization_state() -> MacroAuthorizationState<TestAuthorizationService> {
    let service = MacroAuthorizationServiceImpl::new(
        FakeJwtValidator,
        InternalAuthConfig {
            api_key: INTERNAL_API_KEY.to_string(),
            default_user_id: Some(DEFAULT_INTERNAL_USER_ID.to_string()),
        },
        macro_authorization::NoBotAuthorizer,
        macro_authorization::NoUserApiKeyAuthorizer,
    );
    MacroAuthorizationState::new(Arc::new(service))
}

fn test_router(entity_access_service: FakeEntityAccessService) -> Router {
    let state = PropertiesRouterState::new(
        Arc::new(no_op_properties_service()),
        Arc::new(entity_access_service),
        authorization_state(),
    );

    Router::new()
        .route("/required", get(required_auth_handler))
        .route("/team", get(team_handler))
        .route("/view/{entity_type}/{entity_id}", get(view_handler))
        .route("/edit/{entity_type}/{entity_id}", get(edit_handler))
        .with_state(state)
}

async fn required_auth_handler(
    authorization: MacroAuthorizationExtractor<TestAuthorizationService, UserOrInternal>,
) -> String {
    authorization.authorization.user.macro_user_id.to_string()
}

async fn team_handler(
    team: PropertyTeamExtractor<FakeEntityAccessService, TestAuthorizationService>,
) -> &'static str {
    if team.entity_access_receipt.is_some() {
        "team"
    } else {
        "no-team"
    }
}

async fn view_handler(ViewReceiptExtractor(receipt): ViewReceiptExtractor) -> &'static str {
    match receipt.auth() {
        EntityAccessAuth::Authenticated(_) => "authenticated",
        EntityAccessAuth::Unauthenticated => "unauthenticated",
        EntityAccessAuth::Bot(_) => "bot",
        EntityAccessAuth::Internal => "internal",
    }
}

async fn edit_handler(EditReceiptExtractor(_receipt): EditReceiptExtractor) -> StatusCode {
    StatusCode::OK
}

fn request(uri: &str) -> Request<Body> {
    Request::builder()
        .uri(uri)
        .body(Body::empty())
        .expect("request should be valid")
}

fn bearer_request(uri: &str, token: &str) -> Request<Body> {
    Request::builder()
        .uri(uri)
        .header(header::AUTHORIZATION, format!("Bearer {token}"))
        .body(Body::empty())
        .expect("request should be valid")
}

async fn response_body(response: axum::response::Response) -> String {
    let body = to_bytes(response.into_body(), usize::MAX)
        .await
        .expect("response body should be readable");
    String::from_utf8(body.to_vec()).expect("response body should be UTF-8")
}

#[tokio::test]
async fn required_auth_rejects_bad_credentials_and_accepts_valid_bearer() {
    let router = test_router(FakeEntityAccessService::default());

    let response = router
        .clone()
        .oneshot(request("/required"))
        .await
        .expect("request should complete");
    assert_eq!(response.status(), StatusCode::UNAUTHORIZED);
    assert_eq!(
        response_body(response).await,
        r#"{"message":"unauthorized"}"#
    );

    let response = router
        .clone()
        .oneshot(bearer_request("/required", "invalid"))
        .await
        .expect("request should complete");
    assert_eq!(response.status(), StatusCode::UNAUTHORIZED);
    assert_eq!(
        response_body(response).await,
        r#"{"message":"unauthorized"}"#
    );

    let response = router
        .clone()
        .oneshot(bearer_request("/required", "expired"))
        .await
        .expect("request should complete");
    assert_eq!(response.status(), StatusCode::UNAUTHORIZED);
    assert_eq!(
        response_body(response).await,
        r#"{"message":"jwt expired"}"#
    );

    let response = router
        .oneshot(bearer_request("/required", "valid"))
        .await
        .expect("request should complete");
    assert_eq!(response.status(), StatusCode::OK);
    assert_eq!(response_body(response).await, VALID_USER_ID);
}

#[tokio::test]
async fn property_team_extractor_uses_authorized_user() {
    let entity_access_service = FakeEntityAccessService::default();
    let response = test_router(entity_access_service.clone())
        .oneshot(bearer_request("/team", "valid"))
        .await
        .expect("request should complete");

    assert_eq!(response.status(), StatusCode::OK);
    assert_eq!(response_body(response).await, "no-team");
    assert_eq!(
        entity_access_service.calls(),
        [AccessCall::UserTeam {
            user_id: VALID_USER_ID.to_string(),
        }]
    );
}

#[allow(deprecated)]
#[tokio::test]
async fn standard_and_legacy_internal_headers_use_the_default_user() {
    for key_header in [INTERNAL_API_KEY_HEADER, LEGACY_DSS_INTERNAL_API_KEY_HEADER] {
        let request = Request::builder()
            .uri("/required")
            .header(key_header, INTERNAL_API_KEY)
            .body(Body::empty())
            .expect("request should be valid");
        let response = test_router(FakeEntityAccessService::default())
            .oneshot(request)
            .await
            .expect("request should complete");

        assert_eq!(response.status(), StatusCode::OK);
        assert_eq!(response_body(response).await, DEFAULT_INTERNAL_USER_ID);
    }
}

#[tokio::test]
async fn anonymous_view_mints_public_receipt_but_invalid_token_is_rejected() {
    let entity_access_service = FakeEntityAccessService::default();
    let router = test_router(entity_access_service.clone());

    let response = router
        .clone()
        .oneshot(request("/view/DOCUMENT/public-document"))
        .await
        .expect("request should complete");
    assert_eq!(response.status(), StatusCode::OK);
    assert_eq!(response_body(response).await, "unauthenticated");
    assert_eq!(
        entity_access_service.calls(),
        [AccessCall::PublicAccess {
            entity_id: "public-document".to_string(),
            entity_type: EntityType::Document,
            required_level: AccessLevel::View,
        }]
    );

    let response = router
        .oneshot(bearer_request("/view/DOCUMENT/public-document", "invalid"))
        .await
        .expect("request should complete");
    assert_eq!(response.status(), StatusCode::UNAUTHORIZED);
    assert_eq!(response_body(response).await, "unauthorized");
    assert_eq!(
        entity_access_service.calls().len(),
        1,
        "invalid credentials must not fall back to public authorization"
    );
}

#[tokio::test]
async fn edit_receipt_omits_organization_from_access_check() {
    let entity_access_service = FakeEntityAccessService::default();
    let response = test_router(entity_access_service.clone())
        .oneshot(bearer_request("/edit/DOCUMENT/document-id", "valid"))
        .await
        .expect("request should complete");

    assert_eq!(response.status(), StatusCode::OK);
    assert_eq!(
        entity_access_service.calls(),
        [AccessCall::GenerateReceipt {
            user_id: VALID_USER_ID.to_string(),
            organization_id: None,
            entity_id: "document-id".to_string(),
            entity_type: EntityType::Document,
        }]
    );
}

#[tokio::test]
async fn receipt_rejection_preserves_expired_token_message() {
    let response = test_router(FakeEntityAccessService::default())
        .oneshot(bearer_request("/edit/DOCUMENT/document-id", "expired"))
        .await
        .expect("request should complete");

    assert_eq!(response.status(), StatusCode::UNAUTHORIZED);
    assert_eq!(response_body(response).await, "jwt expired");
}

#[tokio::test]
async fn malformed_typed_path_is_rejected_before_missing_authentication() {
    let response = test_router(FakeEntityAccessService::default())
        .oneshot(request("/edit/not-an-entity/document-id"))
        .await
        .expect("request should complete");

    assert_eq!(response.status(), StatusCode::BAD_REQUEST);
    assert_eq!(
        response_body(response).await,
        "Missing or invalid entity_type / entity_id in path"
    );
}

fn multi_select_def(
    id: Uuid,
) -> models_properties::service::property_definition::PropertyDefinition {
    models_properties::service::property_definition::PropertyDefinition {
        id,
        owner: models_properties::PropertyOwner::System,
        display_name: "Tags".to_string(),
        data_type: models_properties::DataType::SelectString,
        is_multi_select: true,
        specific_entity_type: None,
        created_at: chrono::Utc::now(),
        updated_at: chrono::Utc::now(),
        is_system: false,
        is_metadata: false,
    }
}

/// Builds the full properties router over a mock-repo service so the bulk
/// cross-entity endpoint can be exercised end to end.
fn properties_router(
    service: TestPropertiesService,
    entity_access_service: FakeEntityAccessService,
) -> Router {
    let state = PropertiesRouterState::new(
        Arc::new(service),
        Arc::new(entity_access_service),
        authorization_state(),
    );
    super::router::<TestPropertiesService, FakeEntityAccessService, TestAuthorizationService>()
        .with_state(state)
}

#[tokio::test]
async fn bulk_options_across_entities_skips_denied_and_applies_granted() {
    use crate::domain::model::EntityPropertyOptionSelection;

    let property_id = Uuid::new_v4();
    let option_id = Uuid::new_v4();

    let mut repo = MockPropertiesRepo::new();
    repo.expect_get_property_definition()
        .returning(move |_| Box::pin(async move { Ok(Some(multi_select_def(property_id))) }));
    repo.expect_count_valid_property_options()
        .returning(|_, _| Box::pin(async { Ok(1) }));
    // Only the granted entity reaches the write path.
    repo.expect_bulk_update_entity_property_options()
        .times(1)
        .withf(|entity_id, _, _| entity_id == "doc-granted")
        .returning(move |_, _, _| {
            Box::pin(async move {
                Ok(vec![EntityPropertyOptionSelection {
                    property_definition_id: property_id,
                    option_ids: vec![option_id],
                    mutation: None,
                }])
            })
        });

    let service = PropertiesServiceImpl::new(
        repo,
        None::<MockPermissionService>,
        None::<MockNotificationService>,
    );
    let router = properties_router(service, FakeEntityAccessService::default());

    let body = serde_json::json!({
        "entities": [
            {"entity_type": "DOCUMENT", "entity_id": "doc-denied"},
            {"entity_type": "DOCUMENT", "entity_id": "doc-granted"},
        ],
        "property_id": property_id,
        "add_option_ids": [option_id],
        "remove_option_ids": [],
    });
    let request = Request::builder()
        .method("POST")
        .uri("/options/bulk")
        .header(header::AUTHORIZATION, "Bearer valid")
        .header(header::CONTENT_TYPE, "application/json")
        .body(Body::from(body.to_string()))
        .expect("request should be valid");

    let response = router
        .oneshot(request)
        .await
        .expect("request should complete");
    assert_eq!(response.status(), StatusCode::OK);

    let json: serde_json::Value =
        serde_json::from_str(&response_body(response).await).expect("json body");
    let results = json["results"].as_array().expect("results array");
    assert_eq!(results.len(), 2, "one result per requested entity");

    // Results are returned in request order.
    assert_eq!(results[0]["entity_id"], "doc-denied");
    assert_eq!(results[0]["status"], "skipped_no_permission");
    assert_eq!(results[1]["entity_id"], "doc-granted");
    assert_eq!(results[1]["status"], "applied");
    assert_eq!(results[1]["option_ids"], serde_json::json!([option_id]));
}

/// A tag definition owned by `owner`, for the promote/merge endpoint tests.
fn tag_def(
    id: Uuid,
    owner: models_properties::PropertyOwner,
) -> models_properties::service::property_definition::PropertyDefinition {
    models_properties::service::property_definition::PropertyDefinition {
        id,
        owner,
        display_name: "Tags".to_string(),
        data_type: models_properties::DataType::Tag,
        is_multi_select: true,
        specific_entity_type: None,
        created_at: chrono::Utc::now(),
        updated_at: chrono::Utc::now(),
        is_system: false,
        is_metadata: false,
    }
}

fn tag_option(
    id: Uuid,
    property_definition_id: Uuid,
    value: &str,
    color: &str,
) -> models_properties::service::property_option::PropertyOption {
    models_properties::service::property_option::PropertyOption {
        id,
        property_definition_id,
        display_order: 0,
        value: models_properties::service::property_option::PropertyOptionValue::String(
            value.to_string(),
        ),
        color: Some(color.to_string()),
        created_at: chrono::Utc::now(),
        updated_at: chrono::Utc::now(),
    }
}

/// Wire a mock repo up to the reads promote makes before it writes.
fn tag_promotion_repo(
    personal_definition_id: Uuid,
    team_definition_id: Uuid,
    option_id: Uuid,
) -> MockPropertiesRepo {
    let mut repo = MockPropertiesRepo::new();
    repo.expect_get_property_option().returning(move |_| {
        Box::pin(async move {
            Ok(Some(tag_option(
                option_id,
                personal_definition_id,
                "Urgent",
                "#ff0000",
            )))
        })
    });
    repo.expect_get_tag_definition().returning(move |_| {
        Box::pin(async move {
            Ok(Some(tag_def(
                personal_definition_id,
                models_properties::PropertyOwner::User {
                    user_id: VALID_USER_ID.to_string(),
                },
            )))
        })
    });
    repo.expect_get_or_create_tag_definition()
        .returning(move |_| {
            Box::pin(async move {
                Ok(crate::domain::model::GetOrCreateTagDefinitionResult {
                    definition: tag_def(
                        team_definition_id,
                        models_properties::PropertyOwner::Team {
                            team_id: Uuid::from_u128(0x7EA3),
                        },
                    ),
                    created: false,
                })
            })
        });
    repo
}

fn json_post(uri: &str, body: serde_json::Value) -> Request<Body> {
    Request::builder()
        .method("POST")
        .uri(uri)
        .header(header::AUTHORIZATION, "Bearer valid")
        .header(header::CONTENT_TYPE, "application/json")
        .body(Body::from(body.to_string()))
        .expect("request should be valid")
}

#[tokio::test]
async fn promote_tag_conflict_returns_the_team_label_the_caller_can_merge_into() {
    let personal_definition_id = Uuid::from_u128(0x9001);
    let team_definition_id = Uuid::from_u128(0x9002);
    let option_id = Uuid::from_u128(0x9003);
    let conflict_id = Uuid::from_u128(0x9004);

    let mut repo = tag_promotion_repo(personal_definition_id, team_definition_id, option_id);
    repo.expect_promote_tag_option().returning(move |_, _, _| {
        Box::pin(async move {
            Ok(crate::domain::model::TagPromotionOutcome::Conflict(
                tag_option(conflict_id, team_definition_id, "urgent", "#00ff00"),
            ))
        })
    });

    let service = PropertiesServiceImpl::new(
        repo,
        None::<MockPermissionService>,
        None::<MockNotificationService>,
    );
    let router = properties_router(
        service,
        FakeEntityAccessService::on_team(Uuid::from_u128(0x7EA3)),
    );

    let response = router
        .oneshot(json_post(
            "/tags/promote",
            serde_json::json!({ "option_id": option_id }),
        ))
        .await
        .expect("request should complete");
    assert_eq!(response.status(), StatusCode::CONFLICT);

    // The front end prompts with this label, then confirms via /tags/merge.
    let json: serde_json::Value =
        serde_json::from_str(&response_body(response).await).expect("json body");
    assert_eq!(json["conflicting_option"]["id"], conflict_id.to_string());
    assert_eq!(
        json["conflicting_option"]["value"],
        serde_json::json!({"type": "string", "value": "urgent"})
    );
    assert_eq!(json["conflicting_option"]["color"], "#00ff00");
    assert!(json["message"].is_string());
}

#[tokio::test]
async fn promote_tag_returns_the_label_hanging_off_the_team_definition() {
    let personal_definition_id = Uuid::from_u128(0x9101);
    let team_definition_id = Uuid::from_u128(0x9102);
    let option_id = Uuid::from_u128(0x9103);

    let mut repo = tag_promotion_repo(personal_definition_id, team_definition_id, option_id);
    repo.expect_promote_tag_option().returning(move |_, _, _| {
        Box::pin(async move {
            Ok(crate::domain::model::TagPromotionOutcome::Promoted(
                crate::domain::model::TagRemapOutcome {
                    option: tag_option(option_id, team_definition_id, "Urgent", "#ff0000"),
                    mutations: Vec::new(),
                },
            ))
        })
    });

    let service = PropertiesServiceImpl::new(
        repo,
        None::<MockPermissionService>,
        None::<MockNotificationService>,
    );
    let router = properties_router(
        service,
        FakeEntityAccessService::on_team(Uuid::from_u128(0x7EA3)),
    );

    let response = router
        .oneshot(json_post(
            "/tags/promote",
            serde_json::json!({ "option_id": option_id }),
        ))
        .await
        .expect("request should complete");
    assert_eq!(response.status(), StatusCode::OK);

    let json: serde_json::Value =
        serde_json::from_str(&response_body(response).await).expect("json body");
    assert_eq!(json["id"], option_id.to_string());
    assert_eq!(
        json["propertyDefinitionId"],
        team_definition_id.to_string(),
        "the option id survives, the owning definition is the team's"
    );
}

#[tokio::test]
async fn promote_tag_without_a_team_is_forbidden() {
    let personal_definition_id = Uuid::from_u128(0x9201);
    let option_id = Uuid::from_u128(0x9202);

    let mut repo = MockPropertiesRepo::new();
    repo.expect_get_property_option().returning(move |_| {
        Box::pin(async move {
            Ok(Some(tag_option(
                option_id,
                personal_definition_id,
                "Urgent",
                "#ff0000",
            )))
        })
    });
    repo.expect_get_tag_definition().returning(move |_| {
        Box::pin(async move {
            Ok(Some(tag_def(
                personal_definition_id,
                models_properties::PropertyOwner::User {
                    user_id: VALID_USER_ID.to_string(),
                },
            )))
        })
    });
    repo.expect_promote_tag_option().never();

    let service = PropertiesServiceImpl::new(
        repo,
        None::<MockPermissionService>,
        None::<MockNotificationService>,
    );
    let router = properties_router(service, FakeEntityAccessService::default());

    let response = router
        .oneshot(json_post(
            "/tags/promote",
            serde_json::json!({ "option_id": option_id }),
        ))
        .await
        .expect("request should complete");
    assert_eq!(response.status(), StatusCode::FORBIDDEN);
}

const MANAGED_NAME: &str = "Deal Stage";

fn managed_router(
    service: TestPropertiesService,
    entity_access_service: FakeEntityAccessService,
) -> Router {
    let state = PropertiesRouterState::new(
        Arc::new(service),
        Arc::new(entity_access_service),
        authorization_state(),
    )
    .with_managed_team_definitions([MANAGED_NAME.to_string()]);
    super::router::<TestPropertiesService, FakeEntityAccessService, TestAuthorizationService>()
        .with_state(state)
}

fn select_def(
    id: Uuid,
    owner: models_properties::PropertyOwner,
    display_name: &str,
) -> models_properties::service::property_definition::PropertyDefinition {
    models_properties::service::property_definition::PropertyDefinition {
        id,
        owner,
        display_name: display_name.to_string(),
        data_type: models_properties::DataType::SelectString,
        is_multi_select: false,
        specific_entity_type: None,
        created_at: chrono::Utc::now(),
        updated_at: chrono::Utc::now(),
        is_system: false,
        is_metadata: false,
    }
}

fn repo_with_definition(
    definition: models_properties::service::property_definition::PropertyDefinition,
) -> MockPropertiesRepo {
    let mut repo = MockPropertiesRepo::new();
    let by_id = definition.clone();
    repo.expect_get_property_definition().returning(move |_| {
        let definition = by_id.clone();
        Box::pin(async move { Ok(Some(definition)) })
    });
    repo.expect_get_property_definition_with_owner()
        .returning(move |_, _, _| {
            let definition = definition.clone();
            Box::pin(async move { Ok(Some(definition)) })
        });
    repo
}

fn json_request(method: &str, uri: &str, body: serde_json::Value) -> Request<Body> {
    Request::builder()
        .method(method)
        .uri(uri)
        .header(header::AUTHORIZATION, "Bearer valid")
        .header(header::CONTENT_TYPE, "application/json")
        .body(Body::from(body.to_string()))
        .expect("request should be valid")
}

#[tokio::test]
async fn creating_a_managed_team_definition_is_forbidden() {
    let router = managed_router(
        no_op_properties_service(),
        FakeEntityAccessService::on_team(Uuid::from_u128(0x7EA3)),
    );
    let body = serde_json::json!({
        "scope": "team",
        "display_name": MANAGED_NAME,
        "data_type": { "type": "select_string", "options": [], "multi": false },
    });
    let response = router
        .oneshot(json_request("POST", "/definitions", body))
        .await
        .expect("request should complete");
    assert_eq!(response.status(), StatusCode::FORBIDDEN);
}

#[tokio::test]
async fn writes_to_a_managed_team_definition_are_forbidden() {
    let definition_id = Uuid::from_u128(0xD001);
    let option_id = Uuid::from_u128(0xD002);
    let team_id = Uuid::from_u128(0x7EA3);
    let definition = select_def(
        definition_id,
        models_properties::PropertyOwner::Team { team_id },
        MANAGED_NAME,
    );
    let option_body = serde_json::json!({
        "type": "select_string",
        "option": { "value": "Renewal", "display_order": 0 },
    });
    let requests = [
        json_request(
            "POST",
            &format!("/definitions/{definition_id}/options"),
            option_body,
        ),
        json_request(
            "PATCH",
            &format!("/definitions/{definition_id}/options/{option_id}"),
            serde_json::json!({ "value": "Renamed" }),
        ),
        json_request(
            "DELETE",
            &format!("/definitions/{definition_id}/options/{option_id}"),
            serde_json::json!({}),
        ),
        json_request(
            "DELETE",
            &format!("/definitions/{definition_id}"),
            serde_json::json!({}),
        ),
    ];
    for request in requests {
        let service = PropertiesServiceImpl::new(
            repo_with_definition(definition.clone()),
            None::<MockPermissionService>,
            None::<MockNotificationService>,
        );
        let router = managed_router(service, FakeEntityAccessService::on_team(team_id));
        let response = router
            .oneshot(request)
            .await
            .expect("request should complete");
        assert_eq!(response.status(), StatusCode::FORBIDDEN);
    }
}

#[tokio::test]
async fn the_guard_only_covers_team_definitions_with_a_managed_name() {
    let definition_id = Uuid::from_u128(0xD003);
    let team_id = Uuid::from_u128(0x7EA3);
    let user = MacroUserIdStr::try_from(VALID_USER_ID.to_string()).expect("valid user id");
    let cases = [
        (
            models_properties::PropertyOwner::User {
                user_id: VALID_USER_ID.to_string(),
            },
            MANAGED_NAME,
            true,
        ),
        (
            models_properties::PropertyOwner::Team { team_id },
            "Region",
            true,
        ),
        (
            models_properties::PropertyOwner::Team { team_id },
            MANAGED_NAME,
            false,
        ),
    ];
    for (owner, name, allowed) in cases {
        let service = PropertiesServiceImpl::new(
            repo_with_definition(select_def(definition_id, owner.clone(), name)),
            None::<MockPermissionService>,
            None::<MockNotificationService>,
        );
        let state = PropertiesRouterState::new(
            Arc::new(service),
            Arc::new(FakeEntityAccessService::on_team(team_id)),
            authorization_state(),
        )
        .with_managed_team_definitions([MANAGED_NAME.to_string()]);
        let result = state
            .reject_managed_definition(definition_id, &user, None)
            .await;
        assert_eq!(result.is_ok(), allowed, "{name} owned by {owner:?}");
    }
}