-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlib.rs
245 lines (199 loc) · 8.24 KB
/
lib.rs
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
#![recursion_limit = "256"]
#[cfg(target_os = "android")]
pub mod android_bridge;
#[cfg(target_os = "android")]
pub mod jni_globals;
pub mod actix_route_dumper;
pub mod constants;
pub mod error;
pub mod logging;
pub mod groups;
pub mod media;
pub mod models;
pub mod repos;
pub mod server;
pub mod utils;
#[cfg(test)]
mod tests {
use super::*;
use crate::media::{download_file, scope, upload_file};
use crate::models::GroupRepoPath;
use actix_web::{test, web, App};
use anyhow::Result;
use models::{RequestName, SnowbirdGroup, SnowbirdRepo};
use serde::{Deserialize, Serialize};
use serde_json::json;
use server::server::{get_backend, init_backend, status, BACKEND};
use tmpdir::TmpDir;
#[derive(Serialize, Deserialize)]
struct GroupsResponse {
groups: Vec<SnowbirdGroup>,
}
#[derive(Serialize, Deserialize)]
struct ReposResponse {
repos: Vec<SnowbirdRepo>,
}
#[actix_web::test]
async fn basic_test() -> Result<()> {
let path = TmpDir::new("save-rust-test").await?;
BACKEND.get_or_init(|| init_backend(path.to_path_buf().as_path()));
{
let backend = get_backend().await?;
backend.start().await.expect("Backend failed to start");
}
let app = test::init_service(
App::new()
.service(status)
.service(web::scope("/api").service(groups::scope())),
)
.await;
let req = test::TestRequest::default().uri("/api/groups").to_request();
let resp: GroupsResponse = test::call_and_read_body_json(&app, req).await;
assert_eq!(resp.groups.len(), 0);
let req = test::TestRequest::post()
.uri("/api/groups")
.set_json(RequestName {
name: "example".to_string(),
})
.to_request();
let group: SnowbirdGroup = test::call_and_read_body_json(&app, req).await;
assert_eq!(group.name, Some("example".to_string()));
let req = test::TestRequest::default()
.uri(format!("/api/groups/{}/repos", group.key).as_str())
.to_request();
let resp: ReposResponse = test::call_and_read_body_json(&app, req).await;
assert_eq!(resp.repos.len(), 0, "Should have no repos at first");
let req = test::TestRequest::post()
.uri(format!("/api/groups/{}/repos", group.key).as_str())
.set_json(RequestName {
name: "example repo".to_string(),
})
.to_request();
let repo: SnowbirdRepo = test::call_and_read_body_json(&app, req).await;
assert_eq!(repo.name, "example repo".to_string());
let req = test::TestRequest::default()
.uri(format!("/api/groups/{}/repos", group.key).as_str())
.to_request();
let resp: ReposResponse = test::call_and_read_body_json(&app, req).await;
assert_eq!(resp.repos.len(), 1, "Should have 1 repo after create");
{
let backend = get_backend().await?;
backend.stop().await.expect("Backend failed to start");
}
Ok(())
}
#[actix_web::test]
async fn test_upload_list_delete() -> Result<()> {
// Initialize the app
let path = TmpDir::new("test_api_repo_file_operations").await?;
BACKEND.get_or_init(|| init_backend(path.to_path_buf().as_path()));
{
let backend = get_backend().await?;
backend.start().await.expect("Backend failed to start");
}
let app = test::init_service(
App::new()
.service(status)
.service(web::scope("/api").service(groups::scope())),
)
.await;
// Step 1: Create a group via the API
let create_group_req = test::TestRequest::post()
.uri("/api/groups")
.set_json(json!({ "name": "Test Group" }))
.to_request();
let create_group_resp: serde_json::Value =
test::call_and_read_body_json(&app, create_group_req).await;
let group_id = create_group_resp["key"]
.as_str()
.expect("No group key returned");
// Step 2: Create a repo via the API
let create_repo_req = test::TestRequest::post()
.uri(&format!("/api/groups/{}/repos", group_id))
.set_json(json!({ "name": "Test Repo" }))
.to_request();
let create_repo_resp: serde_json::Value =
test::call_and_read_body_json(&app, create_repo_req).await;
let repo_id = create_repo_resp["key"]
.as_str()
.expect("No repo key returned");
// Step 3: Upload a file to the repository
let file_name = "example.txt";
let file_content = b"Test content for file upload";
let upload_req = test::TestRequest::post()
.uri(&format!(
"/api/groups/{}/repos/{}/media/{}",
group_id, repo_id, file_name
))
.set_payload(file_content.to_vec())
.to_request();
let upload_resp = test::call_service(&app, upload_req).await;
// Check upload success
assert!(upload_resp.status().is_success(), "File upload failed");
// Step 4: List files in the repository
let list_files_req = test::TestRequest::get()
.uri(&format!("/api/groups/{}/repos/{}/media", group_id, repo_id))
.to_request();
let list_files_resp: serde_json::Value =
test::call_and_read_body_json(&app, list_files_req).await;
println!("List files response: {:?}", list_files_resp);
// Now check if the response is an array directly
if let Some(files_array) = list_files_resp.get("files").and_then(|v| v.as_array()) {
assert_eq!(files_array.len(), 1, "There should be one file in the repo");
// Ensure the file name matches what we uploaded
let file_obj = &files_array[0];
assert_eq!(
file_obj["name"].as_str().unwrap(),
"example.txt",
"The listed file should match the uploaded file"
);
let file_name = file_obj["name"].as_str().expect("File name not found");
assert_eq!(file_name, "example.txt", "File name does not match");
} else {
panic!("The response is not an array as expected");
}
let get_file_req = test::TestRequest::get()
.uri(&format!(
"/api/groups/{}/repos/{}/media/{}",
group_id, repo_id, file_name
))
.to_request();
let get_file_resp = test::call_service(&app, get_file_req).await;
assert!(get_file_resp.status().is_success(), "File download failed");
let got_file_data = test::read_body(get_file_resp).await;
assert_eq!(
got_file_data.to_vec().as_slice(),
file_content,
"Downloaded back file content"
);
// Step 5: Delete the file from the repository
let delete_file_req = test::TestRequest::delete()
.uri(&format!(
"/api/groups/{}/repos/{}/media/{}",
group_id, repo_id, "example.txt"
))
.to_request();
let delete_resp = test::call_service(&app, delete_file_req).await;
assert!(delete_resp.status().is_success(), "File deletion failed");
// Step 6: Verify the file is no longer listed
let list_files_after_deletion_req = test::TestRequest::get()
.uri(&format!("/api/groups/{}/repos/{}/media", group_id, repo_id))
.to_request();
let list_files_after_deletion_resp: serde_json::Value =
test::call_and_read_body_json(&app, list_files_after_deletion_req).await;
if let Some(files_array) = list_files_after_deletion_resp.get("files").and_then(|v| v.as_array()) {
assert!(
files_array.is_empty(),
"File list should be empty after file deletion"
);
} else {
panic!("The response does not contain the 'files' array as expected");
}
// Clean up: Stop the backend
{
let backend = get_backend().await?;
backend.stop().await.expect("Backend failed to stop");
}
Ok(())
}
}