Added more tests for session::login

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2021-10-21 00:02:16 +02:00
parent f7ffe1473f
commit e27c4e5f00
No known key found for this signature in database
GPG Key ID: 0C87282F76E61283

View File

@ -18,7 +18,8 @@ pub async fn login(client: &Client<HttpsConnector<HttpConnector>>) -> HeaderValu
.expect("error");
let res = client.request(req).await.unwrap();
let lychee_session = res.headers().get(SET_COOKIE.as_str()).unwrap().clone();
assert_eq!(json::parse(body_to_str(res).await.as_str()).unwrap(), true);
assert_eq!(res.headers().get(CONTENT_TYPE.as_str()).unwrap(), "application/json");
assert!(json::parse(body_to_str(res).await.as_str()).unwrap().as_bool().unwrap());
return lychee_session;
}
@ -27,6 +28,7 @@ pub async fn login(client: &Client<HttpsConnector<HttpConnector>>) -> HeaderValu
mod session_tests {
use hyper_tls::HttpsConnector;
use hyper::{Client, client::HttpConnector};
use json::object;
use mockito::mock;
use crate::session::login;
@ -41,15 +43,82 @@ mod session_tests {
}
#[test]
fn login_test() {
fn login_all_ok() {
let client = setup();
let _m = mock("POST", "/api/Session::login")
let _m1 = mock("POST", "/api/Session::login")
.match_header("content-type", "application/json")
.with_header("content-type", "application/json")
.with_header("set-cookie", "demo")
.with_body("true")
.with_status(200)
.create();
assert_eq!("demo", tokio_test::block_on(login(&client)));
let _m2 = mock("POST", "/api/Session::login")
.match_header("content-type", "application/json")
.with_header("content-type", "application/json")
.with_header("set-cookie", "value")
.with_body("true")
.with_status(200)
.create();
assert_eq!("value", tokio_test::block_on(login(&client)));
}
#[test]
#[should_panic]
fn login_false_returned() {
let client = setup();
let _m = mock("POST", "/api/Session::login")
.with_header("content-type", "application/json")
.with_header("set-cookie", "demo")
.with_body("false")
.create();
tokio_test::block_on(login(&client));
}
#[test]
#[should_panic]
fn login_html_returned() {
let client = setup();
let _m = mock("POST", "/api/Session::login")
.with_header("content-type", "text/html")
.with_header("set-cookie", "demo")
.with_body("true")
.with_status(200)
.create();
tokio_test::block_on(login(&client));
}
#[test]
fn login_check_api_key() {
}
#[test]
fn login_check_username_password() {
let body1 = object!{ "username": "username", "password": "password" };
let client = setup();
let _m1 = mock("POST", "/api/Session::login")
.with_header("content-type", "application/json")
.with_header("set-cookie", "demo")
.with_body("true")
.match_body(body1.dump().as_str())
.create();
tokio_test::block_on(login(&client));
let body2 = object!{ "username": "u", "password": "p" };
let client = setup();
std::env::set_var("LYCHEE_USERNAME", "u");
std::env::set_var("LYCHEE_PASSWORD", "p");
let _m2 = mock("POST", "/api/Session::login")
.with_header("content-type", "application/json")
.with_header("set-cookie", "demo")
.with_body("true")
.match_body(body2.dump().as_str())
.create();
tokio_test::block_on(login(&client));
}
}