test: shared factories and command builders

This commit is contained in:
2026-10-25 22:08:17 +01:00
parent 2ac1d4e0cf
commit 51d92e08a7
58 changed files with 784 additions and 1344 deletions

View File

@@ -1,15 +1,13 @@
use crate::test_support::{login_command, register_command};
use std::sync::Arc;
use domain::models::UserRole;
use domain::testing::InMemoryUserRepository;
use crate::{
auth::{
commands::RegisterCommand,
deps::{LoginDeps, RegisterDeps},
login,
queries::LoginCommand,
register,
login, register,
},
test_helpers::TestContextBuilder,
};
@@ -23,10 +21,8 @@ async fn setup_user(b: &TestContextBuilder, email: &str, password: &str) {
register::execute(
&deps,
RegisterCommand {
email: email.to_string(),
username: "testuser".to_string(),
password: password.to_string(),
role: UserRole::Standard,
..register_command(email, "testuser")
},
)
.await
@@ -46,15 +42,9 @@ async fn test_login_valid_credentials_returns_token() {
refresh_session: b.refresh_session_repo.clone(),
config: b.config.clone(),
};
let result = login::execute(
&deps,
LoginCommand {
email: "carol@example.com".into(),
password: "secret123".into(),
},
)
.await
.unwrap();
let result = login::execute(&deps, login_command("carol@example.com", "secret123"))
.await
.unwrap();
assert!(!result.token.is_empty());
assert!(!result.refresh_token.is_empty());
@@ -74,14 +64,7 @@ async fn test_login_wrong_password_fails() {
refresh_session: b.refresh_session_repo.clone(),
config: b.config.clone(),
};
let result = login::execute(
&deps,
LoginCommand {
email: "dave@example.com".into(),
password: "wrong_password".into(),
},
)
.await;
let result = login::execute(&deps, login_command("dave@example.com", "wrong_password")).await;
assert!(result.is_err());
}
@@ -96,14 +79,7 @@ async fn test_login_unknown_email_fails() {
refresh_session: b.refresh_session_repo.clone(),
config: b.config.clone(),
};
let result = login::execute(
&deps,
LoginCommand {
email: "nobody@example.com".into(),
password: "anything".into(),
},
)
.await;
let result = login::execute(&deps, login_command("nobody@example.com", "anything")).await;
assert!(result.is_err());
}

View File

@@ -1,15 +1,12 @@
use crate::test_support::{login_command, register_command};
use std::sync::Arc;
use domain::models::UserRole;
use domain::testing::InMemoryUserRepository;
use crate::{
auth::{
commands::RegisterCommand,
deps::{LoginDeps, LogoutDeps, RefreshDeps, RegisterDeps},
login, logout,
queries::LoginCommand,
refresh, register,
login, logout, refresh, register,
},
test_helpers::TestContextBuilder,
};
@@ -24,17 +21,9 @@ async fn logout_revokes_refresh_token() {
password_hasher: b.password_hasher.clone(),
config: b.config.clone(),
};
register::execute(
&reg_deps,
RegisterCommand {
email: "bob@example.com".to_string(),
username: "bob".to_string(),
password: "password123".to_string(),
role: UserRole::Standard,
},
)
.await
.unwrap();
register::execute(&reg_deps, register_command("bob@example.com", "bob"))
.await
.unwrap();
let login_deps = LoginDeps {
user: b.user_repo.clone(),
@@ -43,15 +32,9 @@ async fn logout_revokes_refresh_token() {
refresh_session: b.refresh_session_repo.clone(),
config: b.config.clone(),
};
let login_result = login::execute(
&login_deps,
LoginCommand {
email: "bob@example.com".into(),
password: "password123".into(),
},
)
.await
.unwrap();
let login_result = login::execute(&login_deps, login_command("bob@example.com", "password123"))
.await
.unwrap();
let logout_deps = LogoutDeps {
refresh_session: b.refresh_session_repo.clone(),

View File

@@ -1,15 +1,12 @@
use crate::test_support::{login_command, register_command};
use std::sync::Arc;
use domain::models::UserRole;
use domain::testing::InMemoryUserRepository;
use crate::{
auth::{
commands::RegisterCommand,
deps::{LoginDeps, RefreshDeps, RegisterDeps},
login,
queries::LoginCommand,
refresh, register,
login, refresh, register,
},
test_helpers::TestContextBuilder,
};
@@ -20,17 +17,9 @@ async fn login_user(b: &TestContextBuilder) -> login::LoginResult {
password_hasher: b.password_hasher.clone(),
config: b.config.clone(),
};
register::execute(
&reg_deps,
RegisterCommand {
email: "alice@example.com".to_string(),
username: "alice".to_string(),
password: "password123".to_string(),
role: UserRole::Standard,
},
)
.await
.unwrap();
register::execute(&reg_deps, register_command("alice@example.com", "alice"))
.await
.unwrap();
let login_deps = LoginDeps {
user: b.user_repo.clone(),
@@ -41,10 +30,7 @@ async fn login_user(b: &TestContextBuilder) -> login::LoginResult {
};
login::execute(
&login_deps,
LoginCommand {
email: "alice@example.com".into(),
password: "password123".into(),
},
login_command("alice@example.com", "password123"),
)
.await
.unwrap()

View File

@@ -1,6 +1,6 @@
use crate::test_support::register_command;
use std::sync::Arc;
use domain::models::UserRole;
use domain::ports::UserRepository;
use domain::testing::InMemoryUserRepository;
use domain::value_objects::Email;
@@ -11,12 +11,7 @@ use crate::{
};
fn cmd(email: &str) -> RegisterCommand {
RegisterCommand {
email: email.to_string(),
username: "alice".to_string(),
password: "password123".to_string(),
role: UserRole::Standard,
}
register_command(email, "alice")
}
#[tokio::test]
@@ -67,10 +62,8 @@ async fn test_register_short_password_fails() {
let result = register::execute(
&deps,
RegisterCommand {
email: "x@y.com".to_string(),
username: "testuser".to_string(),
password: "short".to_string(),
role: UserRole::Standard,
..register_command("x@y.com", "testuser")
},
)
.await;