@@ -20,11 +20,10 @@ pub mod utils;
2020#[ cfg( test) ]
2121mod tests {
2222 use super :: * ;
23- use crate :: media:: { download_file, scope, upload_file} ;
24- use crate :: models:: GroupRepoPath ;
2523 use actix_web:: { test, web, App } ;
2624 use anyhow:: Result ;
27- use models:: { RequestName , SnowbirdFile , SnowbirdGroup , SnowbirdRepo } ;
25+ use models:: { RequestName , RequestUrl , SnowbirdFile , SnowbirdGroup , SnowbirdRepo } ;
26+ use save_dweb_backend:: { common:: DHTEntity , constants:: TEST_GROUP_NAME } ;
2827 use serde:: { Deserialize , Serialize } ;
2928 use serde_json:: json;
3029 use server:: server:: { get_backend, init_backend, status, BACKEND } ;
@@ -236,4 +235,162 @@ mod tests {
236235
237236 Ok ( ( ) )
238237 }
238+
239+ #[ actix_web:: test]
240+ async fn test_join_group ( ) -> Result < ( ) > {
241+ // Initialize the app
242+ let path = TmpDir :: new ( "test_api_repo_file_operations" ) . await ?;
243+
244+ BACKEND . get_or_init ( || init_backend ( path. to_path_buf ( ) . as_path ( ) ) ) ;
245+ {
246+ let backend = get_backend ( ) . await ?;
247+ backend. start ( ) . await . expect ( "Backend failed to start" ) ;
248+ }
249+
250+ let app = test:: init_service (
251+ App :: new ( )
252+ . service ( status)
253+ . service ( web:: scope ( "/api" ) . service ( groups:: scope ( ) ) ) ,
254+ )
255+ . await ;
256+
257+ let store2 = iroh_blobs:: store:: fs:: Store :: load ( path. to_path_buf ( ) . join ( "iroh2" ) ) . await ?;
258+ let ( veilid_api2, update_rx2) = save_dweb_backend:: common:: init_veilid (
259+ path. to_path_buf ( ) . join ( "test2" ) . as_path ( ) ,
260+ "test2" . to_string ( ) ,
261+ )
262+ . await ?;
263+ let backend2 = save_dweb_backend:: backend:: Backend :: from_dependencies (
264+ & path. to_path_buf ( ) ,
265+ veilid_api2. clone ( ) ,
266+ update_rx2,
267+ store2,
268+ )
269+ . await
270+ . unwrap ( ) ;
271+
272+ let mut group = backend2. create_group ( ) . await ?;
273+
274+ group. set_name ( TEST_GROUP_NAME ) . await ?;
275+
276+ let repo = group. create_repo ( ) . await ?;
277+ repo. set_name ( TEST_GROUP_NAME ) . await ?;
278+
279+ // Step 1: Create a group via the API
280+ let join_group_req = test:: TestRequest :: post ( )
281+ . uri ( "/api/groups/join_from_url" )
282+ . set_json ( RequestUrl {
283+ url : group. get_url ( ) ,
284+ } )
285+ . to_request ( ) ;
286+ let join_group_resp = test:: call_service ( & app, join_group_req) . await ;
287+
288+ assert ! ( join_group_resp. status( ) . is_success( ) ) ;
289+
290+ let joined_group: SnowbirdGroup = test:: read_body_json ( join_group_resp) . await ;
291+
292+ assert_eq ! (
293+ joined_group. name,
294+ Some ( TEST_GROUP_NAME . to_string( ) ) ,
295+ "Joined group has expected name"
296+ ) ;
297+
298+ let groups_req = test:: TestRequest :: default ( ) . uri ( "/api/groups" ) . to_request ( ) ;
299+ let groups_resp = test:: call_service ( & app, groups_req) . await ;
300+
301+ assert ! ( groups_resp. status( ) . is_success( ) , "Group join successful" ) ;
302+
303+ let groups: GroupsResponse = test:: read_body_json ( groups_resp) . await ;
304+
305+ assert_eq ! ( groups. groups. len( ) , 1 ) ;
306+
307+ let req = test:: TestRequest :: default ( )
308+ . uri ( format ! ( "/api/groups/{}/repos" , joined_group. key) . as_str ( ) )
309+ . to_request ( ) ;
310+ let resp: ReposResponse = test:: call_and_read_body_json ( & app, req) . await ;
311+
312+ assert_eq ! ( resp. repos. len( ) , 1 , "Should have 1 repo after joining" ) ;
313+
314+ backend2. stop ( ) . await ?;
315+ {
316+ let backend = get_backend ( ) . await ?;
317+ backend. stop ( ) . await . expect ( "Backend failed to stop" ) ;
318+ }
319+
320+ Ok ( ( ) )
321+ }
322+
323+ #[ actix_web:: test]
324+ async fn test_replicate_group ( ) -> Result < ( ) > {
325+ // Initialize the app
326+ let path = TmpDir :: new ( "test_api_repo_file_operations" ) . await ?;
327+
328+ let store2 = iroh_blobs:: store:: fs:: Store :: load ( path. to_path_buf ( ) . join ( "iroh2" ) ) . await ?;
329+ let ( veilid_api2, update_rx2) = save_dweb_backend:: common:: init_veilid (
330+ path. to_path_buf ( ) . join ( "test2" ) . as_path ( ) ,
331+ "test2" . to_string ( ) ,
332+ )
333+ . await ?;
334+ let backend2 = save_dweb_backend:: backend:: Backend :: from_dependencies (
335+ & path. to_path_buf ( ) ,
336+ veilid_api2. clone ( ) ,
337+ update_rx2,
338+ store2,
339+ )
340+ . await
341+ . unwrap ( ) ;
342+
343+ let mut group = backend2. create_group ( ) . await ?;
344+
345+ let join_url = group. get_url ( ) ;
346+
347+ group. set_name ( TEST_GROUP_NAME ) . await ?;
348+
349+ let repo = group. create_repo ( ) . await ?;
350+ repo. set_name ( TEST_GROUP_NAME ) . await ?;
351+
352+ // Step 3: Upload a file to the repository
353+ let file_name = "example.txt" ;
354+ let file_content = b"Test content for file upload" ;
355+
356+ repo. upload ( & file_name, file_content. to_vec ( ) ) . await ?;
357+
358+ BACKEND . get_or_init ( || init_backend ( path. to_path_buf ( ) . as_path ( ) ) ) ;
359+ {
360+ let backend = get_backend ( ) . await ?;
361+ backend. start ( ) . await . expect ( "Backend failed to start" ) ;
362+ }
363+
364+ let app = test:: init_service (
365+ App :: new ( )
366+ . service ( status)
367+ . service ( web:: scope ( "/api" ) . service ( groups:: scope ( ) ) ) ,
368+ )
369+ . await ;
370+
371+ {
372+ let backend = get_backend ( ) . await ?;
373+ backend. join_from_url ( join_url. as_str ( ) ) . await ?;
374+ }
375+
376+ let get_file_req = test:: TestRequest :: get ( )
377+ . uri ( & format ! (
378+ "/api/groups/{}/repos/{}/media/{}" ,
379+ group. id( ) . to_string( ) ,
380+ repo. id( ) . to_string( ) ,
381+ file_name
382+ ) )
383+ . to_request ( ) ;
384+ let get_file_resp = test:: call_service ( & app, get_file_req) . await ;
385+ assert ! ( get_file_resp. status( ) . is_success( ) , "File download failed" ) ;
386+
387+ let got_file_data = test:: read_body ( get_file_resp) . await ;
388+ assert_eq ! (
389+ got_file_data. to_vec( ) . as_slice( ) ,
390+ file_content,
391+ "Downloaded back file content"
392+ ) ;
393+
394+ Ok ( ( ) )
395+ }
239396}
0 commit comments