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

@@ -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;