141 lines
4.9 KiB
Rust
141 lines
4.9 KiB
Rust
use super::*;
|
|
|
|
use admin_api::error::ApiError;
|
|
use crank_artifacts::MAX_ARTIFACT_BYTES;
|
|
use crank_core::HttpMethod;
|
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
struct CatalogCounts {
|
|
operations: i64,
|
|
operation_versions: i64,
|
|
auth_profiles: i64,
|
|
agent_operation_bindings: i64,
|
|
}
|
|
|
|
async fn catalog_counts(
|
|
registry: &crank_registry::PostgresRegistry,
|
|
workspace_id: &WorkspaceId,
|
|
) -> CatalogCounts {
|
|
let (operations, operation_versions, auth_profiles, agent_operation_bindings) =
|
|
sqlx::query_as::<_, (i64, i64, i64, i64)>(
|
|
"select
|
|
(select count(*) from operations where workspace_id = $1),
|
|
(select count(*) from operation_versions ov
|
|
join operations o on o.id = ov.operation_id where o.workspace_id = $1),
|
|
(select count(*) from auth_profiles where workspace_id = $1),
|
|
(select count(*) from agent_operation_bindings b
|
|
join operations o on o.id = b.operation_id where o.workspace_id = $1)",
|
|
)
|
|
.bind(workspace_id.as_str())
|
|
.fetch_one(registry.pool())
|
|
.await
|
|
.unwrap();
|
|
|
|
CatalogCounts {
|
|
operations,
|
|
operation_versions,
|
|
auth_profiles,
|
|
agent_operation_bindings,
|
|
}
|
|
}
|
|
|
|
fn upload(bytes: Vec<u8>) -> OpenApiUpload {
|
|
OpenApiUpload {
|
|
bytes,
|
|
mime_type: "application/json".to_owned(),
|
|
locale: OpenApiUploadLocale::En,
|
|
}
|
|
}
|
|
|
|
fn assert_upload_error(error: ApiError, variant: &str, code: &str) {
|
|
let context = match (variant, error) {
|
|
("validation", ApiError::Validation { context, .. }) => context,
|
|
("payload_too_large", ApiError::PayloadTooLarge { context, .. }) => context,
|
|
(expected, actual) => panic!("expected {expected} error, got {actual:?}"),
|
|
}
|
|
.expect("OpenAPI upload errors carry a machine-readable code");
|
|
assert_eq!(context["error_code"], format!("openapi_upload.{code}"));
|
|
}
|
|
|
|
#[tokio::test]
|
|
#[serial]
|
|
async fn preview_outcomes_do_not_mutate_operation_catalog() {
|
|
let registry = test_registry().await;
|
|
let service = test_service(
|
|
registry.clone(),
|
|
test_storage_root("openapi_upload_outcomes"),
|
|
test_auth_settings(),
|
|
test_secret_crypto(),
|
|
);
|
|
let workspace_id = WorkspaceId::new("ws_default");
|
|
|
|
let before = catalog_counts(®istry, &workspace_id).await;
|
|
let preview = service
|
|
.preview_openapi_import(
|
|
&workspace_id,
|
|
upload(
|
|
br#"{
|
|
"openapi":"3.0.3",
|
|
"info":{"title":"Upload outcomes","version":"1"},
|
|
"paths":{"/ready":{"get":{"responses":{"200":{"description":"ok"}}}}}
|
|
}"#
|
|
.to_vec(),
|
|
),
|
|
)
|
|
.await
|
|
.expect("valid OpenAPI document should return a preview");
|
|
let candidates = preview
|
|
.preview
|
|
.groups
|
|
.iter()
|
|
.flat_map(|group| &group.operations)
|
|
.collect::<Vec<_>>();
|
|
assert_eq!(candidates.len(), 1);
|
|
assert_eq!(candidates[0].method, HttpMethod::Get);
|
|
assert_eq!(candidates[0].path, "/ready");
|
|
assert_eq!(candidates[0].key, "GET /ready");
|
|
assert_eq!(catalog_counts(®istry, &workspace_id).await, before);
|
|
|
|
let before = catalog_counts(®istry, &workspace_id).await;
|
|
let error = service
|
|
.preview_openapi_import(&workspace_id, upload(Vec::new()))
|
|
.await
|
|
.expect_err("empty upload should be rejected");
|
|
assert_upload_error(error, "validation", "empty_file");
|
|
assert_eq!(catalog_counts(®istry, &workspace_id).await, before);
|
|
|
|
let before = catalog_counts(®istry, &workspace_id).await;
|
|
let error = service
|
|
.preview_openapi_import(&workspace_id, upload(b"{".to_vec()))
|
|
.await
|
|
.expect_err("malformed OpenAPI document should be rejected");
|
|
assert_upload_error(error, "validation", "invalid_document");
|
|
assert_eq!(catalog_counts(®istry, &workspace_id).await, before);
|
|
|
|
let before = catalog_counts(®istry, &workspace_id).await;
|
|
let error = service
|
|
.preview_openapi_import(&workspace_id, upload(vec![b' '; MAX_ARTIFACT_BYTES + 1]))
|
|
.await
|
|
.expect_err("oversized upload should be rejected");
|
|
assert_upload_error(error, "payload_too_large", "file_too_large");
|
|
assert_eq!(catalog_counts(®istry, &workspace_id).await, before);
|
|
|
|
let before = catalog_counts(®istry, &workspace_id).await;
|
|
let error = service
|
|
.preview_openapi_import(
|
|
&workspace_id,
|
|
upload(
|
|
br#"{
|
|
"openapi":"3.0.3",
|
|
"info":{"title":"No methods","version":"1"},
|
|
"paths":{}
|
|
}"#
|
|
.to_vec(),
|
|
),
|
|
)
|
|
.await
|
|
.expect_err("document without supported methods should be rejected");
|
|
assert_upload_error(error, "validation", "no_methods");
|
|
assert_eq!(catalog_counts(®istry, &workspace_id).await, before);
|
|
}
|