From 81c3ccda582e9ff3d23a03df0c9107a80e626b92 Mon Sep 17 00:00:00 2001 From: sciencefidelity <32623301+sciencefidelity@users.noreply.github.com> Date: Thu, 20 Jun 2024 14:53:03 +0100 Subject: [PATCH] refactor: fix clippy lints and improve tests --- Cargo.lock | 13 +- Cargo.toml | 4 +- build.rs | 8 +- handle-errors/Cargo.lock | 2 +- handle-errors/Cargo.toml | 18 +- handle-errors/src/lib.rs | 33 +- mock-server/Cargo.lock | 1121 ++++++++++++++++++++++++++++++++++ mock-server/Cargo.toml | 27 + mock-server/src/lib.rs | 82 +++ src/config.rs | 66 +- src/main.rs | 2 + src/profanity.rs | 47 +- src/routes/authentication.rs | 21 + src/types/pagination.rs | 4 +- 14 files changed, 1409 insertions(+), 39 deletions(-) create mode 100644 mock-server/Cargo.lock create mode 100644 mock-server/Cargo.toml create mode 100644 mock-server/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index ee0e6ad..4033eff 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -556,6 +556,7 @@ dependencies = [ "clap", "dotenv", "handle-errors", + "mock-server", "openssl", "paseto", "platforms", @@ -839,7 +840,7 @@ dependencies = [ [[package]] name = "handle-errors" -version = "0.1.0" +version = "1.0.0" dependencies = [ "reqwest", "reqwest-middleware", @@ -1306,6 +1307,16 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "mock-server" +version = "1.0.0" +dependencies = [ + "bytes", + "serde_json", + "tokio", + "warp", +] + [[package]] name = "multer" version = "2.1.0" diff --git a/Cargo.toml b/Cargo.toml index 0bae87e..a1bfa05 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,9 +9,6 @@ repository = "https://github.com/sciencefidelity/eroteme" license = "MIT or Apache-2.0" exclude = ["./scripts"] -[lints.rust] -unsafe_code = "forbid" - [lints.clippy] enum_glob_use = "deny" pedantic = { level = "deny", priority = 1 } @@ -30,6 +27,7 @@ chrono = "0.4" clap = { version = "4.5.7", features = ["derive"] } dotenv = "0.15.0" handle-errors = { path = "handle-errors" } +mock-server = { path ="mock-server" } openssl = { version = "0.10.32", features = ["vendored"] } paseto = "2.0" rand = "0.8.5" diff --git a/build.rs b/build.rs index 225885f..df45ca4 100644 --- a/build.rs +++ b/build.rs @@ -1,10 +1,10 @@ -use platforms::*; +use platforms::{target::Env, TARGET_ARCH, TARGET_ENV, TARGET_OS}; use std::{borrow::Cow, process::Command}; /// Generate the `cargo:` key output pub fn generate_cargo_keys() { let output = Command::new("git") - .args(&["rev-parse", "--short", "HEAD"]) + .args(["rev-parse", "--short", "HEAD"]) .output(); let commit = match output { @@ -17,7 +17,7 @@ pub fn generate_cargo_keys() { Cow::from("unknown") } Err(err) => { - println!("cargo:warning=Failed to execute git command: {}", err); + println!("cargo:warning=Failed to execute git command: {err}"); Cow::from("unknown") } }; @@ -33,7 +33,7 @@ fn get_platform() -> String { TARGET_ARCH.as_str(), TARGET_OS.as_str(), env_dash, - TARGET_ENV.map(|x| x.as_str()).unwrap_or("") + TARGET_ENV.map_or("", Env::as_str), ) } diff --git a/handle-errors/Cargo.lock b/handle-errors/Cargo.lock index 1453083..180f86d 100644 --- a/handle-errors/Cargo.lock +++ b/handle-errors/Cargo.lock @@ -533,7 +533,7 @@ dependencies = [ [[package]] name = "handle-errors" -version = "0.1.0" +version = "1.0.0" dependencies = [ "reqwest", "reqwest-middleware", diff --git a/handle-errors/Cargo.toml b/handle-errors/Cargo.toml index 93f8647..828184e 100644 --- a/handle-errors/Cargo.toml +++ b/handle-errors/Cargo.toml @@ -1,7 +1,23 @@ [package] name = "handle-errors" -version = "0.1.0" +version = "1.0.0" +authors = ["Matt Cook "] edition = "2021" +repository = "https://github.com/sciencefidelity/eroteme/handle-errors" +license = "MIT or Apache-2.0" + +[lints.clippy] +enum_glob_use = "deny" +pedantic = { level = "deny", priority = 1 } +nursery = { level = "deny", priority = 2 } +unwrap_used = "deny" + +[profile.release] +opt-level = "z" +lto = true +codegen-units = 1 +panic = "abort" +strip = "symbols" [dependencies] reqwest = "0.12" diff --git a/handle-errors/src/lib.rs b/handle-errors/src/lib.rs index c20e07a..461df06 100644 --- a/handle-errors/src/lib.rs +++ b/handle-errors/src/lib.rs @@ -36,18 +36,18 @@ impl std::fmt::Display for APILayerError { impl std::fmt::Display for Error { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { match self { - Error::ParseError(ref err) => write!(f, "cannot parse parameter: {err}"), - Error::MissingParameters => write!(f, "missing parameter"), - Error::WrongPassword => write!(f, "wrong password"), - Error::CannotDecryptToken => write!(f, "cannot decrypt token"), - Error::Unauthorized => write!(f, "no permission to change the underlying resource"), - Error::ArgonLibraryError(_) => write!(f, "cannot verify password"), - Error::DatabaseQueryError(_) => write!(f, "cannot update, invalid data"), - Error::MigrationError(_) => write!(f, "cannot migrate database"), - Error::ReqwestAPIError(err) => write!(f, "cannot execute: {err}"), - Error::MiddlewareReqwestError(err) => write!(f, "cannot execute: {err}"), - Error::ClientError(err) => write!(f, "external client error: {err}"), - Error::ServerError(err) => write!(f, "external server error: {err}"), + Self::ParseError(ref err) => write!(f, "cannot parse parameter: {err}"), + Self::MissingParameters => write!(f, "missing parameter"), + Self::WrongPassword => write!(f, "wrong password"), + Self::CannotDecryptToken => write!(f, "cannot decrypt token"), + Self::Unauthorized => write!(f, "no permission to change the underlying resource"), + Self::ArgonLibraryError(_) => write!(f, "cannot verify password"), + Self::DatabaseQueryError(_) => write!(f, "cannot update, invalid data"), + Self::MigrationError(_) => write!(f, "cannot migrate database"), + Self::ReqwestAPIError(err) => write!(f, "cannot execute: {err}"), + Self::MiddlewareReqwestError(err) => write!(f, "cannot execute: {err}"), + Self::ClientError(err) => write!(f, "external client error: {err}"), + Self::ServerError(err) => write!(f, "external server error: {err}"), } } } @@ -59,11 +59,18 @@ const DUPLICATE_KEY: u32 = 23505; #[instrument] pub async fn return_error(r: Rejection) -> Result { + #[allow(clippy::equatable_if_let)] if let Some(crate::Error::DatabaseQueryError(e)) = r.find() { event!(Level::ERROR, "database query error"); match e { sqlx::Error::Database(err) => { - if err.code().unwrap().parse::().unwrap() == DUPLICATE_KEY { + if err + .code() + .expect("error code not found") + .parse::() + .expect("failed to parse error code") + == DUPLICATE_KEY + { Ok(warp::reply::with_status( "Account already exsists".to_string(), StatusCode::UNPROCESSABLE_ENTITY, diff --git a/mock-server/Cargo.lock b/mock-server/Cargo.lock new file mode 100644 index 0000000..48bd777 --- /dev/null +++ b/mock-server/Cargo.lock @@ -0,0 +1,1121 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "addr2line" +version = "0.22.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e4503c46a5c0c7844e948c9a4d6acd9f50cccb4de1c48eb9e291ea17470c678" +dependencies = [ + "gimli", +] + +[[package]] +name = "adler" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" + +[[package]] +name = "autocfg" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" + +[[package]] +name = "backtrace" +version = "0.3.73" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5cc23269a4f8976d0a4d2e7109211a419fe30e8d88d677cd60b6bc79c5732e0a" +dependencies = [ + "addr2line", + "cc", + "cfg-if", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", +] + +[[package]] +name = "base64" +version = "0.21.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567" + +[[package]] +name = "bitflags" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1" + +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + +[[package]] +name = "bytes" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "514de17de45fdb8dc022b1a7975556c53c86f9f0aa5f534b98977b171857c2c9" + +[[package]] +name = "cc" +version = "1.0.99" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96c51067fd44124faa7f870b4b1c969379ad32b2ba805aa959430ceaa384f695" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "cpufeatures" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53fe5e26ff1b7aef8bca9c6080520cfb8d9333c7568e1829cef191a9723e5504" +dependencies = [ + "libc", +] + +[[package]] +name = "crypto-common" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "data-encoding" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8566979429cf69b49a5c740c60791108e86440e8be149bbea4fe54d2c32d6e2" + +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "crypto-common", +] + +[[package]] +name = "encoding_rs" +version = "0.8.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b45de904aa0b010bce2ab45264d0631681847fa7b6f2eaa7dab7619943bc4f59" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "equivalent" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "form_urlencoded" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e13624c2627564efccf4934284bdd98cbaa14e79b0b5a141218e507b3a823456" +dependencies = [ + "percent-encoding", +] + +[[package]] +name = "futures-channel" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78" +dependencies = [ + "futures-core", + "futures-sink", +] + +[[package]] +name = "futures-core" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d" + +[[package]] +name = "futures-sink" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fb8e00e87438d937621c1c6269e53f536c14d3fbd6a042bb24879e57d474fb5" + +[[package]] +name = "futures-task" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38d84fa142264698cdce1a9f9172cf383a0c82de1bddcf3092901442c4097004" + +[[package]] +name = "futures-util" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48" +dependencies = [ + "futures-core", + "futures-sink", + "futures-task", + "pin-project-lite", + "pin-utils", + "slab", +] + +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "gimli" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40ecd4077b5ae9fd2e9e169b102c6c330d0605168eb0e8bf79952b256dbefffd" + +[[package]] +name = "h2" +version = "0.3.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81fe527a889e1532da5c525686d96d4c2e74cdd345badf8dfef9f6b39dd5f5e8" +dependencies = [ + "bytes", + "fnv", + "futures-core", + "futures-sink", + "futures-util", + "http 0.2.12", + "indexmap", + "slab", + "tokio", + "tokio-util", + "tracing", +] + +[[package]] +name = "hashbrown" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" + +[[package]] +name = "headers" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06683b93020a07e3dbcf5f8c0f6d40080d725bea7936fc01ad345c01b97dc270" +dependencies = [ + "base64", + "bytes", + "headers-core", + "http 0.2.12", + "httpdate", + "mime", + "sha1", +] + +[[package]] +name = "headers-core" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7f66481bfee273957b1f20485a4ff3362987f85b2c236580d81b4eb7a326429" +dependencies = [ + "http 0.2.12", +] + +[[package]] +name = "hermit-abi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" + +[[package]] +name = "http" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "601cbb57e577e2f5ef5be8e7b83f0f63994f25aa94d673e54a92d5c516d101f1" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + +[[package]] +name = "http" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21b9ddb458710bc376481b842f5da65cdf31522de232c1ca8146abce2a358258" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + +[[package]] +name = "http-body" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ceab25649e9960c0311ea418d17bee82c0dcec1bd053b5f9a66e265a693bed2" +dependencies = [ + "bytes", + "http 0.2.12", + "pin-project-lite", +] + +[[package]] +name = "httparse" +version = "1.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fcc0b4a115bf80b728eb8ea024ad5bd707b615bfed49e0665b6e0f86fd082d9" + +[[package]] +name = "httpdate" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" + +[[package]] +name = "hyper" +version = "0.14.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f361cde2f109281a220d4307746cdfd5ee3f410da58a70377762396775634b33" +dependencies = [ + "bytes", + "futures-channel", + "futures-core", + "futures-util", + "h2", + "http 0.2.12", + "http-body", + "httparse", + "httpdate", + "itoa", + "pin-project-lite", + "socket2", + "tokio", + "tower-service", + "tracing", + "want", +] + +[[package]] +name = "idna" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "634d9b1461af396cad843f47fdba5597a4f9e6ddd4bfb6ff5d85028c25cb12f6" +dependencies = [ + "unicode-bidi", + "unicode-normalization", +] + +[[package]] +name = "indexmap" +version = "2.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "168fb715dda47215e360912c096649d23d58bf392ac62f73919e831745e40f26" +dependencies = [ + "equivalent", + "hashbrown", +] + +[[package]] +name = "itoa" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" + +[[package]] +name = "libc" +version = "0.2.155" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" + +[[package]] +name = "lock_api" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17" +dependencies = [ + "autocfg", + "scopeguard", +] + +[[package]] +name = "log" +version = "0.4.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "mime" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" + +[[package]] +name = "mime_guess" +version = "2.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4192263c238a5f0d0c6bfd21f336a313a4ce1c450542449ca191bb657b4642ef" +dependencies = [ + "mime", + "unicase", +] + +[[package]] +name = "miniz_oxide" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8a240ddb74feaf34a79a7add65a741f3167852fba007066dcac1ca548d89c08" +dependencies = [ + "adler", +] + +[[package]] +name = "mio" +version = "0.8.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4a650543ca06a924e8b371db273b2756685faae30f8487da1b56505a8f78b0c" +dependencies = [ + "libc", + "wasi", + "windows-sys 0.48.0", +] + +[[package]] +name = "mock-server" +version = "1.0.0" +dependencies = [ + "bytes", + "serde_json", + "tokio", + "warp", +] + +[[package]] +name = "multer" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01acbdc23469fd8fe07ab135923371d5f5a422fbf9c522158677c8eb15bc51c2" +dependencies = [ + "bytes", + "encoding_rs", + "futures-util", + "http 0.2.12", + "httparse", + "log", + "memchr", + "mime", + "spin", + "version_check", +] + +[[package]] +name = "num_cpus" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" +dependencies = [ + "hermit-abi", + "libc", +] + +[[package]] +name = "object" +version = "0.36.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "576dfe1fc8f9df304abb159d767a29d0476f7750fbf8aa7ad07816004a207434" +dependencies = [ + "memchr", +] + +[[package]] +name = "once_cell" +version = "1.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" + +[[package]] +name = "parking_lot" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-targets 0.52.5", +] + +[[package]] +name = "percent-encoding" +version = "2.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" + +[[package]] +name = "pin-project" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6bf43b791c5b9e34c3d182969b4abb522f9343702850a2e57f460d00d09b4b3" +dependencies = [ + "pin-project-internal", +] + +[[package]] +name = "pin-project-internal" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "pin-project-lite" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02" + +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + +[[package]] +name = "proc-macro2" +version = "1.0.85" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22244ce15aa966053a896d1accb3a6e68469b97c7f33f284b99f0d576879fc23" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + +[[package]] +name = "redox_syscall" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c82cf8cff14456045f55ec4241383baeff27af886adb72ffb2162f99911de0fd" +dependencies = [ + "bitflags", +] + +[[package]] +name = "rustc-demangle" +version = "0.1.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" + +[[package]] +name = "ryu" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" + +[[package]] +name = "scoped-tls" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294" + +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + +[[package]] +name = "serde" +version = "1.0.203" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7253ab4de971e72fb7be983802300c30b5a7f0c2e56fab8abfc6a214307c0094" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.203" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "500cbc0ebeb6f46627f50f3f5811ccf6bf00643be300b4c3eabc0ef55dc5b5ba" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "455182ea6142b14f93f4bc5320a2b31c1f266b66a4a5c858b013302a5d8cbfc3" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "serde_urlencoded" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd" +dependencies = [ + "form_urlencoded", + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "sha1" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3bf829a2d51ab4a5ddf1352d8470c140cadc8301b2ae1789db023f01cedd6ba" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + +[[package]] +name = "signal-hook-registry" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9e9e0b4211b72e7b8b6e85c807d36c212bdb33ea8587f7569562a84df5465b1" +dependencies = [ + "libc", +] + +[[package]] +name = "slab" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67" +dependencies = [ + "autocfg", +] + +[[package]] +name = "smallvec" +version = "1.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" + +[[package]] +name = "socket2" +version = "0.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce305eb0b4296696835b71df73eb912e0f1ffd2556a501fcede6e0c50349191c" +dependencies = [ + "libc", + "windows-sys 0.52.0", +] + +[[package]] +name = "spin" +version = "0.9.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" + +[[package]] +name = "syn" +version = "2.0.66" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c42f3f41a2de00b01c0aaad383c5a45241efc8b2d1eda5661812fda5f3cdcff5" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "thiserror" +version = "1.0.61" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c546c80d6be4bc6a00c0f01730c08df82eaa7a7a61f11d656526506112cc1709" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.61" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46c3384250002a6d5af4d114f2845d37b57521033f30d5c3f46c4d70e1197533" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tinyvec" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87cc5ceb3875bb20c2890005a4e226a4651264a5c75edb2421b52861a0a0cb50" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" + +[[package]] +name = "tokio" +version = "1.38.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba4f4a02a7a80d6f274636f0aa95c7e383b912d41fe721a31f29e29698585a4a" +dependencies = [ + "backtrace", + "bytes", + "libc", + "mio", + "num_cpus", + "parking_lot", + "pin-project-lite", + "signal-hook-registry", + "socket2", + "tokio-macros", + "windows-sys 0.48.0", +] + +[[package]] +name = "tokio-macros" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f5ae998a069d4b5aba8ee9dad856af7d520c3699e6159b185c2acd48155d39a" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tokio-tungstenite" +version = "0.21.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c83b561d025642014097b66e6c1bb422783339e0909e4429cde4749d1990bc38" +dependencies = [ + "futures-util", + "log", + "tokio", + "tungstenite", +] + +[[package]] +name = "tokio-util" +version = "0.7.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9cf6b47b3771c49ac75ad09a6162f53ad4b8088b76ac60e8ec1455b31a189fe1" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "pin-project-lite", + "tokio", +] + +[[package]] +name = "tower-service" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6bc1c9ce2b5135ac7f93c72918fc37feb872bdc6a5533a8b85eb4b86bfdae52" + +[[package]] +name = "tracing" +version = "0.1.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" +dependencies = [ + "log", + "pin-project-lite", + "tracing-core", +] + +[[package]] +name = "tracing-core" +version = "0.1.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" +dependencies = [ + "once_cell", +] + +[[package]] +name = "try-lock" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" + +[[package]] +name = "tungstenite" +version = "0.21.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ef1a641ea34f399a848dea702823bbecfb4c486f911735368f1f137cb8257e1" +dependencies = [ + "byteorder", + "bytes", + "data-encoding", + "http 1.1.0", + "httparse", + "log", + "rand", + "sha1", + "thiserror", + "url", + "utf-8", +] + +[[package]] +name = "typenum" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" + +[[package]] +name = "unicase" +version = "2.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7d2d4dafb69621809a81864c9c1b864479e1235c0dd4e199924b9742439ed89" +dependencies = [ + "version_check", +] + +[[package]] +name = "unicode-bidi" +version = "0.3.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08f95100a766bf4f8f28f90d77e0a5461bbdb219042e7679bebe79004fed8d75" + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "unicode-normalization" +version = "0.1.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a56d1686db2308d901306f92a263857ef59ea39678a5458e7cb17f01415101f5" +dependencies = [ + "tinyvec", +] + +[[package]] +name = "url" +version = "2.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22784dbdf76fdde8af1aeda5622b546b422b6fc585325248a2bf9f5e41e94d6c" +dependencies = [ + "form_urlencoded", + "idna", + "percent-encoding", +] + +[[package]] +name = "utf-8" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" + +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + +[[package]] +name = "want" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa7760aed19e106de2c7c0b581b509f2f25d3dacaf737cb82ac61bc6d760b0e" +dependencies = [ + "try-lock", +] + +[[package]] +name = "warp" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4378d202ff965b011c64817db11d5829506d3404edeadb61f190d111da3f231c" +dependencies = [ + "bytes", + "futures-channel", + "futures-util", + "headers", + "http 0.2.12", + "hyper", + "log", + "mime", + "mime_guess", + "multer", + "percent-encoding", + "pin-project", + "scoped-tls", + "serde", + "serde_json", + "serde_urlencoded", + "tokio", + "tokio-tungstenite", + "tokio-util", + "tower-service", + "tracing", +] + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[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.5", +] + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets 0.52.5", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", +] + +[[package]] +name = "windows-targets" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f0713a46559409d202e70e28227288446bf7841d3211583a4b53e3f6d96e7eb" +dependencies = [ + "windows_aarch64_gnullvm 0.52.5", + "windows_aarch64_msvc 0.52.5", + "windows_i686_gnu 0.52.5", + "windows_i686_gnullvm", + "windows_i686_msvc 0.52.5", + "windows_x86_64_gnu 0.52.5", + "windows_x86_64_gnullvm 0.52.5", + "windows_x86_64_msvc 0.52.5", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7088eed71e8b8dda258ecc8bac5fb1153c5cffaf2578fc8ff5d61e23578d3263" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9985fd1504e250c615ca5f281c3f7a6da76213ebd5ccc9561496568a2752afb6" + +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88ba073cf16d5372720ec942a8ccbf61626074c6d4dd2e745299726ce8b89670" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87f4261229030a858f36b459e748ae97545d6f1ec60e5e0d6a3d32e0dc232ee9" + +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db3c2bf3d13d5b658be73463284eaf12830ac9a26a90c717b7f771dfe97487bf" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e4246f76bdeff09eb48875a0fd3e2af6aada79d409d33011886d3e1581517d9" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "852298e482cd67c356ddd9570386e2862b5673c85bd5f88df9ab6802b334c596" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" diff --git a/mock-server/Cargo.toml b/mock-server/Cargo.toml new file mode 100644 index 0000000..979ca84 --- /dev/null +++ b/mock-server/Cargo.toml @@ -0,0 +1,27 @@ +[package] +name = "mock-server" +version = "1.0.0" +authors = ["Matt Cook "] +edition = "2021" +repository = "https://github.com/sciencefidelity/eroteme/mock-server" +license = "MIT or Apache-2.0" + +[lints.clippy] +enum_glob_use = "deny" +pedantic = { level = "deny", priority = 1 } +nursery = { level = "deny", priority = 2 } +unwrap_used = "deny" + +[profile.release] +opt-level = "z" +lto = true +codegen-units = 1 +panic = "abort" +strip = "symbols" + +[dependencies] +bytes = "1" +serde_json = "1.0" +tokio = { version = "1", features = ["full"] } +warp = "0.3" + diff --git a/mock-server/src/lib.rs b/mock-server/src/lib.rs new file mode 100644 index 0000000..a9a29fb --- /dev/null +++ b/mock-server/src/lib.rs @@ -0,0 +1,82 @@ +use bytes::Bytes; +use serde_json::json; +use std::{collections::HashMap, net::SocketAddr}; +use tokio::sync::oneshot::{self, Sender}; +use warp::{http, reply::Reply, Filter}; + +#[derive(Clone, Debug)] +pub struct MockServer { + socket: SocketAddr, +} + +pub struct OneshotHandler { + pub sender: Sender, +} + +impl MockServer { + #[must_use] + pub const fn new(bind_addr: SocketAddr) -> Self { + Self { socket: bind_addr } + } + + #[allow(clippy::unused_async)] + async fn check_profanity((): (), content: Bytes) -> Result { + let content = String::from_utf8(content.to_vec()).expect("invalid UTF-8"); + if content.contains("shitty") { + Ok(warp::reply::with_status( + warp::reply::json(&json!({ + "bad_words_list": [ + { + "deviations": 0, + "end": 16, + "info": 2, + "original": "shitty", + "replacedLen": 6, + "start": 10, + "word": "shitty" + } + ], + "bad_words_total": 1, + "censored_content": "this is a ****** sentence", + "content": "this is a shitty sentence" + })), + http::StatusCode::OK, + )) + } else { + Ok(warp::reply::with_status( + warp::reply::json(&json!({ + "bad_words_list": [], + "bad_words_total": 0, + "censored_content": "", + "content": "this is a sentence" + })), + http::StatusCode::OK, + )) + } + } + + #[allow(clippy::unused_self)] + fn build_routes(&self) -> impl Filter + Clone { + warp::post() + .and(warp::path("bad_words")) + .and(warp::query()) + .map(|_: HashMap| ()) + .and(warp::path::end()) + .and(warp::body::bytes()) + .and_then(Self::check_profanity) + } + + #[must_use] + pub fn oneshot(&self) -> OneshotHandler { + let (tx, rx) = oneshot::channel::(); + let routes = Self::build_routes(self); + + let (_, server) = warp::serve(routes).bind_with_graceful_shutdown(self.socket, async { + rx.await.ok(); + }); + + tokio::task::spawn(server); + + OneshotHandler { sender: tx } + } +} diff --git a/src/config.rs b/src/config.rs index 9e1b514..7410540 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,9 +1,8 @@ use clap::Parser; -use dotenv; use std::env; /// Eroteme web service API -#[derive(Parser, Debug)] +#[derive(Parser, Debug, PartialEq, Eq)] #[clap(author, version, about, long_about = None)] pub struct Config { /// Which errors we want to log (info, warn, or error) @@ -30,9 +29,15 @@ pub struct Config { } impl Config { + /// # Errors + /// + /// Will return `Err` if the port variable cannot be parsed. + /// + /// # Panics + /// + /// Will panic if env vars are not found in the environment. pub fn new() -> Result { - dotenv::dotenv().ok(); - let config = Config::parse(); + let config = Self::parse(); assert!( env::var("BAD_WORDS_API_KEY").is_ok(), @@ -46,11 +51,12 @@ impl Config { .map_or(Ok(config.port), |val| val.parse::()) .map_err(handle_errors::Error::ParseError)?; - let db_user = env::var("POSTGRES_USER").unwrap_or(config.db_user.to_owned()); - let db_password = env::var("POSTGRES_PASSWORD").unwrap_or(config.db_password.to_owned()); - let db_host = env::var("POSTGRES_HOST").unwrap_or(config.db_host.to_owned()); - let db_port = env::var("POSTGRES_PORT").unwrap_or(config.db_port.to_string()); - let db_name = env::var("POSTGRES_DB").unwrap_or(config.db_name.to_owned()); + let db_user = env::var("POSTGRES_USER").unwrap_or_else(|_| config.db_user.clone()); + let db_password = + env::var("POSTGRES_PASSWORD").unwrap_or_else(|_| config.db_password.clone()); + let db_host = env::var("POSTGRES_HOST").unwrap_or_else(|_| config.db_host.clone()); + let db_port = env::var("POSTGRES_PORT").unwrap_or_else(|_| config.db_port.to_string()); + let db_name = env::var("POSTGRES_DB").unwrap_or_else(|_| config.db_name.clone()); Ok(Self { log_level: config.log_level, @@ -60,8 +66,48 @@ impl Config { db_host, db_port: db_port .parse::() - .map_err(|e| handle_errors::Error::ParseError(e))?, + .map_err(handle_errors::Error::ParseError)?, db_name, }) } } + +#[cfg(test)] +mod config_tests { + use super::*; + + fn set_env() { + unsafe { + env::set_var("BAD_WORDS_API_KEY", "API_KEY"); + env::set_var("PASETO_KEY", "RANDOM WORDS WINTER MACINTOSH PC"); + env::set_var("POSTGRES_USER", "user"); + env::set_var("POSTGRES_PASSWORD", "pass"); + env::set_var("POSTGRES_HOST", "localhost"); + env::set_var("POSTGRES_PORT", "5432"); + env::set_var("POSTGRES_DB", "eroteme"); + } + } + + #[test] + fn unset_set_api_key() { + // TODO: these have default values + // let result = std::panic::catch_unwind(|| Config::new()); + // assert!(result.is_err()); + + set_env(); + + let expected = Config { + log_level: "warn".to_owned(), + port: 8080, + db_user: "user".to_owned(), + db_password: "pass".to_owned(), + db_host: "localhost".to_owned(), + db_port: 5432, + db_name: "eroteme".to_owned(), + }; + + let config = Config::new().unwrap(); + + assert_eq!(config, expected); + } +} diff --git a/src/main.rs b/src/main.rs index f2bd9f4..cad0a21 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,8 @@ use warp::{http::Method, Filter}; #[tokio::main] async fn main() -> Result<(), handle_errors::Error> { + dotenv::dotenv().ok(); + let config = Config::new().expect("failed to set config"); let log_filter = format!( diff --git a/src/profanity.rs b/src/profanity.rs index 01fae95..6db3c86 100644 --- a/src/profanity.rs +++ b/src/profanity.rs @@ -1,8 +1,7 @@ -use std::env; - use reqwest_middleware::ClientBuilder; use reqwest_retry::{policies::ExponentialBackoff, RetryTransientMiddleware}; use serde::{Deserialize, Serialize}; +use std::env; #[derive(Deserialize, Serialize, Debug, Clone)] pub struct APIResponse { @@ -37,14 +36,15 @@ pub struct BadWordsResponse { #[allow(clippy::module_name_repetitions)] pub async fn check_profanity(content: String) -> Result { let api_key = env::var("BAD_WORDS_API_KEY").expect("BadWords API key not set"); - let retry_policy = ExponentialBackoff::builder().build_with_max_retries(3); + let api_layer_url = env::var("API_LAYER_URL").expect("api layer url not set"); + let retry_policy = ExponentialBackoff::builder().build_with_max_retries(3); let client = ClientBuilder::new(reqwest::Client::new()) .with(RetryTransientMiddleware::new_with_policy(retry_policy)) .build(); let res = client - .post("https://api.apilayer.com/bad_words?censor_character=*") + .post(format!("{api_layer_url}/bad_words?censor_character=*")) .header("apikey", api_key) .body(content) .send() @@ -76,3 +76,42 @@ async fn transform_error(res: reqwest::Response) -> handle_errors::APILayerError .message, } } + +#[cfg(test)] +mod tests { + use super::{check_profanity, env}; + use mock_server::{MockServer, OneshotHandler}; + + #[tokio::test] + async fn run() { + let handler = run_mock(); + censor_profane_words().await; + no_profane_words().await; + let _ = handler.sender.send(1); + } + + fn run_mock() -> OneshotHandler { + unsafe { + env::set_var("API_LAYER_URL", "http://127.0.0.1:3030"); + env::set_var("BAD_WORDS_API_KEY", "YES"); + } + let socket = "127.0.0.1:3030" + .to_owned() + .parse() + .expect("not a valid address"); + let mock = MockServer::new(socket); + mock.oneshot() + } + + async fn censor_profane_words() { + let content = "This is a shitty sentence".to_owned(); + let censored_content = check_profanity(content).await; + assert_eq!(censored_content.unwrap(), "this is a ****** sentence"); + } + + async fn no_profane_words() { + let content = "This is a sentence".to_owned(); + let censored_content = check_profanity(content).await; + assert_eq!(censored_content.unwrap(), ""); + } +} diff --git a/src/routes/authentication.rs b/src/routes/authentication.rs index f526b85..1458419 100644 --- a/src/routes/authentication.rs +++ b/src/routes/authentication.rs @@ -114,3 +114,24 @@ pub fn auth() -> impl Filter + Cl future::ready(Ok(token)) }) } + +#[cfg(test)] +mod authentication_tests { + use super::{auth, env, issue_token, AccountId}; + + #[tokio::test] + async fn post_questions_auth() { + unsafe { + env::set_var("PASETO_KEY", "RANDOM WORDS WINTER MACINTOSH PC"); + } + let token = issue_token(&AccountId(3)); + + let filter = auth(); + + let res = warp::test::request() + .header("Authorization", token) + .filter(&filter); + + assert_eq!(res.await.unwrap().account_id, AccountId(3)); + } +} diff --git a/src/types/pagination.rs b/src/types/pagination.rs index 4f41a3a..a6c587e 100644 --- a/src/types/pagination.rs +++ b/src/types/pagination.rs @@ -2,7 +2,7 @@ use handle_errors::Error; use std::collections::HashMap; /// Pagination struct that is getting extracted from query params -#[derive(Debug, Default, PartialEq)] +#[derive(Debug, Default, PartialEq, Eq)] pub struct Pagination { /// The index of the last item to be returned pub limit: Option, @@ -62,7 +62,7 @@ pub fn extract_pagination( } #[cfg(test)] -mod tests { +mod pagination_tests { use super::{extract_pagination, Error, HashMap, Pagination}; #[test]