Added logout method and its correspoding tests
Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
parent
e27c4e5f00
commit
14d801d907
@ -1,5 +1,5 @@
|
|||||||
use hyper_tls::HttpsConnector;
|
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 json::object;
|
||||||
use crate::body_to_str;
|
use crate::body_to_str;
|
||||||
|
|
||||||
@ -24,13 +24,25 @@ pub async fn login(client: &Client<HttpsConnector<HttpConnector>>) -> HeaderValu
|
|||||||
return lychee_session;
|
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)]
|
#[cfg(test)]
|
||||||
mod session_tests {
|
mod session_tests {
|
||||||
use hyper_tls::HttpsConnector;
|
use hyper_tls::HttpsConnector;
|
||||||
use hyper::{Client, client::HttpConnector};
|
use hyper::{Client, client::HttpConnector};
|
||||||
use json::object;
|
use json::object;
|
||||||
use mockito::mock;
|
use mockito::mock;
|
||||||
use crate::session::login;
|
use crate::session::{login, logout};
|
||||||
|
|
||||||
fn setup() -> Client<HttpsConnector<HttpConnector>> {
|
fn setup() -> Client<HttpsConnector<HttpConnector>> {
|
||||||
let https = HttpsConnector::new();
|
let https = HttpsConnector::new();
|
||||||
@ -42,10 +54,38 @@ mod session_tests {
|
|||||||
return client;
|
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]
|
#[test]
|
||||||
fn login_all_ok() {
|
fn login_all_ok() {
|
||||||
let client = setup();
|
let client = setup();
|
||||||
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", "demo")
|
||||||
@ -53,8 +93,9 @@ mod session_tests {
|
|||||||
.with_status(200)
|
.with_status(200)
|
||||||
.create();
|
.create();
|
||||||
assert_eq!("demo", tokio_test::block_on(login(&client)));
|
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")
|
.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", "value")
|
||||||
@ -62,63 +103,84 @@ mod session_tests {
|
|||||||
.with_status(200)
|
.with_status(200)
|
||||||
.create();
|
.create();
|
||||||
assert_eq!("value", tokio_test::block_on(login(&client)));
|
assert_eq!("value", tokio_test::block_on(login(&client)));
|
||||||
|
m2.assert();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
#[should_panic]
|
||||||
fn login_false_returned() {
|
fn login_false_returned() {
|
||||||
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", "demo")
|
||||||
.with_body("false")
|
.with_body("false")
|
||||||
.create();
|
.create();
|
||||||
|
|
||||||
tokio_test::block_on(login(&client));
|
tokio_test::block_on(login(&client));
|
||||||
|
m.assert();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
#[should_panic]
|
||||||
fn login_html_returned() {
|
fn login_html_returned() {
|
||||||
let client = setup();
|
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("content-type", "text/html")
|
||||||
.with_header("set-cookie", "demo")
|
.with_header("set-cookie", "demo")
|
||||||
.with_body("true")
|
.with_body("true")
|
||||||
.with_status(200)
|
.with_status(200)
|
||||||
.create();
|
.create();
|
||||||
tokio_test::block_on(login(&client));
|
tokio_test::block_on(login(&client));
|
||||||
|
m.assert();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn login_check_api_key() {
|
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]
|
#[test]
|
||||||
fn login_check_username_password() {
|
fn login_check_username_password() {
|
||||||
let body1 = object!{ "username": "username", "password": "password" };
|
let body1 = object!{ "username": "username", "password": "password" };
|
||||||
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", "demo")
|
||||||
.with_body("true")
|
.with_body("true")
|
||||||
.match_body(body1.dump().as_str())
|
.match_body(body1.dump().as_str())
|
||||||
.create();
|
.create();
|
||||||
|
|
||||||
tokio_test::block_on(login(&client));
|
tokio_test::block_on(login(&client));
|
||||||
|
m1.assert();
|
||||||
|
|
||||||
let body2 = object!{ "username": "u", "password": "p" };
|
let body2 = object!{ "username": "u", "password": "p" };
|
||||||
let client = setup();
|
let client = setup();
|
||||||
std::env::set_var("LYCHEE_USERNAME", "u");
|
std::env::set_var("LYCHEE_USERNAME", "u");
|
||||||
std::env::set_var("LYCHEE_PASSWORD", "p");
|
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("content-type", "application/json")
|
||||||
.with_header("set-cookie", "demo")
|
.with_header("set-cookie", "demo")
|
||||||
.with_body("true")
|
.with_body("true")
|
||||||
.match_body(body2.dump().as_str())
|
.match_body(body2.dump().as_str())
|
||||||
.create();
|
.create();
|
||||||
|
|
||||||
tokio_test::block_on(login(&client));
|
tokio_test::block_on(login(&client));
|
||||||
|
m2.assert();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user