Added a method to query data about a specific album using its id, with the according unit tests
Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
parent
c74335828d
commit
ae36e25196
116
src/album.rs
Normal file
116
src/album.rs
Normal file
@ -0,0 +1,116 @@
|
|||||||
|
use serde::Deserialize;
|
||||||
|
use serde_json::{from_str, json};
|
||||||
|
use tabled::Tabled;
|
||||||
|
use hyper::{Method, Request, body::Body, header::{AUTHORIZATION, CONTENT_TYPE, COOKIE}};
|
||||||
|
use crate::{utils::{display_option_string, body_to_str}, LycheeClient};
|
||||||
|
|
||||||
|
#[derive(Deserialize, Debug, Tabled)]
|
||||||
|
pub struct Album {
|
||||||
|
pub id: String,
|
||||||
|
pub title: String,
|
||||||
|
pub public: String,
|
||||||
|
pub full_photo: String,
|
||||||
|
pub visible: String,
|
||||||
|
pub nsfw: String,
|
||||||
|
pub parent_id: String,
|
||||||
|
pub cover_id: String,
|
||||||
|
pub description: String,
|
||||||
|
pub downloadable: String,
|
||||||
|
pub share_button_visible: String,
|
||||||
|
pub created_at: String,
|
||||||
|
pub updated_at: String,
|
||||||
|
#[field(display_with="display_option_string")]
|
||||||
|
pub min_taken_at: Option<String>,
|
||||||
|
#[field(display_with="display_option_string")]
|
||||||
|
pub max_taken_at: Option<String>,
|
||||||
|
pub password: String,
|
||||||
|
pub license: String,
|
||||||
|
pub sorting_col: String,
|
||||||
|
pub sorting_order: String,
|
||||||
|
#[field(display_with="display_option_string")]
|
||||||
|
pub thumb: Option<String>,
|
||||||
|
pub has_albums: String,
|
||||||
|
pub owner: String
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_album(c: &LycheeClient, lychee_session: &str, id: &str) -> Album {
|
||||||
|
let b = json!({ "albumID": id });
|
||||||
|
let req = Request::builder()
|
||||||
|
.method(Method::POST)
|
||||||
|
.uri(c.endpoint.to_string() + "/api/Album::get")
|
||||||
|
.header(COOKIE, lychee_session)
|
||||||
|
.header(AUTHORIZATION, c.api_key.to_string())
|
||||||
|
.header(CONTENT_TYPE, "application/json")
|
||||||
|
.body(Body::from(b.to_string()))
|
||||||
|
.expect("Cannot request /api/Album::get.");
|
||||||
|
let res = c.client.request(req).await.unwrap();
|
||||||
|
let s = res.status();
|
||||||
|
let b = body_to_str(res.into_body()).await;
|
||||||
|
assert_ne!(b, "\"false\"", "This albumID doesn't exist.");
|
||||||
|
assert_ne!(s, 500, "The server returned an error, check that the given albumID is valid and try again.");
|
||||||
|
let v: Album = from_str(b.as_str()).unwrap();
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod album_tests {
|
||||||
|
use hyper_tls::HttpsConnector;
|
||||||
|
use hyper::Client;
|
||||||
|
use mockito::mock;
|
||||||
|
use serde_json::json;
|
||||||
|
use crate::{LycheeClient, get_album};
|
||||||
|
|
||||||
|
fn setup() -> LycheeClient {
|
||||||
|
let https = HttpsConnector::new();
|
||||||
|
return LycheeClient {
|
||||||
|
client: Client::builder().build::<_, hyper::Body>(https),
|
||||||
|
endpoint: mockito::server_url(),
|
||||||
|
api_key: "value".to_string()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[should_panic]
|
||||||
|
fn get_album_returned_false() {
|
||||||
|
let client = setup();
|
||||||
|
let m = mock("POST", "/api/Album::get")
|
||||||
|
.with_body("\"false\"")
|
||||||
|
.with_header("content-type", "application/json")
|
||||||
|
.match_header("cookie", "cookie value")
|
||||||
|
.create();
|
||||||
|
|
||||||
|
tokio_test::block_on(get_album(&client, "cookie value", "test"));
|
||||||
|
m.assert();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[should_panic]
|
||||||
|
fn get_album_returned_500() {
|
||||||
|
let client = setup();
|
||||||
|
let m = mock("POST", "/api/Album::get")
|
||||||
|
.with_body("{}")
|
||||||
|
.with_header("content-type", "application/json")
|
||||||
|
.match_header("cookie", "cookie value")
|
||||||
|
.with_status(500)
|
||||||
|
.create();
|
||||||
|
|
||||||
|
tokio_test::block_on(get_album(&client, "cookie value", "test"));
|
||||||
|
m.assert();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn get_album_id() {
|
||||||
|
let payload = json!({"id":"16344947463510","title":"Untitled","public":"0","full_photo":"1","visible":"1","nsfw":"0","parent_id":"","cover_id":"","description":"","downloadable":"0","share_button_visible":"0","created_at":"2021-10-17T18:19:06+00:00","updated_at":"2021-10-17T18:19:06+00:00","min_taken_at":null,"max_taken_at":null,"password":"0","license":"none","sorting_col":"","sorting_order":"DESC","thumb":null,"has_albums":"0","owner":"Admin","albums":[],"photos":[],"num":"0"});
|
||||||
|
let client = setup();
|
||||||
|
let m = mock("POST", "/api/Album::get")
|
||||||
|
.with_body(payload.to_string())
|
||||||
|
.with_header("content-type", "application/json")
|
||||||
|
.match_header("cookie", "cookie value")
|
||||||
|
.match_body(json!({"albumID":"16344947463510"}).to_string().as_str())
|
||||||
|
.create();
|
||||||
|
|
||||||
|
assert_eq!(tokio_test::block_on(get_album(&client, "cookie value", "16344947463510")).id, "16344947463510");
|
||||||
|
m.assert();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
17
src/main.rs
17
src/main.rs
@ -1,4 +1,4 @@
|
|||||||
use crate::{albums::get_albums, config::{get_session_file, read_config, setup_config_data_storage}, session::login, utils::{write_to_file, read_from_file, body_to_str}};
|
use crate::{album::get_album, albums::get_albums, config::{get_session_file, read_config, setup_config_data_storage}, session::login, utils::{write_to_file, read_from_file, body_to_str}};
|
||||||
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};
|
||||||
@ -7,6 +7,7 @@ use cookie::Cookie;
|
|||||||
use tabled::Table;
|
use tabled::Table;
|
||||||
|
|
||||||
mod albums;
|
mod albums;
|
||||||
|
mod album;
|
||||||
mod session;
|
mod session;
|
||||||
mod utils;
|
mod utils;
|
||||||
mod config;
|
mod config;
|
||||||
@ -48,6 +49,16 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
|||||||
.subcommand(
|
.subcommand(
|
||||||
SubCommand::with_name("get_albums")
|
SubCommand::with_name("get_albums")
|
||||||
.about("Get the albums on the server.")
|
.about("Get the albums on the server.")
|
||||||
|
)
|
||||||
|
.subcommand(
|
||||||
|
SubCommand::with_name("get_album")
|
||||||
|
.about("Query some information on a given album")
|
||||||
|
.arg(Arg::with_name("id")
|
||||||
|
.long("id")
|
||||||
|
.short("i")
|
||||||
|
.value_name("ID")
|
||||||
|
.required(true)
|
||||||
|
.help("Specify the album id to query data from."))
|
||||||
);
|
);
|
||||||
let matches = app.clone().get_matches();
|
let matches = app.clone().get_matches();
|
||||||
|
|
||||||
@ -73,6 +84,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
|||||||
} else if let Some(_) = matches.subcommand_matches("get_albums") {
|
} else if let Some(_) = matches.subcommand_matches("get_albums") {
|
||||||
let a = get_albums(&client, &lychee_session_cookie).await.albums;
|
let a = get_albums(&client, &lychee_session_cookie).await.albums;
|
||||||
println!("{}", Table::new(a).to_string());
|
println!("{}", Table::new(a).to_string());
|
||||||
|
} else if let Some(m) = matches.subcommand_matches("get_album") {
|
||||||
|
let id = m.value_of("id").unwrap();
|
||||||
|
let a = get_album(&client, &lychee_session_cookie, id).await;
|
||||||
|
println!("{}", Table::new(vec![a]).to_string());
|
||||||
} else {
|
} else {
|
||||||
App::print_long_help(&mut app).unwrap();
|
App::print_long_help(&mut app).unwrap();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user