Added the set_title_album command and the according tests

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2021-11-01 23:03:08 +01:00
parent c261ab588f
commit 50aec4c297
No known key found for this signature in database
GPG Key ID: 0C87282F76E61283
2 changed files with 95 additions and 3 deletions

View File

@ -88,6 +88,24 @@ pub async fn delete_album(c: &LycheeClient, lychee_session: &str, id: &str) {
assert_eq!(b, "true", "The server returned an unexpected value: {}.", b);
}
pub async fn set_title_album(c: &LycheeClient, l: &str, id: &str, title: &str) {
if title.len() > 100 { panic!("Title can't be longer than 100 characters."); }
let b = json!({"albumIDs" : id, "title": title});
let req = Request::builder()
.method(Method::POST)
.uri(c.endpoint.to_string() + "/api/Album::setTitle")
.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;
@ -96,7 +114,7 @@ mod album_tests {
use serde_json::json;
use crate::{LycheeClient, get_album};
use super::{add_album, delete_album};
use super::{add_album, delete_album, set_title_album};
fn setup() -> LycheeClient {
let https = HttpsConnector::new();
@ -107,6 +125,60 @@ mod album_tests {
};
}
#[test]
fn set_title_album_correct_values() {
let client = setup();
let m1 = mock("POST", "/api/Album::setTitle")
.match_header("cookie", "v")
.match_header("content-type", "application/json")
.with_body("true")
.match_body(json!({"albumIDs": "1", "title": "hello"}).to_string().as_str())
.create();
tokio_test::block_on(set_title_album(&client, "v", "1", "hello"));
m1.assert();
let m2 = mock("POST", "/api/Album::setTitle")
.match_header("cookie", "v")
.match_header("content-type", "application/json")
.with_body("true")
.match_body(json!({"albumIDs": "1,2,3", "title": "k"}).to_string().as_str())
.create();
tokio_test::block_on(set_title_album(&client, "v", "1,2,3", "k"));
m2.assert();
}
#[test]
#[should_panic]
fn set_title_album_title_too_long() {
let client = setup();
let m = mock("POST", "/api/Album::setTitle")
.match_header("cookie", "v")
.match_header("content-type", "application/json")
.with_body("true")
.create();
let mut b = "a".to_string();
for _ in 1..=101 { b += "a"; }
tokio_test::block_on(set_title_album(&client, "v", "1", b.as_str()));
m.assert();
}
#[test]
#[should_panic]
fn set_title_album_false_returned() {
let client = setup();
let m = mock("POST", "/api/Album::setTitle")
.match_header("cookie", "v")
.match_header("content-type", "application/json")
.with_body("false")
.create();
tokio_test::block_on(set_title_album(&client, "v", "1", "k"));
m.assert();
}
#[test]
fn delete_album_correct_values() {
let client = setup();

View File

@ -1,4 +1,4 @@
use crate::{album::{add_album, delete_album, get_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_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};
@ -71,7 +71,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
.arg(Arg::with_name("parent_id")
.long("parent").short("p").help("The parent id for the album to create.")
.value_name("PARENT_ID").validator(numeric_validator))
)
)
.subcommand(
SubCommand::with_name("delete_album")
.about("Delete an album.")
@ -81,6 +81,22 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
.help("The album ID to delete. For multiple IDs, separate them using commas.")
.required(true)
.value_name("ID(s)"))
)
.subcommand(
SubCommand::with_name("set_album_title")
.about("Rename one or more album(s).")
.arg(Arg::with_name("title")
.short("t")
.long("title")
.value_name("TITLE")
.required(true)
.help("The title to set for the albums to rename."))
.arg(Arg::with_name("id")
.short("i")
.long("id")
.help("The album ID to delete. For multiple IDs, separate them using commas.")
.required(true)
.value_name("ID(s)"))
);
let matches = app.clone().get_matches();
@ -118,6 +134,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
} else if let Some(m) = matches.subcommand_matches("delete_album") {
let id = m.value_of("id").unwrap();
delete_album(&client, &lychee_session_cookie, id).await;
} else if let Some(m) = matches.subcommand_matches("set_album_title") {
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 {
App::print_long_help(&mut app).unwrap();
}