Pour tout problème contactez-nous par mail : support@froggit.fr | La FAQ :grey_question: | Rejoignez-nous sur le Chat :speech_balloon:

Skip to content
Snippets Groups Projects
Commit d68e0cef authored by Pierre Jarriges's avatar Pierre Jarriges
Browse files

test delete file

parent 78cf61a1
No related branches found
No related tags found
1 merge request!9Upload files
...@@ -145,7 +145,11 @@ async fn delete_static_file( ...@@ -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(_) => { Ok(_) => {
let indexed_url = std::path::PathBuf::from("/").join(&fbasedir).join(&fpath); let indexed_url = std::path::PathBuf::from("/").join(&fbasedir).join(&fpath);
let mut files_index = static_files_index.lock().unwrap(); let mut files_index = static_files_index.lock().unwrap();
...@@ -326,12 +330,14 @@ mod test_static_files { ...@@ -326,12 +330,14 @@ mod test_static_files {
let pathes_from_public = pathes let pathes_from_public = pathes
.iter() .iter()
.map(|p| { .map(|p| {
std::path::Path::new(p) format!(
.strip_prefix(&public_dir) "/{}",
.unwrap() std::path::Path::new(p)
.to_str() .strip_prefix(&public_dir)
.unwrap() .unwrap()
.to_owned() .to_str()
.unwrap()
)
}) })
.collect::<Vec<String>>(); .collect::<Vec<String>>();
...@@ -344,7 +350,68 @@ mod test_static_files { ...@@ -344,7 +350,68 @@ mod test_static_files {
assert_eq!(f, "test"); assert_eq!(f, "test");
let f = std::fs::read_to_string(iter_pathes.next().unwrap()).unwrap(); let f = std::fs::read_to_string(iter_pathes.next().unwrap()).unwrap();
assert_eq!(f, "data"); 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(); clear_testing_static();
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment