Added logout method and its correspoding tests

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

View File

@ -1,5 +1,5 @@
use hyper_tls::HttpsConnector;
use hyper::{Body, Client, Method, Request, client::HttpConnector, header::{AUTHORIZATION, CONTENT_TYPE, HeaderValue, SET_COOKIE}};
use hyper::{Body, Client, Method, Request, client::HttpConnector, header::{AUTHORIZATION, CONTENT_TYPE, COOKIE, HeaderValue, SET_COOKIE}};
use json::object;
use crate::body_to_str;
@ -24,13 +24,25 @@ pub async fn login(client: &Client<HttpsConnector<HttpConnector>>) -> HeaderValu
return lychee_session;
}
pub async fn logout(client: &Client<HttpsConnector<HttpConnector>>, cookie: String) {
let req = Request::builder()
.method(Method::POST)
.uri(std::env::var("LYCHEE_ENDPOINT").unwrap() + "/api/Session::logout")
.header(COOKIE.as_str(), cookie)
.header(AUTHORIZATION, std::env::var("LYCHEE_API_KEY").unwrap())
.body(Body::empty())
.expect("error");
let res = client.request(req).await.unwrap();
assert!(json::parse(body_to_str(res).await.as_str()).unwrap().as_bool().unwrap());
}
#[cfg(test)]
mod session_tests {
use hyper_tls::HttpsConnector;
use hyper::{Client, client::HttpConnector};
use json::object;
use mockito::mock;
use crate::session::login;
use crate::session::{login, logout};
fn setup() -> Client<HttpsConnector<HttpConnector>> {
let https = HttpsConnector::new();
@ -42,10 +54,38 @@ mod session_tests {
return client;
}
#[test]
#[should_panic]
fn logout_false_returned() {
let client = setup();
let m = mock("POST", "/api/Session::logout")
.with_body("false")
.with_header("content-type", "application/json")
.match_header("cookie", "cookie value")
.create();
tokio_test::block_on(logout(&client, "cookie value".to_string()));
m.assert();
}
#[test]
fn logout_all_ok() {
let client = setup();
let m = mock("POST", "/api/Session::logout")
.with_body("true")
.with_header("content-type", "application/json")
.match_header("cookie", "cookie value")
.create();
tokio_test::block_on(logout(&client, "cookie value".to_string()));
m.assert();
}
#[test]
fn login_all_ok() {
let client = setup();
let _m1 = 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")
@ -53,8 +93,9 @@ mod session_tests {
.with_status(200)
.create();
assert_eq!("demo", tokio_test::block_on(login(&client)));
m1.assert();
let _m2 = mock("POST", "/api/Session::login")
let m2 = mock("POST", "/api/Session::login")
.match_header("content-type", "application/json")
.with_header("content-type", "application/json")
.with_header("set-cookie", "value")
@ -62,63 +103,84 @@ mod session_tests {
.with_status(200)
.create();
assert_eq!("value", tokio_test::block_on(login(&client)));
m2.assert();
}
#[test]
#[should_panic]
fn login_false_returned() {
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("set-cookie", "demo")
.with_body("false")
.create();
tokio_test::block_on(login(&client));
m.assert();
}
#[test]
#[should_panic]
fn login_html_returned() {
let client = setup();
let _m = mock("POST", "/api/Session::login")
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));
m.assert();
}
#[test]
fn login_check_api_key() {
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_header("authorization", "value")
.create();
tokio_test::block_on(login(&client));
m1.assert();
std::env::set_var("LYCHEE_API_KEY", "othervalue");
let m2 = mock("POST", "/api/Session::login")
.with_header("content-type", "application/json")
.with_header("set-cookie", "demo")
.with_body("true")
.match_header("authorization", "othervalue")
.create();
tokio_test::block_on(login(&client));
m2.assert();
}
#[test]
fn login_check_username_password() {
let body1 = object!{ "username": "username", "password": "password" };
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("set-cookie", "demo")
.with_body("true")
.match_body(body1.dump().as_str())
.create();
tokio_test::block_on(login(&client));
m1.assert();
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")
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));
m2.assert();
}
}