Added the set_description_album command and the according tests

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2021-11-02 09:25:52 +01:00
parent 50aec4c297
commit 1fbfa1b122
No known key found for this signature in database
GPG Key ID: 0C87282F76E61283
2 changed files with 96 additions and 4 deletions

View File

@ -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); 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)] #[cfg(test)]
mod album_tests { mod album_tests {
use hyper_tls::HttpsConnector; use hyper_tls::HttpsConnector;
use hyper::Client; use hyper::Client;
use mockito::mock; use mockito::mock;
use serde_json::json; 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}; 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] #[test]
fn set_title_album_correct_values() { fn set_title_album_correct_values() {
let client = setup(); let client = setup();

View File

@ -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_tls::HttpsConnector;
use hyper::{Client, client::HttpConnector}; use hyper::{Client, client::HttpConnector};
use clap::{App, Arg, SubCommand, crate_version}; use clap::{App, Arg, SubCommand, crate_version};
@ -90,13 +90,29 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
.long("title") .long("title")
.value_name("TITLE") .value_name("TITLE")
.required(true) .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") .arg(Arg::with_name("id")
.short("i") .short("i")
.long("id") .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) .required(true)
.value_name("ID(s)")) .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(); let matches = app.clone().get_matches();
@ -138,6 +154,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let t = m.value_of("title").unwrap(); let t = m.value_of("title").unwrap();
let i = m.value_of("id").unwrap(); let i = m.value_of("id").unwrap();
set_title_album(&client, &lychee_session_cookie, i, t).await; 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 { } else {
App::print_long_help(&mut app).unwrap(); App::print_long_help(&mut app).unwrap();
} }