1
//! An already-open SSH session must stop working when the account is switched
2
//! off. The daemon's channel-open check (`refuse_if_withheld`) runs once, and
3
//! most nomiscript natives never touch the tenant pool — so the tenant-pool
4
//! recheck alone leaves a global command surface reachable for as long as the
5
//! user keeps the session open.
6
//!
7
//! Gated on the `db` feature. Run via:
8
//!   DATABASE_URL=postgres://… cargo test -p tests-integration --features db
9

            
10
#![cfg(feature = "db")]
11

            
12
use rpc::{ScriptCtx, Session};
13
use server::db::DB_POOL;
14
use sqlx::PgPool;
15
use supp_macro::local_db_sqlx_test;
16
use uuid::Uuid;
17

            
18
/// Required by `local_db_sqlx_test`, which calls it before each case.
19
2
async fn setup() {}
20

            
21
2
async fn insert_user(pool: &PgPool, id: Uuid, verified: bool) -> anyhow::Result<()> {
22
2
    sqlx::query!(
23
        "INSERT INTO users (
24
            id, user_name, email, photo, verified, user_password,
25
            user_role, database_name, created_at
26
        ) VALUES (
27
            $1, 'disabled-test', $2, 'default.png',
28
            $3, 'irrelevant', 'user', 'disabled-test', NOW()
29
        )",
30
        id,
31
        format!("disabled-{id}@example.test"),
32
        verified,
33
    )
34
2
    .execute(pool)
35
2
    .await?;
36
2
    Ok(())
37
2
}
38

            
39
/// `get-version` reads no database at all, which is the point: the refusal has
40
/// to come from the session gate rather than from a query happening to fail.
41
const HARMLESS_FORM: &str = "(:id 1 :form (get-version))";
42

            
43
#[local_db_sqlx_test]
44
async fn a_guarded_session_refuses_a_disabled_account(pool: PgPool) -> anyhow::Result<()> {
45
    let user_id = Uuid::new_v4();
46
    insert_user(&pool, user_id, false).await?;
47

            
48
    let mut session = Session::guarded(ScriptCtx::new(user_id)).expect("Session::guarded");
49
    let response = session.handle_form(HARMLESS_FORM).await;
50

            
51
    assert!(
52
        response.contains(":error") && response.contains("disabled"),
53
        "a disabled account must be refused on every form, got: {response}"
54
    );
55
}
56

            
57
#[local_db_sqlx_test]
58
async fn a_guarded_session_serves_an_enabled_account(pool: PgPool) -> anyhow::Result<()> {
59
    let user_id = Uuid::new_v4();
60
    insert_user(&pool, user_id, true).await?;
61

            
62
    let mut session = Session::guarded(ScriptCtx::new(user_id)).expect("Session::guarded");
63
    let response = session.handle_form(HARMLESS_FORM).await;
64

            
65
    assert!(
66
        response.contains(":value"),
67
        "an enabled account must be served, got: {response}"
68
    );
69
}