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

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);
}
}
}