Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor SMTP support to track sent emails and quotas #33

Merged
merged 6 commits into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,45 @@ members = [
"core",
"example",
"geo",
"lettre",
"queue",
"smtp",
]
resolver = "2"

[workspace.dependencies]
async-session = "3"
async-trait = "0.1"
axum = "0.6"
base64 = "0.21"
bcrypt = "0.14"
bytes = "1.0"
derivative = "2.2"
derive-getters = "0.2.0"
derive_more = { version = "0.99.0", default-features = false }
env_logger = "0.8"
futures = "0.3"
http = "0.2.8"
http-body = "0.4"
hyper = { version = "0.14", features = ["full"] }
lettre = { version = "0.10.0-rc.6", default-features = false }
log = "0.4"
lru_time_cache = "0.11"
mime = "0.3"
paste = "1.0"
quoted_printable = "0.5"
rand = "0.8"
regex = "1"
reqwest = { version = "0.11", default-features = false, features = ["rustls-tls"] }
serde_json = "1"
serde_test = "1"
serde_urlencoded = "0.7"
serde = { version = "1", features = ["derive"] }
sqlx = "0.7"
temp-env = "0.3.2"
thiserror = "1.0"
time = "0.3"
tokio = "1"
tower = "0.4"
tower-http = { version = "0.3", features = ["cors"] }
url = "2.3"
uuid = { version = "1.0", default-features = false, features = ["serde", "std", "v4"] }
46 changes: 23 additions & 23 deletions authn/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,39 @@ publish = false
default = ["postgres"]
postgres = ["iii-iv-core/postgres", "sqlx/postgres"]
sqlite = ["iii-iv-core/sqlite", "sqlx/sqlite"]
testutils = ["dep:url", "iii-iv-core/sqlite", "iii-iv-core/testutils", "iii-iv-lettre/testutils"]
testutils = ["dep:url", "iii-iv-core/sqlite", "iii-iv-core/testutils", "iii-iv-smtp/testutils"]

[dependencies]
async-trait = "0.1"
axum = "0.6"
base64 = "0.21"
bcrypt = "0.14"
derivative = "2.2"
futures = "0.3"
log = "0.4"
http = "0.2.8"
async-trait = { workspace = true }
axum = { workspace = true }
base64 = { workspace = true }
bcrypt = { workspace = true }
derivative = { workspace = true }
futures = { workspace = true }
http = { workspace = true }
iii-iv-core = { path = "../core" }
iii-iv-lettre = { path = "../lettre" }
lru_time_cache = "0.11"
rand = "0.8"
serde = { version = "1", features = ["derive"] }
serde_urlencoded = "0.7"
time = "0.3"
url = { version = "2.3", optional = true }
iii-iv-smtp = { path = "../smtp" }
log = { workspace = true }
lru_time_cache = { workspace = true }
rand = { workspace = true }
serde_urlencoded = { workspace = true }
serde = { workspace = true }
time = { workspace = true }
url = { workspace = true, optional = true }

[dependencies.sqlx]
version = "0.7"
workspace = true
optional = true
features = ["runtime-tokio-rustls", "time"]

[dev-dependencies]
futures = "0.3"
futures = { workspace = true }
iii-iv-core = { path = "../core", features = ["sqlite", "testutils"] }
iii-iv-lettre = { path = "../lettre", features = ["testutils"] }
temp-env = "0.3.2"
tokio = { version = "1", features = ["rt", "macros", "rt-multi-thread"] }
url = "2.3"
iii-iv-smtp = { path = "../smtp", features = ["testutils"] }
temp-env = { workspace = true }
tokio = { workspace = true, features = ["rt", "macros", "rt-multi-thread"] }
url = { workspace = true }

[dev-dependencies.sqlx]
version = "0.7"
workspace = true
features = ["runtime-tokio-rustls", "sqlite", "time"]
9 changes: 6 additions & 3 deletions authn/src/driver/email.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
use crate::driver::DriverResult;
use iii_iv_core::model::{EmailAddress, Username};
use iii_iv_core::rest::BaseUrls;
use iii_iv_lettre::{EmailTemplate, SmtpMailer};
use iii_iv_smtp::driver::SmtpMailer;
use iii_iv_smtp::model::EmailTemplate;

