feat: atomic goal, watchlist, profile writes
This commit is contained in:
@@ -31,7 +31,7 @@ async fn register_user(b: &TestContextBuilder, users: &Arc<InMemoryStore>) -> Uu
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn updates_display_name() {
|
||||
async fn updating_the_profile_records_the_change_and_its_event_together() {
|
||||
let users = InMemoryStore::new();
|
||||
let events = NoopEventPublisher::new();
|
||||
let b = TestContextBuilder::new()
|
||||
@@ -54,12 +54,9 @@ async fn updates_display_name() {
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let published = events.published();
|
||||
assert!(
|
||||
published
|
||||
.iter()
|
||||
.any(|e| matches!(e, DomainEvent::UserUpdated { .. }))
|
||||
);
|
||||
let recorded = users.recorded_events();
|
||||
assert_eq!(recorded.len(), 1);
|
||||
assert!(matches!(recorded[0], DomainEvent::UserUpdated { .. }));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
@@ -111,14 +108,15 @@ async fn uploads_avatar() {
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let published = events.published();
|
||||
assert!(
|
||||
published
|
||||
users
|
||||
.recorded_events()
|
||||
.iter()
|
||||
.any(|e| matches!(e, DomainEvent::UserUpdated { .. }))
|
||||
);
|
||||
assert!(
|
||||
published
|
||||
events
|
||||
.published()
|
||||
.iter()
|
||||
.any(|e| matches!(e, DomainEvent::ImageStored { .. }))
|
||||
);
|
||||
@@ -149,14 +147,15 @@ async fn uploads_banner() {
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let published = events.published();
|
||||
assert!(
|
||||
published
|
||||
users
|
||||
.recorded_events()
|
||||
.iter()
|
||||
.any(|e| matches!(e, DomainEvent::UserUpdated { .. }))
|
||||
);
|
||||
assert!(
|
||||
published
|
||||
events
|
||||
.published()
|
||||
.iter()
|
||||
.any(|e| matches!(e, DomainEvent::ImageStored { .. }))
|
||||
);
|
||||
@@ -232,16 +231,114 @@ async fn text_only_update_emits_user_updated_no_image_stored() {
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let published = events.published();
|
||||
assert!(
|
||||
published
|
||||
users
|
||||
.recorded_events()
|
||||
.iter()
|
||||
.any(|e| matches!(e, DomainEvent::UserUpdated { .. }))
|
||||
);
|
||||
assert!(
|
||||
!published
|
||||
!events
|
||||
.published()
|
||||
.iter()
|
||||
.any(|e| matches!(e, DomainEvent::ImageStored { .. })),
|
||||
"text-only update should not emit ImageStored"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn changing_also_known_as_records_the_move_with_the_update() {
|
||||
let users = InMemoryStore::new();
|
||||
let b = TestContextBuilder::new().with_users(Arc::clone(&users) as _);
|
||||
let deps = UpdateProfileDeps {
|
||||
user: b.user_repo.clone(),
|
||||
object_storage: b.object_storage.clone(),
|
||||
event_publisher: b.event_publisher.clone(),
|
||||
};
|
||||
let uid = register_user(&b, &users).await;
|
||||
|
||||
update_profile::execute(
|
||||
&deps,
|
||||
UpdateProfileCommand {
|
||||
also_known_as: Some("https://other.example/users/alice".into()),
|
||||
..update_profile_command(uid)
|
||||
},
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let recorded = users.recorded_events();
|
||||
assert_eq!(recorded.len(), 2);
|
||||
assert!(matches!(recorded[0], DomainEvent::UserUpdated { .. }));
|
||||
assert!(matches!(recorded[1], DomainEvent::UserAccountMoved { .. }));
|
||||
}
|
||||
|
||||
/// Loads from the store but refuses the profile write.
|
||||
struct ProfileWriteRefusingUsers(Arc<InMemoryStore>);
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl UserRepository for ProfileWriteRefusingUsers {
|
||||
async fn find_by_email(
|
||||
&self,
|
||||
e: &domain::value_objects::Email,
|
||||
) -> Result<Option<domain::models::User>, domain::errors::DomainError> {
|
||||
self.0.find_by_email(e).await
|
||||
}
|
||||
async fn find_by_username(
|
||||
&self,
|
||||
u: &domain::value_objects::Username,
|
||||
) -> Result<Option<domain::models::User>, domain::errors::DomainError> {
|
||||
self.0.find_by_username(u).await
|
||||
}
|
||||
async fn save(&self, u: &domain::models::User) -> Result<(), domain::errors::DomainError> {
|
||||
self.0.save(u).await
|
||||
}
|
||||
async fn find_by_id(
|
||||
&self,
|
||||
id: &domain::value_objects::UserId,
|
||||
) -> Result<Option<domain::models::User>, domain::errors::DomainError> {
|
||||
self.0.find_by_id(id).await
|
||||
}
|
||||
async fn list_with_stats(
|
||||
&self,
|
||||
) -> Result<Vec<domain::models::UserSummary>, domain::errors::DomainError> {
|
||||
self.0.list_with_stats().await
|
||||
}
|
||||
async fn update_profile(
|
||||
&self,
|
||||
_: &domain::value_objects::UserId,
|
||||
_: &domain::models::UserProfile,
|
||||
_: &[DomainEvent],
|
||||
) -> Result<(), domain::errors::DomainError> {
|
||||
Err(domain::errors::DomainError::InfrastructureError(
|
||||
"refused".into(),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_refused_profile_update_propagates_and_records_nothing() {
|
||||
let users = InMemoryStore::new();
|
||||
let b = TestContextBuilder::new().with_users(Arc::clone(&users) as _);
|
||||
let uid = register_user(&b, &users).await;
|
||||
let deps = UpdateProfileDeps {
|
||||
user: Arc::new(ProfileWriteRefusingUsers(Arc::clone(&users))),
|
||||
object_storage: b.object_storage.clone(),
|
||||
event_publisher: b.event_publisher.clone(),
|
||||
};
|
||||
|
||||
let result = update_profile::execute(
|
||||
&deps,
|
||||
UpdateProfileCommand {
|
||||
display_name: Some("Alice W.".into()),
|
||||
..update_profile_command(uid)
|
||||
},
|
||||
)
|
||||
.await;
|
||||
|
||||
assert!(matches!(
|
||||
result,
|
||||
Err(domain::errors::DomainError::InfrastructureError(m)) if m == "refused"
|
||||
));
|
||||
assert!(users.recorded_events().is_empty());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user