1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
//! V4 API 集成测试
use super::common::{TestConfig, TestCredentials, TestResults};
use cloudreve_api::api::v4::{ApiV4Client, models::*, uri::path_to_uri};
use std::time::Instant;
/// V4 API 测试套件
pub struct V4TestSuite {
client: ApiV4Client,
credentials: TestCredentials,
#[allow(dead_code)]
config: TestConfig,
}
impl V4TestSuite {
/// 创建新的测试套件
pub async fn new(
config: TestConfig,
credentials: TestCredentials,
) -> Result<Self, Box<dyn std::error::Error>> {
let v4_config = config.v4_config().ok_or("V4 配置未找到")?;
let mut client = ApiV4Client::new(&v4_config.base_url);
// 执行登录获取 token
let login_request = LoginRequest {
email: &credentials.username,
password: &credentials.password,
captcha: None,
ticket: None,
};
match client.login(&login_request).await {
Ok(data) => {
client.set_token(data.token.access_token);
println!("│ │ ✓ V4 登录成功: {}", data.user.nickname);
}
Err(e) => {
println!("│ │ ✗ V4 登录失败: {}", e);
return Err(format!("V4 登录失败: {}", e).into());
}
}
Ok(Self {
client,
credentials,
config,
})
}
/// 运行所有 V4 测试
pub async fn run_all(&self) -> TestResults {
let mut results = TestResults::new();
let start = Instant::now();
println!("\n┌─ V4 API 测试 ─────────────────────────────────────");
// Session 测试
results.merge(self.test_session().await);
// File 测试
results.merge(self.test_file().await);
// User 测试
results.merge(self.test_user().await);
// Share 测试
results.merge(self.test_share().await);
// WebDAV 测试
results.merge(self.test_webdav().await);
// Workflow 测试
results.merge(self.test_workflow().await);
// Site 测试
results.merge(self.test_site().await);
results.duration_ms = start.elapsed().as_millis() as u64;
results
}
/// Session 模块测试
async fn test_session(&self) -> TestResults {
let mut results = TestResults::new();
println!("│ ├─ Session 测试...");
// 准备登录
match self.client.prepare_login(&self.credentials.username).await {
Ok(prep) => {
println!("│ │ ✓ 准备登录: password={}", prep.password_enabled);
results.add_success();
}
Err(e) => {
println!("│ │ ✗ 准备登录失败: {}", e);
results.add_failure(
"v4_session_prepare".to_string(),
"v4".to_string(),
e.to_string(),
);
}
}
// 登出测试
match self.client.logout().await {
Ok(_) => {
println!("│ │ ✓ 登出成功");
results.add_success();
}
Err(e) => {
println!("│ │ ✗ 登出失败: {}", e);
results.add_failure(
"v4_session_logout".to_string(),
"v4".to_string(),
e.to_string(),
);
}
}
results
}
/// File 模块测试
async fn test_file(&self) -> TestResults {
let mut results = TestResults::new();
println!("│ ├─ File 测试...");
// 列出根目录
match self
.client
.list_files(&ListFilesRequest {
path: "/",
page: Some(0),
page_size: Some(50),
order_by: None,
order_direction: None,
next_page_token: None,
})
.await
{
Ok(response) => {
println!("│ │ ✓ 列出根目录: {} 个对象", response.files.len());
results.add_success();
}
Err(e) => {
println!("│ │ ✗ 列出根目录失败: {}", e);
results.add_failure("v4_file_list".to_string(), "v4".to_string(), e.to_string());
}
}
// 创建测试目录
let test_dir_name = format!("test_v4_{}", chrono::Utc::now().timestamp());
let test_path = format!("/{}", test_dir_name);
match self.client.create_directory(&test_path).await {
Ok(_) => {
println!("│ │ ✓ 创建目录: {}", test_path);
results.add_success();
// 等待目录创建完成并同步
tokio::time::sleep(tokio::time::Duration::from_millis(1000)).await;
// 获取文件信息 - 注意:这个测试可能会失败,因为某些 V4 实例的 404 响应格式问题
match self.client.get_file_info(&test_path).await {
Ok(file) => {
println!("│ │ ✓ 获取文件信息: {} ({:?})", file.name, file.r#type);
results.add_success();
// 重命名目录
let new_name = format!("{}_renamed", test_dir_name);
let uri = path_to_uri(&test_path);
match self
.client
.rename_file(&RenameFileRequest {
uri: &uri,
new_name: &new_name,
})
.await
{
Ok(_) => {
println!("│ │ ✓ 重命名目录成功");
results.add_success();
// 删除目录
let new_path = format!("/{}", new_name);
match self.client.delete_file(&new_path).await {
Ok(_) => {
println!("│ │ ✓ 删除目录成功");
results.add_success();
}
Err(e) => {
println!("│ │ ✗ 删除目录失败: {} (可能需要手动清理)", e);
results.add_failure(
"v4_file_delete".to_string(),
"v4".to_string(),
e.to_string(),
);
}
}
}
Err(e) => {
println!("│ │ ✗ 重命名目录失败: {}", e);
results.add_failure(
"v4_file_rename".to_string(),
"v4".to_string(),
e.to_string(),
);
// 清理
let _ = self.client.delete_file(&test_path).await;
}
}
}
Err(e) => {
println!("│ │ ✗ 获取文件信息失败: {} (检测到 API 响应格式问题)", e);
results.add_failure(
"v4_file_info".to_string(),
"v4".to_string(),
format!("{} - 这表明该端点在文件不存在时返回了非标准格式的响应", e),
);
// 清理
let _ = self.client.delete_file(&test_path).await;
}
}
}
Err(e) => {
println!("│ │ ✗ 创建目录失败: {}", e);
results.add_failure(
"v4_file_create".to_string(),
"v4".to_string(),
e.to_string(),
);
}
}
results
}
/// User 模块测试
async fn test_user(&self) -> TestResults {
let mut results = TestResults::new();
println!("│ ├─ User 测试...");
// 获取用户设置 - 使用已有的方法
match self.client.get_user_setting().await {
Ok(_) => {
println!("│ │ ✓ 获取用户设置成功");
results.add_success();
}
Err(e) => {
println!("│ │ ✗ 获取用户设置失败: {}", e);
results.add_failure(
"v4_user_setting".to_string(),
"v4".to_string(),
e.to_string(),
);
}
}
// 获取存储策略 - 跳过此测试,端点可能不存在
println!("│ │ ⊘ 跳过存储策略测试(端点可能不存在)");
results.add_skip();
// 获取用户容量 - 使用已有的方法
match self.client.get_user_capacity().await {
Ok(_) => {
println!("│ │ ✓ 获取用户容量成功");
results.add_success();
}
Err(e) => {
println!("│ │ ✗ 获取用户容量失败: {}", e);
results.add_failure(
"v4_user_capacity".to_string(),
"v4".to_string(),
e.to_string(),
);
}
}
results
}
/// Share 模块测试
async fn test_share(&self) -> TestResults {
let mut results = TestResults::new();
println!("│ ├─ Share 测试...");
// 列出分享
match self.client.get::<serde_json::Value>("/share").await {
Ok(_) => {
println!("│ │ ✓ 列出分享成功");
results.add_success();
}
Err(e) => {
println!("│ │ ✗ 列出分享失败: {}", e);
results.add_failure("v4_share_list".to_string(), "v4".to_string(), e.to_string());
}
}
results
}
/// WebDAV 模块测试
async fn test_webdav(&self) -> TestResults {
let mut results = TestResults::new();
println!("│ ├─ WebDAV 测试...");
// 列出 WebDAV 账户
match self.client.get::<serde_json::Value>("/devices/dav").await {
Ok(_) => {
println!("│ │ ✓ 列出 WebDAV 账户成功");
results.add_success();
}
Err(e) => {
println!("│ │ ✗ 列出 WebDAV 账户失败: {}", e);
results.add_failure(
"v4_webdav_list".to_string(),
"v4".to_string(),
e.to_string(),
);
}
}
results
}
/// Workflow 模块测试
async fn test_workflow(&self) -> TestResults {
let mut results = TestResults::new();
println!("│ ├─ Workflow 测试...");
// 列出工作流任务
match self.client.get::<serde_json::Value>("/workflow").await {
Ok(_) => {
println!("│ │ ✓ 列出工作流任务成功");
results.add_success();
}
Err(e) => {
println!("│ │ ✗ 列出工作流任务失败: {}", e);
results.add_failure(
"v4_workflow_list".to_string(),
"v4".to_string(),
e.to_string(),
);
}
}
results
}
/// Site 模块测试
async fn test_site(&self) -> TestResults {
let mut results = TestResults::new();
println!("│ ├─ Site 测试...");
// Ping 测试 - 使用已有的方法
match self.client.ping().await {
Ok(_) => {
println!("│ │ ✓ Site ping 成功");
results.add_success();
}
Err(e) => {
println!("│ │ ✗ Site ping 失败: {}", e);
results.add_failure("v4_site_ping".to_string(), "v4".to_string(), e.to_string());
}
}
// 获取站点版本 - 使用 get_version 方法
match self.client.get_version().await {
Ok(_) => {
println!("│ │ ✓ 获取站点版本成功");
results.add_success();
}
Err(e) => {
println!("│ │ ✗ 获取站点版本失败: {}", e);
results.add_failure(
"v4_site_version".to_string(),
"v4".to_string(),
e.to_string(),
);
}
}
// 获取站点配置 - 测试不同的 section
use cloudreve_api::api::v4::models::SiteConfigSection;
let sections = [
SiteConfigSection::Basic,
SiteConfigSection::Login,
SiteConfigSection::Explorer,
SiteConfigSection::Emojis,
SiteConfigSection::Vas,
SiteConfigSection::App,
SiteConfigSection::Thumb,
];
for section in sections {
match self.client.get_site_config(section).await {
Ok(_config) => {
println!("│ │ ✓ 获取站点配置 [{}] 成功", section.as_str());
results.add_success();
}
Err(e) => {
println!("│ │ ✗ 获取站点配置 [{}] 失败: {}", section.as_str(), e);
results.add_failure(
format!("v4_site_config_{}", section.as_str()),
"v4".to_string(),
e.to_string(),
);
}
}
}
results
}
}