app: add organization + sharing commands/queries

This commit is contained in:
2026-05-31 05:17:51 +02:00
parent 536bf3463a
commit d1394ce7bb
29 changed files with 740 additions and 1 deletions

View File

@@ -2,3 +2,4 @@ mod identity;
mod organization;
mod storage;
mod catalog;
mod sharing;

View File

@@ -0,0 +1,92 @@
use std::sync::Arc;
use application::testing::InMemoryAlbumRepository;
use application::organization::{
AlbumAction, CreateAlbumCommand, CreateAlbumHandler,
ManageAlbumEntriesCommand, ManageAlbumEntriesHandler,
};
use domain::errors::DomainError;
use domain::value_objects::SystemId;
async fn setup_album(repo: &Arc<InMemoryAlbumRepository>, creator: SystemId) -> SystemId {
let handler = CreateAlbumHandler::new(repo.clone());
let album = handler.execute(CreateAlbumCommand {
title: "Test Album".into(),
creator_id: creator,
}).await.unwrap();
album.album_id
}
#[tokio::test]
async fn adds_asset_to_album() {
let repo = Arc::new(InMemoryAlbumRepository::new());
let user = SystemId::new();
let album_id = setup_album(&repo, user).await;
let asset_id = SystemId::new();
let handler = ManageAlbumEntriesHandler::new(repo.clone());
let album = handler.execute(ManageAlbumEntriesCommand {
album_id,
action: AlbumAction::Add { asset_id },
user_id: user,
}).await.unwrap();
assert_eq!(album.asset_count(), 1);
assert_eq!(album.entries[0].asset_id, asset_id);
}
#[tokio::test]
async fn removes_asset_from_album() {
let repo = Arc::new(InMemoryAlbumRepository::new());
let user = SystemId::new();
let album_id = setup_album(&repo, user).await;
let asset_id = SystemId::new();
let handler = ManageAlbumEntriesHandler::new(repo.clone());
handler.execute(ManageAlbumEntriesCommand {
album_id,
action: AlbumAction::Add { asset_id },
user_id: user,
}).await.unwrap();
let album = handler.execute(ManageAlbumEntriesCommand {
album_id,
action: AlbumAction::Remove { asset_id },
user_id: user,
}).await.unwrap();
assert_eq!(album.asset_count(), 0);
}
#[tokio::test]
async fn rejects_nonexistent_album() {
let repo = Arc::new(InMemoryAlbumRepository::new());
let handler = ManageAlbumEntriesHandler::new(repo);
let result = handler.execute(ManageAlbumEntriesCommand {
album_id: SystemId::new(),
action: AlbumAction::Add { asset_id: SystemId::new() },
user_id: SystemId::new(),
}).await;
assert!(matches!(result, Err(DomainError::NotFound(_))));
}
#[tokio::test]
async fn rejects_duplicate_add() {
let repo = Arc::new(InMemoryAlbumRepository::new());
let user = SystemId::new();
let album_id = setup_album(&repo, user).await;
let asset_id = SystemId::new();
let handler = ManageAlbumEntriesHandler::new(repo.clone());
handler.execute(ManageAlbumEntriesCommand {
album_id,
action: AlbumAction::Add { asset_id },
user_id: user,
}).await.unwrap();
let result = handler.execute(ManageAlbumEntriesCommand {
album_id,
action: AlbumAction::Add { asset_id },
user_id: user,
}).await;
assert!(matches!(result, Err(DomainError::Conflict(_))));
}

View File

@@ -1 +1,3 @@
mod create_album;
mod manage_album_entries;
mod tag_asset;

View File

