Skip to content

Commit

Permalink
Merge pull request #88 from superfaceai/test/json_schema
Browse files Browse the repository at this point in the history
Move json schema test next to schemas
  • Loading branch information
freaz authored Aug 16, 2023
2 parents f25f6c6 + 1948ceb commit e938dfa
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 73 deletions.
1 change: 1 addition & 0 deletions core/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 0 additions & 58 deletions core/core/src/sf_core/json_schema_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ impl JsonSchemaValidator {

#[cfg(test)]
mod test {
use std::str::FromStr;

use super::*;
use serde::Deserialize;
use serde_json::json;
Expand Down Expand Up @@ -146,60 +144,4 @@ mod test {
JsonSchemaValidatorError::SchemaError { .. }
));
}

#[test]
fn test_security_values_validator() {
const SECURITY_VALUES_SCHEMA: &str =
include_str!("../../assets/schemas/security_values.json"); // read higher

let security_validator = JsonSchemaValidator::new(
&serde_json::Value::from_str(&SECURITY_VALUES_SCHEMA).unwrap(),
)
.unwrap();

let result = security_validator.validate(
&HostValue::deserialize(&json!({
"my_basic": {
"username": "username",
"password": "password"
},
"my_token": {
"token": "token"
},
"my_api_key": {
"apikey": "api key"
}
}))
.unwrap(),
);
assert!(result.is_ok());

let result = security_validator.validate(
&HostValue::deserialize(&json!({
"security_config": {
"unknown": "so invalid"
}
}))
.unwrap(),
);
assert!(result.is_err());

let result = security_validator.validate(
&HostValue::deserialize(&json!({
"partial_basic": {
"username": "username"
}
}))
.unwrap(),
);
assert!(result.is_err());

let result = security_validator.validate(
&HostValue::deserialize(&json!({
"empty": {}
}))
.unwrap(),
);
assert!(result.is_err());
}
}
3 changes: 3 additions & 0 deletions core/json_schemas/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ publish = false
[dependencies]
serde_json = { workspace = true }
serde_yaml = { version = "0.9" }

[dev-dependencies]
jsonschema = { workspace = true }
10 changes: 7 additions & 3 deletions core/json_schemas/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,16 @@ fn translate(yaml_path: &std::path::PathBuf) {
println!("Translating {:?}", yaml_path);

let content = fs::read_to_string(&yaml_path).expect("Readable JSON schema in YAML format");
let yaml: serde_json::Value = serde_yaml::from_str(&content).expect("Valid YAML");
let json = translate_to_json(&content);

let mut json_path = yaml_path.clone();
json_path.set_extension("json");
let json = serde_json::to_string_pretty(&yaml).unwrap();

let mut file = fs::File::create(json_path.as_path()).expect("Writeble file");
let mut file = fs::File::create(json_path.as_path()).expect("Writable file");
file.write_all(json.as_bytes()).unwrap();
}

pub fn translate_to_json(content: &str) -> String {
let yaml: serde_yaml::Value = serde_yaml::from_str(content).expect("Valid YAML");
serde_json::to_string_pretty(&yaml).unwrap()
}
24 changes: 12 additions & 12 deletions core/json_schemas/src/schemas/security_values.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
{
"type": "object",
"patternProperties": {
"^[a-z][_\\-a-z]*$": {
"type": "object",
"oneOf": [
{
"additionalProperties": false,
"type": "object",
"properties": {
"password": {
"username": {
"type": "string"
},
"username": {
"password": {
"type": "string"
}
},
"required": [
"username",
"password"
],
"type": "object"
"additionalProperties": false
},
{
"additionalProperties": false,
"type": "object",
"properties": {
"token": {
"type": "string"
Expand All @@ -28,10 +30,10 @@
"required": [
"token"
],
"type": "object"
"additionalProperties": false
},
{
"additionalProperties": false,
"type": "object",
"properties": {
"apikey": {
"type": "string"
Expand All @@ -40,11 +42,9 @@
"required": [
"apikey"
],
"type": "object"
"additionalProperties": false
}
],
"type": "object"
]
}
},
"type": "object"
}
}
8 changes: 8 additions & 0 deletions core/json_schemas/tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
macro_rules! json_schema {
($file:expr) => {{
let yaml_str = include_str!($file);
let yaml: serde_yaml::Value = serde_yaml::from_str(yaml_str).expect("Valid YAML");
let json = serde_json::to_value(&yaml).expect("Valid JSON");
jsonschema::JSONSchema::compile(&json).expect("Valid JSON Schema")
}};
}
46 changes: 46 additions & 0 deletions core/json_schemas/tests/security_values_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use serde_json::json;

#[macro_use]
mod common;

#[test]
fn test_security_values() {
let schema = json_schema!("../src/schemas/security_values.yaml");

let instance = json!({
"my_basic": {
"username": "username",
"password": "password"
},
"my_token": {
"token": "token"
},
"my_api_key": {
"apikey": "api key"
}
});
let result = schema.validate(&instance);
assert!(result.is_ok());

let instance = json!({
"security_config": {
"unknown": "so invalid"
}
});
let result = schema.validate(&instance);
assert!(result.is_err());

let instance = json!({
"partial_basic": {
"username": "username"
}
});
let result = schema.validate(&instance);
assert!(result.is_err());

let instance = json!({
"empty": {}
});
let result = schema.validate(&instance);
assert!(result.is_err());
}

0 comments on commit e938dfa

Please sign in to comment.