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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
//! V3 API 集成测试
use super::common::{TestConfig, TestCredentials, TestResults};
use cloudreve_api::api::v3::{ApiV3Client, models::*};
use std::time::Instant;
/// V3 API 测试套件
pub struct V3TestSuite {
client: ApiV3Client,
credentials: TestCredentials,
#[allow(dead_code)]
config: TestConfig,
}
impl V3TestSuite {
/// 创建新的测试套件
pub async fn new(
config: TestConfig,
credentials: TestCredentials,
) -> Result<Self, Box<dyn std::error::Error>> {
let v3_config = config.v3_config().ok_or("V3 配置未找到")?;
let client = ApiV3Client::new(&v3_config.base_url);
Ok(Self {
client,
credentials,
config,
})
}
/// 执行登录
async fn login(&mut self) -> Result<User, Box<dyn std::error::Error>> {
let request = LoginRequest {
user_name: &self.credentials.username,
password: &self.credentials.password,
captcha_code: None,
};
Ok(self.client.login(&request).await?)
}
/// 运行所有 V3 测试
pub async fn run_all(&mut self) -> TestResults {
let mut results = TestResults::new();
let start = Instant::now();
println!("\n┌─ V3 API 测试 ─────────────────────────────────────");
// Session 测试
results.merge(self.test_session().await);
// Directory 测试
results.merge(self.test_directory().await);
// File 测试
results.merge(self.test_file().await);
// Object 测试
results.merge(self.test_object().await);
// Share 测试
results.merge(self.test_share().await);
// Site 测试
results.merge(self.test_site().await);
// User 测试
results.merge(self.test_user().await);
results.duration_ms = start.elapsed().as_millis() as u64;
results
}
/// Session 模块测试
async fn test_session(&mut self) -> TestResults {
let mut results = TestResults::new();
println!("│ ├─ Session 测试...");
// 登录测试
match self.login().await {
Ok(user) => {
println!("│ │ ✓ 登录成功: {}", user.nickname);
results.add_success();
}
Err(e) => {
println!("│ │ ✗ 登录失败: {}", e);
results.add_failure(
"v3_session_login".to_string(),
"v3".to_string(),
e.to_string(),
);
}
}
// 登出测试
match self.client.logout().await {
Ok(_) => {
println!("│ │ ✓ 登出成功");
results.add_success();
}
Err(e) => {
println!("│ │ ✗ 登出失败: {}", e);
results.add_failure(
"v3_session_logout".to_string(),
"v3".to_string(),
e.to_string(),
);
}
}
// 重新登录以便后续测试
let _ = self.login().await;
results
}
/// Directory 模块测试
async fn test_directory(&self) -> TestResults {
let mut results = TestResults::new();
println!("│ ├─ Directory 测试...");
// 列出根目录
match self.client.list_directory("/").await {
Ok(list) => {
println!("│ │ ✓ 列出根目录: {} 个对象", list.objects.len());
results.add_success();
}
Err(e) => {
println!("│ │ ✗ 列出根目录失败: {}", e);
results.add_failure(
"v3_directory_list".to_string(),
"v3".to_string(),
e.to_string(),
);
}
}
// 创建测试目录
let test_dir_name = format!("test_dir_{}", chrono::Utc::now().timestamp());
let test_path = format!("/{}", test_dir_name);
match self
.client
.create_directory(&CreateDirectoryRequest { path: &test_path })
.await
{
Ok(_) => {
println!("│ │ ✓ 创建目录: {}", test_path);
results.add_success();
// 清理:删除测试目录
let _ = self
.client
.delete_object(&DeleteObjectRequest {
dirs: vec![&test_path],
items: vec![],
force: true,
unlink: false,
})
.await;
}
Err(e) => {
println!("│ │ ✗ 创建目录失败: {}", e);
results.add_failure(
"v3_directory_create".to_string(),
"v3".to_string(),
e.to_string(),
);
}
}
results
}
/// File 模块测试
async fn test_file(&self) -> TestResults {
let mut results = TestResults::new();
println!("│ ├─ File 测试...");
// 首先获取存储策略
let policy_id = match self.get_policy_id().await {
Some(id) => id,
None => {
println!("│ │ ⊘ 跳过 File 测试: 无法获取存储策略");
results.add_skip();
results.add_skip();
return results;
}
};
// 创建上传会话
let test_file_name = format!("test_{}.txt", chrono::Utc::now().timestamp());
let upload_request = UploadFileRequest {
path: "/",
size: 1024,
name: &test_file_name,
policy_id: &policy_id,
last_modified: chrono::Utc::now().timestamp(),
mime_type: "text/plain",
};
match self.client.upload_file(&upload_request).await {
Ok(session) => {
println!("│ │ ✓ 创建上传会话: {}", session.session_id);
results.add_success();
// 上传分片(空数据用于测试)
match self
.client
.upload_chunk(&session.session_id, 0, vec![b' '; 1024])
.await
{
Ok(_) => {
println!("│ │ ✓ 上传分片成功");
results.add_success();
}
Err(e) => {
println!("│ │ ✗ 上传分片失败: {}", e);
results.add_failure(
"v3_file_upload_chunk".to_string(),
"v3".to_string(),
e.to_string(),
);
}
}
// 完成上传
match self.client.complete_upload(&session.session_id).await {
Ok(_) => {
println!("│ │ ✓ 完成上传");
results.add_success();
}
Err(e) => {
println!("│ │ ✗ 完成上传失败: {}", e);
results.add_failure(
"v3_file_complete_upload".to_string(),
"v3".to_string(),
e.to_string(),
);
}
}
}
Err(e) => {
println!("│ │ ✗ 创建上传会话失败: {}", e);
results.add_failure(
"v3_file_upload".to_string(),
"v3".to_string(),
e.to_string(),
);
results.add_skip();
results.add_skip();
}
}
results
}
/// Object 模块测试
async fn test_object(&self) -> TestResults {
let mut results = TestResults::new();
println!("│ ├─ Object 测试...");
// 创建测试对象
let test_dir_name = format!("test_obj_{}", chrono::Utc::now().timestamp());
let test_path = format!("/{}", test_dir_name);
// 先创建目录
match self
.client
.create_directory(&CreateDirectoryRequest { path: &test_path })
.await
{
Ok(_) => {
// 等待目录创建完成
tokio::time::sleep(tokio::time::Duration::from_millis(500)).await;
// 获取对象 ID 用于后续操作
let obj_id = match self.client.list_directory("/").await {
Ok(list) => list
.objects
.iter()
.find(|o| o.name == test_dir_name)
.map(|o| o.id.clone()),
Err(_) => None,
};
if let Some(id) = obj_id {
// 获取对象属性
match self.client.get_object_property(&id, Some(true), None).await {
Ok(prop) => {
println!("│ │ ✓ 获取对象属性: {}", prop.path);
results.add_success();
}
Err(e) => {
println!("│ │ ✗ 获取对象属性失败: {}", e);
results.add_failure(
"v3_object_get_property".to_string(),
"v3".to_string(),
e.to_string(),
);
}
}
// 重命名测试 - 使用对象 ID 而不是路径
let new_name = format!("{}_renamed", test_dir_name);
match self
.client
.rename_object(&RenameObjectRequest {
action: "rename",
src: SourceItems {
dirs: vec![id.as_str()],
items: vec![],
},
new_name: &new_name,
})
.await
{
Ok(_) => {
println!("│ │ ✓ 重命名对象成功");
results.add_success();
// 清理:删除重命名后的对象
let new_path = format!("/{}", new_name);
let _ = self
.client
.delete_object(&DeleteObjectRequest {
dirs: vec![new_path.as_str()],
items: vec![],
force: true,
unlink: false,
})
.await;
}
Err(e) => {
println!("│ │ ✗ 重命名对象失败: {}", e);
results.add_failure(
"v3_object_rename".to_string(),
"v3".to_string(),
e.to_string(),
);
// 清理
let _ = self
.client
.delete_object(&DeleteObjectRequest {
dirs: vec![test_path.as_str()],
items: vec![],
force: true,
unlink: false,
})
.await;
}
}
} else {
println!("│ │ ⊘ 跳过测试: 未找到创建的目录 (可能需要更多时间)");
results.add_skip();
results.add_skip();
}
}
Err(e) => {
println!("│ │ ✗ 创建目录失败: {}", e);
results.add_failure(
"v3_object_create".to_string(),
"v3".to_string(),
e.to_string(),
);
}
}
results
}
/// Share 模块测试
async fn test_share(&self) -> TestResults {
let mut results = TestResults::new();
println!("│ ├─ Share 测试...");
// 创建测试目录用于分享
let test_dir_name = format!("test_share_{}", chrono::Utc::now().timestamp());
let test_path = format!("/{}", test_dir_name);
let obj_id = if self
.client
.create_directory(&CreateDirectoryRequest { path: &test_path })
.await
.is_ok()
{
// 获取目录 ID
match self.client.list_directory("/").await {
Ok(list) => list
.objects
.iter()
.find(|o| o.name == test_dir_name)
.map(|o| o.id.clone()),
_ => None,
}
} else {
None
};
if let Some(id) = obj_id {
match self
.client
.create_share(&ShareRequest {
id: id.clone(),
is_dir: true,
password: "".to_string(),
downloads: 0,
expire: 0,
preview: true,
})
.await
{
Ok(share) => {
println!("│ │ ✓ 创建分享: {}", share.key);
results.add_success();
}
Err(e) => {
println!("│ │ ✗ 创建分享失败: {}", e);
results.add_failure(
"v3_share_create".to_string(),
"v3".to_string(),
e.to_string(),
);
}
}
// 清理
let _ = self
.client
.delete_object(&DeleteObjectRequest {
dirs: vec![&test_path],
items: vec![],
force: true,
unlink: false,
})
.await;
} else {
results.add_skip();
}
results
}
/// Site 模块测试
async fn test_site(&self) -> TestResults {
let mut results = TestResults::new();
println!("│ ├─ Site 测试...");
// Ping 测试
match self.client.get::<serde_json::Value>("/site/ping").await {
Ok(_) => {
println!("│ │ ✓ Site ping 成功");
results.add_success();
}
Err(e) => {
println!("│ │ ✗ Site ping 失败: {}", e);
results.add_failure("v3_site_ping".to_string(), "v3".to_string(), e.to_string());
}
}
// 获取站点配置
match self.client.get::<SiteConfig>("/site/config").await {
Ok(config) => {
println!("│ │ ✓ 获取站点配置: {}", config.title);
results.add_success();
}
Err(e) => {
println!("│ │ ✗ 获取站点配置失败: {}", e);
results.add_failure(
"v3_site_config".to_string(),
"v3".to_string(),
e.to_string(),
);
}
}
results
}
/// User 模块测试
async fn test_user(&self) -> TestResults {
let mut results = TestResults::new();
println!("│ ├─ User 测试...");
// 获取用户设置
match self.client.get::<serde_json::Value>("/user/setting").await {
Ok(_) => {
println!("│ │ ✓ 获取用户设置成功");
results.add_success();
}
Err(e) => {
println!("│ │ ✗ 获取用户设置失败: {}", e);
results.add_failure(
"v3_user_setting".to_string(),
"v3".to_string(),
e.to_string(),
);
}
}
// 获取存储信息 - 使用 ApiResponse 包装
match self
.client
.get::<ApiResponse<StorageInfo>>("/user/storage")
.await
{
Ok(response) => {
if let Some(storage) = response.data {
println!(
"│ │ ✓ 获取存储信息: {} / {} bytes",
storage.used, storage.total
);
results.add_success();
} else {
println!("│ │ ✗ 获取存储信息失败: 无数据");
results.add_failure(
"v3_user_storage".to_string(),
"v3".to_string(),
"无数据".to_string(),
);
}
}
Err(e) => {
println!("│ │ ✗ 获取存储信息失败: {}", e);
results.add_failure(
"v3_user_storage".to_string(),
"v3".to_string(),
e.to_string(),
);
}
}
// 获取 WebDAV 账户 - 使用已有的方法
match self.client.get_webdav_accounts().await {
Ok(accounts) => {
println!("│ │ ✓ 获取 WebDAV 账户: {} 个", accounts.len());
results.add_success();
}
Err(e) => {
println!("│ │ ✗ 获取 WebDAV 账户失败: {}", e);
results.add_failure(
"v3_user_webdav".to_string(),
"v3".to_string(),
e.to_string(),
);
}
}
results
}
/// 辅助方法:获取存储策略 ID
async fn get_policy_id(&self) -> Option<String> {
match self.client.list_directory("/").await {
Ok(list) => Some(list.policy.id),
Err(_) => None,
}
}
}