@@ -0,0 +1,81 @@
use std::sync::Arc;
use application::testing::{InMemoryAssetRepository, InMemoryTagRepository};
use application::organization::{TagAssetCommand, TagAssetHandler};
use domain::entities::{Asset, AssetType, SourceReference};
use domain::errors::DomainError;
use domain::ports::{AssetRepository, TagRepository};
use domain::value_objects::{Checksum, SystemId};
async fn seed_asset(repo: &Arc<InMemoryAssetRepository>) -> SystemId {
let owner = SystemId::new();
let asset = Asset::new(
SourceReference {
volume_id: SystemId::new(),
relative_path: "photos/test.jpg".into(),
checksum: Checksum::new("a".repeat(64)).unwrap(),
},
AssetType::Image,
"image/jpeg",
1024,
owner,
);
let id = asset.asset_id;
repo.save(&asset).await.unwrap();
id
}
#[tokio::test]
async fn tags_asset_creates_new_tag() {
let asset_repo = Arc::new(InMemoryAssetRepository::new());
let tag_repo = Arc::new(InMemoryTagRepository::new());
let asset_id = seed_asset(&asset_repo).await;
let user = SystemId::new();
let handler = TagAssetHandler::new(asset_repo, tag_repo);
let (tag, asset_tag) = handler.execute(TagAssetCommand {
asset_id,
tag_name: "sunset".into(),
user_id: user,
}).await.unwrap();
assert_eq!(tag.name, "sunset");
assert_eq!(asset_tag.asset_id, asset_id);
assert_eq!(asset_tag.tag_id, tag.tag_id);
assert_eq!(asset_tag.tagged_by_user_id, Some(user));
}
#[tokio::test]
async fn reuses_existing_tag() {
let asset_repo = Arc::new(InMemoryAssetRepository::new());
let tag_repo = Arc::new(InMemoryTagRepository::new());
let asset_id = seed_asset(&asset_repo).await;
let user = SystemId::new();
// Pre-create tag
let existing = domain::entities::Tag::new_manual("sunset");
let existing_id = existing.tag_id;
tag_repo.save_tag(&existing).await.unwrap();
let handler = TagAssetHandler::new(asset_repo, tag_repo);
let (tag, _) = handler.execute(TagAssetCommand {
asset_id,
tag_name: "sunset".into(),
user_id: user,
}).await.unwrap();
assert_eq!(tag.tag_id, existing_id);
}
#[tokio::test]
async fn rejects_nonexistent_asset() {
let asset_repo = Arc::new(InMemoryAssetRepository::new());
let tag_repo = Arc::new(InMemoryTagRepository::new());
let handler = TagAssetHandler::new(asset_repo, tag_repo);
let result = handler.execute(TagAssetCommand {
asset_id: SystemId::new(),
tag_name: "sunset".into(),
user_id: SystemId::new(),
}).await;
assert!(matches!(result, Err(DomainError::NotFound(_))));
}

View File

@@ -1 +1,2 @@
mod commands;
mod queries;

View File

@@ -0,0 +1,38 @@
use std::sync::Arc;
use application::testing::InMemoryAlbumRepository;
use application::organization::{
CreateAlbumCommand, CreateAlbumHandler,
GetAlbumQuery, GetAlbumHandler,
};
use domain::errors::DomainError;
use domain::value_objects::SystemId;
#[tokio::test]
async fn returns_album() {
let repo = Arc::new(InMemoryAlbumRepository::new());
let creator = SystemId::new();
let create_handler = CreateAlbumHandler::new(repo.clone());
let album = create_handler.execute(CreateAlbumCommand {
title: "My Album".into(),
creator_id: creator,
}).await.unwrap();
let query_handler = GetAlbumHandler::new(repo);
let found = query_handler.execute(GetAlbumQuery {
album_id: album.album_id,
}).await.unwrap();
assert_eq!(found.album_id, album.album_id);
assert_eq!(found.title, "My Album");
}
#[tokio::test]
async fn rejects_nonexistent() {
let repo = Arc::new(InMemoryAlbumRepository::new());
let handler = GetAlbumHandler::new(repo);
let result = handler.execute(GetAlbumQuery {
album_id: SystemId::new(),
}).await;
assert!(matches!(result, Err(DomainError::NotFound(_))));
}

View File

@@ -0,0 +1 @@
mod get_album;

View File

