Added a first draft for the client unit testing using mockito

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2021-10-20 11:47:13 +02:00
parent 539bf51bab
commit b6dd77f26d
No known key found for this signature in database
GPG Key ID: 0C87282F76E61283
4 changed files with 179 additions and 4 deletions

140
Cargo.lock generated
View File

@ -2,6 +2,25 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "aho-corasick"
version = "0.7.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f"
dependencies = [
"memchr",
]
[[package]]
name = "assert-json-diff"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "50f1c3703dd33532d7f0ca049168930e9099ecac238e23cf932f3a69c42f06da"
dependencies = [
"serde",
"serde_json",
]
[[package]]
name = "async-stream"
version = "0.3.2"
@ -23,6 +42,17 @@ dependencies = [
"syn",
]
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi",
"libc",
"winapi",
]
[[package]]
name = "autocfg"
version = "1.0.1"
@ -53,6 +83,17 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "colored"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b3616f750b84d8f0de8a58bda93e08e2a81ad3f523089b05f1dffecab48c6cbd"
dependencies = [
"atty",
"lazy_static",
"winapi",
]
[[package]]
name = "core-foundation"
version = "0.9.1"
@ -69,6 +110,12 @@ version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea221b5284a47e40033bf9b66f35f984ec0ea2931eb03505246cd27a963f981b"
[[package]]
name = "difference"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "524cbf6897b527295dff137cec09ecf3a05f4fddffd7dfcd1585403449e74198"
[[package]]
name = "dotenv"
version = "0.15.0"
@ -96,6 +143,16 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b"
[[package]]
name = "form_urlencoded"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5fc25a87fa4fd2094bffb06925852034d90a17f0d1e05197d4956d3555752191"
dependencies = [
"matches",
"percent-encoding",
]
[[package]]
name = "futures-channel"
version = "0.3.17"
@ -321,10 +378,17 @@ dependencies = [
"hyper",
"hyper-tls",
"json",
"mockito",
"tokio",
"tokio-test",
]
[[package]]
name = "matches"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a3e378b66a060d48947b590737b30a1be76706c8dd7b8ba0f2fe3989c68a853f"
[[package]]
name = "memchr"
version = "2.4.1"
@ -353,6 +417,24 @@ dependencies = [
"winapi",
]
[[package]]
name = "mockito"
version = "0.30.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d10030163d67f681db11810bc486df3149e6d91c8b4f3f96fa8b62b546c2cef8"
dependencies = [
"assert-json-diff",
"colored",
"difference",
"httparse",
"lazy_static",
"log",
"rand",
"regex",
"serde_json",
"serde_urlencoded",
]
[[package]]
name = "native-tls"
version = "0.2.8"
@ -454,6 +536,12 @@ dependencies = [
"winapi",
]
[[package]]
name = "percent-encoding"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e"
[[package]]
name = "pin-project-lite"
version = "0.2.7"
@ -545,6 +633,23 @@ dependencies = [
"bitflags",
]
[[package]]
name = "regex"
version = "1.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.6.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b"
[[package]]
name = "remove_dir_all"
version = "0.5.3"
@ -554,6 +659,12 @@ dependencies = [
"winapi",
]
[[package]]
name = "ryu"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e"
[[package]]
name = "schannel"
version = "0.1.19"
@ -593,6 +704,35 @@ dependencies = [
"libc",
]
[[package]]
name = "serde"
version = "1.0.130"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f12d06de37cf59146fbdecab66aa99f9fe4f78722e3607577a5375d66bd0c913"
[[package]]
name = "serde_json"
version = "1.0.68"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f690853975602e1bfe1ccbf50504d67174e3bcf340f23b5ea9992e0587a52d8"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]]
name = "serde_urlencoded"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "edfa57a7f8d9c1d260a549e7224100f6c43d43f9103e06dd8b4095a9b2b43ce9"
dependencies = [
"form_urlencoded",
"itoa",
"ryu",
"serde",
]
[[package]]
name = "signal-hook-registry"
version = "1.4.0"

View File

@ -12,3 +12,6 @@ tokio-test = "0.4.2"
hyper-tls = "0.5.0"
dotenv = "0.15.0"
json = "0.12.4"
[dev-dependencies]
mockito = "0.30.0"

View File

@ -3,12 +3,13 @@ use hyper::{Body, Client, Method, Request, client::HttpConnector, header::{AUTHO
use json::JsonValue;
use crate::body_to_str;
pub async fn get_albums(client: &Client<HttpsConnector<HttpConnector>>, lychee_session: HeaderValue) -> JsonValue {
let req = Request::builder()
.method(Method::POST)
.uri(std::env::var("LYCHEE_ENDPOINT").unwrap() + "/api/Albums::get")
.header(COOKIE, lychee_session)
.header(AUTHORIZATION, std::env::var("API_KEY").unwrap())
.header(AUTHORIZATION, std::env::var("LYCHEE_API_KEY").unwrap())
.body(Body::empty())
.expect("error");
let _res = client.request(req).await.unwrap();

View File

@ -5,14 +5,14 @@ use crate::body_to_str;
pub async fn login(client: &Client<HttpsConnector<HttpConnector>>) -> HeaderValue {
let login_data = object! {
"username": std::env::var("USERNAME").unwrap(),
"password": std::env::var("PASSWORD").unwrap()
"username": std::env::var("LYCHEE_USERNAME").unwrap(),
"password": std::env::var("LYCHEE_PASSWORD").unwrap()
};
let req = Request::builder()
.method(Method::POST)
.uri(std::env::var("LYCHEE_ENDPOINT").unwrap() + "/api/Session::login")
.header(AUTHORIZATION, std::env::var("API_KEY").unwrap())
.header(AUTHORIZATION, std::env::var("LYCHEE_API_KEY").unwrap())
.header(CONTENT_TYPE, "application/json")
.body(Body::from(login_data.dump()))
.expect("error");
@ -22,3 +22,34 @@ pub async fn login(client: &Client<HttpsConnector<HttpConnector>>) -> HeaderValu
return lychee_session;
}
#[cfg(test)]
mod session_tests {
use hyper_tls::HttpsConnector;
use hyper::{Client, client::HttpConnector};
use mockito::mock;
use crate::session::login;
fn setup() -> Client<HttpsConnector<HttpConnector>> {
let https = HttpsConnector::new();
let client = Client::builder().build::<_, hyper::Body>(https);
std::env::set_var("LYCHEE_ENDPOINT", mockito::server_url());
std::env::set_var("LYCHEE_USERNAME", "username");
std::env::set_var("LYCHEE_PASSWORD", "password");
std::env::set_var("LYCHEE_API_KEY", "value");
return client;
}
#[test]
fn login_test() {
let client = setup();
let _m = mock("POST", "/api/Session::login")
.match_header("content-type", "application/json")
.with_header("set-cookie", "demo")
.with_body("true")
.create();
tokio_test::block_on(login(&client));
}
}