299 lines
8.6 KiB
Rust
299 lines
8.6 KiB
Rust
use super::*;
|
|
|
|
use crank_core::Target;
|
|
use serde_json::json;
|
|
|
|
#[tokio::test]
|
|
#[serial]
|
|
async fn http_contract_preview_apply_and_replay_preserve_json_multipart_schemas_mapping_and_mime() {
|
|
let registry = test_registry().await;
|
|
let service = test_service(
|
|
registry.clone(),
|
|
test_storage_root("openapi_import_http_contract_replay"),
|
|
test_auth_settings(),
|
|
test_secret_crypto(),
|
|
);
|
|
let workspace_id = WorkspaceId::new("ws_default");
|
|
let preview = service
|
|
.preview_openapi_import(&workspace_id, rich_http_contract_upload())
|
|
.await
|
|
.unwrap();
|
|
let job_id: crank_registry::ImportJobId = preview.job_id.as_str().into();
|
|
let persisted_preview_before_apply = registry
|
|
.get_import_job(&workspace_id, &job_id)
|
|
.await
|
|
.unwrap()
|
|
.unwrap()
|
|
.preview_payload["preview"]
|
|
.clone();
|
|
assert_eq!(
|
|
persisted_preview_before_apply,
|
|
serde_json::to_value(&preview.preview).unwrap()
|
|
);
|
|
|
|
let operations = preview
|
|
.preview
|
|
.groups
|
|
.iter()
|
|
.flat_map(|group| group.operations.iter())
|
|
.collect::<Vec<_>>();
|
|
assert_eq!(operations.len(), 1, "media variants must not split a Tool");
|
|
let candidate = operations[0];
|
|
assert_eq!(candidate.key, "POST /documents");
|
|
assert!(
|
|
candidate.findings.iter().all(|finding| {
|
|
finding.severity != crank_import::rest::ImportFindingSeverity::Error
|
|
})
|
|
);
|
|
|
|
let json_arguments = json!({
|
|
"content_type": "application/json",
|
|
"body": {"title": "Quarterly report"}
|
|
});
|
|
let multipart_arguments = json!({
|
|
"content_type": "multipart/form-data",
|
|
"body": {
|
|
"file": {
|
|
"data_base64": "AA==",
|
|
"filename": "report.png",
|
|
"content_type": "image/png"
|
|
},
|
|
"caption": "Quarterly report"
|
|
}
|
|
});
|
|
candidate
|
|
.draft
|
|
.input_schema
|
|
.validate_shape(&json_arguments)
|
|
.unwrap();
|
|
candidate
|
|
.draft
|
|
.input_schema
|
|
.validate_shape(&multipart_arguments)
|
|
.unwrap();
|
|
assert!(
|
|
candidate
|
|
.draft
|
|
.input_schema
|
|
.validate_shape(&json!({
|
|
"content_type": "multipart/form-data",
|
|
"body": {"title": "wrong branch"}
|
|
}))
|
|
.is_err()
|
|
);
|
|
|
|
let mapped = candidate
|
|
.draft
|
|
.input_mapping
|
|
.apply(&json!({"mcp": multipart_arguments}))
|
|
.unwrap();
|
|
assert_eq!(
|
|
mapped["request"]["body_content_type"],
|
|
"multipart/form-data"
|
|
);
|
|
assert_eq!(mapped["request"]["body"]["file"]["data_base64"], "AA==");
|
|
|
|
let preview_target = &candidate.draft.target;
|
|
let preview_contract = preview_target.request_contract.as_ref().unwrap();
|
|
assert_eq!(preview_contract.body_field, "body");
|
|
assert_eq!(
|
|
preview_contract.selector_field.as_deref(),
|
|
Some("content_type")
|
|
);
|
|
assert_eq!(preview_contract.variants.len(), 2);
|
|
assert_eq!(
|
|
preview_target.multipart_field_mime_types["multipart/form-data"]["file"],
|
|
["image/png", "image/jpeg"]
|
|
);
|
|
|
|
let payload = OpenApiImportCreatePayload {
|
|
selected_operation_keys: vec![candidate.key.clone()],
|
|
auth_mappings: vec![],
|
|
server_url: None,
|
|
conflict_mode: "skip".to_owned(),
|
|
};
|
|
let created = service
|
|
.create_openapi_import(&workspace_id, &job_id, payload.clone())
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(created.created.len(), 1);
|
|
|
|
let replayed = service
|
|
.create_openapi_import(&workspace_id, &job_id, payload)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(replayed.created, created.created);
|
|
let persisted_preview_after_replay = registry
|
|
.get_import_job(&workspace_id, &job_id)
|
|
.await
|
|
.unwrap()
|
|
.unwrap()
|
|
.preview_payload["preview"]
|
|
.clone();
|
|
assert_eq!(
|
|
persisted_preview_after_replay,
|
|
persisted_preview_before_apply
|
|
);
|
|
|
|
let version = service
|
|
.get_operation_version(
|
|
&workspace_id,
|
|
&created.created[0].operation_id.as_str().into(),
|
|
created.created[0].version,
|
|
)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(version.snapshot.input_schema, candidate.draft.input_schema);
|
|
assert_eq!(
|
|
version.snapshot.output_schema,
|
|
candidate.draft.output_schema
|
|
);
|
|
assert_eq!(
|
|
version.snapshot.input_mapping,
|
|
candidate.draft.input_mapping
|
|
);
|
|
assert_eq!(
|
|
version.snapshot.output_mapping,
|
|
candidate.draft.output_mapping
|
|
);
|
|
assert_eq!(
|
|
version.snapshot.target,
|
|
Target::Rest(candidate.draft.target.clone())
|
|
);
|
|
}
|
|
|
|
#[tokio::test]
|
|
#[serial]
|
|
async fn xml_and_unsupported_media_block_apply_without_registry_mutation() {
|
|
let registry = test_registry().await;
|
|
let service = test_service(
|
|
registry.clone(),
|
|
test_storage_root("openapi_import_http_contract_blockers"),
|
|
test_auth_settings(),
|
|
test_secret_crypto(),
|
|
);
|
|
let workspace_id = WorkspaceId::new("ws_default");
|
|
let preview = service
|
|
.preview_openapi_import(&workspace_id, unsupported_media_upload())
|
|
.await
|
|
.unwrap();
|
|
let candidate = preview
|
|
.preview
|
|
.groups
|
|
.iter()
|
|
.flat_map(|group| group.operations.iter())
|
|
.find(|operation| operation.key == "POST /blocked")
|
|
.unwrap();
|
|
assert!(candidate.findings.iter().any(|finding| {
|
|
finding.code == "unsupported_xml_request_media_type"
|
|
&& finding.severity == crank_import::rest::ImportFindingSeverity::Error
|
|
}));
|
|
assert!(candidate.findings.iter().any(|finding| {
|
|
finding.code == "unsupported_request_media_type"
|
|
&& finding.severity == crank_import::rest::ImportFindingSeverity::Error
|
|
}));
|
|
|
|
assert!(
|
|
service
|
|
.create_openapi_import(
|
|
&workspace_id,
|
|
&preview.job_id.as_str().into(),
|
|
OpenApiImportCreatePayload {
|
|
selected_operation_keys: vec![candidate.key.clone()],
|
|
auth_mappings: vec![],
|
|
server_url: None,
|
|
conflict_mode: "skip".to_owned(),
|
|
},
|
|
)
|
|
.await
|
|
.is_err()
|
|
);
|
|
assert!(
|
|
service
|
|
.list_operations(&workspace_id)
|
|
.await
|
|
.unwrap()
|
|
.is_empty()
|
|
);
|
|
|
|
let job = registry
|
|
.get_import_job(&workspace_id, &preview.job_id.as_str().into())
|
|
.await
|
|
.unwrap()
|
|
.unwrap();
|
|
assert_eq!(job.status, ImportJobStatus::Failed);
|
|
assert_eq!(
|
|
job.error_text.as_deref(),
|
|
Some("reference_resolution_blocked")
|
|
);
|
|
}
|
|
|
|
fn rich_http_contract_upload() -> OpenApiUpload {
|
|
OpenApiUpload {
|
|
bytes: br#"
|
|
openapi: 3.0.3
|
|
info: { title: Document import }
|
|
servers: [{ url: https://api.example.test/v1 }]
|
|
paths:
|
|
/documents:
|
|
post:
|
|
operationId: importDocument
|
|
requestBody:
|
|
required: true
|
|
content:
|
|
application/json:
|
|
schema:
|
|
type: object
|
|
required: [title]
|
|
properties:
|
|
title: { type: string }
|
|
multipart/form-data:
|
|
schema:
|
|
type: object
|
|
required: [file]
|
|
properties:
|
|
file: { type: string, format: binary }
|
|
caption: { type: string }
|
|
encoding:
|
|
file: { contentType: 'image/png, image/jpeg' }
|
|
responses:
|
|
'201':
|
|
description: Created
|
|
content:
|
|
application/json:
|
|
schema: { type: object, properties: { id: { type: string } } }
|
|
"#
|
|
.to_vec(),
|
|
mime_type: "application/yaml".to_owned(),
|
|
locale: OpenApiUploadLocale::En,
|
|
}
|
|
}
|
|
|
|
fn unsupported_media_upload() -> OpenApiUpload {
|
|
OpenApiUpload {
|
|
bytes: br#"
|
|
openapi: 3.0.3
|
|
info: { title: Unsupported request media }
|
|
servers: [{ url: https://api.example.test/v1 }]
|
|
paths:
|
|
/blocked:
|
|
post:
|
|
operationId: blockedMedia
|
|
requestBody:
|
|
required: true
|
|
content:
|
|
application/json:
|
|
schema: { type: object, properties: { name: { type: string } } }
|
|
application/xml:
|
|
schema: { type: string }
|
|
application/pdf:
|
|
schema: { type: string, format: binary }
|
|
responses:
|
|
'200': { description: OK }
|
|
"#
|
|
.to_vec(),
|
|
mime_type: "application/yaml".to_owned(),
|
|
locale: OpenApiUploadLocale::En,
|
|
}
|
|
}
|