ctb_io_webui/controllers/
eite.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2/*
3This file is part of Collective Toolbox, a database and document workspace and utilities.
4Copyright (C) 2026 Collective Toolbox Developers
5Contact: info@collectivetoolbox.com
6
7This program is free software: you can redistribute it and/or modify it under
8the terms of the GNU Affero General Public License as published by the Free
9Software Foundation, either version 3 of the License, or (at your option) any
10later version.
11
12This program is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
15
16You should have received a copy of the GNU Affero General Public License along
17with this program.  If not, see <https://www.gnu.org/licenses/>.
18*/
19
20//! Controller for the EITE edit tool.
21
22#[allow(
23    unused_imports,
24    clippy::wildcard_imports,
25    reason = "Standard workspace module prelude"
26)]
27use crate::utilities::*;
28
29use axum::{extract::State, response::Response};
30
31use crate::json_value;
32use crate::session_auth::AuthenticatedUser;
33use crate::{AppState, RequestState, respond_page};
34
35/// Renders the EITE Edit Tool page.
36pub async fn get_edit_tool(
37    State(state): State<AppState>,
38    req: RequestState,
39    _user: AuthenticatedUser,
40) -> Response {
41    respond_page(&state, req, "tools.eite-edit-tool", &json_value!({}))
42}
43
44#[cfg(test)]
45#[allow(
46    clippy::panic,
47    clippy::expect_used,
48    clippy::unwrap_used,
49    clippy::unwrap_in_result,
50    clippy::panic_in_result_fn,
51    clippy::indexing_slicing,
52    clippy::arithmetic_side_effects,
53    reason = "Standard repository test boilerplate"
54)]
55mod tests {
56    use crate::test_helpers::{
57        TestApp, assert_eq_or_print_body, test_get_no_login,
58        test_get_with_login,
59    };
60    use crate::utilities::anyhow;
61    use axum::http::StatusCode;
62    use ctb_storage::user::NameAndIdLock;
63    use serde_json::json;
64
65    async fn test_post_json_with_login<T: serde::Serialize>(
66        uri: &str,
67        payload: &T,
68        user_name: &str,
69    ) -> anyhow::Result<(StatusCode, String, NameAndIdLock)> {
70        let test_app = TestApp::new();
71        let (cookie_val, lock) = test_app.register_and_login(user_name).await?;
72        let mut headers = axum::http::HeaderMap::new();
73        headers.insert(
74            axum::http::header::CONTENT_TYPE,
75            axum::http::HeaderValue::from_static("application/json"),
76        );
77        let (status, body) = test_app
78            .request::<()>(
79                axum::http::Method::POST,
80                uri,
81                Some(headers),
82                Some(&cookie_val),
83                Some(serde_json::to_vec(payload)?),
84                None,
85            )
86            .await;
87        Ok((status, body, lock))
88    }
89
90    #[crate::ctb_test("tokio")]
91    async fn unauthenticated_edit_tool_fails() {
92        let (status, body) = test_get_no_login("/tools/eite-edit-tool").await;
93        assert_eq!(status, 401);
94        assert!(body.contains("401") || body.contains("Unauthorized"));
95    }
96
97    #[crate::ctb_test("tokio")]
98    async fn unauthenticated_rpc_fails() {
99        let (status, _body) = crate::test_helpers::test_post_no_login::<()>(
100            "/api/rpc/eite",
101            Some(Vec::new()),
102            None,
103            None,
104        )
105        .await;
106        assert_eq!(status, 401);
107    }
108
109    #[crate::ctb_test("tokio")]
110    async fn authenticated_can_load_edit_tool() {
111        let (status, body, _lock) = test_get_with_login(
112            "/tools/eite-edit-tool",
113            None,
114            function_name!(),
115        )
116        .await
117        .unwrap();
118        assert_eq_or_print_body(status, 200, &body);
119        assert!(body.contains("eiteDocumentRoot"));
120    }
121
122    #[crate::ctb_test("tokio")]
123    async fn authenticated_can_rpc_call() {
124        let payload = json!({
125            "function": "isKnownDc",
126            "args": [18]
127        });
128        let (status, body, _lock) = test_post_json_with_login(
129            "/api/rpc/eite",
130            &payload,
131            function_name!(),
132        )
133        .await
134        .unwrap();
135        assert_eq_or_print_body(status, 200, &body);
136        let resp: serde_json::Value = serde_json::from_str(&body).unwrap();
137        assert_eq!(resp["value"], true);
138    }
139
140    #[crate::ctb_test("tokio")]
141    async fn authenticated_rpc_rejects_unallowed() {
142        let payload = json!({
143            "function": "nonExistentFunction",
144            "args": []
145        });
146        let (status, _body, _lock) = test_post_json_with_login(
147            "/api/rpc/eite",
148            &payload,
149            function_name!(),
150        )
151        .await
152        .unwrap();
153        assert_eq!(status, 400);
154    }
155}