@@ -0,0 +1,46 @@
use std::sync::Arc;
use application::testing::InMemoryShareRepository;
use application::sharing::{GenerateShareLinkCommand, GenerateShareLinkHandler};
use domain::entities::{LinkAccessLevel, ScopeType, ShareableType};
use domain::value_objects::{DateTimeStamp, SystemId};
#[tokio::test]
async fn generates_link() {
let share_repo = Arc::new(InMemoryShareRepository::new());
let handler = GenerateShareLinkHandler::new(share_repo);
let (scope, link) = handler.execute(GenerateShareLinkCommand {
shareable_type: ShareableType::Album,
shareable_id: SystemId::new(),
access_level: LinkAccessLevel::ViewOnly,
created_by: SystemId::new(),
expires_at: None,
max_uses: None,
}).await.unwrap();
assert_eq!(scope.scope_type, ScopeType::Link);
assert!(!link.token.is_empty());
assert_eq!(link.access_level, LinkAccessLevel::ViewOnly);
assert!(link.expires_at.is_none());
assert!(link.max_uses.is_none());
}
#[tokio::test]
async fn generates_link_with_expiry_and_max_uses() {
let share_repo = Arc::new(InMemoryShareRepository::new());
let handler = GenerateShareLinkHandler::new(share_repo);
let expiry = DateTimeStamp::now();
let (_, link) = handler.execute(GenerateShareLinkCommand {
shareable_type: ShareableType::Collection,
shareable_id: SystemId::new(),
access_level: LinkAccessLevel::LimitedSearch,
created_by: SystemId::new(),
expires_at: Some(expiry),
max_uses: Some(10),
}).await.unwrap();
assert!(link.expires_at.is_some());
assert_eq!(link.max_uses, Some(10));
assert_eq!(link.access_level, LinkAccessLevel::LimitedSearch);
}

View File

@@ -0,0 +1,3 @@
mod share_resource;
mod generate_share_link;
mod revoke_share;

View File

@@ -0,0 +1,48 @@
use std::sync::Arc;
use application::testing::{InMemoryShareRepository, StubEventPublisher};
use application::sharing::{
GenerateShareLinkCommand, GenerateShareLinkHandler,
RevokeShareCommand, RevokeShareHandler,
};
use domain::entities::{LinkAccessLevel, ShareableType};
use domain::errors::DomainError;
use domain::value_objects::SystemId;
#[tokio::test]
async fn revokes_share() {
let share_repo = Arc::new(InMemoryShareRepository::new());
let event_pub = Arc::new(StubEventPublisher::new());
// Create a scope first via generate_share_link
let gen_handler = GenerateShareLinkHandler::new(share_repo.clone());
let (scope, _) = gen_handler.execute(GenerateShareLinkCommand {
shareable_type: ShareableType::Album,
shareable_id: SystemId::new(),
access_level: LinkAccessLevel::ViewOnly,
created_by: SystemId::new(),
expires_at: None,
max_uses: None,
}).await.unwrap();
let handler = RevokeShareHandler::new(share_repo, event_pub.clone());
handler.execute(RevokeShareCommand {
scope_id: scope.scope_id,
revoked_by: SystemId::new(),
}).await.unwrap();
let events = event_pub.published().await;
assert_eq!(events.len(), 1);
}
#[tokio::test]
async fn rejects_nonexistent_scope() {
let share_repo = Arc::new(InMemoryShareRepository::new());
let event_pub = Arc::new(StubEventPublisher::new());
let handler = RevokeShareHandler::new(share_repo, event_pub);
let result = handler.execute(RevokeShareCommand {
scope_id: SystemId::new(),
revoked_by: SystemId::new(),
}).await;
assert!(matches!(result, Err(DomainError::NotFound(_))));
}

View File

