|
1 | 1 | use super::user_deletion_fences::{self as fences, FenceIdentity}; |
2 | | -use crate::{event, Db, DbError}; |
3 | | -use buzz_core::CommunityId; |
4 | | -use nostr::{Event, EventBuilder, Keys, Kind}; |
5 | | -use sqlx::{PgPool, Postgres, Transaction}; |
| 2 | +use super::user_deletion_test_support::{message, restricted, wait_for_waiter, Fixture}; |
| 3 | +use crate::{event, DbError}; |
| 4 | +use nostr::{Event, Keys}; |
6 | 5 | use std::time::Duration; |
7 | 6 | use uuid::Uuid; |
8 | | - |
9 | | -struct Fixture { |
10 | | - pool: PgPool, |
11 | | - db: Db, |
12 | | - community: CommunityId, |
13 | | - lease: crate::entity_deletion::EntityLease, |
14 | | -} |
15 | | - |
16 | | -impl Fixture { |
17 | | - async fn new() -> Self { |
18 | | - let pool = PgPool::connect(&crate::test_support::database_url()) |
19 | | - .await |
20 | | - .unwrap(); |
21 | | - let db = Db::from_pool(pool.clone()); |
22 | | - let community = db |
23 | | - .ensure_configured_community(&format!("message-fences-{}.test", Uuid::new_v4())) |
24 | | - .await |
25 | | - .unwrap() |
26 | | - .id; |
27 | | - let request = db |
28 | | - .user_deletion_store() |
29 | | - .submit(&format!("demo:{}", Uuid::new_v4()), "operator", None) |
30 | | - .await |
31 | | - .unwrap(); |
32 | | - let store = db.entity_deletion_store(); |
33 | | - store |
34 | | - .approve( |
35 | | - request.id, |
36 | | - "reviewer", |
37 | | - request.inventory_digest.as_deref().unwrap(), |
38 | | - ) |
39 | | - .await |
40 | | - .unwrap(); |
41 | | - let lease = store |
42 | | - .claim(request.id, "worker", Duration::from_secs(300)) |
43 | | - .await |
44 | | - .unwrap() |
45 | | - .unwrap(); |
46 | | - Self { |
47 | | - pool, |
48 | | - db, |
49 | | - community, |
50 | | - lease, |
51 | | - } |
52 | | - } |
53 | | - |
54 | | - async fn fence(&self, keys: &Keys, owner: Option<&Keys>) { |
55 | | - let mut tx = self.pool.begin().await.unwrap(); |
56 | | - self.fence_in(&mut tx, keys, owner).await; |
57 | | - tx.commit().await.unwrap(); |
58 | | - } |
59 | | - |
60 | | - async fn fence_in( |
61 | | - &self, |
62 | | - tx: &mut Transaction<'_, Postgres>, |
63 | | - keys: &Keys, |
64 | | - owner: Option<&Keys>, |
65 | | - ) { |
66 | | - let identity = if owner.is_some() { |
67 | | - FenceIdentity::agent(self.community, keys.public_key()) |
68 | | - } else { |
69 | | - FenceIdentity::user(keys.public_key()) |
70 | | - }; |
71 | | - fences::insert(tx, identity, &self.lease, owner.map(Keys::public_key)) |
72 | | - .await |
73 | | - .unwrap(); |
74 | | - } |
75 | | - |
76 | | - async fn agent(&self, owner: &Keys) -> Keys { |
77 | | - self.db |
78 | | - .ensure_user(self.community, owner.public_key().as_bytes()) |
79 | | - .await |
80 | | - .unwrap(); |
81 | | - let agent = Keys::generate(); |
82 | | - self.db |
83 | | - .ensure_user(self.community, agent.public_key().as_bytes()) |
84 | | - .await |
85 | | - .unwrap(); |
86 | | - self.db |
87 | | - .set_agent_owner( |
88 | | - self.community, |
89 | | - agent.public_key().as_bytes(), |
90 | | - owner.public_key().as_bytes(), |
91 | | - ) |
92 | | - .await |
93 | | - .unwrap(); |
94 | | - agent |
95 | | - } |
96 | | -} |
97 | | - |
98 | | -fn message(keys: &Keys, kind: u16) -> Event { |
99 | | - EventBuilder::new(Kind::Custom(kind), Uuid::new_v4().to_string()) |
100 | | - .sign_with_keys(keys) |
101 | | - .unwrap() |
102 | | -} |
103 | | - |
104 | | -fn restricted(result: crate::Result<(buzz_core::StoredEvent, bool)>) { |
105 | | - assert!( |
106 | | - matches!(result, Err(DbError::UserDeletionRestricted)), |
107 | | - "{result:?}" |
108 | | - ); |
109 | | -} |
110 | | - |
111 | | -async fn wait_for_waiter(pool: &PgPool, pid: i32) { |
112 | | - tokio::time::timeout(Duration::from_secs(4), async { |
113 | | - loop { |
114 | | - let waiting: bool = sqlx::query_scalar("SELECT EXISTS (SELECT 1 FROM pg_locks WHERE pid = $1 AND locktype = 'advisory' AND NOT granted)").bind(pid).fetch_one(pool).await.unwrap(); |
115 | | - if waiting { break; } |
116 | | - tokio::time::sleep(Duration::from_millis(10)).await; |
117 | | - } |
118 | | - }).await.expect("production writer must wait for the identity lock"); |
119 | | -} |
120 | | - |
121 | 7 | async fn writer( |
122 | 8 | f: &Fixture, |
123 | 9 | event: Event, |
@@ -376,6 +262,3 @@ async fn ownership_change_while_waiting_rejects_and_retry_uses_current_owner() { |
376 | 262 | .await, |
377 | 263 | ); |
378 | 264 | } |
379 | | - |
380 | | -#[path = "user_deletion_lock_order_postgres_tests.rs"] |
381 | | -mod lock_order_postgres_tests; |
0 commit comments