/// Sends the activation code `code` for `username` to the given `email` address.
///
Expand Down Expand Up @@ -51,7 +52,8 @@ pub(crate) mod testutils {
//! Utilities to help testing services that integrate with the `authn` features.

use super::*;
use iii_iv_lettre::testutils::{parse_message, RecorderSmtpMailer};
use iii_iv_smtp::driver::testutils::RecorderSmtpMailer;
use iii_iv_smtp::model::testutils::parse_message;
use url::Url;

/// Creates an email activation template to capture activation codes during tests.
Expand Down Expand Up @@ -105,7 +107,8 @@ pub(crate) mod testutils {
mod tests {
use super::testutils::*;
use super::*;
use iii_iv_lettre::testutils::*;
use iii_iv_smtp::driver::testutils::RecorderSmtpMailer;
use iii_iv_smtp::model::testutils::parse_message;

#[tokio::test]
async fn test_send_activation_code() {
Expand Down
3 changes: 2 additions & 1 deletion authn/src/driver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ use iii_iv_core::db::{Db, DbError, TxExecutor};
use iii_iv_core::driver::{DriverError, DriverResult};
use iii_iv_core::env::get_optional_var;
use iii_iv_core::rest::BaseUrls;
use iii_iv_lettre::{EmailTemplate, SmtpMailer};
use iii_iv_smtp::driver::SmtpMailer;
use iii_iv_smtp::model::EmailTemplate;
use log::warn;
use lru_time_cache::LruCache;
use std::sync::Arc;
Expand Down
2 changes: 1 addition & 1 deletion authn/src/driver/testutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use iii_iv_core::db::Executor;
use iii_iv_core::model::EmailAddress;
use iii_iv_core::model::Username;
use iii_iv_core::rest::BaseUrls;
use iii_iv_lettre::testutils::RecorderSmtpMailer;
use iii_iv_smtp::driver::testutils::RecorderSmtpMailer;
use std::sync::Arc;

/// State of a running test.
Expand Down
2 changes: 1 addition & 1 deletion authn/src/rest/testutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use {
iii_iv_core::clocks::testutils::MonotonicClock,
iii_iv_core::db::{Db, DbError},
iii_iv_core::rest::BaseUrls,
iii_iv_lettre::testutils::RecorderSmtpMailer,
iii_iv_smtp::driver::testutils::RecorderSmtpMailer,
std::sync::Arc,
};

Expand Down
60 changes: 30 additions & 30 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,35 +38,35 @@ testutils = [
]

[dependencies]
async-trait = "0.1"
axum = "0.6"
base64 = { version = "0.21", optional = true }
bytes = { version = "1.0", optional = true }
derivative = "2.2"
env_logger = { version = "0.8", optional = true }
futures = { version = "0.3", optional = true }
http = "0.2.8"
http-body = { version = "0.4", optional = true }
hyper = { version = "0.14", optional = true, features = ["full"] }
log = { version = "0.4", optional = true }
mime = { version = "0.3", optional = true }
paste = { version = "1.0", optional = true }
rand = { version = "0.8", optional = true }
regex = { version = "1", optional = true }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
serde_urlencoded = { version = "0.7", optional = true }
sqlx = "0.7"
thiserror = "1.0"
time = "0.3"
tokio = { version = "1", optional = true }
tower = { version = "0.4", optional = true }
url = "2.3"
async-trait = { workspace = true }
axum = { workspace = true }
base64 = { workspace = true, optional = true }
bytes = { workspace = true, optional = true }
derivative = { workspace = true }
env_logger = { workspace = true, optional = true }
futures = { workspace = true, optional = true }
http-body = { workspace = true, optional = true }
http = { workspace = true }
hyper = { workspace = true, optional = true }
log = { workspace = true, optional = true }
mime = { workspace = true, optional = true }
paste = { workspace = true, optional = true }
rand = { workspace = true, optional = true }
regex = { workspace = true, optional = true }
serde_json = { workspace = true }
serde_urlencoded = { workspace = true, optional = true }
serde = { workspace = true }
sqlx = { workspace = true }
thiserror = { workspace = true }
time = { workspace = true }
tokio = { workspace = true, optional = true }
tower = { workspace = true, optional = true }
url = { workspace = true }

[dev-dependencies]
env_logger = "0.8"
paste = "1.0"
rand = "0.8"
serde_test = "1"
temp-env = "0.3.2"
tokio = { version = "1", features = ["macros"] }
env_logger = { workspace = true }
paste = { workspace = true }
rand = { workspace = true }
serde_test = { workspace = true }
temp-env = { workspace = true }
tokio = { workspace = true, features = ["macros"] }
19 changes: 19 additions & 0 deletions core/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,25 @@ pub trait Db {
async fn begin(&self) -> DbResult<TxExecutor>;
}

/// Parses a `COUNT` result as a `usize`.
pub fn count_as_usize(count: i64) -> DbResult<usize> {
match usize::try_from(count) {
Ok(count) => Ok(count),
Err(_) => Err(DbError::BackendError(
"COUNT should have returned a positive value that fits in usize".to_owned(),
)),
}
}

/// Helper to verify that an insert and/or update opeeration affected just one row.
pub fn ensure_one_upsert(rows_affected: u64) -> DbResult<()> {
if rows_affected != 1 {
Err(DbError::BackendError(format!("Expected 1 new/modified row but got {}", rows_affected)))
} else {
Ok(())
}
}

/// Macros to help instantiate tests for multiple database systems.
#[cfg(any(test, feature = "testutils"))]
pub mod testutils {
Expand Down
33 changes: 16 additions & 17 deletions example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,28 @@ edition = "2021"
publish = false

[dependencies]
async-session = "3"
async-trait = "0.1"
axum = "0.6"
derive-getters = "0.2.0"
env_logger = "0.8"
futures = "0.3"
hyper = { version = "0.14", features = ["full"] }
log = "0.4"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
sqlx = { version = "0.7", features = ["runtime-tokio-rustls", "sqlite"] }
thiserror = "1.0"
tokio = { version = "1", features = ["rt", "macros", "rt-multi-thread"] }
tower-http = { version = "0.3", features = ["cors"] }
url = { version = "2.3", features = ["serde"] }
async-session = { workspace = true }
async-trait = { workspace = true }
axum = { workspace = true }
derive-getters = { workspace = true }
env_logger = { workspace = true }
futures = { workspace = true }
hyper = { workspace = true }
log = { workspace = true }
serde_json = { workspace = true }
serde = { workspace = true }
sqlx = { workspace = true, features = ["runtime-tokio-rustls", "sqlite"] }
thiserror = { workspace = true }
tokio = { workspace = true, features = ["rt", "macros", "rt-multi-thread"] }
tower-http = { workspace = true }
url = { workspace = true, features = ["serde"] }

[dependencies.iii-iv-core]
path = "../core"
features = ["postgres"]

[dependencies.derive_more]
version = "0.99.0"
default-features = false
workspace = true
features = ["as_ref", "constructor"]

[dev-dependencies.iii-iv-core]
Expand Down
28 changes: 14 additions & 14 deletions geo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ default = []
testutils = []

[dependencies]
async-trait = "0.1"
bytes = "1.0"
derivative = "2.2"
futures = "0.3"
async-trait = { workspace = true }
bytes = { workspace = true }
derivative = { workspace = true }
futures = { workspace = true }
iii-iv-core = { path = "../core" }
log = "0.4"
lru_time_cache = "0.11"
reqwest = { version = "0.11", default-features = false, features = ["rustls-tls"] }
serde = "1"
serde_json = "1"
time = "0.3"
log = { workspace = true }
lru_time_cache = { workspace = true }
reqwest = { workspace = true }
serde_json = { workspace = true }
serde = { workspace = true }
time = { workspace = true }

[dev-dependencies]
iii-iv-core = { path = "../core", features = ["testutils"] }
serde_test = "1"
temp-env = "0.3.2"
time = { version = "0.3", features = ["macros"] }
tokio = { version = "1", features = ["rt", "macros", "rt-multi-thread"] }
serde_test = { workspace = true }
temp-env = { workspace = true }
time = { workspace = true, features = ["macros"] }
tokio = { workspace = true, features = ["rt", "macros", "rt-multi-thread"] }
35 changes: 0 additions & 35 deletions lettre/Cargo.toml

This file was deleted.

Loading
Loading