Skip to main content

threexui_rs/api/
settings.rs

1use crate::models::settings::AllSetting;
2use crate::{Client, Result};
3
4pub struct SettingsApi<'a> {
5    pub(crate) client: &'a Client,
6}
7
8impl<'a> SettingsApi<'a> {
9    pub async fn get_all(&self) -> Result<AllSetting> {
10        self.client
11            .post("panel/setting/all", &serde_json::json!({}))
12            .await
13    }
14
15    pub async fn get_defaults(&self) -> Result<serde_json::Value> {
16        self.client
17            .post("panel/setting/defaultSettings", &serde_json::json!({}))
18            .await
19    }
20
21    pub async fn update(&self, settings: &AllSetting) -> Result<()> {
22        self.client
23            .post_empty("panel/setting/update", settings)
24            .await
25    }
26
27    pub async fn update_user(
28        &self,
29        old_username: &str,
30        old_password: &str,
31        new_username: &str,
32        new_password: &str,
33    ) -> Result<()> {
34        let body = serde_json::json!({
35            "oldUsername": old_username,
36            "oldPassword": old_password,
37            "newUsername": new_username,
38            "newPassword": new_password,
39        });
40        self.client
41            .post_empty("panel/setting/updateUser", &body)
42            .await
43    }
44
45    pub async fn restart_panel(&self) -> Result<()> {
46        self.client
47            .post_empty("panel/setting/restartPanel", &serde_json::json!({}))
48            .await
49    }
50
51    pub async fn default_xray_config(&self) -> Result<serde_json::Value> {
52        self.client.get("panel/setting/getDefaultJsonConfig").await
53    }
54}
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59    use crate::config::ClientConfig;
60    use wiremock::matchers::{method, path};
61    use wiremock::{Mock, MockServer, ResponseTemplate};
62
63    async fn auth_client(server: &MockServer) -> Client {
64        Mock::given(method("POST"))
65            .and(path("/login"))
66            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
67                "success": true, "msg": "", "obj": null
68            })))
69            .mount(server)
70            .await;
71        let config = ClientConfig::builder()
72            .host("127.0.0.1")
73            .port(server.address().port())
74            .build()
75            .unwrap();
76        let client = Client::new(config);
77        client.login("admin", "pass").await.unwrap();
78        client
79    }
80
81    #[tokio::test]
82    async fn get_all_returns_settings() {
83        let server = MockServer::start().await;
84        Mock::given(method("POST"))
85            .and(path("/panel/setting/all"))
86            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
87                "success": true, "msg": "", "obj": {"webPort": 2053, "tgBotEnable": false}
88            })))
89            .mount(&server)
90            .await;
91
92        let client = auth_client(&server).await;
93        let settings = client.settings().get_all().await.unwrap();
94        assert_eq!(settings.web_port, 2053);
95    }
96
97    #[tokio::test]
98    async fn update_user_succeeds() {
99        let server = MockServer::start().await;
100        Mock::given(method("POST"))
101            .and(path("/panel/setting/updateUser"))
102            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
103                "success": true, "msg": "updated", "obj": null
104            })))
105            .mount(&server)
106            .await;
107
108        let client = auth_client(&server).await;
109        client
110            .settings()
111            .update_user("admin", "old", "admin", "new123")
112            .await
113            .unwrap();
114    }
115}