forked from Furumi/furumi-web
801 lines
29 KiB
Rust
801 lines
29 KiB
Rust
//! Public podcast discovery against PostgreSQL: the share toggle and its
|
|
//! privacy confirmation, the aggregation of several accounts, the
|
|
//! publication from the web's federation node to another loopback node and
|
|
//! the first publication after a restart.
|
|
//!
|
|
//! 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, Discovery, 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<ShareOutcome, api::ApiError> {
|
|
api::set_share(store, user, podcast_id, body, Discovery::Off, now_ms()).await
|
|
}
|
|
|
|
fn done(outcome: Result<ShareOutcome, api::ApiError>) -> Box<api::ShareResponse> {
|
|
match outcome.unwrap() {
|
|
ShareOutcome::Done(response) => response,
|
|
ShareOutcome::NeedsConfirmation(view) => panic!("unexpected confirmation: {view:?}"),
|
|
}
|
|
}
|
|
|
|
fn needs_confirmation(outcome: Result<ShareOutcome, api::ApiError>) -> 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().shares,
|
|
&Default::default(),
|
|
);
|
|
aggregation.representatives.into_iter().collect()
|
|
}
|
|
|
|
/// Active accounts (only they publish); random ids like the store tests.
|
|
async fn accounts<const N: usize>(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, Discovery::Off)
|
|
.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, Discovery::Off)
|
|
.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, Discovery::Off)
|
|
.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, Discovery::Off)
|
|
.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().shares,
|
|
&Default::default(),
|
|
);
|
|
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, Discovery::Off)
|
|
.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, Discovery::Off)
|
|
.await
|
|
.unwrap();
|
|
assert!(!view.shared);
|
|
|
|
remove_accounts(&pool, &users).await;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Loopback: publish from the web node, find from another node
|
|
// ---------------------------------------------------------------------------
|
|
|
|
struct Node {
|
|
service: Arc<MusicDhtService>,
|
|
dht: Arc<PodcastDht>,
|
|
tasks: Vec<tokio::task::JoinHandle<()>>,
|
|
}
|
|
|
|
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<T>(what: &str, mut check: impl AsyncFnMut() -> Option<T>) -> 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;
|
|
// An account of the stranger's side (another server in real life) that
|
|
// subscribes from the card it found.
|
|
let finder = random_user();
|
|
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.
|
|
meet(&web, &stranger).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 server = super::test_server::TestServer::start();
|
|
server.set(
|
|
"/loopback.xml",
|
|
super::test_server::Route::new(
|
|
format!(
|
|
r#"<?xml version="1.0"?><rss version="2.0"><channel><title>{title}</title>
|
|
<item><title>First</title><guid>1</guid><pubDate>Mon, 01 Jan 2024 10:00:00 GMT</pubDate>
|
|
<enclosure url="/audio/1.mp3" type="audio/mpeg"/></item></channel></rss>"#
|
|
),
|
|
"application/rss+xml",
|
|
),
|
|
);
|
|
let feed = server.url("/loopback.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, Discovery::Running(&web.dht))
|
|
.await
|
|
.unwrap();
|
|
assert!(view.published && view.held.is_none());
|
|
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());
|
|
|
|
// The finder sees the card in the federated search, looks at its
|
|
// episodes and subscribes: its own private subscription with the
|
|
// card's id, fetched from the feed (never from the publisher).
|
|
let views = super::search::hit_views(&store, finder, &title, &[], vec![hit.clone()])
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(views.len(), 1);
|
|
assert!(!views[0].subscribed && views[0].exact_match);
|
|
let found = json!({ "podcast_id": views[0].podcast_id, "feed_url": views[0].feed_url });
|
|
let fetcher = super::postgres_tests::local_fetcher();
|
|
let preview = api::preview_found(&store, &fetcher, finder, found.clone())
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(preview.episodes.len(), 1);
|
|
let subscribed = api::subscribe_found(&store, &fetcher, finder, found, now_ms())
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(subscribed.podcast.podcast_id, podcast_id.to_string());
|
|
assert!(subscribed.podcast.subscribed && !subscribed.podcast.shared);
|
|
|
|
// 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, Discovery::Running(&web.dht))
|
|
.await
|
|
.unwrap();
|
|
assert!(!view.shared && !view.published);
|
|
// The revocation does not touch the finder's subscription.
|
|
let kept = store.podcast(finder, &podcast_id).await.unwrap().unwrap();
|
|
assert!(kept.subscribed && !kept.shared);
|
|
cleanup(&pool, [finder]).await;
|
|
|
|
web.stop().await;
|
|
stranger.stop().await;
|
|
remove_accounts(&pool, &users).await;
|
|
let _ = std::fs::remove_dir_all(&directory);
|
|
}
|
|
|
|
/// Connects `stranger` to `web` and fills both overlay routing tables.
|
|
async fn meet(web: &Node, stranger: &Node) {
|
|
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;
|
|
}
|
|
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
|
|
#[ignore = "requires a disposable database in FURU_DEVICE_SYNC_TEST_DATABASE_URL"]
|
|
async fn postgres_a_restarted_server_publishes_only_after_the_first_device_sync() {
|
|
tokio::time::timeout(Duration::from_secs(240), restart_scenario())
|
|
.await
|
|
.unwrap();
|
|
}
|
|
|
|
/// Interop scenario 7c with the web server as the device that was stopped:
|
|
/// a TUI of the account turned sharing off meanwhile, and the server comes
|
|
/// back with the old "shared" rows.
|
|
async fn restart_scenario() {
|
|
let pool = test_pool().await;
|
|
let store = PodcastStore::new(pool.clone());
|
|
let users = accounts::<1>(&pool).await;
|
|
let [user] = users;
|
|
let tag = random_hex(6);
|
|
let directory = std::env::temp_dir().join(format!("furumusic-podcast-restart-{tag}"));
|
|
let network = NetworkId::from_name(&format!("furumusic-podcast-restart-{tag}"));
|
|
|
|
let kept_title = format!("Kept Show {tag}");
|
|
let revoked_title = format!("Revoked Show {tag}");
|
|
let kept = subscribe(
|
|
&store,
|
|
user,
|
|
&format!("https://podcasts.example/kept-{tag}.xml"),
|
|
&kept_title,
|
|
)
|
|
.await;
|
|
let revoked = subscribe(
|
|
&store,
|
|
user,
|
|
&format!("https://podcasts.example/revoked-{tag}.xml"),
|
|
&revoked_title,
|
|
)
|
|
.await;
|
|
done(share(&store, user, &kept, json!({"shared": true})).await);
|
|
done(share(&store, user, &revoked, json!({"shared": true})).await);
|
|
let web = start_node(&directory.join("web"), network).await;
|
|
let report = discovery::reconcile_once(&pool, &web.dht, Some(&users))
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(report.stats.published, 2);
|
|
web.stop().await;
|
|
|
|
// The server restarts from the same directory (same key, same overlay
|
|
// cache) and meets a stranger before its first device-sync round.
|
|
let web = start_node(&directory.join("web"), network).await;
|
|
let stranger = start_node(&directory.join("stranger"), network).await;
|
|
meet(&web, &stranger).await;
|
|
let (first_sync_done, first_sync) = tokio::sync::watch::channel(false);
|
|
let reconciler = tokio::spawn(discovery::run_rounds(
|
|
pool.clone(),
|
|
Arc::clone(&web.dht),
|
|
first_sync,
|
|
Duration::from_secs(600),
|
|
Some(users.to_vec()),
|
|
));
|
|
// Time for a round: nothing is announced from the stored decisions.
|
|
tokio::time::sleep(Duration::from_secs(5)).await;
|
|
for podcast_id in [&kept, &revoked] {
|
|
assert!(
|
|
stranger
|
|
.dht
|
|
.lookup_podcast(podcast_id)
|
|
.await
|
|
.unwrap()
|
|
.is_none(),
|
|
"a card was announced before the first device-sync round"
|
|
);
|
|
}
|
|
assert!(
|
|
stranger
|
|
.dht
|
|
.search(&revoked_title, 10)
|
|
.await
|
|
.unwrap()
|
|
.is_empty()
|
|
);
|
|
|
|
// The first device-sync round brings the share-off the TUI made while
|
|
// the server was stopped.
|
|
let share_off = share_op("tui_while_stopped", 1, &revoked, false, 100);
|
|
assert_eq!(
|
|
store.apply_remote_op(user, &share_off).await.unwrap(),
|
|
ApplyOutcome::Changed
|
|
);
|
|
first_sync_done.send_replace(true);
|
|
|
|
let hit = wait_for("the card still shared", async || {
|
|
stranger.dht.lookup_podcast(&kept).await.unwrap()
|
|
})
|
|
.await;
|
|
assert_eq!(hit.card.title, kept_title);
|
|
assert_eq!(hit.publishers, vec![web.service.endpoint_id()]);
|
|
assert!(
|
|
stranger
|
|
.dht
|
|
.lookup_podcast(&revoked)
|
|
.await
|
|
.unwrap()
|
|
.is_none()
|
|
);
|
|
assert!(
|
|
stranger
|
|
.dht
|
|
.search(&revoked_title, 10)
|
|
.await
|
|
.unwrap()
|
|
.is_empty()
|
|
);
|
|
let revoked_record = web
|
|
.dht
|
|
.published()
|
|
.await
|
|
.unwrap()
|
|
.into_iter()
|
|
.find(|publication| publication.podcast_id == revoked)
|
|
.unwrap();
|
|
assert!(!revoked_record.active, "the revoked card is tombstoned");
|
|
|
|
reconciler.abort();
|
|
web.stop().await;
|
|
stranger.stop().await;
|
|
remove_accounts(&pool, &users).await;
|
|
let _ = std::fs::remove_dir_all(&directory);
|
|
}
|
|
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
|
#[ignore = "requires a disposable database in FURU_DEVICE_SYNC_TEST_DATABASE_URL"]
|
|
async fn postgres_an_unreadable_row_does_not_stop_other_revocations() {
|
|
tokio::time::timeout(Duration::from_secs(120), unreadable_row_scenario())
|
|
.await
|
|
.unwrap();
|
|
}
|
|
|
|
async fn unreadable_row_scenario() {
|
|
let pool = test_pool().await;
|
|
let store = PodcastStore::new(pool.clone());
|
|
let users = accounts::<2>(&pool).await;
|
|
let [a, b] = users;
|
|
let tag = random_hex(6);
|
|
let directory = std::env::temp_dir().join(format!("furumusic-podcast-unreadable-{tag}"));
|
|
let network = NetworkId::from_name(&format!("furumusic-podcast-unreadable-{tag}"));
|
|
let web = start_node(&directory.join("web"), network).await;
|
|
|
|
let a_feed = format!("https://podcasts.example/a-{tag}.xml");
|
|
let b_feed = format!("https://podcasts.example/b-{tag}.xml");
|
|
let a_id = subscribe(&store, a, &a_feed, "A Show").await;
|
|
let b_id = subscribe(&store, b, &b_feed, "B Show").await;
|
|
done(share(&store, a, &a_id, json!({"shared": true})).await);
|
|
done(share(&store, b, &b_id, json!({"shared": true})).await);
|
|
let report = discovery::reconcile_once(&pool, &web.dht, Some(&users))
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(report.stats.published, 2);
|
|
let active = async || {
|
|
web.dht
|
|
.published()
|
|
.await
|
|
.unwrap()
|
|
.into_iter()
|
|
.filter(|publication| publication.active)
|
|
.map(|publication| publication.podcast_id)
|
|
.collect::<Vec<_>>()
|
|
};
|
|
assert_eq!(active().await.len(), 2);
|
|
|
|
// B's stored alias no longer decodes (an imported row, a stricter
|
|
// parser after an upgrade); A turns sharing off.
|
|
sqlx::query(
|
|
"INSERT INTO furumusic__podcast_feed_alias (user_id, podcast_id, feed_url)
|
|
VALUES ($1, $2, 'not a feed url')",
|
|
)
|
|
.bind(b)
|
|
.bind(b_id.as_str())
|
|
.execute(&pool)
|
|
.await
|
|
.unwrap();
|
|
done(share(&store, a, &a_id, json!({"shared": false})).await);
|
|
|
|
// The round still runs: A's card is revoked, and B's card (which can no
|
|
// longer be read) fails closed instead of being republished forever.
|
|
let report = discovery::reconcile_once(&pool, &web.dht, Some(&users))
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(report.aggregation.unreadable, 1);
|
|
assert!(report.aggregation.states.is_empty());
|
|
assert_eq!(report.stats.unpublished, 2);
|
|
assert!(active().await.is_empty());
|
|
|
|
// Repaired: B's card is published again; A's stays revoked.
|
|
sqlx::query(
|
|
"DELETE FROM furumusic__podcast_feed_alias WHERE user_id = $1 AND feed_url = 'not a feed url'",
|
|
)
|
|
.bind(b)
|
|
.execute(&pool)
|
|
.await
|
|
.unwrap();
|
|
let report = discovery::reconcile_once(&pool, &web.dht, Some(&users))
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(report.aggregation.unreadable, 0);
|
|
assert_eq!(active().await, vec![b_id.clone()]);
|
|
|
|
web.stop().await;
|
|
remove_accounts(&pool, &users).await;
|
|
let _ = std::fs::remove_dir_all(&directory);
|
|
}
|