Added the set_public_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 22:11:02 +01:00
parent f77667b44f
commit 230cb328d5
No known key found for this signature in database
GPG Key ID: 0C87282F76E61283
2 changed files with 125 additions and 3 deletions

View File

@ -116,7 +116,7 @@ pub async fn set_description_album(c: &LycheeClient, l: &str, id: &str, desc: &s
.header(AUTHORIZATION, c.api_key.to_string())
.header(CONTENT_TYPE, "application/json")
.body(Body::from(b.to_string()))
.expect("Cannot request /api/Album:setTitle.");
.expect("Cannot request /api/Album:setDescription.");
let res = c.client.request(req).await.unwrap();
let s = res.status();
let b = body_to_str(res.into_body()).await;
@ -124,13 +124,40 @@ pub async fn set_description_album(c: &LycheeClient, l: &str, id: &str, desc: &s
assert_eq!(b, "true", "The server returned an unexpected value: {}.", b);
}
pub async fn set_public_album(c: &LycheeClient, l: &str, id: &str, public: bool, visible: bool, downloadable: bool, share_button_visible: bool, full_photo: bool, nsfw: bool, password: &str) {
let b = json!({
"albumID" : id,
"public": if public { "1" } else { "0" },
"visible": if visible { "1" } else { "0" },
"downloadable": if downloadable { "1" } else { "0" },
"share_button_visible": if share_button_visible { "1" } else { "0" },
"full_photo": if full_photo { "1" } else { "0" },
"nsfw": if nsfw { "1" } else { "0" },
"password": password
});
let req = Request::builder()
.method(Method::POST)
.uri(c.endpoint.to_string() + "/api/Album::setPublic")
.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:setPublic.");
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, album::set_description_album, get_album};
use crate::{LycheeClient, album::{set_description_album, set_public_album}, get_album};
use super::{add_album, delete_album, set_title_album};
@ -143,6 +170,52 @@ mod album_tests {
};
}
#[test]
fn set_public_album_correct_values() {
let client = setup();
let b1 = json!({ "albumID" : "12222", "public": "1", "visible": "0",
"downloadable": "1", "share_button_visible": "0", "full_photo": "1",
"nsfw": "0", "password": ""
});
let m1 = mock("POST", "/api/Album::setPublic")
.match_header("cookie", "v")
.match_header("content-type", "application/json")
.with_body("true")
.match_body(b1.to_string().as_str())
.create();
tokio_test::block_on(set_public_album(&client, "v", "12222", true, false, true, false, true, false, ""));
m1.assert();
let b2 = json!({ "albumID" : "123", "public": "0", "visible": "1",
"downloadable": "0", "share_button_visible": "1", "full_photo": "0",
"nsfw": "1", "password": "pass"
});
let m2 = mock("POST", "/api/Album::setPublic")
.match_header("cookie", "v")
.match_header("content-type", "application/json")
.with_body("true")
.match_body(b2.to_string().as_str())
.create();
tokio_test::block_on(set_public_album(&client, "v", "123", false, true, false, true, false, true, "pass"));
m2.assert();
}
#[test]
#[should_panic]
fn set_public_album_false_returned() {
let client = setup();
let m = mock("POST", "/api/Album::setPublic")
.match_header("cookie", "v")
.match_header("content-type", "application/json")
.with_body("false")
.create();
tokio_test::block_on(set_public_album(&client, "v", "123", false, false, false, false, false, false, ""));
m.assert();
}
#[test]
fn set_description_album_correct_values() {
let client = setup();

View File

@ -1,4 +1,4 @@
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 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}, 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};
@ -113,6 +113,45 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
.help("The album ID to set the description for.")
.required(true)
.value_name("ID"))
)
.subcommand(
SubCommand::with_name("set_album_public")
.about("Set the visibility for an album.")
.arg(Arg::with_name("id")
.short("i")
.long("id")
.help("The album's id to set visibility for.")
.value_name("ID")
.required(true))
.arg(Arg::with_name("public")
.short("p")
.long("public")
.help("Set the album as public."))
.arg(Arg::with_name("visible")
.short("v")
.long("visible")
.help("Set the album as visible"))
.arg(Arg::with_name("downloadable")
.short("d")
.long("downloadable")
.help("Set the album as downloadable."))
.arg(Arg::with_name("share_button_visible")
.short("s")
.long("share_button_visible")
.help("Set the share button as visible for that album."))
.arg(Arg::with_name("full_photo")
.short("f")
.long("full_photo")
.help("Allow users to see the full resolution pictures."))
.arg(Arg::with_name("nsfw")
.short("n")
.long("nsfw")
.help("Set the album as being Not Safe For Work."))
.arg(Arg::with_name("password")
.long("password")
.short("P")
.help("Set the password for the album. Leave empty for none.")
.value_name("PASSWORD"))
);
let matches = app.clone().get_matches();
@ -158,6 +197,16 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
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 if let Some(m) = matches.subcommand_matches("set_album_public") {
let i = m.value_of("id").unwrap();
let f = m.is_present("full_photo");
let p = m.is_present("public");
let n = m.is_present("nsfw");
let v = m.is_present("visible");
let d = m.is_present("downloadable");
let s = m.is_present("share_button_visible");
let pass = m.value_of("password").unwrap_or("");
set_public_album(&client, &lychee_session_cookie, i, p, v, d, s, f, n, pass).await;
} else {
App::print_long_help(&mut app).unwrap();
}