use std::{collections::BTreeMap, fs};
use axum::{
extract::{Path, State},
http::StatusCode,
response::{IntoResponse, Response},
Json,
};
use serde::Deserialize;
use serde_json::Value;
use crate::AppState;
#[derive(Clone, Default)]
pub struct Catalog {
documents: BTreeMap<String, String>,
selections: BTreeMap<String, Value>,
}
#[derive(Deserialize)]
struct CatalogConfig {
platforms: Vec<PlatformConfig>,
}
#[derive(Deserialize)]
struct PlatformConfig {
name: String,
openapi: String,
#[serde(default)]
overlays: Vec<String>,
#[serde(default)]
selection: Option<serde_json::Map<String, Value>>,
}
#[derive(Deserialize)]
struct Overlay {
#[serde(default)]
extends: Option<String>,
#[serde(default)]
info: Option<Value>,
actions: Vec<Action>,
}
#[derive(Deserialize)]
struct Action {
target: String,
update: Value,
}
impl Catalog {
#[cfg(test)]
pub(crate) fn from_test_document(platform: &str, document: Value, selection: Value) -> Self {
Self {
documents: [(
platform.to_owned(),
serde_yaml::to_string(&document).expect("test document serializes"),
)]
.into(),
selections: [(platform.to_owned(), selection)].into(),
}
}
#[cfg(test)]
pub(crate) fn for_test(platform: &str) -> Self {
Self {
documents: [(platform.into(), serde_json::json!({
"components": {"securitySchemes": {"fixture": {"type": "oauth2", "flows": {
"authorizationCode": {"authorizationUrl": "https://auth.example/authorize",
"tokenUrl": "https://auth.example/token", "scopes": {"read": "Read records"}}
}}}}, "security": [{"fixture": ["read"]}], "paths": {"/records": {"get": {}}}
}).to_string())].into(),
..Self::default()
}
}
pub async fn load(path: &str, client: &reqwest::Client) -> Result<Self, String> {
Self::load_with_mirror(path, client, None).await
}
#[cfg(test)]
pub(crate) async fn load_checked_in(client: &reqwest::Client) -> Result<Self, String> {
Self::load_checked_in_file(client, crate::config::DEFAULT_CATALOG_FILE).await
}
#[cfg(test)]
pub(crate) async fn load_checked_in_file(
client: &reqwest::Client,
file: &str,
) -> Result<Self, String> {
let overlays = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../overlays");
let catalog = overlays.join(file);
Self::load_with_mirror(&catalog.to_string_lossy(), client, Some(&overlays)).await
}
async fn load_with_mirror(
path: &str,
client: &reqwest::Client,
mirror: Option<&std::path::Path>,
) -> Result<Self, String> {
let source = if path.starts_with("https://") {
fetch_yaml(client, path).await?
} else {
fs::read_to_string(path).map_err(|err| format!("cannot read {path}: {err}"))?
};
let config = parse_catalog_config(&source, path)?;
let mut documents = BTreeMap::new();
let mut selections = BTreeMap::new();
for platform in config.platforms {
valid_platform_name(&platform.name)?;
let base = fetch_source(client, &platform.openapi, mirror).await?;
let mut document: Value = serde_yaml::from_str(&base)
.map_err(|err| format!("cannot parse OAD for {}: {err}", platform.name))?;
let mut infos = Vec::new();
for overlay_url in &platform.overlays {
let mut overlay: Overlay =
serde_yaml::from_str(&fetch_source(client, overlay_url, mirror).await?)
.map_err(|err| {
format!("cannot parse overlay for {}: {err}", platform.name)
})?;
infos.push((overlay_url.clone(), overlay.info.take()));
apply_overlay(&mut document, overlay, &platform.openapi, overlay_url)?;
}
let problems = overlay_requirement_problems(&platform.overlays, &infos)?;
if !problems.is_empty() {
return Err(format!(
"catalog selection for {} breaks Overlay Requirements: {}",
platform.name,
problems.join("; ")
));
}
refuse_ref_path_items(&platform.name, &document)?;
if let Some(selection) = platform.selection {
for problem in crate::pacing::plan_selection_problems(&document, &selection) {
tracing::warn!(platform = %platform.name, "{problem}");
}
selections.insert(platform.name.clone(), Value::Object(selection));
}
insert_document(
&mut documents,
&platform.name,
serde_yaml::to_string(&document).map_err(|err| err.to_string())?,
)?;
}
Ok(Self {
documents,
selections,
})
}
pub fn names(&self) -> Vec<String> {
self.documents.keys().cloned().collect()
}
pub fn allows(&self, platform: &str, method: &str, path: &str) -> Option<url::Url> {
let document: Value = serde_yaml::from_str(self.documents.get(platform)?).ok()?;
let server = document
.get("servers")?
.as_array()?
.first()?
.get("url")?
.as_str()?;
let server_url = url::Url::parse(server).ok()?;
let base_path = server_url.path().trim_end_matches('/');
let relative_path = path.strip_prefix(base_path)?;
if !relative_path.starts_with('/') {
return None;
}
let paths = document.get("paths")?.as_object()?;
let template = paths
.keys()
.find(|template| path_matches(template, relative_path))?;
let operation = paths.get(template)?.get(method.to_ascii_lowercase())?;
if !operation.is_object() || !self.profile_covers(platform, &document, operation) {
return None;
}
Some(server_url)
}
pub(crate) fn server_base_path(&self, platform: &str) -> Option<String> {
let document: Value = serde_yaml::from_str(self.documents.get(platform)?).ok()?;
let server = document
.get("servers")?
.as_array()?
.first()?
.get("url")?
.as_str()?;
Some(
url::Url::parse(server)
.ok()?
.path()
.trim_end_matches('/')
.to_owned(),
)
}
pub(crate) fn webhook_document(&self, platform: &str) -> Option<Value> {
let document: Value = serde_yaml::from_str(self.documents.get(platform)?).ok()?;
document.get("x-webhook-deliveries")?;
Some(document)
}
pub fn required_headers(
&self,
platform: &str,
method: &str,
path: &str,
) -> Option<Vec<(String, String)>> {
let document: Value = serde_yaml::from_str(self.documents.get(platform)?).ok()?;
let server = document
.get("servers")?
.as_array()?
.first()?
.get("url")?
.as_str()?;
let server_url = url::Url::parse(server).ok()?;
let relative = path.strip_prefix(server_url.path().trim_end_matches('/'))?;
let paths = document.get("paths")?.as_object()?;
let template = paths.keys().find(|t| path_matches(t, relative))?;
let path_item = paths.get(template)?.as_object()?;
let operation = path_item.get(&method.to_ascii_lowercase())?.as_object()?;
let mut out = Vec::new();
let mut seen = std::collections::BTreeSet::new();
for parameter in operation
.get("parameters")
.into_iter()
.chain(path_item.get("parameters"))
.flat_map(Value::as_array)
.flatten()
{
let parameter = if let Some(reference) = parameter.get("$ref").and_then(Value::as_str) {
document.pointer(reference.strip_prefix('#')?)?
} else {
parameter
};
if parameter.get("in").and_then(Value::as_str) != Some("header") {
continue;
}
let name = parameter.get("name").and_then(Value::as_str)?;
if !seen.insert(name.to_ascii_lowercase()) {
continue;
}
if name.eq_ignore_ascii_case("authorization") || name.eq_ignore_ascii_case("host") {
return None;
}
let value = parameter
.get("schema")
.and_then(|s| s.get("default"))
.or_else(|| {
parameter
.get("schema")
.and_then(|s| s.get("enum"))
.and_then(|e| e.as_array())
.filter(|a| a.len() == 1)
.and_then(|a| a.first())
});
let Some(value) = value.and_then(Value::as_str) else {
if parameter.get("required").and_then(Value::as_bool) == Some(true) {
return None;
} else {
continue;
}
};
out.push((name.to_owned(), value.to_owned()));
}
Some(out)
}
pub fn declares_header_parameter(
&self,
platform: &str,
method: &str,
path: &str,
name: &str,
) -> bool {
let declared = || -> Option<bool> {
let document: Value = serde_yaml::from_str(self.documents.get(platform)?).ok()?;
let server = document
.get("servers")?
.as_array()?
.first()?
.get("url")?
.as_str()?;
let server_url = url::Url::parse(server).ok()?;
let relative = path.strip_prefix(server_url.path().trim_end_matches('/'))?;
let paths = document.get("paths")?.as_object()?;
let template = paths.keys().find(|t| path_matches(t, relative))?;
let path_item = paths.get(template)?.as_object()?;
let operation = path_item.get(&method.to_ascii_lowercase())?.as_object()?;
Some(
operation
.get("parameters")
.into_iter()
.chain(path_item.get("parameters"))
.flat_map(Value::as_array)
.flatten()
.filter_map(|parameter| match parameter.get("$ref") {
Some(reference) => document.pointer(reference.as_str()?.strip_prefix('#')?),
None => Some(parameter),
})
.any(|parameter| {
parameter.get("in").and_then(Value::as_str) == Some("header")
&& parameter
.get("name")
.and_then(Value::as_str)
.is_some_and(|declared| declared.eq_ignore_ascii_case(name))
}),
)
};
declared().unwrap_or(false)
}
pub fn paced_buckets(
&self,
platform: &str,
method: &str,
path: &str,
) -> Vec<crate::pacing::PacedBucket> {
let resolve = || -> Option<Vec<crate::pacing::PacedBucket>> {
let document: Value = serde_yaml::from_str(self.documents.get(platform)?).ok()?;
document.get("x-throttling")?;
let server = document
.get("servers")?
.as_array()?
.first()?
.get("url")?
.as_str()?;
let server_url = url::Url::parse(server).ok()?;
let relative = path.strip_prefix(server_url.path().trim_end_matches('/'))?;
let paths = document.get("paths")?.as_object()?;
let template = paths.keys().find(|t| path_matches(t, relative))?;
let operation = paths.get(template)?.get(method.to_ascii_lowercase())?;
let plans = self
.selections
.get(platform)
.and_then(|selection| selection.get("throttlingPlans"))
.and_then(Value::as_object);
Some(crate::pacing::selected_buckets(&document, operation, plans))
};
resolve().unwrap_or_default()
}
pub fn validate_request(
&self,
platform: &str,
method: &str,
path: &str,
query: Option<&str>,
content_type: Option<&str>,
has_body: bool,
) -> Result<(), &'static str> {
let document: Value =
serde_yaml::from_str(self.documents.get(platform).ok_or("unknown platform")?)
.map_err(|_| "invalid catalog document")?;
let server = document
.get("servers")
.and_then(Value::as_array)
.and_then(|servers| servers.first())
.and_then(|server| server.get("url"))
.and_then(Value::as_str)
.ok_or("invalid catalog document")?;
let server_url = url::Url::parse(server).map_err(|_| "invalid catalog document")?;
let relative = path
.strip_prefix(server_url.path().trim_end_matches('/'))
.ok_or("method or path is not in the catalog")?;
let paths = document
.get("paths")
.and_then(Value::as_object)
.ok_or("invalid catalog document")?;
let template = paths
.keys()
.find(|t| path_matches(t, relative))
.ok_or("method or path is not in the catalog")?;
let path_item = paths
.get(template)
.and_then(Value::as_object)
.ok_or("invalid catalog document")?;
let operation = path_item
.get(&method.to_ascii_lowercase())
.and_then(Value::as_object)
.ok_or("method or path is not in the catalog")?;
let query_pairs: Vec<(String, String)> = query
.map(|q| {
url::form_urlencoded::parse(q.as_bytes())
.into_owned()
.collect()
})
.unwrap_or_default();
for parameter in operation
.get("parameters")
.into_iter()
.chain(path_item.get("parameters"))
.flat_map(Value::as_array)
.flatten()
{
let parameter = if let Some(reference) = parameter.get("$ref").and_then(Value::as_str) {
document
.pointer(
reference
.strip_prefix('#')
.ok_or("invalid catalog document")?,
)
.ok_or("invalid catalog document")?
} else {
parameter
};
if parameter.get("in").and_then(Value::as_str) != Some("query") {
continue;
}
let name = parameter
.get("name")
.and_then(Value::as_str)
.ok_or("invalid catalog document")?;
let values: Vec<&str> = query_pairs
.iter()
.filter(|(key, _)| key == name)
.map(|(_, value)| value.as_str())
.collect();
if values.is_empty() {
if parameter.get("required").and_then(Value::as_bool) == Some(true) {
return Err("missing required query parameter");
}
continue;
}
if let Some(allowed) = parameter
.get("schema")
.and_then(|schema| schema.get("enum"))
.and_then(Value::as_array)
{
let allowed: Vec<&str> = allowed.iter().filter_map(Value::as_str).collect();
if values.iter().any(|value| !allowed.contains(value)) {
return Err("query parameter value is not permitted");
}
}
}
match operation.get("requestBody") {
None => {
if has_body {
return Err("operation does not accept a request body");
}
}
Some(request_body) => {
if !has_body {
if request_body.get("required").and_then(Value::as_bool) == Some(true) {
return Err("operation requires a request body");
}
return Ok(());
}
let content = request_body
.get("content")
.and_then(Value::as_object)
.ok_or("invalid catalog document")?;
let media_type = content_type
.and_then(|value| value.split(';').next())
.map(str::trim)
.unwrap_or("");
if !content.contains_key(media_type) {
return Err("unsupported content type");
}
}
}
Ok(())
}
pub fn oauth_provider(&self, platform: &str) -> Result<crate::providers::Provider, String> {
match self.security_scheme(platform)? {
crate::providers::SecurityScheme::OAuth(provider) => Ok(provider),
_ => Err("platform does not use an OAuth security scheme".into()),
}
}
pub fn security_scheme(
&self,
platform: &str,
) -> Result<crate::providers::SecurityScheme, String> {
let source = self.get(platform).ok_or("unknown catalog platform")?;
let document = serde_yaml::from_str(source).map_err(|_| "invalid catalog document")?;
if let Some(profile) = self.selected_profile(platform)? {
return crate::providers::SecurityScheme::for_profile(&document, profile);
}
let oauth_scheme = self.selected_string(platform, "oauthSecurityScheme")?;
let api_key_scheme = self.selected_string(platform, "apiKeySecurityScheme")?;
let http_scheme = self.selected_string(platform, "httpSecurityScheme")?;
crate::providers::SecurityScheme::from_document(
&document,
oauth_scheme,
api_key_scheme,
http_scheme,
)
}
fn selected_string(&self, platform: &str, key: &str) -> Result<Option<&str>, String> {
match self
.selections
.get(platform)
.and_then(|selection| selection.get(key))
{
Some(Value::String(value)) => Ok(Some(value.as_str())),
Some(_) => Err(format!("{key} selection must be a string")),
None => Ok(None),
}
}
fn selected_profile(&self, platform: &str) -> Result<Option<&str>, String> {
let profile = self.selected_string(platform, "authenticationProfile")?;
if profile.is_some()
&& (self
.selected_string(platform, "oauthSecurityScheme")?
.is_some()
|| self
.selected_string(platform, "apiKeySecurityScheme")?
.is_some()
|| self
.selected_string(platform, "httpSecurityScheme")?
.is_some())
{
return Err(
"authenticationProfile selection excludes oauthSecurityScheme, apiKeySecurityScheme and httpSecurityScheme"
.into(),
);
}
Ok(profile)
}
fn profile_covers(&self, platform: &str, document: &Value, operation: &Value) -> bool {
match self.selected_profile(platform) {
Ok(None) => true,
Ok(Some(profile)) => crate::providers::profile_scheme(document, profile)
.is_ok_and(|scheme| crate::providers::covers(document, operation, scheme)),
Err(_) => false,
}
}
fn get(&self, platform: &str) -> Option<&str> {
self.documents.get(platform).map(String::as_str)
}
}
fn parse_catalog_config(source: &str, path: &str) -> Result<CatalogConfig, String> {
serde_yaml::from_str(source).map_err(|err| format!("cannot parse {path}: {err}"))
}
fn refuse_ref_path_items(platform: &str, document: &Value) -> Result<(), String> {
let Some(paths) = document.get("paths").and_then(Value::as_object) else {
return Ok(());
};
for (path, item) in paths {
if !item.is_object() || item.get("$ref").is_some() {
return Err(format!(
"platform {platform:?}: path item {path:?} is a $ref or not an object; this proxy does not resolve $ref path items"
));
}
}
Ok(())
}
fn insert_document(
documents: &mut BTreeMap<String, String>,
name: &str,
document: String,
) -> Result<(), String> {
if documents.insert(name.to_string(), document).is_some() {
return Err(format!("duplicate platform name {name:?}"));
}
Ok(())
}
fn path_matches(template: &str, path: &str) -> bool {
let left: Vec<_> = template.trim_matches('/').split('/').collect();
let right: Vec<_> = path.trim_matches('/').split('/').collect();
left.len() == right.len()
&& left
.iter()
.zip(right)
.all(|(template, path)| path_segment_matches(template, path))
}
fn path_segment_matches(template: &str, path: &str) -> bool {
let Some(open) = template.find('{') else {
return template == path;
};
let Some(close) = template[open + 1..].find('}') else {
return false;
};
let close = open + 1 + close;
if template[close + 1..].contains(['{', '}'])
|| template[..open].contains('}')
|| template[open + 1..close].contains('{')
|| template[open + 1..close].is_empty()
{
return false;
}
let prefix = &template[..open];
let suffix = &template[close + 1..];
path.starts_with(prefix)
&& path.ends_with(suffix)
&& path.len() >= prefix.len() + suffix.len()
&& !path[prefix.len()..path.len() - suffix.len()].is_empty()
}
async fn fetch_source(
client: &reqwest::Client,
url: &str,
mirror: Option<&std::path::Path>,
) -> Result<String, String> {
match (mirror, url.strip_prefix(crate::config::OVERLAYS_PAGES_BASE)) {
(Some(dir), Some(relative)) => {
let file = dir.join(relative);
fs::read_to_string(&file)
.map_err(|err| format!("cannot read {}: {err}", file.display()))
}
_ => fetch_yaml(client, url).await,
}
}
async fn fetch_yaml(client: &reqwest::Client, url: &str) -> Result<String, String> {
let url = url::Url::parse(url).map_err(|err| format!("invalid catalog URL: {err}"))?;
if url.scheme() != "https" {
return Err("catalog sources must use HTTPS".to_string());
}
client
.get(url)
.send()
.await
.map_err(|err| err.to_string())?
.error_for_status()
.map_err(|err| err.to_string())?
.text()
.await
.map_err(|err| err.to_string())
}
fn valid_platform_name(name: &str) -> Result<(), String> {
if !name.is_empty()
&& name
.bytes()
.all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-')
{
Ok(())
} else {
Err(format!("invalid platform name {name:?}"))
}
}
fn overlay_requirement_problems(
selected: &[String],
infos: &[(String, Option<Value>)],
) -> Result<Vec<String>, String> {
let present: std::collections::BTreeSet<&str> = selected.iter().map(String::as_str).collect();
let mut problems = Vec::new();
for (overlay_url, info) in infos {
let Some(info) = info else { continue };
let resolve = |reference: &Value| -> Result<String, String> {
let reference = reference
.as_str()
.filter(|r| !r.is_empty() && !r.contains(char::is_whitespace))
.ok_or_else(|| format!("{overlay_url}: an x-requires or x-conflicts reference is not a URI reference"))?;
url::Url::parse(overlay_url)
.and_then(|base| base.join(reference))
.map(|url| url.to_string())
.map_err(|err| format!("{overlay_url}: invalid reference {reference:?}: {err}"))
};
if let Some(requires) = info.get("x-requires") {
let groups = requires
.as_array()
.filter(|groups| !groups.is_empty())
.ok_or_else(|| {
format!("{overlay_url}: info.x-requires must be a nonempty array")
})?;
for group in groups {
let alternatives = group
.as_object()
.filter(|object| object.len() == 1)
.and_then(|object| object.get("anyOf"))
.and_then(Value::as_array)
.filter(|any_of| !any_of.is_empty())
.ok_or_else(|| {
format!("{overlay_url}: each info.x-requires group must be {{anyOf: [...]}}, nonempty")
})?;
let resolved = alternatives
.iter()
.map(&resolve)
.collect::<Result<Vec<_>, _>>()?;
if !resolved.iter().any(|url| present.contains(url.as_str())) {
problems.push(format!(
"{overlay_url} requires one of {}",
resolved.join(", ")
));
}
}
}
if let Some(conflicts) = info.get("x-conflicts") {
let conflicts = conflicts
.as_array()
.filter(|conflicts| !conflicts.is_empty())
.ok_or_else(|| {
format!("{overlay_url}: info.x-conflicts must be a nonempty array")
})?;
for conflict in conflicts {
let url = resolve(conflict)?;
if present.contains(url.as_str()) {
problems.push(format!("{overlay_url} conflicts with {url}"));
}
}
}
}
Ok(problems)
}
fn apply_overlay(
document: &mut Value,
overlay: Overlay,
openapi: &str,
overlay_url: &str,
) -> Result<(), String> {
if let Some(extends) = overlay.extends {
let target = url::Url::parse(overlay_url)
.and_then(|base| base.join(&extends))
.map_err(|err| format!("invalid extends in {overlay_url}: {err}"))?;
if target.as_str() != openapi {
return Err(format!(
"overlay {overlay_url} extends {target}, but catalog OAD is {openapi}"
));
}
}
for action in overlay.actions {
merge_at_target(document, &action.target, action.update)?;
}
Ok(())
}
fn merge_at_target(document: &mut Value, target: &str, update: Value) -> Result<(), String> {
let keys = parse_target(target)?;
let mut current = document;
for key in keys {
current = current
.get_mut(&key)
.ok_or_else(|| format!("overlay target {target:?} does not exist"))?;
}
merge(current, update);
Ok(())
}
fn parse_target(target: &str) -> Result<Vec<String>, String> {
let mut rest = target
.strip_prefix('$')
.ok_or_else(|| format!("unsupported overlay target {target:?}"))?;
let mut keys = Vec::new();
while !rest.is_empty() {
if let Some(after) = rest.strip_prefix('.') {
let end = after.find(['.', '[']).unwrap_or(after.len());
if end == 0 {
return Err(format!("unsupported overlay target {target:?}"));
}
keys.push(after[..end].to_string());
rest = &after[end..];
} else if let Some(after) = rest.strip_prefix("['") {
let end = after
.find("']")
.ok_or_else(|| format!("unsupported overlay target {target:?}"))?;
keys.push(after[..end].to_string());
rest = &after[end + 2..];
} else {
return Err(format!("unsupported overlay target {target:?}"));
}
}
Ok(keys)
}
fn merge(destination: &mut Value, update: Value) {
match (destination, update) {
(Value::Object(destination), Value::Object(update)) => {
for (key, value) in update {
merge(destination.entry(key).or_insert(Value::Null), value);
}
}
(destination, update) => *destination = update,
}
}
pub async fn list(State(state): State<AppState>) -> Json<Vec<String>> {
Json(state.catalog.names())
}
pub async fn document(Path(file): Path<String>, State(state): State<AppState>) -> Response {
if let Some(platform) = file.strip_suffix(".selection.json") {
if state.catalog.get(platform).is_none() {
return (StatusCode::NOT_FOUND, "catalog platform not found").into_response();
}
return Json(
state
.catalog
.selections
.get(platform)
.cloned()
.unwrap_or_else(|| serde_json::json!({})),
)
.into_response();
}
let Some(platform) = file.strip_suffix(".yaml") else {
return (StatusCode::NOT_FOUND, "catalog platform not found").into_response();
};
match state.catalog.get(platform) {
Some(document) => (
[("content-type", "application/yaml; charset=utf-8")],
document.to_owned(),
)
.into_response(),
None => (StatusCode::NOT_FOUND, "catalog platform not found").into_response(),
}
}
#[cfg(test)]
mod tests {
const BASE: &str = "https://overlays.example/APIs/example/1/";
fn selection(names: &[&str]) -> Vec<String> {
names.iter().map(|name| format!("{BASE}{name}")).collect()
}
#[test]
fn overlay_requirements_are_met_or_reported() {
let requires = serde_json::json!({
"x-requires": [{"anyOf": ["crud-v2.yaml", "crud-v3.yaml"]}],
"x-conflicts": ["crud-v1.yaml"]
});
let infos = |names: &[&str]| -> Vec<(String, Option<serde_json::Value>)> {
names
.iter()
.map(|name| {
let info = (*name == "completeness.yaml").then(|| requires.clone());
(format!("{BASE}{name}"), info)
})
.collect()
};
for names in [
&["crud-v2.yaml", "completeness.yaml"][..],
&["crud-v3.yaml", "completeness.yaml"][..],
&["auth.yaml"][..],
] {
assert_eq!(
overlay_requirement_problems(&selection(names), &infos(names)).unwrap(),
Vec::<String>::new(),
"{names:?}"
);
}
let names = ["completeness.yaml"];
let problems = overlay_requirement_problems(&selection(&names), &infos(&names)).unwrap();
assert_eq!(problems.len(), 1);
assert!(problems[0].contains("requires one of"), "{problems:?}");
let names = ["crud-v1.yaml", "crud-v2.yaml", "completeness.yaml"];
let problems = overlay_requirement_problems(&selection(&names), &infos(&names)).unwrap();
assert_eq!(
problems,
[format!(
"{BASE}completeness.yaml conflicts with {BASE}crud-v1.yaml"
)]
);
}
#[test]
fn malformed_overlay_requirements_are_errors() {
for info in [
serde_json::json!({"x-requires": []}),
serde_json::json!({"x-requires": [["crud-v2.yaml"]]}),
serde_json::json!({"x-requires": [{"anyOf": []}]}),
serde_json::json!({"x-requires": [{"anyOf": ["a b.yaml"]}]}),
serde_json::json!({"x-conflicts": "crud-v1.yaml"}),
] {
let infos = vec![(format!("{BASE}completeness.yaml"), Some(info.clone()))];
assert!(
overlay_requirement_problems(&selection(&["completeness.yaml"]), &infos).is_err(),
"{info}"
);
}
}
#[test]
fn a_header_parameter_is_declared_only_where_the_operation_or_its_path_says_so() {
use serde_json::json;
let catalog = super::Catalog::from_test_document(
"test",
json!({"servers":[{"url":"https://example.com/v1"}],
"components":{"parameters":{"key":{"in":"header","name":"Idempotency-Key"}}},
"paths":{
"/items":{"post":{"parameters":[{"$ref":"#/components/parameters/key"}]},
"put":{"parameters":[{"in":"query","name":"Idempotency-Key"}]}},
"/items/{id}":{"parameters":[{"in":"header","name":"idempotency-key"}],
"patch":{}},
"/other":{"post":{}}
}}),
json!({}),
);
let declared = |method, path| {
catalog.declares_header_parameter("test", method, path, "idempotency-key")
};
assert!(declared("POST", "/v1/items"));
assert!(declared("PATCH", "/v1/items/7"));
assert!(!declared("PUT", "/v1/items"));
assert!(!declared("GET", "/v1/items"));
assert!(!declared("POST", "/v1/other"));
assert!(!declared("POST", "/items"));
assert!(!catalog.declares_header_parameter(
"other",
"POST",
"/v1/items",
"idempotency-key"
));
}
#[test]
fn throttling_plans_from_the_selection_reach_the_pacing() {
use serde_json::json;
let document = json!({"servers":[{"url":"https://example.com/v1"}],
"x-throttling": {"limits": {"source": {"partitionBy": ["sourceIp"], "tiers": [
{"plans": ["FREE"], "requests": 30, "window": {"seconds": 3600}},
{"plans": ["PRO"], "requests": 20, "window": {"seconds": 1}}]}},
"applies": ["source"]},
"paths": {"/items": {"get": {}}}});
let tiers = |selection: Value| -> Vec<Option<usize>> {
super::Catalog::from_test_document("test", document.clone(), selection)
.paced_buckets("test", "GET", "/v1/items")
.into_iter()
.map(|bucket| bucket.tier)
.collect()
};
assert_eq!(
tiers(json!({"throttlingPlans": {"source": "PRO"}})),
[Some(1)]
);
assert_eq!(tiers(json!({})), [Some(0), Some(1)]);
assert_eq!(
tiers(json!({"throttlingPlans": {"source": "GOLD"}})),
[Some(0), Some(1)]
);
}
#[test]
fn fixed_header_defaults_are_explicit_and_operation_overrides_path() {
use serde_json::json;
let mut doc = json!({"servers":[{"url":"https://example.com/v1"}], "paths":{"/items": {
"parameters":[{"in":"header","name":"X-Version","required":true,"schema":{"default":"old"}}],
"get":{"parameters":[
{"in":"header","name":"X-Version","required":true,"schema":{"enum":["new"]}},
{"in":"header","name":"X-Optional","example":"not a default","schema":{"type":"string"}}
]}
}}});
let headers = |doc: &serde_json::Value| {
let catalog = super::Catalog {
documents: [("test".into(), serde_yaml::to_string(doc).unwrap())].into(),
..Default::default()
};
catalog.required_headers("test", "GET", "/v1/items")
};
assert_eq!(
headers(&doc).unwrap(),
vec![("X-Version".into(), "new".into())]
);
doc["paths"]["/items"]["get"]["parameters"][1]["required"] = json!(true);
assert!(headers(&doc).is_none());
doc["paths"]["/items"]["get"]["parameters"][1]["schema"]["enum"] = json!(["one", "two"]);
assert!(headers(&doc).is_none());
doc["paths"]["/items"]["get"]["parameters"][1]["schema"]["default"] = json!("one");
assert_eq!(headers(&doc).unwrap().len(), 2);
}
#[test]
fn composed_clockify_fixture_declares_an_api_key_scheme_and_pagination() {
let document: Value =
serde_yaml::from_str(include_str!("../tests/fixtures/clockify-composed.yaml")).unwrap();
let catalog = Catalog::from_test_document("clockify", document, serde_json::json!({}));
let scheme = catalog.security_scheme("clockify").unwrap();
match scheme {
crate::providers::SecurityScheme::ApiKey(scheme) => {
assert_eq!(scheme.name, "X-Api-Key");
assert_eq!(scheme.location, crate::providers::ApiKeyLocation::Header);
}
_ => panic!("expected an apiKey scheme"),
}
assert!(catalog
.allows(
"clockify",
"GET",
"/api/v1/workspaces/ws1/user/u1/time-entries"
)
.is_some());
assert_eq!(
catalog
.required_headers(
"clockify",
"GET",
"/api/v1/workspaces/ws1/user/u1/time-entries"
)
.unwrap(),
Vec::<(String, String)>::new()
);
}
#[test]
fn validate_request_checks_required_query_parameters_enums_and_body() {
use serde_json::json;
let doc = json!({"servers":[{"url":"https://example.com/v1"}], "paths":{"/items": {
"get":{"parameters":[
{"in":"query","name":"state","required":true,"schema":{"enum":["open","closed"]}},
{"in":"query","name":"page","schema":{"type":"integer"}}
]},
"post": {"requestBody": {"required": true, "content": {"application/json": {}}}}
}}});
let catalog = super::Catalog {
documents: [("test".into(), serde_yaml::to_string(&doc).unwrap())].into(),
..Default::default()
};
assert!(catalog
.validate_request("test", "GET", "/v1/items", None, None, false)
.is_err());
assert!(catalog
.validate_request(
"test",
"GET",
"/v1/items",
Some("state=pending"),
None,
false
)
.is_err());
assert!(catalog
.validate_request(
"test",
"GET",
"/v1/items",
Some("state=open&page=2&undeclared=1"),
None,
false
)
.is_ok());
assert!(catalog
.validate_request(
"test",
"GET",
"/v1/items",
Some("state=open"),
Some("application/json"),
true
)
.is_err());
assert!(catalog
.validate_request("test", "POST", "/v1/items", None, None, false)
.is_err());
assert!(catalog
.validate_request("test", "POST", "/v1/items", None, Some("text/plain"), true)
.is_err());
assert!(catalog
.validate_request(
"test",
"POST",
"/v1/items",
None,
Some("application/json; charset=utf-8"),
true
)
.is_ok());
assert!(catalog
.validate_request("test", "DELETE", "/v1/items", None, None, false)
.is_err());
}
#[test]
fn composed_notion_preserves_base_path_and_resolves_version_header() {
let catalog = super::Catalog {
documents: [(
"notion".into(),
include_str!("../tests/fixtures/notion-composed.yaml").into(),
)]
.into(),
..Default::default()
};
assert!(catalog.allows("notion", "POST", "/search").is_none());
assert!(catalog.allows("notion", "POST", "/v1/search").is_some());
assert_eq!(
catalog
.required_headers("notion", "POST", "/v1/search")
.unwrap(),
vec![("Notion-Version".into(), "2026-03-11".into())]
);
}
use super::*;
#[test]
fn default_catalog_is_the_published_checked_in_catalog() {
use crate::config::{DEFAULT_CATALOG_FILE, DEFAULT_CATALOG_PATH, OVERLAYS_PAGES_BASE};
assert_eq!(
DEFAULT_CATALOG_PATH,
format!("{OVERLAYS_PAGES_BASE}{DEFAULT_CATALOG_FILE}")
);
let overlays = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../overlays");
assert!(overlays.join(DEFAULT_CATALOG_FILE).is_file());
let mut catalogs: Vec<_> = fs::read_dir(overlays.join("catalog"))
.unwrap()
.map(|entry| entry.unwrap().path())
.filter(|path| path.extension().is_some_and(|ext| ext == "json"))
.collect();
catalogs.sort();
assert!(catalogs.len() >= 2, "{catalogs:?}");
let platforms = catalogs.iter().flat_map(|path| {
parse_catalog_config(&fs::read_to_string(path).unwrap(), &path.to_string_lossy())
.unwrap()
.platforms
});
for platform in platforms {
if let Some(relative) = platform.openapi.strip_prefix(OVERLAYS_PAGES_BASE) {
assert!(
overlays.join(relative).is_file(),
"{}: missing overlays/{relative}",
platform.name
);
}
for url in platform.overlays {
let relative = url.strip_prefix(OVERLAYS_PAGES_BASE).unwrap_or_else(|| {
panic!("{}: {url} is not published from overlays/", platform.name)
});
assert!(
overlays.join(relative).is_file(),
"{}: missing overlays/{relative}",
platform.name
);
}
}
}
#[tokio::test]
async fn default_catalog_pets_is_a_credential_free_read_of_one_collection() {
let overlays = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../overlays");
let catalog: Value = serde_json::from_str(
&fs::read_to_string(overlays.join(crate::config::DEFAULT_CATALOG_FILE)).unwrap(),
)
.unwrap();
let pets = catalog["platforms"]
.as_array()
.unwrap()
.iter()
.find(|platform| platform["name"] == "pets")
.expect("the default catalog lists pets")
.clone();
let only_pets = tempfile_path("pets-catalog.json");
fs::write(
&only_pets,
serde_json::json!({"platforms": [pets]}).to_string(),
)
.unwrap();
let catalog = Catalog::load_with_mirror(
&only_pets.to_string_lossy(),
&crate::build_http_client(),
Some(&overlays),
)
.await
.unwrap();
assert_eq!(
catalog.security_scheme("pets"),
Ok(crate::providers::SecurityScheme::NoCredential)
);
let base = "/atomic-plugins/overlays/pets-demo/1.0.0/api";
let upstream = catalog
.allows("pets", "GET", &format!("{base}/pets"))
.unwrap();
assert_eq!(
upstream.as_str(),
"https://ontola.github.io/atomic-plugins/overlays/pets-demo/1.0.0/api"
);
let served: Value = serde_json::from_str(
&fs::read_to_string(overlays.join("pets-demo/1.0.0/api/pets")).unwrap(),
)
.unwrap();
assert_eq!(served.as_array().unwrap().len(), 5);
for method in ["POST", "PUT", "PATCH", "DELETE"] {
assert!(catalog
.allows("pets", method, &format!("{base}/pets"))
.is_none());
}
for path in [
"/pets".to_owned(),
format!("{base}/pets/1"),
format!("{base}/owners"),
"/atomic-plugins/overlays/catalog/2026-10-02.json".to_owned(),
"/atomic-plugins/apps/pets/0.1.2/ui.js".to_owned(),
] {
assert!(catalog.allows("pets", "GET", &path).is_none(), "{path}");
}
assert!(catalog
.validate_request("pets", "GET", &format!("{base}/pets"), None, None, false)
.is_ok());
}
#[tokio::test]
#[ignore = "downloads the pinned Swagger 2.0 OADs the overlays extend"]
async fn swagger2_overlay_revisions_compose_without_components() {
use crate::config::OVERLAYS_PAGES_BASE;
let overlays = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../overlays");
let mut files = Vec::new();
collect_overlays(&overlays.join("APIs"), &mut files);
files.sort();
let client = crate::build_http_client();
let (mut current, mut superseded) = (0, 0);
for path in files {
let overlay: Value = serde_yaml::from_str(&fs::read_to_string(&path).unwrap()).unwrap();
let Some(extends) = overlay["extends"].as_str() else {
continue;
};
if !extends.ends_with("/swagger.yaml") {
continue;
}
let relative = path.strip_prefix(&overlays).unwrap().to_str().unwrap();
let name = path.file_name().unwrap().to_str().unwrap();
let (kind, rest) = name.split_at(name.len() - "-".len() - 40 - "-overlay.yaml".len());
let is_superseded =
!kind.ends_with("-v2") && path.with_file_name(format!("{kind}-v2{rest}")).is_file();
let catalog = tempfile_path("swagger.json");
fs::write(
&catalog,
serde_json::json!({"platforms": [{
"name": "swagger",
"openapi": extends,
"overlays": [format!("{OVERLAYS_PAGES_BASE}{relative}")],
}]})
.to_string(),
)
.unwrap();
let result =
Catalog::load_with_mirror(&catalog.to_string_lossy(), &client, Some(&overlays))
.await;
if is_superseded {
let error = result
.err()
.unwrap_or_else(|| panic!("{relative}: a superseded revision should not load"));
assert!(
error.contains("overlay target \"$.components\" does not exist"),
"{relative}: {error}"
);
superseded += 1;
} else {
if let Err(error) = result {
panic!("{relative}: {error}");
}
current += 1;
}
}
assert_eq!(
superseded, 21,
"the audit's 21 superseded Swagger 2.0 overlays"
);
assert!(current >= 21, "{current} current Swagger 2.0 overlays");
}
fn collect_overlays(dir: &std::path::Path, out: &mut Vec<std::path::PathBuf>) {
for entry in fs::read_dir(dir).unwrap() {
let path = entry.unwrap().path();
if path.is_dir() {
collect_overlays(&path, out);
} else if path.to_string_lossy().ends_with("-overlay.yaml") {
out.push(path);
}
}
}
fn tempfile_path(name: &str) -> std::path::PathBuf {
let dir = std::env::temp_dir().join(format!(
"integration-proxy-test-{}-{}",
std::process::id(),
crate::connect::random()
));
fs::create_dir_all(&dir).unwrap();
dir.join(name)
}
#[tokio::test]
#[ignore = "downloads the pinned OAD sources the default catalog composes"]
async fn default_catalog_pin_selects_google_offline_and_spotify_pkce_profiles() {
let catalog = Catalog::load_checked_in(&crate::build_http_client())
.await
.unwrap();
for name in catalog.names() {
let scheme = catalog
.security_scheme(&name)
.unwrap_or_else(|error| panic!("{name}: {error}"));
if let crate::providers::SecurityScheme::OAuth(provider) = &scheme {
if name != "notion" {
assert!(!provider.scopes.is_empty(), "{name}");
}
}
assert!(
!matches!(scheme, crate::providers::SecurityScheme::Http(_)),
"{name}"
);
}
let crate::providers::SecurityScheme::ApiKey(clockify) =
catalog.security_scheme("clockify").unwrap()
else {
panic!("clockify: expected an apiKey scheme");
};
assert!(clockify
.help_url
.as_deref()
.is_some_and(|url| url.starts_with("https://clockify.me/help/")));
assert!(clockify
.description
.as_deref()
.is_some_and(|text| text.contains("Manage API keys")));
let check = clockify.key_check.unwrap();
assert_eq!(check.url.as_str(), "https://api.clockify.me/api/v1/user");
assert_eq!(check.label_pointer.as_deref(), Some("/email"));
let google = catalog.oauth_provider("google-calendar").unwrap();
assert!(google.use_pkce);
assert!(google
.authorization_params
.contains(&("access_type".into(), "offline".into())));
let spotify = catalog.oauth_provider("spotify").unwrap();
assert!(spotify.use_pkce);
assert_eq!(
catalog.security_scheme("pets"),
Ok(crate::providers::SecurityScheme::NoCredential)
);
assert!(catalog
.allows(
"pets",
"GET",
"/atomic-plugins/overlays/pets-demo/1.0.0/api/pets"
)
.is_some());
assert!(catalog
.allows("github-issues", "GET", "/repositories/123/issues")
.is_some());
let repos = "/user/repos";
assert!(catalog.allows("github-issues", "GET", repos).is_some());
for method in ["POST", "PATCH", "PUT", "DELETE"] {
assert!(
catalog.allows("github-issues", method, repos).is_none(),
"{method} {repos}"
);
}
assert!(catalog
.validate_request(
"github-issues",
"GET",
repos,
Some("per_page=100&page=2&sort=updated"),
None,
false
)
.is_ok());
assert_eq!(
catalog.validate_request(
"github-issues",
"GET",
repos,
Some("sort=stars"),
None,
false
),
Err("query parameter value is not permitted")
);
for path in ["/users/octocat/repos", "/orgs/ontola/repos", "/user/orgs"] {
assert!(
catalog.allows("github-issues", "GET", path).is_none(),
"{path}"
);
}
let labels = "/repos/owner/repo/issues/7/labels";
let doing = "/repos/owner/repo/issues/7/labels/atomic%3Adoing";
assert!(catalog.allows("github-issues", "POST", labels).is_some());
assert!(catalog.allows("github-issues", "DELETE", doing).is_some());
for method in ["GET", "PUT", "DELETE"] {
assert!(
catalog.allows("github-issues", method, labels).is_none(),
"{method} {labels}"
);
}
for method in ["GET", "POST", "PATCH"] {
assert!(
catalog.allows("github-issues", method, doing).is_none(),
"{method} {doing}"
);
}
assert!(catalog
.allows("github-issues", "POST", "/repos/owner/repo/labels")
.is_none());
assert!(catalog
.allows("github-issues", "DELETE", "/repos/owner/repo/labels/bug")
.is_none());
assert!(catalog
.validate_request(
"github-issues",
"POST",
labels,
None,
Some("application/json"),
true
)
.is_ok());
assert_eq!(
catalog.validate_request("github-issues", "POST", labels, None, None, false),
Err("operation requires a request body")
);
assert_eq!(
catalog.validate_request(
"github-issues",
"DELETE",
doing,
None,
Some("application/json"),
true
),
Err("operation does not accept a request body")
);
let github = catalog.oauth_provider("github-issues").unwrap();
assert_eq!(github.scopes, vec!["repo".to_string()]);
assert!(catalog
.allows(
"google-calendar",
"PATCH",
"/calendar/v3/calendars/team%40example.com/events/id"
)
.is_some());
let entry = "/api/v1/workspaces/ws/time-entries/entry-1";
for method in ["GET", "PUT", "DELETE"] {
assert!(
catalog.allows("clockify", method, entry).is_some(),
"{method}"
);
}
assert!(catalog.allows("clockify", "PATCH", entry).is_none());
assert!(catalog
.allows("clockify", "POST", "/api/v1/workspaces/ws/time-entries")
.is_some());
for path in ["/api/v1/user", "/api/v1/workspaces"] {
assert!(catalog.allows("clockify", "GET", path).is_some(), "{path}");
assert!(catalog.allows("clockify", "POST", path).is_none(), "{path}");
assert!(catalog
.validate_request("clockify", "GET", path, None, None, false)
.is_ok());
}
let list = "/api/v1/workspaces/ws/user/u/time-entries";
assert!(catalog.allows("clockify", "GET", list).is_some());
assert!(catalog.allows("clockify", "POST", list).is_none());
for (method, path) in [
("POST", "/api/v1/workspaces/ws/time-entries"),
("PUT", entry),
] {
assert!(catalog
.validate_request(
"clockify",
method,
path,
None,
Some("application/json"),
true
)
.is_ok());
assert!(catalog
.validate_request("clockify", method, path, None, None, false)
.is_err());
}
assert!(catalog
.validate_request("clockify", "DELETE", entry, None, None, false)
.is_ok());
assert_eq!(
catalog.required_headers("clockify", "PUT", entry),
Some(vec![])
);
}
#[tokio::test]
#[ignore = "downloads the pinned production catalog sources"]
async fn pinned_catalog_supplies_oauth_and_canonical_pagination_paths() {
let path = std::env::var("TEST_CATALOG_PATH").unwrap_or_else(|_| "https://raw.githubusercontent.com/localthought/overlays/d83c3ce0afd9f8ca0e4c42e142fa89d5fa9d8f70/catalog.json".into());
let catalog = Catalog::load(&path, &crate::build_http_client())
.await
.unwrap();
for name in catalog.names() {
let provider = catalog
.oauth_provider(&name)
.unwrap_or_else(|error| panic!("{name}: {error}"));
assert!(!provider.scopes.is_empty(), "{name}");
}
let moneybird: Value = serde_yaml::from_str(catalog.get("moneybird").unwrap()).unwrap();
let resources = moneybird
.pointer("/components/crudResources")
.unwrap()
.as_object()
.unwrap();
let collection_count: usize = resources
.values()
.filter_map(|r| r.get("collections").and_then(Value::as_object))
.map(|c| c.len())
.sum();
assert_eq!(collection_count, 32);
assert_eq!(
catalog.selections["moneybird"]["query_overrides"]
.as_array()
.unwrap()
.len(),
6
);
assert!(moneybird.get("selection").is_none());
assert_eq!(
moneybird.pointer("/x-throttling/limits/apiRequests/requests"),
Some(&serde_json::json!(150))
);
assert_eq!(
moneybird.pointer("/x-throttling/limits/apiRequests/window/kind"),
Some(&serde_json::json!("unspecified"))
);
assert!(catalog
.allows("github-issues", "GET", "/repositories/123/issues")
.is_some());
assert!(catalog
.allows(
"github-issues",
"GET",
"/repositories/123/issues/1/comments"
)
.is_some());
assert!(catalog
.allows("github-issues", "POST", "/repositories/123/issues")
.is_none());
}
#[tokio::test]
#[ignore = "downloads pinned OAuth authentication-details metadata"]
async fn pinned_catalog_selects_google_offline_and_spotify_pkce_profiles() {
let catalog = Catalog::load(
"https://raw.githubusercontent.com/localthought/overlays/d83c3ce0afd9f8ca0e4c42e142fa89d5fa9d8f70/catalog.json",
&crate::build_http_client(),
)
.await
.unwrap();
let google = catalog.oauth_provider("google-calendar").unwrap();
assert!(google.use_pkce);
assert!(google
.authorization_params
.contains(&("access_type".into(), "offline".into())));
let spotify = catalog.oauth_provider("spotify").unwrap();
assert!(spotify.use_pkce);
}
#[tokio::test]
#[ignore = "downloads the pinned production catalog sources"]
async fn pinned_todoist_catalog_is_read_only_and_preserves_api_prefix() {
let catalog = Catalog::load("https://raw.githubusercontent.com/localthought/overlays/a53b5e75641dabc04af18813348312edcd453bf9/catalog.json", &crate::build_http_client())
.await
.unwrap();
let document: Value = serde_yaml::from_str(catalog.get("todoist").unwrap()).unwrap();
let resources = document
.pointer("/components/crudResources")
.unwrap()
.as_object()
.unwrap();
assert_eq!(resources.len(), 2);
for resource in resources.values() {
let reference = resource.pointer("/schema/$ref").unwrap().as_str().unwrap();
assert!(document
.pointer(reference.strip_prefix('#').unwrap())
.is_some());
}
for path in [
"/api/v1/projects",
"/api/v1/tasks",
"/api/v1/projects/example",
"/api/v1/tasks/example",
] {
assert_eq!(
catalog.allows("todoist", "GET", path).unwrap().as_str(),
"https://api.todoist.com/api/v1"
);
for method in ["POST", "PUT", "PATCH", "DELETE"] {
assert!(catalog.allows("todoist", method, path).is_none());
}
}
for path in ["/tasks", "/api/v10/tasks", "/api/v1/access_tokens"] {
assert!(catalog.allows("todoist", "GET", path).is_none());
}
}
#[tokio::test]
#[ignore = "downloads the pinned production catalog sources"]
async fn pinned_discord_catalog_composes_and_allows_only_user_reads() {
let catalog = Catalog::load("https://raw.githubusercontent.com/localthought/overlays/a53b5e75641dabc04af18813348312edcd453bf9/catalog.json", &crate::build_http_client())
.await
.unwrap();
let document: Value = serde_yaml::from_str(catalog.get("discord").unwrap()).unwrap();
assert!(document
.pointer("/components/crudResources/guild")
.is_some());
for path in ["/api/v10/users/@me", "/api/v10/users/@me/guilds"] {
assert_eq!(
catalog.allows("discord", "GET", path).unwrap().as_str(),
"https://discord.com/api/v10"
);
assert!(catalog.allows("discord", "POST", path).is_none());
}
assert!(catalog
.allows("discord", "GET", "/api/v10/channels/123/messages")
.is_none());
assert!(catalog
.allows("discord", "GET", "/users/@me/guilds")
.is_none());
}
#[test]
fn discord_only_allows_profile_and_membership_reads_with_api_prefix() {
let catalog = Catalog { documents: BTreeMap::from([("discord".into(),
"servers:\n - url: https://discord.com/api/v10\npaths:\n /users/@me:\n get: {}\n /users/@me/guilds:\n get: {}\n".into())]), ..Catalog::default() };
for path in ["/api/v10/users/@me", "/api/v10/users/@me/guilds"] {
assert_eq!(
catalog.allows("discord", "GET", path).unwrap().as_str(),
"https://discord.com/api/v10"
);
assert!(catalog.allows("discord", "POST", path).is_none());
assert!(catalog.allows("discord", "DELETE", path).is_none());
}
for path in [
"/users/@me",
"/api/v100/users/@me",
"/api/v10/users/123",
"/api/v10/channels/123/messages",
] {
assert!(catalog.allows("discord", "GET", path).is_none());
}
}
#[test]
fn matches_moneybird_parameterized_contact_paths() {
let template = "/{administration_id}/contacts/{id}.json";
assert!(path_matches(template, "/123/contacts/456.json"));
assert!(!path_matches(template, "/123/contacts/.json"));
assert!(!path_matches(template, "/123/contacts/456.xml"));
assert!(!path_matches(template, "/123/invoices/456.json"));
}
#[test]
fn rejects_malformed_or_ambiguous_path_variables() {
assert!(!path_matches("/{id", "/123"));
assert!(!path_matches("/{{id}", "/123"));
assert!(!path_matches("/{id}/{other}{third}", "/123/456789"));
assert!(!path_matches("/{id}/contacts", "//contacts"));
}
#[tokio::test]
#[ignore = "downloads the pinned production catalog sources"]
async fn pinned_moneybird_catalog_composes_and_allows_contacts() {
let catalog = Catalog::load("https://raw.githubusercontent.com/localthought/overlays/a53b5e75641dabc04af18813348312edcd453bf9/catalog.json", &crate::build_http_client())
.await
.unwrap();
assert!(catalog.names().contains(&"moneybird".to_string()));
let document: Value = serde_yaml::from_str(catalog.get("moneybird").unwrap()).unwrap();
assert!(document
.pointer("/components/crudResources/contact/collections/contacts")
.is_some());
let schema_ref = document
.pointer("/components/crudResources/contact/schema/$ref")
.and_then(Value::as_str)
.expect("contacts must declare their schema");
let schema = document
.pointer(schema_ref.strip_prefix('#').unwrap())
.expect("contacts schema reference must resolve");
assert!(schema.pointer("/properties/company_name").is_some());
let upstream = catalog
.allows("moneybird", "GET", "/api/v2/123/contacts.json")
.expect("Moneybird contacts path must include the server base path");
assert_eq!(upstream.as_str(), "https://moneybird.com/api/v2");
assert!(catalog
.allows("moneybird", "GET", "/api/v2/123/contacts/456.json")
.is_some());
assert!(catalog
.allows("moneybird", "POST", "/api/v2/123/contacts.json")
.is_none());
assert!(catalog
.allows("moneybird", "GET", "/123/contacts.json")
.is_none());
assert!(catalog
.allows("moneybird", "GET", "/api/v20/123/contacts.json")
.is_none());
}
#[tokio::test]
#[ignore = "downloads the pinned production catalog sources"]
async fn pinned_spotify_catalog_composes_and_allows_readonly_playlists() {
let catalog = Catalog::load("https://raw.githubusercontent.com/localthought/overlays/a53b5e75641dabc04af18813348312edcd453bf9/catalog.json", &crate::build_http_client())
.await
.unwrap();
let document: Value = serde_yaml::from_str(catalog.get("spotify").unwrap()).unwrap();
assert!(document
.pointer("/components/crudResources")
.unwrap()
.as_object()
.is_some_and(|resources| !resources.is_empty()));
assert_eq!(
catalog
.allows("spotify", "GET", "/v1/me/playlists")
.unwrap()
.as_str(),
"https://api.spotify.com/v1"
);
assert!(catalog
.allows("spotify", "POST", "/v1/me/playlists")
.is_none());
assert!(catalog.allows("spotify", "GET", "/me/playlists").is_none());
assert!(catalog.allows("spotify", "GET", "/v1/me/tracks").is_none());
}
#[test]
fn validates_google_endpoints_relative_to_server_base_path() {
for server in [
"https://www.googleapis.com/calendar/v3",
"https://www.googleapis.com/calendar/v3/",
] {
let catalog = Catalog { documents: BTreeMap::from([("google-calendar".into(), format!("servers:\n - url: {server}\npaths:\n /users/me/calendarList:\n get: {{}}\n /calendars/{{calendarId}}/events:\n get: {{}}\n"))]), ..Catalog::default() };
assert!(catalog
.allows(
"google-calendar",
"GET",
"/calendar/v3/users/me/calendarList"
)
.is_some());
assert!(catalog
.allows(
"google-calendar",
"GET",
"/calendar/v3/calendars/a%40example.com/events"
)
.is_some());
assert!(catalog
.allows("google-calendar", "GET", "/users/me/calendarList")
.is_none());
assert!(catalog
.allows(
"google-calendar",
"GET",
"/calendar/v30/users/me/calendarList"
)
.is_none());
assert!(catalog
.allows(
"google-calendar",
"POST",
"/calendar/v3/users/me/calendarList"
)
.is_none());
}
}
use axum::{
body::{to_bytes, Body},
http::Request,
};
use tower::ServiceExt;
fn test_router() -> axum::Router {
let config: CatalogConfig = serde_yaml::from_str(
"platforms:\n - { name: github-issues, openapi: https://example.com/github.yaml }\n - { name: google-calendar, openapi: https://example.com/google.yaml }\n - { name: moneybird, openapi: https://example.com/moneybird.yaml }\n - { name: todoist, openapi: https://example.com/todoist.yaml }\n - { name: spotify, openapi: https://example.com/spotify.yaml }\n - { name: discord, openapi: https://example.com/discord.yaml }\n",
)
.unwrap();
let documents = config
.platforms
.into_iter()
.map(|platform| {
(
platform.name,
"openapi: 3.0.0\ninfo: {title: Test, version: '1'}\npaths: {}\n".to_string(),
)
})
.collect();
let mut state = crate::test_support::state(None);
state.base_url = "http://localhost".into();
state.catalog = Catalog { documents, selections: [("moneybird".into(), serde_json::json!({"query_overrides": [{"path":"/records", "values":{"include_archived":true}}]}))].into() };
crate::router(state)
}
#[tokio::test]
async fn router_serves_every_advertised_catalog_document() {
let app = test_router();
let response = app
.clone()
.oneshot(
Request::builder()
.uri("/catalog")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(response.status(), StatusCode::OK);
let names: Vec<String> =
serde_json::from_slice(&to_bytes(response.into_body(), usize::MAX).await.unwrap())
.unwrap();
assert!(!names.is_empty());
for name in names {
let response = app
.clone()
.oneshot(
Request::builder()
.uri(format!("/catalog/{name}.yaml"))
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(response.status(), StatusCode::OK, "{name}");
let document: Value =
serde_yaml::from_slice(&to_bytes(response.into_body(), usize::MAX).await.unwrap())
.unwrap();
assert_eq!(document["openapi"], "3.0.0");
assert!(document["paths"].is_object());
}
for path in ["/catalog/unknown.yaml", "/catalog/github-issues.json"] {
let response = app
.clone()
.oneshot(Request::builder().uri(path).body(Body::empty()).unwrap())
.await
.unwrap();
assert_eq!(response.status(), StatusCode::NOT_FOUND);
}
}
#[tokio::test]
async fn a_produced_class_declaration_passes_through_unchanged() {
use crate::config::OVERLAYS_PAGES_BASE;
let produces = serde_json::json!([
{
"class": "https://ontology.example/classes/time-entry-v1",
"lens": "https://ontology.example/lenses/example-time-entry-v1",
"description": "Een lopende timer heeft geen einde: \"end\" ontbreekt.",
"x-note": {"since": "2026-10-08", "weights": [1, 2.5, null, true]}
},
{"class": "urn:example:classes:cost-centre"}
]);
let mirror = tempfile_path("mirror");
let source = mirror.join("example");
fs::create_dir_all(&source).unwrap();
fs::write(
source.join("openapi.yaml"),
"openapi: 3.0.3\ninfo: {title: Example, version: '1'}\nservers: [{url: 'https://api.example'}]\npaths: {}\ncomponents: {}\n",
)
.unwrap();
let resources = serde_json::json!({
"overlay": "1.0.0",
"info": {"title": "CRUD", "version": "1"},
"actions": [{"target": "$.components", "update": {"crudResources": {
"timeEntry": {"identity": {"urlTemplate": "/time-entries/{id}"}},
"project": {"identity": {"urlTemplate": "/projects/{id}"}}
}}}]
});
let classes = serde_json::json!({
"overlay": "1.0.0",
"info": {"title": "Produced classes", "version": "1"},
"actions": [{"target": "$.components.crudResources.timeEntry",
"update": {"x-produces": produces}}]
});
fs::write(
source.join("crud-overlay.yaml"),
serde_yaml::to_string(&resources).unwrap(),
)
.unwrap();
fs::write(
source.join("produces-overlay.yaml"),
serde_yaml::to_string(&classes).unwrap(),
)
.unwrap();
let catalog_file = mirror.join("catalog.json");
fs::write(
&catalog_file,
serde_json::json!({"platforms": [{
"name": "example",
"openapi": format!("{OVERLAYS_PAGES_BASE}example/openapi.yaml"),
"overlays": [
format!("{OVERLAYS_PAGES_BASE}example/crud-overlay.yaml"),
format!("{OVERLAYS_PAGES_BASE}example/produces-overlay.yaml"),
],
}]})
.to_string(),
)
.unwrap();
let catalog = Catalog::load_with_mirror(
&catalog_file.to_string_lossy(),
&crate::build_http_client(),
Some(&mirror),
)
.await
.unwrap();
let mut state = crate::test_support::state(None);
state.catalog = catalog;
let app = crate::router(state);
let get = |uri: &'static str| {
let app = app.clone();
async move {
let response = app
.oneshot(Request::builder().uri(uri).body(Body::empty()).unwrap())
.await
.unwrap();
assert_eq!(response.status(), StatusCode::OK, "{uri}");
to_bytes(response.into_body(), usize::MAX).await.unwrap()
}
};
let document: Value = serde_yaml::from_slice(&get("/catalog/example.yaml").await).unwrap();
assert_eq!(
document["components"]["crudResources"]["timeEntry"]["x-produces"],
produces
);
assert!(document["components"]["crudResources"]["project"]
.get("x-produces")
.is_none());
assert!(document.get("x-produces").is_none());
fs::remove_dir_all(mirror.parent().unwrap()).unwrap();
}
#[tokio::test]
async fn consumer_selection_is_separate_from_api_metadata() {
let app = test_router();
for (name, expected) in [
(
"moneybird",
serde_json::json!({"query_overrides":[{"path":"/records","values":{"include_archived":true}}]}),
),
("github-issues", serde_json::json!({})),
] {
let response = app
.clone()
.oneshot(
Request::builder()
.uri(format!("/catalog/{name}.selection.json"))
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(response.status(), StatusCode::OK);
let value: Value =
serde_json::from_slice(&to_bytes(response.into_body(), usize::MAX).await.unwrap())
.unwrap();
assert_eq!(value, expected);
let response = app
.clone()
.oneshot(
Request::builder()
.uri(format!("/catalog/{name}.yaml"))
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
let document: Value =
serde_yaml::from_slice(&to_bytes(response.into_body(), usize::MAX).await.unwrap())
.unwrap();
assert!(document.get("selection").is_none());
assert!(document.get("query_overrides").is_none());
}
let response = app
.oneshot(
Request::builder()
.uri("/catalog/unknown.selection.json")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(response.status(), StatusCode::NOT_FOUND);
assert!(parse_catalog_config(
r#"{"platforms":[{"name":"example","openapi":"https://example.com","selection":[]}]}"#,
"fixture"
)
.is_err());
}
#[test]
fn oauth_scheme_selection_rejects_non_string_values() {
let mut catalog = Catalog::for_test("fixture");
catalog.selections.insert(
"fixture".into(),
serde_json::json!({"oauthSecurityScheme": null}),
);
assert_eq!(
catalog.oauth_provider("fixture").unwrap_err(),
"oauthSecurityScheme selection must be a string"
);
}
fn mixed_catalog(selection: Value) -> Catalog {
Catalog::from_test_document(
"mixed",
crate::test_support::mixed_profiles_document("https://api.example/v1"),
selection,
)
}
#[test]
fn a_selected_profile_allows_only_the_operations_it_covers() {
use serde_json::json;
let user = mixed_catalog(json!({"authenticationProfile": "user"}));
assert_eq!(
user.oauth_provider("mixed").unwrap().scopes,
["guilds", "identify"]
);
for path in ["/v1/users/@me", "/v1/users/@me/guilds"] {
assert!(user.allows("mixed", "GET", path).is_some(), "{path}");
}
for (method, path) in [
("GET", "/v1/channels/1/messages"),
("POST", "/v1/channels/1/messages"),
("GET", "/v1/public"),
("GET", "/v1/combined"),
("GET", "/v1/implicit"),
] {
assert!(
user.allows("mixed", method, path).is_none(),
"{method} {path}"
);
}
let bot = mixed_catalog(json!({"authenticationProfile": "bot"}));
assert!(matches!(
bot.security_scheme("mixed"),
Ok(crate::providers::SecurityScheme::ApiKey(_))
));
assert!(bot.oauth_provider("mixed").is_err());
for (method, path) in [
("GET", "/v1/users/@me"),
("GET", "/v1/channels/1/messages"),
("POST", "/v1/channels/1/messages"),
] {
assert!(
bot.allows("mixed", method, path).is_some(),
"{method} {path}"
);
}
for path in ["/v1/users/@me/guilds", "/v1/public", "/v1/combined"] {
assert!(bot.allows("mixed", "GET", path).is_none(), "{path}");
}
}
#[test]
fn a_mixed_kind_platform_without_a_valid_profile_selection_is_refused() {
use serde_json::json;
for selection in [
json!({}),
json!({"oauthSecurityScheme": "userOAuth"}),
json!({"apiKeySecurityScheme": "botToken"}),
json!({"authenticationProfile": "nobody"}),
json!({"authenticationProfile": null}),
json!({"authenticationProfile": ["user"]}),
json!({"authenticationProfile": "user", "oauthSecurityScheme": "userOAuth"}),
json!({"authenticationProfile": "bot", "apiKeySecurityScheme": "botToken"}),
json!({"authenticationProfile": "bot", "httpSecurityScheme": "botToken"}),
json!({"httpSecurityScheme": "botToken"}),
] {
let catalog = mixed_catalog(selection.clone());
assert!(catalog.security_scheme("mixed").is_err(), "{selection}");
assert!(catalog.oauth_provider("mixed").is_err(), "{selection}");
}
for selection in [
json!({"authenticationProfile": "nobody"}),
json!({"authenticationProfile": 1}),
json!({"authenticationProfile": "user", "oauthSecurityScheme": "userOAuth"}),
json!({"authenticationProfile": "user", "httpSecurityScheme": "userOAuth"}),
] {
let catalog = mixed_catalog(selection.clone());
assert!(
catalog.allows("mixed", "GET", "/v1/users/@me").is_none(),
"{selection}"
);
}
}
#[test]
fn an_http_selection_picks_one_of_several_http_schemes() {
use serde_json::json;
let catalog = |selection| {
Catalog::from_test_document(
"service",
json!({
"servers": [{"url": "https://api.service.example"}],
"components": {"securitySchemes": {
"pat": {"type": "http", "scheme": "bearer"},
"basicToken": {"type": "http", "scheme": "basic",
"x-api-key-details": {"basicCredentials":
{"token": "username", "password": ""}}}
}},
"security": [{"pat": []}, {"basicToken": []}],
"paths": {"/items": {"get": {}}}
}),
selection,
)
};
assert!(catalog(json!({})).security_scheme("service").is_err());
assert!(catalog(json!({"httpSecurityScheme": 1}))
.security_scheme("service")
.is_err());
assert!(matches!(
catalog(json!({"httpSecurityScheme": "pat"})).security_scheme("service"),
Ok(crate::providers::SecurityScheme::Http(
crate::providers::HttpScheme {
auth: crate::providers::HttpAuth::Bearer,
..
}
))
));
assert!(matches!(
catalog(json!({"httpSecurityScheme": "basicToken"})).security_scheme("service"),
Ok(crate::providers::SecurityScheme::Http(
crate::providers::HttpScheme {
auth: crate::providers::HttpAuth::Basic(_),
..
}
))
));
assert!(catalog(json!({"httpSecurityScheme": "pat"}))
.oauth_provider("service")
.is_err());
}
async fn checked_in_platform(file: &str, name: &str) -> Catalog {
let overlays = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../overlays");
let catalog: Value =
serde_json::from_str(&fs::read_to_string(overlays.join(file)).unwrap()).unwrap();
let platform = catalog["platforms"]
.as_array()
.unwrap()
.iter()
.find(|platform| platform["name"] == name)
.unwrap_or_else(|| panic!("{file} lists {name}"))
.clone();
let only = tempfile_path(&format!("{name}-catalog.json"));
fs::write(
&only,
serde_json::json!({"platforms": [platform]}).to_string(),
)
.unwrap();
Catalog::load_with_mirror(
&only.to_string_lossy(),
&crate::build_http_client(),
Some(&overlays),
)
.await
.unwrap()
}
#[tokio::test]
#[ignore = "downloads the pinned Google Keep OAD"]
async fn default_catalog_keep_selects_note_reads_and_writes() {
let catalog = checked_in_platform(crate::config::DEFAULT_CATALOG_FILE, "google-keep").await;
let provider = catalog.oauth_provider("google-keep").unwrap();
assert_eq!(provider.scopes, ["https://www.googleapis.com/auth/keep"]);
for (method, path) in [
("GET", "/v1/notes"),
("POST", "/v1/notes"),
("GET", "/v1/notes/invented-note"),
("DELETE", "/v1/notes/invented-note"),
] {
assert!(catalog.allows("google-keep", method, path).is_some());
}
for (method, path) in [
("PATCH", "/v1/notes/invented-note"),
("PUT", "/v1/notes/invented-note"),
("GET", "/v1/notes/invented-note/attachments/invented-image"),
("POST", "/v1/notes/invented-note/permissions:batchCreate"),
("POST", "/v1/notes/invented-note/permissions:batchDelete"),
] {
assert!(catalog.allows("google-keep", method, path).is_none());
}
let document: Value = serde_yaml::from_str(catalog.get("google-keep").unwrap()).unwrap();
assert_eq!(
document["components"]["crudResources"]["note"]["identity"]["bindings"]["name"]
["field"],
"name"
);
assert_eq!(
document["components"]["paginationSchemes"]["forwardPages"]["response"]["envelope"]
["emptyWhenAbsent"],
true
);
}
#[tokio::test]
#[ignore = "downloads the pinned full Discord OAD"]
async fn default_catalog_discord_selects_the_user_profile_of_the_full_oad() {
let catalog = checked_in_platform(crate::config::DEFAULT_CATALOG_FILE, "discord").await;
let crate::providers::SecurityScheme::OAuth(provider) =
catalog.security_scheme("discord").unwrap()
else {
panic!("discord: expected the OAuth user profile");
};
assert_eq!(provider.scopes, ["guilds", "identify"]);
assert_eq!(
provider.authorization_url,
"https://discord.com/oauth2/authorize"
);
assert_eq!(
provider.token_url,
"https://discord.com/api/v10/oauth2/token"
);
assert_eq!(catalog.oauth_provider("discord"), Ok(provider));
let document: Value = serde_yaml::from_str(catalog.get("discord").unwrap()).unwrap();
let operations: Vec<(&String, &str, &Value)> = document["paths"]
.as_object()
.unwrap()
.iter()
.flat_map(|(path, item)| {
["get", "put", "post", "delete", "patch"]
.into_iter()
.filter_map(move |method| Some((path, method, item.get(method)?)))
})
.collect();
assert!(operations.len() > 200, "{}", operations.len());
let covered: Vec<_> = operations
.iter()
.filter(|(_, _, operation)| {
crate::providers::covers(&document, operation, "discordOAuth")
})
.map(|(path, method, _)| format!("{method} {path}"))
.collect();
assert_eq!(covered, ["get /users/@me", "get /users/@me/guilds"]);
for path in ["/api/v10/users/@me", "/api/v10/users/@me/guilds"] {
assert_eq!(
catalog.allows("discord", "GET", path).unwrap().as_str(),
"https://discord.com/api/v10"
);
}
for (method, path) in [
("GET", "/api/v10/channels/123/messages"),
("POST", "/api/v10/channels/123/messages"),
("GET", "/api/v10/guilds/123"),
("GET", "/api/v10/users/@me/guilds/123/member"),
("GET", "/api/v10/oauth2/@me"),
("DELETE", "/api/v10/users/@me/guilds/123"),
] {
assert!(
operations.iter().any(|(template, m, _)| {
*m == method.to_ascii_lowercase()
&& path_matches(template, path.strip_prefix("/api/v10").unwrap())
}),
"{method} {path} should be in the OAD"
);
assert!(
catalog.allows("discord", method, path).is_none(),
"{method} {path}"
);
}
let crate::providers::SecurityScheme::ApiKey(bot) =
crate::providers::SecurityScheme::for_profile(&document, "discordBot").unwrap()
else {
panic!("discordBot: expected an apiKey profile");
};
assert_eq!(bot.name, "Authorization");
let bot_covered = operations
.iter()
.filter(|(_, _, operation)| crate::providers::covers(&document, operation, "BotToken"))
.count();
assert!(bot_covered > 200, "{bot_covered}");
let dated = checked_in_platform("catalog/2026-10-02.json", "discord").await;
assert!(dated
.security_scheme("discord")
.unwrap_err()
.contains("mixed-kind catalogs are not supported"));
assert!(dated.oauth_provider("discord").is_err());
}
#[tokio::test]
async fn router_reaches_parameterized_oauth_handlers() {
let app = test_router();
for provider in ["github-issues", "google-calendar", "moneybird", "discord"] {
for (action, query) in [("callback", "code=test&state=test")] {
let response = app
.clone()
.oneshot(
Request::builder()
.uri(format!("/oauth/{provider}/{action}?{query}"))
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(
response.status(),
StatusCode::BAD_REQUEST,
"{provider}/{action}"
);
let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
assert_eq!(&body[..], b"OAuth request could not be completed");
}
}
}
#[tokio::test]
async fn catalog_load_checks_extends_before_applying_overlays() {
use crate::config::OVERLAYS_PAGES_BASE;
let root = std::env::temp_dir().join(format!("overlay-extends-{}", rand::random::<u64>()));
fs::create_dir_all(root.join("sub")).unwrap();
let openapi = format!("{OVERLAYS_PAGES_BASE}base.yaml");
let overlay_url = format!("{OVERLAYS_PAGES_BASE}sub/overlay.yaml");
fs::write(
root.join("base.yaml"),
"openapi: 3.0.0\ninfo: {title: base, version: 1.0.0}\npaths: {}\n",
)
.unwrap();
let config = root.join("catalog.json");
fs::write(&config, serde_json::json!({"platforms": [{"name": "fixture", "openapi": openapi, "overlays": [overlay_url]}]}).to_string()).unwrap();
let client = reqwest::Client::new();
for extends in [None, Some(openapi.as_str()), Some("../base.yaml")] {
let mut overlay = serde_json::json!({"overlay": "1.0.0", "actions": [{"target": "$", "update": {"info": {"title": "overlaid"}}}]});
if let Some(extends) = extends {
overlay["extends"] = serde_json::json!(extends);
}
fs::write(root.join("sub/overlay.yaml"), overlay.to_string()).unwrap();
let loaded = Catalog::load_with_mirror(config.to_str().unwrap(), &client, Some(&root))
.await
.unwrap();
let document: Value = serde_yaml::from_str(loaded.get("fixture").unwrap()).unwrap();
assert_eq!(document["info"]["title"], "overlaid");
}
for extends in ["../other.yaml", "https://example.com/revision-b/base.yaml"] {
fs::write(root.join("sub/overlay.yaml"), serde_json::json!({"extends": extends, "actions": [{"target": "$.missing", "update": {}}]}).to_string()).unwrap();
let error = Catalog::load_with_mirror(config.to_str().unwrap(), &client, Some(&root))
.await
.err()
.unwrap();
assert!(error.contains("but catalog OAD is"), "{error}");
assert!(error.contains(&overlay_url));
}
fs::remove_dir_all(root).unwrap();
}
#[test]
fn target_parser_supports_overlay_paths() {
assert_eq!(
parse_target("$.paths['/things/{id}'].get").unwrap(),
["paths", "/things/{id}", "get"]
);
}
#[test]
fn merge_preserves_existing_document_fields() {
let mut document = serde_json::json!({"components": {"schemas": {"old": true}}});
merge_at_target(
&mut document,
"$.components",
serde_json::json!({"securitySchemes": {"token": {"type": "http"}}}),
)
.unwrap();
assert_eq!(document["components"]["schemas"]["old"], true);
assert_eq!(
document["components"]["securitySchemes"]["token"]["type"],
"http"
);
}
#[test]
fn catalog_config_accepts_json_fixture_and_rejects_invalid_names() {
let config = parse_catalog_config(
r#"{"platforms":[{"name":"example","openapi":"https://example.com/oad.json"}]}"#,
"fixture.json",
)
.unwrap();
assert_eq!(config.platforms[0].name, "example");
assert!(valid_platform_name("bad_name").is_err());
}
#[tokio::test]
async fn a_ref_path_item_is_refused_at_load() {
use crate::config::OVERLAYS_PAGES_BASE;
let mirror = tempfile_path("mirror");
let source = mirror.join("example");
fs::create_dir_all(&source).unwrap();
let load = |paths: Value| {
let (mirror, source) = (mirror.clone(), source.clone());
async move {
fs::write(
source.join("openapi.yaml"),
serde_yaml::to_string(&serde_json::json!({
"openapi": "3.1.0",
"info": {"title": "Example", "version": "1"},
"servers": [{"url": "https://api.example"}],
"paths": paths,
"components": {"pathItems": {"shared": {"get": {}}}}
}))
.unwrap(),
)
.unwrap();
let catalog_file = mirror.join("catalog.json");
fs::write(
&catalog_file,
serde_json::json!({"platforms": [{
"name": "example",
"openapi": format!("{OVERLAYS_PAGES_BASE}example/openapi.yaml"),
}]})
.to_string(),
)
.unwrap();
Catalog::load_with_mirror(
&catalog_file.to_string_lossy(),
&crate::build_http_client(),
Some(&mirror),
)
.await
}
};
let catalog = load(serde_json::json!({"/items": {"get": {}}}))
.await
.unwrap();
assert!(catalog.allows("example", "GET", "/items").is_some());
for paths in [
serde_json::json!({"/items": {"get": {}}, "/shared": {"$ref": "#/components/pathItems/shared"}}),
serde_json::json!({"/shared": "not an object"}),
] {
let error = load(paths).await.err().expect("refused");
assert!(
error.contains(
"platform \"example\": path item \"/shared\" is a $ref or not an object"
),
"{error}"
);
}
fs::remove_dir_all(mirror.parent().unwrap()).unwrap();
}
#[test]
fn catalog_rejects_duplicate_platform_names() {
let mut documents = BTreeMap::new();
insert_document(&mut documents, "example", "{}".to_string()).unwrap();
assert_eq!(
insert_document(&mut documents, "example", "{}".to_string()).unwrap_err(),
"duplicate platform name \"example\""
);
}
}