Added the set_album_photo subcommand and its according tests

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2021-11-04 23:09:44 +01:00
parent f802a56913
commit 85ffdfc6af
No known key found for this signature in database
GPG Key ID: 0C87282F76E61283
2 changed files with 83 additions and 3 deletions

View File

@ -1,4 +1,4 @@
use crate::{album::{add_album, delete_album, get_album, set_description_album, set_public_album, set_title_album}, albums::get_albums, config::{get_session_file, read_config, setup_config_data_storage}, photo::{add_photo, delete_photo}, 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_public_album, set_title_album}, albums::get_albums, config::{get_session_file, read_config, setup_config_data_storage}, photo::{add_photo, delete_photo, set_album_photo}, 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};
@ -179,6 +179,22 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
.value_name("IDs")
.help("The id of the picture(s) which you want to delete. For multiple ids, use commas."))
)
.subcommand(
SubCommand::with_name("set_album_photo")
.about("Move the picture to a given album.")
.arg(Arg::with_name("photo_id")
.required(true)
.long("photo_id")
.short("i")
.value_name("IDs")
.help("The id of the picture(s) which you want to move. For multiple ids, use commas."))
.arg(Arg::with_name("album_id")
.required(true)
.short("a")
.long("album_id")
.value_name("ID")
.help("The album id to move the picture(s) to."))
)
;
let matches = app.clone().get_matches();
@ -240,6 +256,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
} else if let Some(m) = matches.subcommand_matches("delete_photo") {
let id = m.value_of("id").unwrap();
delete_photo(&client, &lychee_session_cookie, id).await;
} else if let Some(m) = matches.subcommand_matches("set_album_photo") {
let i = m.value_of("photo_id").unwrap();
let a = m.value_of("album_id").unwrap();
set_album_photo(&client, &lychee_session_cookie, i, a).await;
} else {
App::print_long_help(&mut app).unwrap();
}

View File

@ -60,9 +60,25 @@ pub async fn delete_photo(c: &LycheeClient, l: &str, i: &str) {
assert_ne!(s, 500, "The server returned an internal error.");
}
pub async fn set_album_photo(c: &LycheeClient, l: &str, i: &str, a: &str) {
let req = Request::builder()
.method(Method::POST)
.uri(c.endpoint.to_string() + "/api/Photo::setAlbum")
.header(COOKIE, l)
.header(AUTHORIZATION, c.api_key.to_string())
.header(CONTENT_TYPE, "application/json")
.body(Body::from(json!({"photoIDs": i, "albumID": a}).to_string()))
.expect("Cannot request /api/Photo::setAlbum.");
let res = c.client.request(req).await.unwrap();
let s = res.status();
let b = body_to_str(res.into_body()).await;
assert_eq!(b, "true", "The server didn't reply true: '{}'.", b);
assert_ne!(s, 500, "The server returned an internal error.");
}
#[cfg(test)]
mod photo_tests {
use crate::{LycheeClient, photo::delete_photo};
use crate::{LycheeClient, photo::{delete_photo, set_album_photo}};
use hyper_tls::HttpsConnector;
use hyper::Client;
use mockito::mock;
@ -77,6 +93,51 @@ mod photo_tests {
};
}
#[test]
fn set_album_photo_correct_values() {
let client = setup();
let m1 = mock("POST", "/api/Photo::setAlbum")
.match_header("cookie", "v")
.match_header("content-type", "application/json")
.with_body("true")
.match_body(json!({"photoIDs": "123", "albumID": "12"}).to_string().as_str())
.create();
tokio_test::block_on(set_album_photo(&client, "v", "123", "12"));
m1.assert();
}
#[test]
#[should_panic]
fn set_album_photo_false_returned() {
let client = setup();
let m1 = mock("POST", "/api/Photo::setAlbum")
.match_header("cookie", "v")
.match_header("content-type", "application/json")
.with_body("false")
.match_body(json!({"photoIDs": "123", "albumID": "1234"}).to_string().as_str())
.create();
tokio_test::block_on(set_album_photo(&client, "v", "123", "1234"));
m1.assert();
}
#[test]
#[should_panic]
fn set_album_photo_500_returned() {
let client = setup();
let m1 = mock("POST", "/api/Photo::setAlbum")
.match_header("cookie", "value")
.match_header("content-type", "application/json")
.with_body("true")
.with_status(500)
.match_body(json!({"photoIDs": "123", "albumID": "12222"}).to_string().as_str())
.create();
tokio_test::block_on(set_album_photo(&client, "value", "123", "12222"));
m1.assert();
}
#[test]
fn delete_photo_correct_values() {
let client = setup();
@ -106,7 +167,6 @@ mod photo_tests {
m1.assert();
}
#[test]
#[should_panic]
fn delete_photo_500_returned() {