diff --git a/Cargo.lock b/Cargo.lock index 67c1915..509343b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -81,120 +81,6 @@ dependencies = [ "libc", ] -[[package]] -name = "async-channel" -version = "1.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf46fee83e5ccffc220104713af3292ff9bc7c64c7de289f66dae8e38d826833" -dependencies = [ - "concurrent-queue", - "event-listener", - "futures-core", -] - -[[package]] -name = "async-executor" -version = "1.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6fa3dc5f2a8564f07759c008b9109dc0d39de92a88d5588b8a5036d286383afb" -dependencies = [ - "async-lock", - "async-task", - "concurrent-queue", - "fastrand", - "futures-lite", - "slab", -] - -[[package]] -name = "async-global-executor" -version = "2.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1b6f5d7df27bd294849f8eec66ecfc63d11814df7a4f5d74168a2394467b776" -dependencies = [ - "async-channel", - "async-executor", - "async-io", - "async-lock", - "blocking", - "futures-lite", - "once_cell", -] - -[[package]] -name = "async-io" -version = "1.13.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fc5b45d93ef0529756f812ca52e44c221b35341892d3dcc34132ac02f3dd2af" -dependencies = [ - "async-lock", - "autocfg", - "cfg-if", - "concurrent-queue", - "futures-lite", - "log", - "parking", - "polling", - "rustix 0.37.7", - "slab", - "socket2", - "waker-fn", -] - -[[package]] -name = "async-lock" -version = "2.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa24f727524730b077666307f2734b4a1a1c57acb79193127dcc8914d5242dd7" -dependencies = [ - "event-listener", -] - -[[package]] -name = "async-process" -version = "1.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a9d28b1d97e08915212e2e45310d47854eafa69600756fc735fb788f75199c9" -dependencies = [ - "async-io", - "async-lock", - "autocfg", - "blocking", - "cfg-if", - "event-listener", - "futures-lite", - "rustix 0.37.7", - "signal-hook", - "windows-sys 0.48.0", -] - -[[package]] -name = "async-std" -version = "1.12.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62565bb4402e926b29953c785397c6dc0391b7b446e45008b0049eb43cec6f5d" -dependencies = [ - "async-channel", - "async-global-executor", - "async-io", - "async-lock", - "async-process", - "crossbeam-utils", - "futures-channel", - "futures-core", - "futures-io", - "futures-lite", - "gloo-timers", - "kv-log-macro", - "log", - "memchr", - "once_cell", - "pin-project-lite", - "pin-utils", - "slab", - "wasm-bindgen-futures", -] - [[package]] name = "async-stream" version = "0.3.4" @@ -217,12 +103,6 @@ dependencies = [ "syn 1.0.109", ] -[[package]] -name = "async-task" -version = "4.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ecc7ab41815b3c653ccd2978ec3255c81349336702dfdf62ee6f7069b12a3aae" - [[package]] name = "async-trait" version = "0.1.68" @@ -252,12 +132,6 @@ dependencies = [ "autocfg", ] -[[package]] -name = "atomic-waker" -version = "1.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1181e1e0d1fce796a03db1ae795d67167da795f9cf4a39c37589e85ef57f26d3" - [[package]] name = "atty" version = "0.2.14" @@ -320,21 +194,6 @@ dependencies = [ "generic-array", ] -[[package]] -name = "blocking" -version = "1.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77231a1c8f801696fc0123ec6150ce92cffb8e164a02afb9c8ddee0e9b65ad65" -dependencies = [ - "async-channel", - "async-lock", - "async-task", - "atomic-waker", - "fastrand", - "futures-lite", - "log", -] - [[package]] name = "bumpalo" version = "3.12.0" @@ -400,15 +259,6 @@ dependencies = [ "unicode-width", ] -[[package]] -name = "concurrent-queue" -version = "2.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62ec6771ecfa0762d24683ee5a32ad78487a3d3afdc0fb8cae19d2c5deb50b7c" -dependencies = [ - "crossbeam-utils", -] - [[package]] name = "cookie" version = "0.16.2" @@ -487,16 +337,6 @@ dependencies = [ "typenum", ] -[[package]] -name = "ctor" -version = "0.1.26" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d2301688392eb071b0bf1a37be05c469d3cc4dbbd95df672fe28ab021e6a096" -dependencies = [ - "quote", - "syn 1.0.109", -] - [[package]] name = "ctr" version = "0.9.2" @@ -693,17 +533,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "errno" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4bcfec3a70f97c962c307b2d2c56e358cf1d00b558d74262b5f929ee8cc7e73a" -dependencies = [ - "errno-dragonfly", - "libc", - "windows-sys 0.48.0", -] - [[package]] name = "errno-dragonfly" version = "0.1.2" @@ -805,32 +634,6 @@ version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4fff74096e71ed47f8e023204cfd0aa1289cd54ae5430a9523be060cdb849964" -[[package]] -name = "futures-lite" -version = "1.13.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49a9d51ce47660b1e808d3c990b4709f2f415d928835a17dfd16991515c46bce" -dependencies = [ - "fastrand", - "futures-core", - "futures-io", - "memchr", - "parking", - "pin-project-lite", - "waker-fn", -] - -[[package]] -name = "futures-rustls" -version = "0.22.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d2411eed028cdf8c8034eaf21f9915f956b6c3abec4d4c7949ee67f0721127bd" -dependencies = [ - "futures-io", - "rustls", - "webpki", -] - [[package]] name = "futures-sink" version = "0.3.28" @@ -910,18 +713,6 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" -[[package]] -name = "gloo-timers" -version = "0.2.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b995a66bb87bebce9a0f4a95aed01daca4872c050bfcb21653361c03bc35e5c" -dependencies = [ - "futures-channel", - "futures-core", - "js-sys", - "wasm-bindgen", -] - [[package]] name = "h2" version = "0.3.16" @@ -1199,15 +990,6 @@ dependencies = [ "sha2", ] -[[package]] -name = "kv-log-macro" -version = "1.0.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0de8b303297635ad57c9f5059fd9cee7a47f8e8daa09df0fcd07dd39fb22977f" -dependencies = [ - "log", -] - [[package]] name = "lazy_static" version = "1.4.0" @@ -1235,12 +1017,6 @@ version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f051f77a7c8e6957c0696eac88f26b0117e54f52d3fc682ab19397a8812846a4" -[[package]] -name = "linux-raw-sys" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d59d8c75012853d2e872fb56bc8a2e53718e2cafe1a4c823143141c6d90c322f" - [[package]] name = "lock_api" version = "0.4.9" @@ -1258,7 +1034,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" dependencies = [ "cfg-if", - "value-bag", ] [[package]] @@ -1442,12 +1217,6 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" -[[package]] -name = "parking" -version = "2.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "14f2252c834a40ed9bb5422029649578e63aa341ac401f74e719dd1afda8394e" - [[package]] name = "parking_lot" version = "0.11.2" @@ -1543,22 +1312,6 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" -[[package]] -name = "polling" -version = "2.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4be1c66a6add46bff50935c313dae30a5030cf8385c5206e8a95e9e9def974aa" -dependencies = [ - "autocfg", - "bitflags", - "cfg-if", - "concurrent-queue", - "libc", - "log", - "pin-project-lite", - "windows-sys 0.48.0", -] - [[package]] name = "polyval" version = "0.6.0" @@ -1848,24 +1601,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "db4165c9963ab29e422d6c26fbc1d37f15bace6b2810221f9d925023480fcf0e" dependencies = [ "bitflags", - "errno 0.2.8", + "errno", "io-lifetimes", "libc", - "linux-raw-sys 0.1.4", - "windows-sys 0.45.0", -] - -[[package]] -name = "rustix" -version = "0.37.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2aae838e49b3d63e9274e1c01833cc8139d3fec468c3b84688c628f44b1ae11d" -dependencies = [ - "bitflags", - "errno 0.3.1", - "io-lifetimes", - "libc", - "linux-raw-sys 0.3.1", + "linux-raw-sys", "windows-sys 0.45.0", ] @@ -2028,16 +1767,6 @@ dependencies = [ "lazy_static", ] -[[package]] -name = "signal-hook" -version = "0.3.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "732768f1176d21d09e076c23a93123d40bba92d50c4058da34d45c8de8e682b9" -dependencies = [ - "libc", - "signal-hook-registry", -] - [[package]] name = "signal-hook-registry" version = "1.4.1" @@ -2152,6 +1881,7 @@ dependencies = [ "sqlx-rt", "stringprep", "thiserror", + "tokio-stream", "url", "webpki-roots", "whoami", @@ -2182,8 +1912,9 @@ version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "804d3f245f894e61b1e6263c84b23ca675d96753b5abfd5cc8597d86806e8024" dependencies = [ - "async-std", - "futures-rustls", + "once_cell", + "tokio", + "tokio-rustls", ] [[package]] @@ -2257,7 +1988,7 @@ dependencies = [ "cfg-if", "fastrand", "redox_syscall", - "rustix 0.36.11", + "rustix", "windows-sys 0.42.0", ] @@ -2383,6 +2114,17 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "tokio-rustls" +version = "0.23.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c43ee83903113e03984cb9e5cebe6c04a5116269e900e3ddba8f068a62adda59" +dependencies = [ + "rustls", + "tokio", + "webpki", +] + [[package]] name = "tokio-stream" version = "0.1.12" @@ -2603,28 +2345,12 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" -[[package]] -name = "value-bag" -version = "1.0.0-alpha.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2209b78d1249f7e6f3293657c9779fe31ced465df091bbd433a1cf88e916ec55" -dependencies = [ - "ctor", - "version_check", -] - [[package]] name = "version_check" version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" -[[package]] -name = "waker-fn" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d5b2c62b4012a3e1eca5a7e077d13b3bf498c4073e33ccd58626607748ceeca" - [[package]] name = "want" version = "0.3.0" @@ -2672,18 +2398,6 @@ dependencies = [ "wasm-bindgen-shared", ] -[[package]] -name = "wasm-bindgen-futures" -version = "0.4.34" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f219e0d211ba40266969f6dbdd90636da12f75bee4fc9d6c23d1260dadb51454" -dependencies = [ - "cfg-if", - "js-sys", - "wasm-bindgen", - "web-sys", -] - [[package]] name = "wasm-bindgen-macro" version = "0.2.84" @@ -2825,15 +2539,6 @@ dependencies = [ "windows-targets 0.42.2", ] -[[package]] -name = "windows-sys" -version = "0.48.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" -dependencies = [ - "windows-targets 0.48.0", -] - [[package]] name = "windows-targets" version = "0.42.2" diff --git a/Cargo.toml b/Cargo.toml index dfba1e9..66bebb0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,4 +18,4 @@ rocket_okapi = { version="0.8.0-rc.2", features = ["swagger"] } schemars = "0.8.12" serde = { version = "1.0", features = ["derive"] } sha2 = "0.10.6" -sqlx = { version = "0.6.3", features = [ "runtime-async-std-rustls", "postgres" ] } +sqlx = { version = "0.6.3", features = [ "runtime-tokio-rustls", "postgres" ] } diff --git a/src/views/index.rs b/src/views/index.rs index 5de664a..d36f2e1 100644 --- a/src/views/index.rs +++ b/src/views/index.rs @@ -2,25 +2,3 @@ pub fn index() -> &'static str { "Hello, world!" } - -#[cfg(test)] -mod test { - use rocket::http::Status; - use rocket::local::asynchronous::Client; - use sqlx::PgPool; - use crate::rocket_main::create_rocket; - - #[sqlx::test] - pub async fn test_index(pool: PgPool) { - let rocket = create_rocket(pool); - - let client = Client::tracked(rocket).await.unwrap(); - - let req = client.get("/"); - - let response = req.dispatch().await; - - assert_eq!(response.status(), Status::Ok); - assert_eq!(response.into_string().await.unwrap().as_str(), "Hello, world!"); - } -} \ No newline at end of file diff --git a/src/views/login.rs b/src/views/login.rs index 76d7379..f67bd93 100644 --- a/src/views/login.rs +++ b/src/views/login.rs @@ -10,16 +10,16 @@ use crate::models::user_role::UserRole; use crate::password_utils::{create_jwt, hash_password}; use crate::views::base::{ErrorJson, format_to_error_json}; -#[derive(Debug, Deserialize, JsonSchema)] +#[derive(Debug, Deserialize, Serialize, JsonSchema)] pub struct LoginRequest { - email: String, - password: String + pub email: String, + pub password: String } -#[derive(Debug, Serialize, JsonSchema)] +#[derive(Debug, Deserialize, Serialize, JsonSchema)] pub struct LoginResponse { - token: String, - roles: Vec + pub token: String, + pub roles: Vec } /// # Log in and get token diff --git a/tests/views/index.rs b/tests/views/index.rs new file mode 100644 index 0000000..c7d0945 --- /dev/null +++ b/tests/views/index.rs @@ -0,0 +1,20 @@ +use rocket::http::Status; +use rocket::local::asynchronous::Client; +use sqlx::PgPool; + +use login_service::rocket_main::create_rocket; + + +#[sqlx::test] +pub async fn test_index(pool: PgPool) { + let rocket = create_rocket(pool); + + let client = Client::tracked(rocket).await.unwrap(); + + let req = client.get("/"); + + let response = req.dispatch().await; + + assert_eq!(response.status(), Status::Ok); + assert_eq!(response.into_string().await.unwrap().as_str(), "Hello, world!"); +} diff --git a/tests/views/login.rs b/tests/views/login.rs new file mode 100644 index 0000000..3de267e --- /dev/null +++ b/tests/views/login.rs @@ -0,0 +1,53 @@ +use rocket::http::Status; +use rocket::local::asynchronous::Client; +use sqlx::PgPool; +use login_service::models::user::create_user; +use login_service::models::user_role::{add_roles_by_email, UserRole}; +use login_service::rocket_main::create_rocket; +use login_service::views::login::{LoginRequest, LoginResponse}; +use crate::views::{TEST_USER_EMAIL, TEST_USER_EMPLOYEE_ID, TEST_USER_PASSWORD}; + +#[sqlx::test] +pub async fn test_login(pool: PgPool) -> Result<(), String> { + + create_user(TEST_USER_EMAIL, TEST_USER_PASSWORD, + TEST_USER_EMPLOYEE_ID, + &pool).await?; + + let roles = [UserRole::HumanResources]; + + add_roles_by_email(TEST_USER_EMAIL, &roles, &pool).await?; + + let rocket = create_rocket(pool); + + let client = Client::tracked(rocket).await.unwrap(); + + let req = client.post("/auth/login").json(&LoginRequest { + email: TEST_USER_EMAIL.to_string(), + password: TEST_USER_PASSWORD.to_string(), + }); + + let response = req.dispatch().await; + + assert_eq!(response.status(), Status::Ok); + let result: LoginResponse = response.into_json().await.expect("Json response expected"); + assert_eq!(result.roles, roles); + + Ok(()) +} + +#[sqlx::test] +pub async fn test_login_into_non_existent_user(pool: PgPool) { + let rocket = create_rocket(pool); + + let client = Client::tracked(rocket).await.unwrap(); + + let req = client.post("/auth/login").json(&LoginRequest { + email: TEST_USER_EMAIL.to_string(), + password: TEST_USER_PASSWORD.to_string(), + }); + + let response = req.dispatch().await; + + assert_eq!(response.status(), Status::Forbidden); +} diff --git a/tests/views/mod.rs b/tests/views/mod.rs index e69de29..68d3061 100644 --- a/tests/views/mod.rs +++ b/tests/views/mod.rs @@ -0,0 +1,6 @@ +mod index; +mod login; + +static TEST_USER_EMAIL: &str = "test@test.com"; +static TEST_USER_PASSWORD: &str = "qwerty"; +static TEST_USER_EMPLOYEE_ID: i32 = 1;