Refactored to only return lychee_session cookie and to return String from login function
Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
parent
028adbff93
commit
8520b1e8a3
@ -12,6 +12,6 @@ pub async fn get_albums(client: &Client<HttpsConnector<HttpConnector>>, lychee_s
|
|||||||
.header(AUTHORIZATION, std::env::var("LYCHEE_API_KEY").unwrap())
|
.header(AUTHORIZATION, std::env::var("LYCHEE_API_KEY").unwrap())
|
||||||
.body(Body::empty())
|
.body(Body::empty())
|
||||||
.expect("error");
|
.expect("error");
|
||||||
let _res = client.request(req).await.unwrap();
|
let res = client.request(req).await.unwrap();
|
||||||
return json::parse(body_to_str(_res).await.as_str()).unwrap();
|
return json::parse(body_to_str(res.into_body()).await.as_str()).unwrap();
|
||||||
}
|
}
|
||||||
|
20
src/main.rs
20
src/main.rs
@ -1,5 +1,5 @@
|
|||||||
use hyper_tls::HttpsConnector;
|
use hyper_tls::HttpsConnector;
|
||||||
use hyper::{Body, Client, Response, client::HttpConnector};
|
use hyper::{Body, Client, body, client::HttpConnector};
|
||||||
use clap::{Arg, App, SubCommand};
|
use clap::{Arg, App, SubCommand};
|
||||||
use crate::session::login;
|
use crate::session::login;
|
||||||
|
|
||||||
@ -13,10 +13,8 @@ pub struct LycheeClient {
|
|||||||
api_key: String
|
api_key: String
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn body_to_str(res: Response<Body>) -> String {
|
pub async fn body_to_str(res: Body) -> String {
|
||||||
let body = hyper::body::to_bytes(res).await.unwrap();
|
return String::from_utf8(body::to_bytes(res).await.unwrap().to_vec()).unwrap();
|
||||||
let body_str = std::str::from_utf8(&body).unwrap().clone();
|
|
||||||
return body_str.to_string();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -64,22 +62,18 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests_main {
|
mod tests_main {
|
||||||
use hyper::{Body, Response};
|
use hyper::Body;
|
||||||
use crate::body_to_str;
|
use crate::body_to_str;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn body_to_str_empty() {
|
fn body_to_str_empty() {
|
||||||
let res = Response::new(Body::empty());
|
assert_eq!("", tokio_test::block_on(body_to_str(Body::empty())));
|
||||||
assert_eq!("", tokio_test::block_on(body_to_str(res)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn body_to_str_not_empty() {
|
fn body_to_str_not_empty() {
|
||||||
let res = Response::new(Body::from("demo"));
|
assert_eq!("demo", tokio_test::block_on(body_to_str(Body::from("demo"))));
|
||||||
assert_eq!("demo", tokio_test::block_on(body_to_str(res)));
|
assert_eq!("hello world", tokio_test::block_on(body_to_str(Body::from("hello world"))));
|
||||||
|
|
||||||
let res2 = Response::new(Body::from("hello world"));
|
|
||||||
assert_eq!("hello world", tokio_test::block_on(body_to_str(res2)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@ use hyper::{Body, Method, Request, header::{AUTHORIZATION, CONTENT_TYPE, COOKIE,
|
|||||||
use json::object;
|
use json::object;
|
||||||
use crate::{LycheeClient, body_to_str};
|
use crate::{LycheeClient, body_to_str};
|
||||||
|
|
||||||
pub async fn login(client: &LycheeClient, login: &str, password: &str) -> HeaderValue {
|
pub async fn login(client: &LycheeClient, login: &str, password: &str) -> String {
|
||||||
let login_data = object! { "username": login, "password": password };
|
let login_data = object! { "username": login, "password": password };
|
||||||
|
|
||||||
let req = Request::builder()
|
let req = Request::builder()
|
||||||
@ -13,10 +13,12 @@ pub async fn login(client: &LycheeClient, login: &str, password: &str) -> Header
|
|||||||
.body(Body::from(login_data.dump()))
|
.body(Body::from(login_data.dump()))
|
||||||
.expect("error");
|
.expect("error");
|
||||||
let res = client.client.request(req).await.unwrap();
|
let res = client.client.request(req).await.unwrap();
|
||||||
let lychee_session = res.headers().get(SET_COOKIE.as_str()).unwrap().clone();
|
let headers = res.headers().get_all(SET_COOKIE)
|
||||||
assert!(json::parse(body_to_str(res).await.as_str()).unwrap().as_bool().unwrap());
|
.iter().find(|&x| x.to_str().unwrap().starts_with("lychee_session")).unwrap().clone();
|
||||||
|
let body = body_to_str(res.into_body()).await;
|
||||||
|
if body != "true" { panic!("Error while logging in, expected true, server returned: {}", body); }
|
||||||
|
|
||||||
return lychee_session;
|
return headers.to_str().unwrap().to_string();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn logout(client: &LycheeClient, lychee_session: &HeaderValue) {
|
pub async fn logout(client: &LycheeClient, lychee_session: &HeaderValue) {
|
||||||
@ -28,7 +30,7 @@ pub async fn logout(client: &LycheeClient, lychee_session: &HeaderValue) {
|
|||||||
.body(Body::empty())
|
.body(Body::empty())
|
||||||
.expect("error");
|
.expect("error");
|
||||||
let res = client.client.request(req).await.unwrap();
|
let res = client.client.request(req).await.unwrap();
|
||||||
assert!(json::parse(body_to_str(res).await.as_str()).unwrap().as_bool().unwrap());
|
assert!(json::parse(body_to_str(res.into_body()).await.as_str()).unwrap().as_bool().unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@ -82,21 +84,21 @@ mod session_tests {
|
|||||||
let m1 = mock("POST", "/api/Session::login")
|
let m1 = mock("POST", "/api/Session::login")
|
||||||
.match_header("content-type", "application/json")
|
.match_header("content-type", "application/json")
|
||||||
.with_header("content-type", "application/json")
|
.with_header("content-type", "application/json")
|
||||||
.with_header("set-cookie", "demo")
|
.with_header("set-cookie", "lychee_session=demo")
|
||||||
.with_body("true")
|
.with_body("true")
|
||||||
.with_status(200)
|
.with_status(200)
|
||||||
.create();
|
.create();
|
||||||
assert_eq!("demo", tokio_test::block_on(login(&client, "u", "p")));
|
assert_eq!("lychee_session=demo", tokio_test::block_on(login(&client, "u", "p")));
|
||||||
m1.assert();
|
m1.assert();
|
||||||
|
|
||||||
let m2 = mock("POST", "/api/Session::login")
|
let m2 = mock("POST", "/api/Session::login")
|
||||||
.match_header("content-type", "application/json")
|
.match_header("content-type", "application/json")
|
||||||
.with_header("content-type", "application/json")
|
.with_header("content-type", "application/json")
|
||||||
.with_header("set-cookie", "value")
|
.with_header("set-cookie", "lychee_session=value")
|
||||||
.with_body("true")
|
.with_body("true")
|
||||||
.with_status(200)
|
.with_status(200)
|
||||||
.create();
|
.create();
|
||||||
assert_eq!("value", tokio_test::block_on(login(&client, "u", "p")));
|
assert_eq!("lychee_session=value", tokio_test::block_on(login(&client, "u", "p")));
|
||||||
m2.assert();
|
m2.assert();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -106,7 +108,7 @@ mod session_tests {
|
|||||||
let client = setup();
|
let client = setup();
|
||||||
let m = mock("POST", "/api/Session::login")
|
let m = mock("POST", "/api/Session::login")
|
||||||
.with_header("content-type", "application/json")
|
.with_header("content-type", "application/json")
|
||||||
.with_header("set-cookie", "demo")
|
.with_header("set-cookie", "lychee_session=demo")
|
||||||
.with_body("false")
|
.with_body("false")
|
||||||
.create();
|
.create();
|
||||||
|
|
||||||
@ -119,7 +121,7 @@ mod session_tests {
|
|||||||
let mut client = setup();
|
let mut client = setup();
|
||||||
let m1 = mock("POST", "/api/Session::login")
|
let m1 = mock("POST", "/api/Session::login")
|
||||||
.with_header("content-type", "application/json")
|
.with_header("content-type", "application/json")
|
||||||
.with_header("set-cookie", "demo")
|
.with_header("set-cookie", "lychee_session=demo")
|
||||||
.with_body("true")
|
.with_body("true")
|
||||||
.match_header("authorization", "value")
|
.match_header("authorization", "value")
|
||||||
.create();
|
.create();
|
||||||
@ -129,7 +131,7 @@ mod session_tests {
|
|||||||
client.api_key = "othervalue".to_string();
|
client.api_key = "othervalue".to_string();
|
||||||
let m2 = mock("POST", "/api/Session::login")
|
let m2 = mock("POST", "/api/Session::login")
|
||||||
.with_header("content-type", "application/json")
|
.with_header("content-type", "application/json")
|
||||||
.with_header("set-cookie", "demo")
|
.with_header("set-cookie", "lychee_session=demo")
|
||||||
.with_body("true")
|
.with_body("true")
|
||||||
.match_header("authorization", "othervalue")
|
.match_header("authorization", "othervalue")
|
||||||
.create();
|
.create();
|
||||||
@ -143,7 +145,7 @@ mod session_tests {
|
|||||||
let client = setup();
|
let client = setup();
|
||||||
let m1 = mock("POST", "/api/Session::login")
|
let m1 = mock("POST", "/api/Session::login")
|
||||||
.with_header("content-type", "application/json")
|
.with_header("content-type", "application/json")
|
||||||
.with_header("set-cookie", "demo")
|
.with_header("set-cookie", "lychee_session=demo")
|
||||||
.with_body("true")
|
.with_body("true")
|
||||||
.match_body(body1.dump().as_str())
|
.match_body(body1.dump().as_str())
|
||||||
.create();
|
.create();
|
||||||
@ -154,7 +156,7 @@ mod session_tests {
|
|||||||
let client = setup();
|
let client = setup();
|
||||||
let m2 = mock("POST", "/api/Session::login")
|
let m2 = mock("POST", "/api/Session::login")
|
||||||
.with_header("content-type", "application/json")
|
.with_header("content-type", "application/json")
|
||||||
.with_header("set-cookie", "demo")
|
.with_header("set-cookie", "lychee_session=demo")
|
||||||
.with_body("true")
|
.with_body("true")
|
||||||
.match_body(body2.dump().as_str())
|
.match_body(body2.dump().as_str())
|
||||||
.create();
|
.create();
|
||||||
|
Loading…
Reference in New Issue
Block a user