Elffuss BraidHub

kikocis/utopia public

dev · 4.6 KiBRawPermalink
1//! 停用账号的四条硬性质——打在真库上(见 `users.deactivated_at`)。
2//!
3//! 软删除的风险全在「漏过滤一处」:只要有一条读 `users` 的路径忘了带
4//! `deactivated_at IS NULL`,停用就成了摆设,而且**不会有任何报错**。
5//! 所以这几条守的不是函数,是**路径**。
6//!
7//! 反过来,归因那几处必须**照旧查得到**:审计事件、合并日志、改类账本的
8//! `actor_id` 指着这个人,而那些是审计材料——人走了不等于那件事没发生。
9
10use sqlx::PgPool;
11use uuid::Uuid;
12
13async fn org_with_two_admins(pool: &PgPool) -> anyhow::Result<(Uuid, Uuid, Uuid)> {
14 let (org, a, b) = (Uuid::now_v7(), Uuid::now_v7(), Uuid::now_v7());
15 sqlx::query("INSERT INTO organizations (id, name) VALUES ($1, 'retire-test')")
16 .bind(org)
17 .execute(pool)
18 .await?;
19 for (id, mail) in [(a, "a"), (b, "b")] {
20 sqlx::query(
21 "INSERT INTO users (id, org_id, email, display_name, password_hash, is_admin)
22 VALUES ($1, $2, $3, 'u', 'x', TRUE)",
23 )
24 .bind(id)
25 .bind(org)
26 .bind(format!("{}-{mail}@retire.test", id.simple()))
27 .execute(pool)
28 .await?;
29 }
30 Ok((org, a, b))
31}
32
33#[tokio::test]
34async fn a_retired_account_cannot_get_back_in() -> anyhow::Result<()> {
35 let Ok(url) = std::env::var("UTOPIA_DATABASE_URL") else {
36 eprintln!("跳过:未设 UTOPIA_DATABASE_URL");
37 return Ok(());
38 };
39 let pool = PgPool::connect(&url).await?;
40 let (org, a, b) = org_with_two_admins(&pool).await?;
41
42 let run = async {
43 let email: String = sqlx::query_scalar("SELECT email FROM users WHERE id = $1")
44 .bind(b)
45 .fetch_one(&pool)
46 .await?;
47 assert!(
48 utopia_store::accounts::find_user_by_email(&pool, &email)
49 .await?
50 .is_some(),
51 "停用前该找得到"
52 );
53
54 utopia_store::accounts::deactivate_user(&pool, b, a).await?;
55
56 // 登录这条路
57 assert!(
58 utopia_store::accounts::find_user_by_email(&pool, &email)
59 .await?
60 .is_none(),
61 "停用的账号还能按 email 找到——登录挡不住"
62 );
63 // **已经签发出去的 token 那条路**。会话校验走 find_user_by_id,
64 // 所以停用立即生效,不必等 token 过期
65 assert!(
66 utopia_store::accounts::find_user_by_id(&pool, b)
67 .await?
68 .is_none(),
69 "停用的账号还能按 id 找到——已签发的 token 仍然有效"
70 );
71
72 // 归因照旧:那一行还在,只是打了时间戳
73 let still_there: i64 = sqlx::query_scalar("SELECT count(*) FROM users WHERE id = $1")
74 .bind(b)
75 .fetch_one(&pool)
76 .await?;
77 assert_eq!(still_there, 1, "软删除不该把行删掉——审计要靠它");
78 Ok::<_, anyhow::Error>(())
79 }
80 .await;
81
82 sqlx::query("DELETE FROM organizations WHERE id = $1")
83 .bind(org)
84 .execute(&pool)
85 .await?;
86 run
87}
88
89#[tokio::test]
90async fn the_last_admin_and_oneself_are_protected() -> anyhow::Result<()> {
91 let Ok(url) = std::env::var("UTOPIA_DATABASE_URL") else {
92 eprintln!("跳过:未设 UTOPIA_DATABASE_URL");
93 return Ok(());
94 };
95 let pool = PgPool::connect(&url).await?;
96 let (org, a, b) = org_with_two_admins(&pool).await?;
97
98 let run = async {
99 assert!(
100 utopia_store::accounts::deactivate_user(&pool, a, a)
101 .await
102 .is_err(),
103 "不能停用自己——这个系统没有超级管理员那一层,停完就没人能放回来"
104 );
105
106 utopia_store::accounts::deactivate_user(&pool, b, a).await?;
107 assert!(
108 utopia_store::accounts::deactivate_user(&pool, a, b)
109 .await
110 .is_err(),
111 "最后一个管理员不能停用,否则组织从此没人能管成员"
112 );
113
114 // 幂等:重复停用不是错误
115 utopia_store::accounts::deactivate_user(&pool, b, a).await?;
116
117 // 恢复之后一切照旧
118 utopia_store::accounts::reactivate_user(&pool, b).await?;
119 assert!(
120 utopia_store::accounts::find_user_by_id(&pool, b)
121 .await?
122 .is_some(),
123 "恢复之后该能登录了"
124 );
125 Ok::<_, anyhow::Error>(())
126 }
127 .await;
128
129 sqlx::query("DELETE FROM organizations WHERE id = $1")
130 .bind(org)
131 .execute(&pool)
132 .await?;
133 run
134}