ctb_io_webui/controllers/
app.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 general app UI pages.
21
22use axum::{extract::State, response::Response};
23
24use crate::extractors::request_state::RequestState;
25use crate::json_value;
26use crate::session_auth::AuthenticatedUser;
27use crate::{AppState, respond_page};
28
29use crate::controllers::base::redirect_temporary;
30
31/// This is *not* the index page; it's the home page after logging in.
32pub async fn get_home(
33    State(state): State<AppState>,
34    req: RequestState,
35    user: AuthenticatedUser,
36) -> Response {
37    let mut u = user.user.lock().await;
38    if let Some(refreshed_info) =
39        ctb_storage::user::UserPublicInfo::get_by_id(u.local_id())
40    {
41        u.set_remote_status(refreshed_info.remote_status().to_string());
42    }
43    if u.remote_status() == "Conflict" {
44        return redirect_temporary(req.is_js_request, "/rename");
45    }
46    respond_page(&state, req, "home", &json_value!({}))
47}
48
49#[cfg(test)]
50#[allow(
51    clippy::panic,
52    clippy::expect_used,
53    clippy::unwrap_used,
54    clippy::unwrap_in_result,
55    clippy::panic_in_result_fn,
56    clippy::indexing_slicing,
57    clippy::arithmetic_side_effects,
58    reason = "Standard repository test boilerplate"
59)]
60mod tests {
61
62    use crate::test_helpers::{test_get_no_login, test_get_redirect_no_login};
63
64    #[crate::ctb_test("tokio")]
65    async fn unauthenticated_home_redirects_to_root() {
66        let (status, location) = test_get_redirect_no_login("/home").await;
67        assert_eq!(status, 303);
68        assert_eq!(location, "/");
69    }
70
71    #[crate::ctb_test("tokio")]
72    async fn unauthenticated_nodes_returns_401_error_page() {
73        let (status, body) = test_get_no_login("/nodes").await;
74        assert_eq!(status, 401);
75        assert!(body.contains("401") || body.contains("Unauthorized"));
76    }
77}