@@ -26,15 +26,15 @@ pub mod utils;
2626#[ cfg( test) ]
2727mod tests {
2828 use super :: * ;
29+ use crate :: media:: { download_file, scope, upload_file} ;
30+ use crate :: models:: GroupRepoPath ;
2931 use actix_web:: { test, web, App } ;
3032 use anyhow:: Result ;
3133 use models:: { RequestName , SnowbirdGroup , SnowbirdRepo } ;
3234 use serde:: { Deserialize , Serialize } ;
35+ use serde_json:: json;
3336 use server:: server:: { get_backend, init_backend, status, BACKEND } ;
3437 use tmpdir:: TmpDir ;
35- use serde_json:: json;
36- use crate :: media:: { download_file, upload_file, scope} ;
37- use crate :: models:: GroupRepoPath ;
3838
3939 #[ derive( Serialize , Deserialize ) ]
4040 struct GroupsResponse {
@@ -136,24 +136,33 @@ mod tests {
136136 . uri ( "/api/groups" )
137137 . set_json ( json ! ( { "name" : "Test Group" } ) )
138138 . to_request ( ) ;
139- let create_group_resp: serde_json:: Value = test:: call_and_read_body_json ( & app, create_group_req) . await ;
140- let group_id = create_group_resp[ "key" ] . as_str ( ) . expect ( "No group key returned" ) ;
139+ let create_group_resp: serde_json:: Value =
140+ test:: call_and_read_body_json ( & app, create_group_req) . await ;
141+ let group_id = create_group_resp[ "key" ]
142+ . as_str ( )
143+ . expect ( "No group key returned" ) ;
141144
142145 // Step 2: Create a repo via the API
143146 let create_repo_req = test:: TestRequest :: post ( )
144147 . uri ( & format ! ( "/api/groups/{}/repos" , group_id) )
145148 . set_json ( json ! ( { "name" : "Test Repo" } ) )
146149 . to_request ( ) ;
147- let create_repo_resp: serde_json:: Value = test:: call_and_read_body_json ( & app, create_repo_req) . await ;
150+ let create_repo_resp: serde_json:: Value =
151+ test:: call_and_read_body_json ( & app, create_repo_req) . await ;
148152
149- let repo_id = create_repo_resp[ "key" ] . as_str ( ) . expect ( "No repo key returned" ) ;
153+ let repo_id = create_repo_resp[ "key" ]
154+ . as_str ( )
155+ . expect ( "No repo key returned" ) ;
150156
151157 // Step 3: Upload a file to the repository
152158 let file_name = "example.txt" ;
153159 let file_content = b"Test content for file upload" ;
154160
155161 let upload_req = test:: TestRequest :: post ( )
156- . uri ( & format ! ( "/api/groups/{}/repos/{}/media/{}" , group_id, repo_id, file_name) )
162+ . uri ( & format ! (
163+ "/api/groups/{}/repos/{}/media/{}" ,
164+ group_id, repo_id, file_name
165+ ) )
157166 . set_payload ( file_content. to_vec ( ) )
158167 . to_request ( ) ;
159168 let upload_resp = test:: call_service ( & app, upload_req) . await ;
@@ -165,17 +174,14 @@ mod tests {
165174 let list_files_req = test:: TestRequest :: get ( )
166175 . uri ( & format ! ( "/api/groups/{}/repos/{}/media" , group_id, repo_id) )
167176 . to_request ( ) ;
168- let list_files_resp: serde_json:: Value = test:: call_and_read_body_json ( & app, list_files_req) . await ;
177+ let list_files_resp: serde_json:: Value =
178+ test:: call_and_read_body_json ( & app, list_files_req) . await ;
169179
170180 println ! ( "List files response: {:?}" , list_files_resp) ;
171181
172182 // Now check if the response is an array directly
173183 if let Some ( files_array) = list_files_resp. as_array ( ) {
174- assert_eq ! (
175- files_array. len( ) ,
176- 1 ,
177- "There should be one file in the repo"
178- ) ;
184+ assert_eq ! ( files_array. len( ) , 1 , "There should be one file in the repo" ) ;
179185
180186 // Ensure the file name matches what we uploaded
181187 let file_obj = & files_array[ 0 ] ;
@@ -191,9 +197,28 @@ mod tests {
191197 panic ! ( "The response is not an array as expected" ) ;
192198 }
193199
200+ let get_file_req = test:: TestRequest :: get ( )
201+ . uri ( & format ! (
202+ "/api/groups/{}/repos/{}/media/{}" ,
203+ group_id, repo_id, file_name
204+ ) )
205+ . to_request ( ) ;
206+ let get_file_resp = test:: call_service ( & app, get_file_req) . await ;
207+ assert ! ( get_file_resp. status( ) . is_success( ) , "File download failed" ) ;
208+
209+ let got_file_data = test:: read_body ( get_file_resp) . await ;
210+ assert_eq ! (
211+ got_file_data. to_vec( ) . as_slice( ) ,
212+ file_content,
213+ "Downloaded back file content"
214+ ) ;
215+
194216 // Step 5: Delete the file from the repository
195217 let delete_file_req = test:: TestRequest :: delete ( )
196- . uri ( & format ! ( "/api/groups/{}/repos/{}/media/{}" , group_id, repo_id, "example.txt" ) )
218+ . uri ( & format ! (
219+ "/api/groups/{}/repos/{}/media/{}" ,
220+ group_id, repo_id, "example.txt"
221+ ) )
197222 . to_request ( ) ;
198223 let delete_resp = test:: call_service ( & app, delete_file_req) . await ;
199224
@@ -203,13 +228,17 @@ mod tests {
203228 let list_files_after_deletion_req = test:: TestRequest :: get ( )
204229 . uri ( & format ! ( "/api/groups/{}/repos/{}/media" , group_id, repo_id) )
205230 . to_request ( ) ;
206- let list_files_after_deletion_resp: serde_json:: Value = test:: call_and_read_body_json ( & app, list_files_after_deletion_req) . await ;
231+ let list_files_after_deletion_resp: serde_json:: Value =
232+ test:: call_and_read_body_json ( & app, list_files_after_deletion_req) . await ;
207233
208234 assert ! (
209- list_files_after_deletion_resp. as_array( ) . unwrap( ) . is_empty( ) ,
235+ list_files_after_deletion_resp
236+ . as_array( )
237+ . unwrap( )
238+ . is_empty( ) ,
210239 "File list should be empty after file deletion"
211240 ) ;
212-
241+
213242 // Clean up: Stop the backend
214243 {
215244 let backend = get_backend ( ) . await ?;
0 commit comments