improve and move around tests, make the API part of it function

This commit is contained in:
askiiart 2024-11-25 22:23:05 -06:00
parent 1beab57613
commit 85a876b221
Signed by untrusted user who does not match committer: askiiart
GPG key ID: EA85979611654C30
5 changed files with 152 additions and 106 deletions

39
Cargo.lock generated
View file

@ -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",

View file

@ -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"

View file

@ -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<Config> = None;
// FIXME: VERY incomplete
// TODO: Finish it (duh) and add optional apikey
#[get("/api?t=caps")]
pub(crate) fn caps() -> status::Custom<String> {
pub(crate) fn caps() -> status::Custom<RawXml<String>> {
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<String> {
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<String>) -> Result<String, String> {
return Ok("hi".to_string());
}
fn dummy_auth_func(a: String) -> Result<bool, String> {
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()));
}

99
src/dummy.rs Normal file
View file

@ -0,0 +1,99 @@
//! Some dummy stuff for testing the API
use crate::data::*;
fn dummy_search_func(a: String, b: Vec<String>) -> Result<String, String> {
return Ok("hi".to_string());
}
fn dummy_auth_func(a: String) -> Result<bool, String> {
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();
}
}

View file

@ -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<bool, String> {
rocket::build().mount("/", rocket::routes![]).launch();
return Ok(true);
pub async fn run(conf: data::Config) -> Result<bool, rocket::Error> {
unsafe {
api::CONFIG = Some(conf);
}
match rocket::build()
.mount("/", rocket::routes![api::caps])
.launch()
.await
{
Ok(_) => {
return Ok(true);
}
Err(e) => {
return Err(e);
}
}
}