mirror of
https://git.proxmox.com/git/proxmox
synced 2025-08-07 05:22:00 +00:00
api-macro: add expanded data to tests for verification
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
parent
b0ef405186
commit
679ad01c03
@ -16,6 +16,7 @@ syn = { version = "1.0", features = [ "full" ] }
|
|||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
futures = "0.3"
|
futures = "0.3"
|
||||||
proxmox = { path = "../proxmox" }
|
proxmox = { path = "../proxmox" }
|
||||||
|
proxmox-api = { path = "../proxmox-api", features = [ "test-harness" ] }
|
||||||
serde = "1.0"
|
serde = "1.0"
|
||||||
serde_derive = "1.0"
|
serde_derive = "1.0"
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
|
@ -42,6 +42,61 @@ pub fn create_ticket(_param: Value) -> Result<Value, Error> {
|
|||||||
panic!("implement me");
|
panic!("implement me");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn create_ticket_schema_check() {
|
||||||
|
const TEST_METHOD: ::proxmox::api::ApiMethod = ::proxmox::api::ApiMethod::new(
|
||||||
|
&::proxmox::api::ApiHandler::Sync(&api_function_create_ticket),
|
||||||
|
&::proxmox::api::schema::ObjectSchema::new(
|
||||||
|
"Create or verify authentication ticket.",
|
||||||
|
&[
|
||||||
|
(
|
||||||
|
"password",
|
||||||
|
false,
|
||||||
|
&::proxmox::api::schema::StringSchema::new(
|
||||||
|
"The secret password or a valid ticket.",
|
||||||
|
)
|
||||||
|
.schema(),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"username",
|
||||||
|
false,
|
||||||
|
&::proxmox::api::schema::StringSchema::new("User name")
|
||||||
|
.max_length(64)
|
||||||
|
.schema(),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.returns(
|
||||||
|
&::proxmox::api::schema::ObjectSchema::new(
|
||||||
|
"A ticket.",
|
||||||
|
&[
|
||||||
|
(
|
||||||
|
"CSRFPreventionToken",
|
||||||
|
false,
|
||||||
|
&::proxmox::api::schema::StringSchema::new(
|
||||||
|
"Cross Site Request Forgerty Prevention Token.",
|
||||||
|
)
|
||||||
|
.schema(),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"ticket",
|
||||||
|
false,
|
||||||
|
&::proxmox::api::schema::StringSchema::new("Auth ticket.").schema(),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"username",
|
||||||
|
false,
|
||||||
|
&::proxmox::api::schema::StringSchema::new("User name.").schema(),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
.schema(),
|
||||||
|
)
|
||||||
|
.protected(true);
|
||||||
|
assert_eq!(TEST_METHOD, API_METHOD_CREATE_TICKET);
|
||||||
|
}
|
||||||
|
|
||||||
#[api(
|
#[api(
|
||||||
input: {
|
input: {
|
||||||
properties: {
|
properties: {
|
||||||
@ -85,6 +140,61 @@ pub fn create_ticket_direct(username: String, password: String) -> Result<&'stat
|
|||||||
Ok("an:invalid:ticket")
|
Ok("an:invalid:ticket")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn create_ticket_direct_schema_check() {
|
||||||
|
const TEST_METHOD: ::proxmox::api::ApiMethod = ::proxmox::api::ApiMethod::new(
|
||||||
|
&::proxmox::api::ApiHandler::Sync(&api_function_create_ticket_direct),
|
||||||
|
&::proxmox::api::schema::ObjectSchema::new(
|
||||||
|
"Create or verify authentication ticket.",
|
||||||
|
&[
|
||||||
|
(
|
||||||
|
"password",
|
||||||
|
false,
|
||||||
|
&::proxmox::api::schema::StringSchema::new(
|
||||||
|
"The secret password or a valid ticket.",
|
||||||
|
)
|
||||||
|
.schema(),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"username",
|
||||||
|
false,
|
||||||
|
&::proxmox::api::schema::StringSchema::new("User name")
|
||||||
|
.max_length(64)
|
||||||
|
.schema(),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.returns(
|
||||||
|
&::proxmox::api::schema::ObjectSchema::new(
|
||||||
|
"A ticket.",
|
||||||
|
&[
|
||||||
|
(
|
||||||
|
"CSRFPreventionToken",
|
||||||
|
false,
|
||||||
|
&::proxmox::api::schema::StringSchema::new(
|
||||||
|
"Cross Site Request Forgerty Prevention Token.",
|
||||||
|
)
|
||||||
|
.schema(),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"ticket",
|
||||||
|
false,
|
||||||
|
&::proxmox::api::schema::StringSchema::new("Auth ticket.").schema(),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"username",
|
||||||
|
false,
|
||||||
|
&::proxmox::api::schema::StringSchema::new("User name.").schema(),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
.schema(),
|
||||||
|
)
|
||||||
|
.protected(true);
|
||||||
|
assert_eq!(TEST_METHOD, API_METHOD_CREATE_TICKET_DIRECT);
|
||||||
|
}
|
||||||
|
|
||||||
#[api(
|
#[api(
|
||||||
input: {
|
input: {
|
||||||
properties: {
|
properties: {
|
||||||
@ -124,6 +234,24 @@ pub fn func_with_option(verbose: Option<bool>) -> Result<(), Error> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn func_with_option_schema_check() {
|
||||||
|
const TEST_METHOD: ::proxmox::api::ApiMethod = ::proxmox::api::ApiMethod::new(
|
||||||
|
&::proxmox::api::ApiHandler::Sync(&api_function_func_with_option),
|
||||||
|
&::proxmox::api::schema::ObjectSchema::new(
|
||||||
|
"Optional parameter",
|
||||||
|
&[(
|
||||||
|
"verbose",
|
||||||
|
true,
|
||||||
|
&::proxmox::api::schema::BooleanSchema::new("Verbose output.").schema(),
|
||||||
|
)],
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.protected(false);
|
||||||
|
|
||||||
|
assert_eq!(TEST_METHOD, API_METHOD_FUNC_WITH_OPTION);
|
||||||
|
}
|
||||||
|
|
||||||
struct RpcEnv;
|
struct RpcEnv;
|
||||||
impl proxmox::api::RpcEnvironment for RpcEnv {
|
impl proxmox::api::RpcEnvironment for RpcEnv {
|
||||||
fn set_result_attrib(&mut self, name: &str, value: Value) {
|
fn set_result_attrib(&mut self, name: &str, value: Value) {
|
||||||
|
@ -25,6 +25,19 @@ pub fn get_archive(archive_name: String) {
|
|||||||
let _ = archive_name;
|
let _ = archive_name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn get_archive_schema_check() {
|
||||||
|
const TEST_METHOD: ::proxmox::api::ApiMethod = ::proxmox::api::ApiMethod::new(
|
||||||
|
&::proxmox::api::ApiHandler::Sync(&api_function_get_archive),
|
||||||
|
&::proxmox::api::schema::ObjectSchema::new(
|
||||||
|
"Get an archive.",
|
||||||
|
&[("archive-name", false, &NAME_SCHEMA)],
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.protected(false);
|
||||||
|
assert_eq!(TEST_METHOD, API_METHOD_GET_ARCHIVE);
|
||||||
|
}
|
||||||
|
|
||||||
#[api(
|
#[api(
|
||||||
input: {
|
input: {
|
||||||
properties: {
|
properties: {
|
||||||
@ -41,6 +54,19 @@ pub fn get_archive_2(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<Va
|
|||||||
Ok(json!("test"))
|
Ok(json!("test"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn get_archive_2_schema_check() {
|
||||||
|
const TEST_METHOD: ::proxmox::api::ApiMethod = ::proxmox::api::ApiMethod::new(
|
||||||
|
&::proxmox::api::ApiHandler::Sync(&api_function_get_archive_2),
|
||||||
|
&::proxmox::api::schema::ObjectSchema::new(
|
||||||
|
"Get an archive.",
|
||||||
|
&[("archive-name", false, &NAME_SCHEMA)],
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.protected(false);
|
||||||
|
assert_eq!(TEST_METHOD, API_METHOD_GET_ARCHIVE_2);
|
||||||
|
}
|
||||||
|
|
||||||
#[api(
|
#[api(
|
||||||
input: {
|
input: {
|
||||||
properties: {
|
properties: {
|
||||||
@ -59,3 +85,21 @@ pub fn get_data(param: Value) -> Result<(), Error> {
|
|||||||
let _ = param;
|
let _ = param;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn get_data_schema_test() {
|
||||||
|
const TEST_METHOD: ::proxmox::api::ApiMethod = ::proxmox::api::ApiMethod::new(
|
||||||
|
&::proxmox::api::ApiHandler::Sync(&api_function_get_data),
|
||||||
|
&::proxmox::api::schema::ObjectSchema::new(
|
||||||
|
"Get data.",
|
||||||
|
&[(
|
||||||
|
"data",
|
||||||
|
false,
|
||||||
|
&::proxmox::api::schema::ArraySchema::new("The data", &NAME_SCHEMA).schema(),
|
||||||
|
)],
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.protected(false);
|
||||||
|
|
||||||
|
assert_eq!(TEST_METHOD, API_METHOD_GET_DATA);
|
||||||
|
}
|
||||||
|
@ -16,21 +16,88 @@ use serde_json::Value;
|
|||||||
//#[derive(Clone, Debug, Deserialize, Serialize)]
|
//#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||||
pub struct OkString(String);
|
pub struct OkString(String);
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn ok_string() {
|
||||||
|
const TEST_SCHEMA: &'static ::proxmox::api::schema::Schema =
|
||||||
|
&::proxmox::api::schema::StringSchema::new("A string")
|
||||||
|
.format(&schema::ApiStringFormat::Enum(&["ok", "not-ok"]))
|
||||||
|
.schema();
|
||||||
|
assert_eq!(TEST_SCHEMA, OkString::API_SCHEMA);
|
||||||
|
}
|
||||||
|
|
||||||
#[api]
|
#[api]
|
||||||
/// A Foo.
|
/// An example of a simple struct type.
|
||||||
pub struct Foo {
|
pub struct TestStruct {
|
||||||
/// A test string.
|
/// A test string.
|
||||||
test: String,
|
test_string: String,
|
||||||
|
|
||||||
/// An optional auto-derived value for testing:
|
/// An optional auto-derived value for testing:
|
||||||
another: Option<String>,
|
another: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
// generates the following without the '_' prefix in the constant:
|
#[test]
|
||||||
impl OkString {
|
fn test_struct() {
|
||||||
pub const _API_SCHEMA: &'static schema::Schema = &schema::StringSchema::new("A string")
|
pub const TEST_SCHEMA: &'static ::proxmox::api::schema::Schema =
|
||||||
.format(&schema::ApiStringFormat::Enum(&["ok", "not-ok"]))
|
&::proxmox::api::schema::ObjectSchema::new(
|
||||||
|
"An example of a simple struct type.",
|
||||||
|
&[
|
||||||
|
(
|
||||||
|
"test_string",
|
||||||
|
false,
|
||||||
|
&::proxmox::api::schema::StringSchema::new("A test string.").schema(),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"another",
|
||||||
|
true,
|
||||||
|
&::proxmox::api::schema::StringSchema::new(
|
||||||
|
"An optional auto-derived value for testing:",
|
||||||
|
)
|
||||||
|
.schema(),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
.schema();
|
.schema();
|
||||||
|
|
||||||
|
assert_eq!(TEST_SCHEMA, TestStruct::API_SCHEMA);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[api]
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
#[serde(rename_all = "kebab-case")]
|
||||||
|
/// An example of a struct with renamed fields.
|
||||||
|
pub struct RenamedStruct {
|
||||||
|
/// A test string.
|
||||||
|
test_string: String,
|
||||||
|
|
||||||
|
/// An optional auto-derived value for testing:
|
||||||
|
#[serde(rename = "SomeOther")]
|
||||||
|
another: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn renamed_struct() {
|
||||||
|
const TEST_SCHEMA: &'static ::proxmox::api::schema::Schema =
|
||||||
|
&::proxmox::api::schema::ObjectSchema::new(
|
||||||
|
"An example of a struct with renamed fields.",
|
||||||
|
&[
|
||||||
|
(
|
||||||
|
"test-string",
|
||||||
|
false,
|
||||||
|
&::proxmox::api::schema::StringSchema::new("A test string.").schema(),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"SomeOther",
|
||||||
|
true,
|
||||||
|
&::proxmox::api::schema::StringSchema::new(
|
||||||
|
"An optional auto-derived value for testing:",
|
||||||
|
)
|
||||||
|
.schema(),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
.schema();
|
||||||
|
|
||||||
|
assert_eq!(TEST_SCHEMA, RenamedStruct::API_SCHEMA);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[api]
|
#[api]
|
||||||
@ -44,6 +111,22 @@ pub enum Selection {
|
|||||||
SelectionNumberThree,
|
SelectionNumberThree,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn selection_test() {
|
||||||
|
const TEST_SCHEMA: &'static ::proxmox::api::schema::Schema =
|
||||||
|
&::proxmox::api::schema::StringSchema::new(
|
||||||
|
"A selection of either \'onekind\', \'another-kind\' or \'selection-number-three\'.",
|
||||||
|
)
|
||||||
|
.format(&::proxmox::api::schema::ApiStringFormat::Enum(&[
|
||||||
|
"onekind",
|
||||||
|
"another-kind",
|
||||||
|
"selection-number-three",
|
||||||
|
]))
|
||||||
|
.schema();
|
||||||
|
|
||||||
|
assert_eq!(TEST_SCHEMA, Selection::API_SCHEMA);
|
||||||
|
}
|
||||||
|
|
||||||
// Initial test:
|
// Initial test:
|
||||||
#[api(
|
#[api(
|
||||||
input: {
|
input: {
|
||||||
@ -62,3 +145,21 @@ pub fn string_check(arg: Value, selection: Selection) -> Result<bool, Error> {
|
|||||||
let _ = selection;
|
let _ = selection;
|
||||||
panic!("body")
|
panic!("body")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn string_check_schema_test() {
|
||||||
|
const TEST_METHOD: ::proxmox::api::ApiMethod = ::proxmox::api::ApiMethod::new(
|
||||||
|
&::proxmox::api::ApiHandler::Sync(&api_function_string_check),
|
||||||
|
&::proxmox::api::schema::ObjectSchema::new(
|
||||||
|
"Check a string.",
|
||||||
|
&[
|
||||||
|
("arg", false, OkString::API_SCHEMA),
|
||||||
|
("selection", false, Selection::API_SCHEMA),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.returns(&::proxmox::api::schema::BooleanSchema::new("Whether the string was \"ok\".").schema())
|
||||||
|
.protected(false);
|
||||||
|
|
||||||
|
assert_eq!(TEST_METHOD, API_METHOD_STRING_CHECK);
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user