diff --git a/src/podcasts/discovery_tests.rs b/src/podcasts/discovery_tests.rs new file mode 100644 index 0000000..364fffe --- /dev/null +++ b/src/podcasts/discovery_tests.rs @@ -0,0 +1,535 @@ +//! Public podcast discovery against PostgreSQL: the share toggle and its +//! privacy confirmation, the aggregation of several accounts and the +//! publication from the web's federation node to another loopback node. +//! +//! Ignored by default, like the other store tests: set +//! `FURU_DEVICE_SYNC_TEST_DATABASE_URL` to a disposable database. The +//! loopback test starts two iroh nodes in temporary directories (the same +//! setup as frid's `tests/podcast_dht.rs`). + +use std::sync::Arc; +use std::time::Duration; + +use music_dht::device_sync::{SyncOpPayload, SyncOpWire, random_hex}; +use music_dht::podcast::{FeedUrl, PodcastId, PodcastMeta}; +use music_dht::podcast_dht::{PODCAST_DHT_ALPN, PodcastDht}; +use music_dht::{MusicDhtConfig, MusicDhtService, NetworkId}; +use serde_json::json; + +use super::api::{self, ShareOutcome}; +use super::discovery::{self, aggregate_share_states, load_shared}; +use super::postgres_tests::{cleanup, random_user, test_pool}; +use super::*; + +fn url(value: &str) -> FeedUrl { + FeedUrl::parse(value).unwrap() +} + +fn meta(feed: &str, title: &str) -> PodcastMeta { + PodcastMeta { + feed_url: url(feed), + title: title.into(), + author: Some("Host".into()), + description: Some("A public show".into()), + link: None, + image_url: None, + } +} + +async fn subscribe(store: &PodcastStore, user: i64, feed: &str, title: &str) -> PodcastId { + store + .subscribe(user, &meta(feed, title)) + .await + .unwrap() + .podcast_id +} + +fn share_op( + origin: &str, + seq: i64, + podcast_id: &PodcastId, + shared: bool, + generation: u64, +) -> SyncOpWire { + SyncOpWire { + op_id: format!("{origin}:{seq}"), + origin_device_id: origin.into(), + seq, + hlc_ms: 9_000_000_000_000 + seq, + payload: SyncOpPayload::PodcastShareSet { + podcast_id: podcast_id.clone(), + shared, + generation, + }, + } +} + +async fn share( + store: &PodcastStore, + user: i64, + podcast_id: &PodcastId, + body: serde_json::Value, +) -> Result { + api::set_share(store, user, podcast_id, body, None, now_ms()).await +} + +fn done(outcome: Result) -> Box { + match outcome.unwrap() { + ShareOutcome::Done(response) => response, + ShareOutcome::NeedsConfirmation(view) => panic!("unexpected confirmation: {view:?}"), + } +} + +fn needs_confirmation(outcome: Result) -> api::ShareView { + match outcome.unwrap() { + ShareOutcome::NeedsConfirmation(view) => view, + ShareOutcome::Done(response) => panic!("shared without confirmation: {:?}", response.share), + } +} + +fn confirm_body(view: &api::ShareView) -> serde_json::Value { + json!({ + "shared": true, + "confirm": { "feed_url": view.feed_url, "id_feed_url": view.id_feed_url }, + }) +} + +async fn published_ids(pool: &sqlx::PgPool, users: &[i64]) -> Vec<(PodcastId, i64)> { + let aggregation = aggregate_share_states(load_shared(pool, Some(users)).await.unwrap()); + aggregation.representatives.into_iter().collect() +} + +/// Active accounts (only they publish); random ids like the store tests. +async fn accounts(pool: &sqlx::PgPool) -> [i64; N] { + let users: [i64; N] = std::array::from_fn(|_| random_user()); + for user in users { + sqlx::query( + "INSERT INTO furumusic__user (id, username, role, is_active) + VALUES ($1, $2, 'user', TRUE)", + ) + .bind(user) + .bind(format!("podcast-discovery-test-{user}")) + .execute(pool) + .await + .unwrap(); + } + users +} + +async fn remove_accounts(pool: &sqlx::PgPool, users: &[i64]) { + cleanup(pool, users.iter().copied()).await; + sqlx::query("DELETE FROM furumusic__user WHERE id = ANY($1)") + .bind(users) + .execute(pool) + .await + .unwrap(); +} + +async fn log_len(pool: &sqlx::PgPool, user: i64) -> i64 { + sqlx::query_scalar("SELECT COUNT(*) FROM furumusic__fed_sync_ops WHERE user_id = $1") + .bind(user) + .fetch_one(pool) + .await + .unwrap() +} + +#[tokio::test] +#[ignore = "requires a disposable database in FURU_DEVICE_SYNC_TEST_DATABASE_URL"] +async fn postgres_share_toggle_asks_before_publishing_a_private_looking_feed() { + let pool = test_pool().await; + let store = PodcastStore::new(pool.clone()); + let [user, other] = accounts(&pool).await; + let secret = format!( + "https://members.example/feed.xml?token=8f3a9c2e7d1b4a6f{}", + random_hex(4) + ); + let podcast_id = subscribe(&store, user, &secret, "Members Only").await; + + // Private by default; the dialog needs the warnings and the URL. + let view = api::share_view(&store, user, &podcast_id, None) + .await + .unwrap(); + assert!(!view.shared && !view.pending_confirmation && view.can_share); + assert_eq!(view.discovery, "off"); + assert!(!view.warnings.is_empty()); + assert_eq!(view.feed_url.as_deref(), Some(secret.as_str())); + + // Enabling without confirming writes nothing. + let before = log_len(&pool, user).await; + let asked = needs_confirmation(share(&store, user, &podcast_id, json!({"shared": true})).await); + assert!(!asked.warnings.is_empty()); + assert!(store.share(user, &podcast_id).await.unwrap().is_none()); + // Confirming other URLs than the card's writes nothing either. + let wrong = + json!({"shared": true, "confirm": {"feed_url": "https://members.example/feed.xml"}}); + needs_confirmation(share(&store, user, &podcast_id, wrong).await); + assert_eq!(log_len(&pool, user).await, before); + assert!(published_ids(&pool, &[user]).await.is_empty()); + + // Confirming exactly the shown URLs shares and publishes. + let response = done(share(&store, user, &podcast_id, confirm_body(&asked)).await); + assert!(response.podcast.shared); + assert!(response.share.shared && !response.share.pending_confirmation); + assert_eq!(log_len(&pool, user).await, before + 1); + assert_eq!( + published_ids(&pool, &[user]).await, + vec![(podcast_id.clone(), user)] + ); + + // Another account cannot see or change this account's podcast. + let error = share(&store, other, &podcast_id, json!({"shared": false})) + .await + .unwrap_err(); + assert_eq!(error.status, 404); + // Malformed requests. + let error = share(&store, user, &podcast_id, json!({"share": true})) + .await + .unwrap_err(); + assert_eq!(error.status, 400); + + // Turning sharing off revokes and forgets the confirmation: sharing + // again asks again. + let response = done(share(&store, user, &podcast_id, json!({"shared": false})).await); + assert!(!response.share.shared); + assert!(published_ids(&pool, &[user]).await.is_empty()); + let confirmations: i64 = sqlx::query_scalar( + "SELECT COUNT(*) FROM furumusic__podcast_share_confirmation WHERE user_id = $1", + ) + .bind(user) + .fetch_one(&pool) + .await + .unwrap(); + assert_eq!(confirmations, 0); + needs_confirmation(share(&store, user, &podcast_id, json!({"shared": true})).await); + + // Shared on another trusted device (which asked its own user): the web + // still asks this account before its server publishes the URL. + let remote = share_op("tui_a", 1, &podcast_id, true, 100); + assert_eq!( + store.apply_remote_op(user, &remote).await.unwrap(), + ApplyOutcome::Changed + ); + let view = api::share_view(&store, user, &podcast_id, None) + .await + .unwrap(); + assert!(view.shared && view.pending_confirmation); + assert!(published_ids(&pool, &[user]).await.is_empty()); + let before = log_len(&pool, user).await; + let response = done(share(&store, user, &podcast_id, confirm_body(&view)).await); + assert!(response.share.shared && !response.share.pending_confirmation); + // Confirming an existing decision emits no operation. + assert_eq!(log_len(&pool, user).await, before); + assert_eq!(published_ids(&pool, &[user]).await.len(), 1); + + // A feed that moves to another URL that looks private asks again. + let moved = format!( + "https://members.example/v2/feed.xml?key=9a8b7c6d5e4f3a2b{}", + random_hex(4) + ); + sqlx::query( + "UPDATE furumusic__podcast SET feed_url = $3 WHERE user_id = $1 AND podcast_id = $2", + ) + .bind(user) + .bind(podcast_id.as_str()) + .bind(&moved) + .execute(&pool) + .await + .unwrap(); + let view = api::share_view(&store, user, &podcast_id, None) + .await + .unwrap(); + assert!(view.shared && view.pending_confirmation); + assert_eq!(view.feed_url.as_deref(), Some(moved.as_str())); + assert_eq!(view.id_feed_url.as_deref(), Some(secret.as_str())); + assert!(published_ids(&pool, &[user]).await.is_empty()); + + // Unsubscribing revokes and forgets; the podcast is private again. + api::unsubscribe(&store, user, &podcast_id).await.unwrap(); + let view = api::share_view(&store, user, &podcast_id, None) + .await + .unwrap(); + assert!(!view.shared && !view.can_share); + let error = share(&store, user, &podcast_id, json!({"shared": true})) + .await + .unwrap_err(); + assert_eq!(error.status, 409); + + remove_accounts(&pool, &[user, other]).await; +} + +#[tokio::test] +#[ignore = "requires a disposable database in FURU_DEVICE_SYNC_TEST_DATABASE_URL"] +async fn postgres_shared_podcasts_aggregate_across_accounts() { + let pool = test_pool().await; + let store = PodcastStore::new(pool.clone()); + let users = accounts::<3>(&pool).await; + let [a, b, c] = users; + let feed = format!("https://podcasts.example/shared-{}.xml", random_hex(4)); + let private = format!("https://podcasts.example/private-{}.xml", random_hex(4)); + let podcast_id = subscribe(&store, a, &feed, "Shared Show").await; + assert_eq!(subscribe(&store, b, &feed, "Shared Show").await, podcast_id); + assert_eq!(subscribe(&store, c, &feed, "Shared Show").await, podcast_id); + let private_id = subscribe(&store, a, &private, "Private Show").await; + + // Nothing shared: nothing published, not even the private podcast. + assert!(published_ids(&pool, &users).await.is_empty()); + + done(share(&store, a, &podcast_id, json!({"shared": true})).await); + done(share(&store, b, &podcast_id, json!({"shared": true})).await); + let published = published_ids(&pool, &users).await; + assert_eq!(published, vec![(podcast_id.clone(), a.min(b))]); + let aggregation = aggregate_share_states(load_shared(&pool, Some(&users)).await.unwrap()); + let state = &aggregation.states[0]; + assert!(state.is_shared() && state.privacy_confirmed); + let card = state.card.as_ref().unwrap(); + assert_eq!(card.feed_url, url(&feed)); + assert_eq!(card.title, "Shared Show"); + + // A deactivated account publishes nothing; the other one still shares. + let set_active = async |user: i64, active: bool| { + sqlx::query("UPDATE furumusic__user SET is_active = $2 WHERE id = $1") + .bind(user) + .bind(active) + .execute(&pool) + .await + .unwrap(); + }; + set_active(a.min(b), false).await; + assert_eq!( + published_ids(&pool, &users).await, + vec![(podcast_id.clone(), a.max(b))] + ); + set_active(a.max(b), false).await; + assert!(published_ids(&pool, &users).await.is_empty()); + set_active(a, true).await; + set_active(b, true).await; + + // One account stops sharing: the other still shares the same card. + done(share(&store, a, &podcast_id, json!({"shared": false})).await); + assert_eq!( + published_ids(&pool, &users).await, + vec![(podcast_id.clone(), b)] + ); + + // A stale `shared = true` of A (delivered late from an old device log) + // cannot republish for A. + let stale = share_op("old_device", 1, &podcast_id, true, 1); + assert_eq!( + store.apply_remote_op(a, &stale).await.unwrap(), + ApplyOutcome::Unchanged + ); + let view = api::share_view(&store, a, &podcast_id, None).await.unwrap(); + assert!(!view.shared); + + // The last sharing account unsubscribes: the card is revoked, and + // subscribing again is private. + api::unsubscribe(&store, b, &podcast_id).await.unwrap(); + assert!(published_ids(&pool, &users).await.is_empty()); + api::resubscribe(&store, b, &podcast_id).await.unwrap(); + assert!(published_ids(&pool, &users).await.is_empty()); + // Share decisions of C never existed; the private podcast of A stays + // private throughout. + let view = api::share_view(&store, a, &private_id, None).await.unwrap(); + assert!(!view.shared); + + remove_accounts(&pool, &users).await; +} + +// --------------------------------------------------------------------------- +// Loopback: publish from the web node, find from another node +// --------------------------------------------------------------------------- + +struct Node { + service: Arc, + dht: Arc, + tasks: Vec>, +} + +impl Node { + async fn stop(self) { + for task in &self.tasks { + task.abort(); + } + self.service.shutdown().await.unwrap(); + } +} + +async fn start_node(directory: &std::path::Path, network: NetworkId) -> Node { + std::fs::create_dir_all(directory).unwrap(); + let config = MusicDhtConfig::builder() + .data_dir(directory) + .network_id(network) + .request_timeout(Duration::from_secs(2)) + .lookup_timeout(Duration::from_secs(5)) + .transport_timeout(Duration::from_secs(5)) + .dial_timeout(Duration::from_secs(2)) + .schema_independent_stream_protocol(PODCAST_DHT_ALPN) + .build() + .unwrap(); + let (service, mut events) = MusicDhtService::start(config).await.unwrap(); + let service = Arc::new(service); + let events = tokio::spawn(async move { while events.recv().await.is_some() {} }); + // The web's own wiring: the overlay on the node's endpoint, its cache + // in the federation data directory. + let (dht, serve) = crate::federation::podcast_discovery::open(&service, directory) + .await + .expect("overlay opens"); + assert!( + directory + .join(crate::federation::podcast_discovery::CACHE_FILE) + .exists() + ); + Node { + service, + dht, + tasks: vec![events, serve], + } +} + +async fn wait_for(what: &str, mut check: impl AsyncFnMut() -> Option) -> T { + let started = std::time::Instant::now(); + loop { + if let Some(value) = check().await { + return value; + } + assert!( + started.elapsed() < Duration::from_secs(90), + "timed out waiting for {what}" + ); + tokio::time::sleep(Duration::from_millis(300)).await; + } +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +#[ignore = "requires a disposable database in FURU_DEVICE_SYNC_TEST_DATABASE_URL"] +async fn postgres_web_node_publishes_shared_podcasts_to_another_node() { + tokio::time::timeout(Duration::from_secs(240), loopback_scenario()) + .await + .unwrap(); +} + +async fn loopback_scenario() { + let pool = test_pool().await; + let store = PodcastStore::new(pool.clone()); + let users = accounts::<2>(&pool).await; + let [u1, u2] = users; + let tag = random_hex(6); + let directory = std::env::temp_dir().join(format!("furumusic-podcast-dht-{tag}")); + let network = NetworkId::from_name(&format!("furumusic-podcast-loopback-{tag}")); + let web = start_node(&directory.join("web"), network).await; + let stranger = start_node(&directory.join("stranger"), network).await; + + // The stranger reaches the web node through the music-DHT transport + // only: no device-sync pairing, another identity. + stranger + .service + .connect(web.service.ticket().await.unwrap()) + .await + .unwrap(); + wait_for("music DHT contacts", async || { + (!web.service.known_peers().is_empty() && !stranger.service.known_peers().is_empty()) + .then_some(()) + }) + .await; + web.dht.bootstrap().await.unwrap(); + stranger.dht.bootstrap().await.unwrap(); + wait_for("overlay contacts", async || { + (web.dht.known_peers() >= 1 && stranger.dht.known_peers() >= 1).then_some(()) + }) + .await; + + // Two accounts share one podcast; one of them also has a private one. + let title = format!("Loopback Radio {tag}"); + let hidden = format!("Hidden Garden {tag}"); + let feed = format!("https://podcasts.example/loopback-{tag}.xml"); + let podcast_id = subscribe(&store, u1, &feed, &title).await; + subscribe(&store, u2, &feed, &title).await; + subscribe( + &store, + u1, + &format!("https://podcasts.example/hidden-{tag}.xml"), + &hidden, + ) + .await; + // Sharing while the stranger already looks: nothing before. + assert!(stranger.dht.search(&title, 10).await.unwrap().is_empty()); + done(share(&store, u1, &podcast_id, json!({"shared": true})).await); + done(share(&store, u2, &podcast_id, json!({"shared": true})).await); + + let report = discovery::reconcile_once(&pool, &web.dht, Some(&users)) + .await + .unwrap(); + assert_eq!(report.aggregation.states.len(), 1, "one card per podcast"); + assert_eq!(report.stats.published, 1); + assert!(report.stats.held_back.is_empty() && report.stats.failed.is_empty()); + let view = api::share_view(&store, u1, &podcast_id, Some(&web.dht)) + .await + .unwrap(); + assert!(view.published); + assert_eq!(view.discovery, "running"); + + let hit = wait_for("the shared card", async || { + stranger + .dht + .search(&title, 10) + .await + .unwrap() + .into_iter() + .find(|hit| hit.podcast_id == podcast_id) + }) + .await; + assert_eq!(hit.feed_url, url(&feed)); + assert_eq!(hit.card.title, title); + assert_eq!(hit.publishers, vec![web.service.endpoint_id()]); + // The private podcast is not discoverable, by text or by id. + assert!(stranger.dht.search(&hidden, 10).await.unwrap().is_empty()); + + // Idempotent; one account stopping keeps the card of the other. + done(share(&store, u1, &podcast_id, json!({"shared": false})).await); + let report = discovery::reconcile_once(&pool, &web.dht, Some(&users)) + .await + .unwrap(); + assert_eq!( + ( + report.stats.published, + report.stats.unchanged, + report.stats.unpublished + ), + (0, 1, 0) + ); + + // The last account unsubscribes: the card is revoked with a tombstone. + api::unsubscribe(&store, u2, &podcast_id).await.unwrap(); + let report = discovery::reconcile_once(&pool, &web.dht, Some(&users)) + .await + .unwrap(); + assert_eq!(report.stats.unpublished, 1); + wait_for("the revocation", async || { + stranger + .dht + .lookup_podcast(&podcast_id) + .await + .unwrap() + .is_none() + .then_some(()) + }) + .await; + assert!( + stranger + .dht + .search(&title, 10) + .await + .unwrap() + .iter() + .all(|hit| hit.podcast_id != podcast_id) + ); + let view = api::share_view(&store, u2, &podcast_id, Some(&web.dht)) + .await + .unwrap(); + assert!(!view.shared && !view.published); + + web.stop().await; + stranger.stop().await; + remove_accounts(&pool, &users).await; + let _ = std::fs::remove_dir_all(&directory); +} diff --git a/src/podcasts/mod.rs b/src/podcasts/mod.rs index 91a5ec9..da7cdd3 100644 --- a/src/podcasts/mod.rs +++ b/src/podcasts/mod.rs @@ -76,6 +76,8 @@ mod refresh; pub(crate) mod runner; pub(crate) mod schema; +#[cfg(test)] +mod discovery_tests; #[cfg(test)] mod fetch_tests; #[cfg(test)]