feat: UserUnblocked + UserRegistered events, fix unblock_user and register signatures
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
use domain::{
|
||||
errors::DomainError,
|
||||
events::DomainEvent,
|
||||
models::user::User,
|
||||
ports::{AuthService, EventPublisher, PasswordHasher, UserRepository},
|
||||
value_objects::{Email, UserId, Username},
|
||||
@@ -13,7 +14,7 @@ pub async fn register(
|
||||
users: &dyn UserRepository,
|
||||
hasher: &dyn PasswordHasher,
|
||||
auth: &dyn AuthService,
|
||||
_events: &dyn EventPublisher,
|
||||
events: &dyn EventPublisher,
|
||||
input: RegisterInput,
|
||||
) -> Result<RegisterOutput, DomainError> {
|
||||
let username = Username::new(input.username)?;
|
||||
@@ -27,6 +28,7 @@ pub async fn register(
|
||||
let hash = hasher.hash(&input.password).await?;
|
||||
let user = User::new_local(UserId::new(), username, email, hash);
|
||||
users.save(&user).await?;
|
||||
events.publish(&DomainEvent::UserRegistered { user_id: user.id.clone() }).await?;
|
||||
let token = auth.generate_token(&user.id)?;
|
||||
Ok(RegisterOutput { user, token: token.token })
|
||||
}
|
||||
@@ -56,6 +58,7 @@ mod tests {
|
||||
use async_trait::async_trait;
|
||||
use domain::{
|
||||
errors::DomainError,
|
||||
events::DomainEvent,
|
||||
ports::{AuthService, GeneratedToken, PasswordHasher},
|
||||
testing::{NoOpEventPublisher, TestStore},
|
||||
value_objects::{PasswordHash, UserId},
|
||||
@@ -112,4 +115,13 @@ mod tests {
|
||||
let err = login(&store, &FakeHasher, &FakeAuth, LoginInput { email: "alice@ex.com".into(), password: "wrong".into() }).await.unwrap_err();
|
||||
assert!(matches!(err, DomainError::Unauthorized));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn register_publishes_user_registered_event() {
|
||||
let store = TestStore::default();
|
||||
register(&store, &FakeHasher, &FakeAuth, &store, input()).await.unwrap();
|
||||
let events = store.events.lock().unwrap();
|
||||
assert_eq!(events.len(), 1);
|
||||
assert!(matches!(events[0], DomainEvent::UserRegistered { .. }));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,8 +67,17 @@ pub async fn block_user(blocks: &dyn BlockRepository, events: &dyn EventPublishe
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn unblock_user(blocks: &dyn BlockRepository, blocker_id: &UserId, blocked_id: &UserId) -> Result<(), DomainError> {
|
||||
pub async fn unblock_user(
|
||||
blocks: &dyn BlockRepository,
|
||||
events: &dyn EventPublisher,
|
||||
blocker_id: &UserId,
|
||||
blocked_id: &UserId,
|
||||
) -> Result<(), DomainError> {
|
||||
blocks.delete(blocker_id, blocked_id).await?;
|
||||
events.publish(&DomainEvent::UserUnblocked {
|
||||
blocker_id: blocker_id.clone(),
|
||||
blocked_id: blocked_id.clone(),
|
||||
}).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -114,4 +123,17 @@ mod tests {
|
||||
let err = follow_user(&store, &store, &alice.id, &alice.id).await.unwrap_err();
|
||||
assert!(matches!(err, DomainError::InvalidInput(_)));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn unblock_user_publishes_event() {
|
||||
let store = TestStore::default();
|
||||
let alice = user("alice");
|
||||
let bob = user("bob");
|
||||
block_user(&store, &store, &alice.id, &bob.id).await.unwrap();
|
||||
store.events.lock().unwrap().clear();
|
||||
unblock_user(&store, &store, &alice.id, &bob.id).await.unwrap();
|
||||
let events = store.events.lock().unwrap();
|
||||
assert_eq!(events.len(), 1);
|
||||
assert!(matches!(events[0], DomainEvent::UserUnblocked { .. }));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user