use std::sync::Arc;
use async_trait::async_trait;
use serde_json::Value;
use crate::principal::SubjectRef;
use crate::subject::{SubjectDataStore, SubjectScope};
#[async_trait]
pub trait SubjectDataStoreHarness: Send + Sync {
async fn open(&self) -> Arc<dyn SubjectDataStore>;
async fn seed(&self, app_name: &str, user_id: &str);
async fn visible(&self, app_name: &str, user_id: &str) -> usize;
}
fn is_empty(value: &Value) -> bool {
match value {
Value::Null => true,
Value::Array(items) => items.iter().all(is_empty),
Value::Object(fields) => fields.values().all(is_empty),
Value::Bool(_) | Value::Number(_) | Value::String(_) => false,
}
}
fn plain(prefix: &str) -> String {
format!("conformance-{prefix}-{}", uuid::Uuid::new_v4().simple())
}
fn app() -> String {
plain("app")
}
fn person() -> SubjectRef {
SubjectRef::human(plain("person"))
}
pub async fn export_returns_only_the_subject<H: SubjectDataStoreHarness + ?Sized>(harness: &H) {
let store = harness.open().await;
let (app, alice, bob) = (app(), person(), person());
harness.seed(&app, &alice.id).await;
let exported = store.export(&SubjectScope::new([&app], alice)).await.expect("export");
assert!(!is_empty(&exported), "the export holds the subject's data: {exported}");
let nobody = store.export(&SubjectScope::new([&app], bob)).await.expect("export");
assert!(is_empty(&nobody), "nothing is exported for a subject never seeded: {nobody}");
}
pub async fn erase_removes_only_the_subject<H: SubjectDataStoreHarness + ?Sized>(harness: &H) {
let store = harness.open().await;
let (app, alice, bob) = (app(), person(), person());
harness.seed(&app, &alice.id).await;
harness.seed(&app, &bob.id).await;
let bob_before = harness.visible(&app, &bob.id).await;
assert!(harness.visible(&app, &alice.id).await > 0, "the harness seeded data");
let scope = SubjectScope::new([&app], alice.clone());
let erasure = store.erase(&scope).await.expect("erase");
assert!(erasure.deleted > 0, "the erasure reports what it deleted: {erasure:?}");
assert_eq!(harness.visible(&app, &alice.id).await, 0, "no read path returns the subject");
let exported = store.export(&scope).await.expect("export");
assert!(is_empty(&exported), "nothing is exported after erasure: {exported}");
assert_eq!(harness.visible(&app, &bob.id).await, bob_before, "another subject is untouched");
}
pub async fn erase_is_idempotent<H: SubjectDataStoreHarness + ?Sized>(harness: &H) {
let store = harness.open().await;
let (app, alice) = (app(), person());
harness.seed(&app, &alice.id).await;
let scope = SubjectScope::new([&app], alice.clone());
store.erase(&scope).await.expect("erase");
let again = store.erase(&scope).await.expect("repeat");
assert_eq!(again.deleted, 0, "a repeat deletes nothing: {again:?}");
assert_eq!(harness.visible(&app, &alice.id).await, 0);
}
pub async fn erase_covers_linked_ids<H: SubjectDataStoreHarness + ?Sized>(harness: &H) {
let store = harness.open().await;
let (app, alice) = (app(), person());
let visitor = SubjectRef::visitor(plain("visitor"));
harness.seed(&app, &alice.id).await;
harness.seed(&app, &visitor.id).await;
let scope = SubjectScope::new([&app], alice.clone()).with_linked(vec![visitor.clone()]);
let exported = store.export(&SubjectScope::new([&app], visitor.clone())).await.expect("export");
assert!(!is_empty(&exported), "the visitor id holds data: {exported}");
store.erase(&scope).await.expect("erase");
assert_eq!(harness.visible(&app, &alice.id).await, 0, "the subject's data is gone");
assert_eq!(harness.visible(&app, &visitor.id).await, 0, "the linked id's data is gone");
}
pub async fn erase_stays_in_its_applications<H: SubjectDataStoreHarness + ?Sized>(harness: &H) {
let store = harness.open().await;
let (first, second, alice) = (app(), app(), person());
harness.seed(&first, &alice.id).await;
harness.seed(&second, &alice.id).await;
let kept = harness.visible(&second, &alice.id).await;
store.erase(&SubjectScope::new([&first], alice.clone())).await.expect("erase");
assert_eq!(harness.visible(&first, &alice.id).await, 0);
assert_eq!(harness.visible(&second, &alice.id).await, kept, "the other application keeps it");
}