diff --git a/src/album.rs b/src/album.rs index a17c16b..a331c90 100644 --- a/src/album.rs +++ b/src/album.rs @@ -106,13 +106,31 @@ pub async fn set_title_album(c: &LycheeClient, l: &str, id: &str, title: &str) { assert_eq!(b, "true", "The server returned an unexpected value: {}.", b); } +pub async fn set_description_album(c: &LycheeClient, l: &str, id: &str, desc: &str) { + if desc.len() > 1000 { panic!("Description can't be longer than 1000 characters."); } + let b = json!({"albumID" : id, "description": desc}); + let req = Request::builder() + .method(Method::POST) + .uri(c.endpoint.to_string() + "/api/Album::setDescription") + .header(COOKIE, l) + .header(AUTHORIZATION, c.api_key.to_string()) + .header(CONTENT_TYPE, "application/json") + .body(Body::from(b.to_string())) + .expect("Cannot request /api/Album:setTitle."); + let res = c.client.request(req).await.unwrap(); + let s = res.status(); + let b = body_to_str(res.into_body()).await; + assert_ne!(s, 500, "The server returned an internal error."); + assert_eq!(b, "true", "The server returned an unexpected value: {}.", b); +} + #[cfg(test)] mod album_tests { use hyper_tls::HttpsConnector; use hyper::Client; use mockito::mock; use serde_json::json; - use crate::{LycheeClient, get_album}; + use crate::{LycheeClient, album::set_description_album, get_album}; use super::{add_album, delete_album, set_title_album}; @@ -125,6 +143,60 @@ mod album_tests { }; } + #[test] + fn set_description_album_correct_values() { + let client = setup(); + let m1 = mock("POST", "/api/Album::setDescription") + .match_header("cookie", "v") + .match_header("content-type", "application/json") + .with_body("true") + .match_body(json!({"albumID": "1", "description": "a"}).to_string().as_str()) + .create(); + + tokio_test::block_on(set_description_album(&client, "v", "1", "a")); + m1.assert(); + + let m2 = mock("POST", "/api/Album::setDescription") + .match_header("cookie", "v") + .match_header("content-type", "application/json") + .with_body("true") + .match_body(json!({"albumID": "1", "description": "abc"}).to_string().as_str()) + .create(); + + tokio_test::block_on(set_description_album(&client, "v", "1", "abc")); + m2.assert(); + } + + #[test] + #[should_panic] + fn set_description_album_description_too_long() { + let client = setup(); + let m = mock("POST", "/api/Album::setDescription") + .match_header("cookie", "v") + .match_header("content-type", "application/json") + .with_body("true") + .create(); + + let mut b = "a".to_string(); + for _ in 1..=1001 { b += "a"; } + tokio_test::block_on(set_description_album(&client, "v", "1", b.as_str())); + m.assert(); + } + + #[test] + #[should_panic] + fn set_description_album_false_returned() { + let client = setup(); + let m = mock("POST", "/api/Album::setDescription") + .match_header("cookie", "v") + .match_header("content-type", "application/json") + .with_body("false") + .create(); + + tokio_test::block_on(set_description_album(&client, "v", "1", "k")); + m.assert(); + } + #[test] fn set_title_album_correct_values() { let client = setup(); diff --git a/src/main.rs b/src/main.rs index ac940bc..30cb0ab 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use crate::{album::{add_album, delete_album, get_album, set_title_album}, albums::get_albums, config::{get_session_file, read_config, setup_config_data_storage}, session::login, utils::{body_to_str, numeric_validator, read_from_file, write_to_file}}; +use crate::{album::{add_album, delete_album, get_album, set_description_album, set_title_album}, albums::get_albums, config::{get_session_file, read_config, setup_config_data_storage}, session::login, utils::{body_to_str, numeric_validator, read_from_file, write_to_file}}; use hyper_tls::HttpsConnector; use hyper::{Client, client::HttpConnector}; use clap::{App, Arg, SubCommand, crate_version}; @@ -90,13 +90,29 @@ async fn main() -> Result<(), Box> { .long("title") .value_name("TITLE") .required(true) - .help("The title to set for the albums to rename.")) + .help("The title to set for the albums to rename. Must be under 100 characters long.")) .arg(Arg::with_name("id") .short("i") .long("id") - .help("The album ID to delete. For multiple IDs, separate them using commas.") + .help("The album ID(s) to rename. For multiple IDs, separate them using commas.") .required(true) .value_name("ID(s)")) + ) + .subcommand( + SubCommand::with_name("set_album_description") + .about("Set the description for one or more album(s).") + .arg(Arg::with_name("description") + .short("d") + .long("description") + .value_name("DESCRIPTION") + .required(true) + .help("The description to set for the albums to rename. Must be under 1000 characters long.")) + .arg(Arg::with_name("id") + .short("i") + .long("id") + .help("The album ID to rename.") + .required(true) + .value_name("ID")) ); let matches = app.clone().get_matches(); @@ -138,6 +154,10 @@ async fn main() -> Result<(), Box> { let t = m.value_of("title").unwrap(); let i = m.value_of("id").unwrap(); set_title_album(&client, &lychee_session_cookie, i, t).await; + } else if let Some(m) = matches.subcommand_matches("set_album_description") { + let d = m.value_of("description").unwrap(); + let i = m.value_of("id").unwrap(); + set_description_album(&client, &lychee_session_cookie, i, d).await; } else { App::print_long_help(&mut app).unwrap(); }