diff --git a/src/service/static_files.rs b/src/service/static_files.rs index 46215780f7a25b15e7328ec3ad26090095718380..898a5fac73cb89948e95ccc97361e63aef06c58f 100644 --- a/src/service/static_files.rs +++ b/src/service/static_files.rs @@ -145,7 +145,11 @@ async fn delete_static_file( } }; - match remove_file(app_state.env.public_dir.join(&fbasedir).join(&fpath)) { + match remove_file( + StaticFilesIndex::get_public_dir(&app_state.env) + .join(&fbasedir) + .join(&fpath), + ) { Ok(_) => { let indexed_url = std::path::PathBuf::from("/").join(&fbasedir).join(&fpath); let mut files_index = static_files_index.lock().unwrap(); @@ -326,12 +330,14 @@ mod test_static_files { let pathes_from_public = pathes .iter() .map(|p| { - std::path::Path::new(p) - .strip_prefix(&public_dir) - .unwrap() - .to_str() - .unwrap() - .to_owned() + format!( + "/{}", + std::path::Path::new(p) + .strip_prefix(&public_dir) + .unwrap() + .to_str() + .unwrap() + ) }) .collect::<Vec<String>>(); @@ -344,7 +350,68 @@ mod test_static_files { assert_eq!(f, "test"); let f = std::fs::read_to_string(iter_pathes.next().unwrap()).unwrap(); assert_eq!(f, "data"); + clear_testing_static(); + } + #[tokio::test] + async fn test_delete_file() { + let app_state = AppState::for_test().await; + + let admin_user = Administrator::authenticated( + &app_state, + AdminAuthCredentials { + username: app_state.env.default_admin_username.to_owned(), + password: app_state.env.default_admin_password.to_owned(), + }, + ) + .await + .unwrap(); + + let static_files_index = create_files_index(&app_state); + + let mut app = test::init_service( + App::new() + .app_data(Data::new(app_state.clone())) + .app_data(Data::clone(&static_files_index)) + .app_data(Data::new(AuthenticatedAdminMiddleware::new( + "kuadrado-admin-auth", + ))) + .service(post_files) + .service(delete_static_file), + ) + .await; + + let auth_token = admin_user.auth_token.unwrap(); + + let req = test::TestRequest::with_uri("/post-files") + .method(Method::POST) + .header( + "Content-Type", + "multipart/form-data; boundary=\"abbc761f78ff4d7cb7573b5a23f96ef0\"", + ) + .cookie(get_auth_cookie( + "kuadrado-admin-auth", + app_state.encryption.decrypt(&auth_token).to_owned(), + )) + .set_payload(create_simple_request()) + .to_request(); + + let resp = test::call_service(&mut app, req).await; + let status = resp.status(); + + assert_eq!(status, StatusCode::OK); + + let req = test::TestRequest::with_uri("/delete-file/uploads/docs/test.txt") + .method(Method::DELETE) + .cookie(get_auth_cookie( + "kuadrado-admin-auth", + app_state.encryption.decrypt(&auth_token).to_owned(), + )) + .to_request(); + + let resp = test::call_service(&mut app, req).await; + let status = resp.status(); + assert_eq!(status, StatusCode::ACCEPTED); clear_testing_static(); } }