gewe_http/group/
admin.rs

1use crate::client::GeweHttpClient;
2use gewe_core::{AdminOperateRequest, GeweError};
3use tracing::instrument;
4
5impl GeweHttpClient {
6    #[instrument(skip(self))]
7    pub async fn admin_operate(&self, req: AdminOperateRequest<'_>) -> Result<(), GeweError> {
8        let _ = self
9            .post_api::<_, ()>("gewe/v2/api/group/adminOperate", &req)
10            .await?;
11        Ok(())
12    }
13}
14
15#[cfg(test)]
16mod tests {
17    use gewe_core::AdminOperateRequest;
18
19    #[test]
20    fn test_admin_operate_request_serialization() {
21        let req = AdminOperateRequest {
22            app_id: "test_app",
23            chatroom_id: "room123@chatroom",
24            wxid: "wxid_test123",
25            is_admin: true,
26        };
27
28        let json = serde_json::to_string(&req).unwrap();
29        assert!(json.contains("appId"));
30        assert!(json.contains("test_app"));
31        assert!(json.contains("chatroomId"));
32        assert!(json.contains("room123@chatroom"));
33        assert!(json.contains("wxid"));
34        assert!(json.contains("wxid_test123"));
35        assert!(json.contains("isAdmin"));
36        assert!(json.contains("true"));
37    }
38
39    #[test]
40    fn test_admin_operate_request_field_names() {
41        let req = AdminOperateRequest {
42            app_id: "app",
43            chatroom_id: "room",
44            wxid: "wx123",
45            is_admin: false,
46        };
47
48        let json = serde_json::to_string(&req).unwrap();
49        let value: serde_json::Value = serde_json::from_str(&json).unwrap();
50
51        // 验证字段名是 camelCase
52        assert!(value.get("appId").is_some());
53        assert!(value.get("chatroomId").is_some());
54        assert!(value.get("wxid").is_some());
55        assert!(value.get("isAdmin").is_some());
56        assert_eq!(value.get("isAdmin").unwrap().as_bool().unwrap(), false);
57    }
58
59    #[test]
60    fn test_admin_operate_request_set_admin_true() {
61        let req = AdminOperateRequest {
62            app_id: "app_id_123",
63            chatroom_id: "34757816141@chatroom",
64            wxid: "wxid_0xsqb3o0tsvz22",
65            is_admin: true,
66        };
67
68        let json = serde_json::to_string(&req).unwrap();
69        let value: serde_json::Value = serde_json::from_str(&json).unwrap();
70
71        assert_eq!(value.get("appId").unwrap().as_str().unwrap(), "app_id_123");
72        assert_eq!(
73            value.get("chatroomId").unwrap().as_str().unwrap(),
74            "34757816141@chatroom"
75        );
76        assert_eq!(
77            value.get("wxid").unwrap().as_str().unwrap(),
78            "wxid_0xsqb3o0tsvz22"
79        );
80        assert_eq!(value.get("isAdmin").unwrap().as_bool().unwrap(), true);
81    }
82
83    #[test]
84    fn test_admin_operate_request_set_admin_false() {
85        let req = AdminOperateRequest {
86            app_id: "app_id_123",
87            chatroom_id: "34757816141@chatroom",
88            wxid: "wxid_0xsqb3o0tsvz22",
89            is_admin: false,
90        };
91
92        let json = serde_json::to_string(&req).unwrap();
93        let value: serde_json::Value = serde_json::from_str(&json).unwrap();
94
95        assert_eq!(value.get("isAdmin").unwrap().as_bool().unwrap(), false);
96    }
97
98    #[test]
99    fn test_admin_operate_request_with_special_chars() {
100        let req = AdminOperateRequest {
101            app_id: "测试应用",
102            chatroom_id: "房间123@chatroom",
103            wxid: "微信号",
104            is_admin: true,
105        };
106
107        let json = serde_json::to_string(&req).unwrap();
108        // 验证 Unicode 字符能正确序列化
109        assert!(json.contains("测试应用") || json.contains("\\u"));
110    }
111
112    #[test]
113    fn test_admin_operate_request_clone() {
114        let req = AdminOperateRequest {
115            app_id: "app",
116            chatroom_id: "room",
117            wxid: "wx123",
118            is_admin: true,
119        };
120
121        let cloned = req.clone();
122        assert_eq!(req.app_id, cloned.app_id);
123        assert_eq!(req.chatroom_id, cloned.chatroom_id);
124        assert_eq!(req.wxid, cloned.wxid);
125        assert_eq!(req.is_admin, cloned.is_admin);
126    }
127
128    #[test]
129    fn test_admin_operate_request_debug() {
130        let req = AdminOperateRequest {
131            app_id: "app",
132            chatroom_id: "room",
133            wxid: "wx123",
134            is_admin: true,
135        };
136
137        let debug_str = format!("{:?}", req);
138        assert!(debug_str.contains("AdminOperateRequest"));
139        assert!(debug_str.contains("app"));
140        assert!(debug_str.contains("room"));
141        assert!(debug_str.contains("wx123"));
142    }
143
144    #[test]
145    fn test_admin_operate_request_deserialization() {
146        let json = r#"{
147            "appId": "test_app",
148            "chatroomId": "room@chatroom",
149            "wxid": "wxid_test",
150            "isAdmin": true
151        }"#;
152
153        let req: AdminOperateRequest = serde_json::from_str(json).unwrap();
154        assert_eq!(req.app_id, "test_app");
155        assert_eq!(req.chatroom_id, "room@chatroom");
156        assert_eq!(req.wxid, "wxid_test");
157        assert_eq!(req.is_admin, true);
158    }
159}