@@ -0,0 +1,47 @@
use std::sync::Arc;
use application::testing::{InMemoryShareRepository, StubEventPublisher};
use application::sharing::{ShareResourceCommand, ShareResourceHandler};
use domain::entities::{ScopeType, ShareableType, TargetType};
use domain::value_objects::SystemId;
#[tokio::test]
async fn shares_with_user() {
let share_repo = Arc::new(InMemoryShareRepository::new());
let event_pub = Arc::new(StubEventPublisher::new());
let handler = ShareResourceHandler::new(share_repo, event_pub.clone());
let (scope, target) = handler.execute(ShareResourceCommand {
shareable_type: ShareableType::Album,
shareable_id: SystemId::new(),
target_type: TargetType::User,
target_id: SystemId::new(),
role_id: SystemId::new(),
created_by: SystemId::new(),
}).await.unwrap();
assert_eq!(scope.scope_type, ScopeType::User);
assert_eq!(target.target_type, TargetType::User);
assert_eq!(target.scope_id, scope.scope_id);
let events = event_pub.published().await;
assert_eq!(events.len(), 1);
}
#[tokio::test]
async fn shares_with_group() {
let share_repo = Arc::new(InMemoryShareRepository::new());
let event_pub = Arc::new(StubEventPublisher::new());
let handler = ShareResourceHandler::new(share_repo, event_pub.clone());
let (scope, target) = handler.execute(ShareResourceCommand {
shareable_type: ShareableType::Asset,
shareable_id: SystemId::new(),
target_type: TargetType::Group,
target_id: SystemId::new(),
role_id: SystemId::new(),
created_by: SystemId::new(),
}).await.unwrap();
assert_eq!(scope.scope_type, ScopeType::Group);
assert_eq!(target.target_type, TargetType::Group);
}

View File

@@ -0,0 +1,2 @@
mod commands;
mod queries;

View File

@@ -0,0 +1,67 @@
use std::sync::Arc;
use application::testing::InMemoryShareRepository;
use application::sharing::{
AccessSharedResourceQuery, AccessSharedResourceHandler,
GenerateShareLinkCommand, GenerateShareLinkHandler,
};
use chrono::{DateTime, Utc};
use domain::entities::{LinkAccessLevel, ShareableType};
use domain::errors::DomainError;
use domain::value_objects::{DateTimeStamp, SystemId};
async fn create_link(
repo: &Arc<InMemoryShareRepository>,
expires_at: Option<DateTimeStamp>,
max_uses: Option<u32>,
) -> String {
let handler = GenerateShareLinkHandler::new(repo.clone());
let (_, link) = handler.execute(GenerateShareLinkCommand {
shareable_type: ShareableType::Album,
shareable_id: SystemId::new(),
access_level: LinkAccessLevel::ViewOnly,
created_by: SystemId::new(),
expires_at,
max_uses,
}).await.unwrap();
link.token
}
#[tokio::test]
async fn valid_link_returns_scope() {
let repo = Arc::new(InMemoryShareRepository::new());
let token = create_link(&repo, None, None).await;
let handler = AccessSharedResourceHandler::new(repo);
let (scope, access_level) = handler.execute(AccessSharedResourceQuery {
token,
}).await.unwrap();
assert_eq!(access_level, LinkAccessLevel::ViewOnly);
assert_eq!(scope.shareable_type, ShareableType::Album);
}
#[tokio::test]
async fn expired_link_rejected() {
let repo = Arc::new(InMemoryShareRepository::new());
// Create link with past expiry
let past = DateTimeStamp::from_datetime(DateTime::<Utc>::from_timestamp(0, 0).unwrap());
let token = create_link(&repo, Some(past), None).await;
let handler = AccessSharedResourceHandler::new(repo);
let result = handler.execute(AccessSharedResourceQuery { token }).await;
assert!(matches!(result, Err(DomainError::Forbidden(_))));
}
#[tokio::test]
async fn exhausted_link_rejected() {
let repo = Arc::new(InMemoryShareRepository::new());
let token = create_link(&repo, None, Some(1)).await;
// Use it once
let handler = AccessSharedResourceHandler::new(repo.clone());
handler.execute(AccessSharedResourceQuery { token: token.clone() }).await.unwrap();
// Second use should fail
let result = handler.execute(AccessSharedResourceQuery { token }).await;
assert!(matches!(result, Err(DomainError::Forbidden(_))));
}

View File

@@ -0,0 +1 @@
mod access_shared_resource;