From 85a876b221156b5b7190dd8159ef4b52f083dcf7 Mon Sep 17 00:00:00 2001 From: askiiart Date: Mon, 25 Nov 2024 22:23:05 -0600 Subject: [PATCH] improve and move around tests, make the API part of it function --- Cargo.lock | 39 ++++++++++++++++----- Cargo.toml | 1 + src/api.rs | 99 +++------------------------------------------------- src/dummy.rs | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 20 +++++++++-- 5 files changed, 152 insertions(+), 106 deletions(-) create mode 100644 src/dummy.rs diff --git a/Cargo.lock b/Cargo.lock index e4ade1d..7effb62 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,27 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "actix-macros" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e01ed3140b2f8d422c68afa1ed2e85d996ea619c988ac834d255db32138655cb" +dependencies = [ + "quote", + "syn", +] + +[[package]] +name = "actix-rt" +version = "2.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24eda4e2a6e042aa4e55ac438a2ae052d3b5da0ecf83d7411e1a368946925208" +dependencies = [ + "actix-macros", + "futures-core", + "tokio", +] + [[package]] name = "addr2line" version = "0.24.2" @@ -372,9 +393,9 @@ dependencies = [ [[package]] name = "hashbrown" -version = "0.15.1" +version = "0.15.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3a9bfc1af68b1726ea47d3d5109de126281def866b33970e10fbab11b5dafab3" +checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289" [[package]] name = "hermit-abi" @@ -487,9 +508,9 @@ dependencies = [ [[package]] name = "itoa" -version = "1.0.13" +version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "540654e97a3f4470a492cd30ff187bc95d89557a903a2bbf112e2fae98104ef2" +checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674" [[package]] name = "lazy_static" @@ -499,9 +520,9 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" [[package]] name = "libc" -version = "0.2.164" +version = "0.2.165" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "433bfe06b8c75da9b2e3fbea6e5329ff87748f0b144ef75306e674c3f6f7c13f" +checksum = "fcb4d3d38eab6c5239a362fa8bae48c03baf980a6e7079f063942d563ef3533e" [[package]] name = "linux-raw-sys" @@ -1174,6 +1195,7 @@ dependencies = [ "bytes", "libc", "mio", + "parking_lot", "pin-project-lite", "signal-hook-registry", "socket2", @@ -1254,6 +1276,7 @@ dependencies = [ name = "torznab-toolkit" version = "0.1.0" dependencies = [ + "actix-rt", "rocket", "serde", "xml-rs", @@ -1289,9 +1312,9 @@ dependencies = [ [[package]] name = "tracing-core" -version = "0.1.32" +version = "0.1.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" +checksum = "e672c95779cf947c5311f83787af4fa8fffd12fb27e4993211a84bdfd9610f9c" dependencies = [ "once_cell", "valuable", diff --git a/Cargo.toml b/Cargo.toml index 4dbfc86..ca3ef68 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" edition = "2021" [dependencies] +actix-rt = "2.10.0" rocket = "0.5.1" serde = { version = "1.0.215", features = ["derive"] } xml-rs = "0.8.23" diff --git a/src/api.rs b/src/api.rs index d93cdff..c49e3cb 100644 --- a/src/api.rs +++ b/src/api.rs @@ -2,9 +2,9 @@ use std::io::stdout; use crate::data::*; -use rocket::get; use rocket::http::Status; use rocket::response::status; +use rocket::{get, response::content::RawXml}; use xml::writer::{EmitterConfig, XmlEvent}; /// Holds the config for torznab-toolkit. @@ -20,13 +20,13 @@ pub static mut CONFIG: Option = None; // FIXME: VERY incomplete // TODO: Finish it (duh) and add optional apikey #[get("/api?t=caps")] -pub(crate) fn caps() -> status::Custom { +pub(crate) fn caps() -> status::Custom> { unsafe { match CONFIG { None => { return status::Custom( Status::InternalServerError, - "500 Internal server error: Config not specified".to_string(), + RawXml("500 Internal server error: Config not specified".to_string()), ); } Some(_) => {} @@ -41,96 +41,5 @@ pub(crate) fn caps() -> status::Custom { writer.write(XmlEvent::end_element()).unwrap(); writer.write(XmlEvent::start_element("caps")).unwrap(); writer.write(XmlEvent::end_element()).unwrap(); - return status::Custom(Status::Ok, stringify!(writer).to_string()); -} - -#[cfg(test)] -mod tests { - use super::*; - - fn dummy_search_func(a: String, b: Vec) -> Result { - return Ok("hi".to_string()); - } - - fn dummy_auth_func(a: String) -> Result { - return Ok(true); - } - - fn create_config() -> Config { - let mut searching = Vec::new(); - searching.push(SearchInfo { - search_type: "search".to_string(), - available: true, - supported_params: vec!["id".to_string()], - }); - - let mut subcategories = Vec::new(); - subcategories.push(Subcategory { - id: "a".to_string(), - name: "b".to_string(), - }); - - let mut categories = Vec::new(); - categories.push(Category { - id: "a".to_string(), - name: "b".to_string(), - subcategories: subcategories, - }); - - let mut genres = Vec::new(); - genres.push(Genre { - id: "a".to_string(), - category_id: "b".to_string(), - name: "c".to_string(), - }); - - let mut tags = Vec::new(); - tags.push(Tag { - id: "a".to_string(), - category_id: "b".to_string(), - name: "c".to_string(), - }); - - return Config { - search: dummy_search_func, - auth: Some(dummy_auth_func), - caps: Caps { - server_info: ServerInfo { - title: Some("Test Torznab server".to_string()), - email: Some("test@example.com".to_string()), - image: None, - version: Some("1.0".to_string()), - }, - limits: Limits { - max: 100, - default: 20, - }, - searching: searching, - categories: categories, - genres: Some(genres), - tags: Some(tags), - }, - book: None, - movie: None, - music: None, - tvsearch: None, - }; - } - - #[test] - fn test_with_config() { - unsafe { - CONFIG = Some(create_config()); - println!("{:?}", CONFIG); - } - println!("{:?}", caps()); - } - - #[test] - fn test_empty_config() { - unsafe { - println!("{:?}", CONFIG); - } - println!("{:?}", caps()); - } + return status::Custom(Status::Ok, RawXml(stringify!(writer).to_string())); } diff --git a/src/dummy.rs b/src/dummy.rs new file mode 100644 index 0000000..29e0ba1 --- /dev/null +++ b/src/dummy.rs @@ -0,0 +1,99 @@ +//! Some dummy stuff for testing the API +use crate::data::*; + +fn dummy_search_func(a: String, b: Vec) -> Result { + return Ok("hi".to_string()); +} + +fn dummy_auth_func(a: String) -> Result { + return Ok(true); +} + +/// Creates a bare-minimum config +pub fn create_empty_config() -> Config { + let mut searching = Vec::new(); + searching.push(SearchInfo { + search_type: "search".to_string(), + available: true, + supported_params: vec!["id".to_string()], + }); + + let mut subcategories = Vec::new(); + subcategories.push(Subcategory { + id: "a".to_string(), + name: "b".to_string(), + }); + + let mut categories = Vec::new(); + categories.push(Category { + id: "a".to_string(), + name: "b".to_string(), + subcategories: subcategories, + }); + + let mut genres = Vec::new(); + genres.push(Genre { + id: "a".to_string(), + category_id: "b".to_string(), + name: "c".to_string(), + }); + + let mut tags = Vec::new(); + tags.push(Tag { + id: "a".to_string(), + category_id: "b".to_string(), + name: "c".to_string(), + }); + + return Config { + search: dummy_search_func, + auth: Some(dummy_auth_func), + caps: Caps { + server_info: ServerInfo { + title: Some("Test Torznab server".to_string()), + email: Some("test@example.com".to_string()), + image: None, + version: Some("1.0".to_string()), + }, + limits: Limits { + max: 100, + default: 20, + }, + searching: searching, + categories: categories, + genres: Some(genres), + tags: Some(tags), + }, + book: None, + movie: None, + music: None, + tvsearch: None, + }; +} + +#[cfg(test)] +mod tests { + use crate::{dummy::create_empty_config, run}; + + #[test] + fn caps_test_with_empty_config() { + unsafe { + crate::api::CONFIG = Some(create_empty_config()); + println!("{:?}", crate::api::CONFIG); + } + println!("{:?}", crate::api::caps()); + } + + #[test] + fn caps_test_no_config() { + unsafe { + println!("{:?}", crate::api::CONFIG); + } + println!("{:?}", crate::api::caps()); + } + + #[actix_rt::test] + async fn api_with_empty() { + run(create_empty_config()).await.unwrap(); + } +} diff --git a/src/lib.rs b/src/lib.rs index 9e08eec..b01bf93 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,11 +20,25 @@ pub mod api; pub mod data; +mod dummy; use rocket::{self}; /// Runs the server -pub fn run(conf: data::Config, caps: data::Caps) -> Result { - rocket::build().mount("/", rocket::routes![]).launch(); - return Ok(true); +pub async fn run(conf: data::Config) -> Result { + unsafe { + api::CONFIG = Some(conf); + } + match rocket::build() + .mount("/", rocket::routes![api::caps]) + .launch() + .await + { + Ok(_) => { + return Ok(true); + } + Err(e) => { + return Err(e); + } + } }