Files
k-photos/crates/application/tests/identity/commands/register_user.rs
Gabriel Kaszewski fa36bb8c0e refactor: restructure application to CQRS, update api-types + presentation
- application: replace flat use_cases/ with identity/{commands,queries}/ and organization/commands/
- each use case now split into Command/Query struct + Handler struct
- api-types: add username to RegisterRequest/UserResponse, add CreateAlbumRequest/AlbumResponse
- presentation: update state, handlers, factory to use new handler types
- tests: restructured to match CQRS module layout, added get_profile tests
2026-05-31 05:00:34 +02:00

78 lines
2.8 KiB
Rust

use std::sync::Arc;
use application::testing::{InMemoryUserRepository, StubPasswordHasher};
use application::identity::{RegisterUserCommand, RegisterUserHandler};
use domain::errors::DomainError;
#[tokio::test]
async fn registers_new_user() {
let repo = Arc::new(InMemoryUserRepository::new());
let handler = RegisterUserHandler::new(repo.clone(), Arc::new(StubPasswordHasher));
let cmd = RegisterUserCommand {
username: "testuser".into(),
email: "test@example.com".into(),
password: "password123".into(),
};
let user = handler.execute(cmd).await.unwrap();
assert_eq!(user.username, "testuser");
assert_eq!(user.email.as_str(), "test@example.com");
assert_eq!(repo.all().await.len(), 1);
}
#[tokio::test]
async fn rejects_duplicate_email() {
let repo = Arc::new(InMemoryUserRepository::new());
let handler = RegisterUserHandler::new(repo.clone(), Arc::new(StubPasswordHasher));
handler.execute(RegisterUserCommand {
username: "user1".into(),
email: "test@example.com".into(),
password: "password123".into(),
}).await.unwrap();
let result = handler.execute(RegisterUserCommand {
username: "user2".into(),
email: "test@example.com".into(),
password: "different1".into(),
}).await;
assert!(matches!(result, Err(DomainError::Conflict(_))));
}
#[tokio::test]
async fn rejects_duplicate_username() {
let repo = Arc::new(InMemoryUserRepository::new());
let handler = RegisterUserHandler::new(repo.clone(), Arc::new(StubPasswordHasher));
handler.execute(RegisterUserCommand {
username: "sameuser".into(),
email: "a@example.com".into(),
password: "password123".into(),
}).await.unwrap();
let result = handler.execute(RegisterUserCommand {
username: "sameuser".into(),
email: "b@example.com".into(),
password: "password123".into(),
}).await;
assert!(matches!(result, Err(DomainError::Conflict(_))));
}
#[tokio::test]
async fn rejects_short_password() {
let repo = Arc::new(InMemoryUserRepository::new());
let handler = RegisterUserHandler::new(repo, Arc::new(StubPasswordHasher));
let result = handler.execute(RegisterUserCommand {
username: "user".into(),
email: "test@example.com".into(),
password: "short".into(),
}).await;
assert!(matches!(result, Err(DomainError::Validation(_))));
}
#[tokio::test]
async fn rejects_empty_username() {
let repo = Arc::new(InMemoryUserRepository::new());
let handler = RegisterUserHandler::new(repo, Arc::new(StubPasswordHasher));
let result = handler.execute(RegisterUserCommand {
username: "".into(),
email: "test@example.com".into(),
password: "password123".into(),
}).await;
assert!(matches!(result, Err(DomainError::Validation(_))));
}