diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..905c100 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,65 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "lldb", + "request": "launch", + "name": "Debug unit tests in library 'actix_admin'", + "cargo": { + "args": [ + "test", + "--no-run", + "--lib", + "--package=actix_admin" + ], + "filter": { + "name": "actix_admin", + "kind": "lib" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + }, + { + "type": "lldb", + "request": "launch", + "name": "Debug integration test 'integration_test_get_resp_is_success'", + "cargo": { + "args": [ + "test", + "--no-run", + "--test=integration_test_get_resp_is_success", + "--package=actix_admin" + ], + "filter": { + "name": "integration_test_get_resp_is_success", + "kind": "test" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + }, + { + "type": "lldb", + "request": "launch", + "name": "Debug integration test 'integration_test_post_resp_is_success'", + "cargo": { + "args": [ + "test", + "--no-run", + "--test=integration_test_post_resp_is_success", + "--package=actix_admin" + ], + "filter": { + "name": "integration_test_post_resp_is_success", + "kind": "test" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + } + ] +} \ No newline at end of file diff --git a/src/routes/create_or_edit_post.rs b/src/routes/create_or_edit_post.rs index 5007c7e..b2500a5 100644 --- a/src/routes/create_or_edit_post.rs +++ b/src/routes/create_or_edit_post.rs @@ -85,8 +85,8 @@ impl From for ActixAdminModel { if !key_value.is_empty() { let mut iter = key_value.splitn(2, '='); hashmap.insert( - iter.next().unwrap().to_string(), - iter.next().unwrap().to_string(), + iter.next().unwrap().to_string().replace("%3A", ":"), + iter.next().unwrap().to_string().replace("%3A", ":"), ); } } diff --git a/tests/integration_test_post_resp_is_success.rs b/tests/integration_test_post_resp_is_success.rs index 35d2e77..18a1499 100644 --- a/tests/integration_test_post_resp_is_success.rs +++ b/tests/integration_test_post_resp_is_success.rs @@ -9,38 +9,20 @@ mod tests { use actix_web::http::header::ContentType; use actix_web::test; use actix_web::{middleware, web, App}; + use serde::{Serialize}; + use sea_orm::EntityTrait; + use sea_orm::PaginatorTrait; #[actix_web::test] - async fn comment_create_post() { - test_post_is_success("/admin/comment/create_post_from_plaintext").await - } - - #[actix_web::test] - async fn post_create_post() { - test_post_is_success("/admin/post/create_post_from_plaintext").await - } - - #[actix_web::test] - async fn post_edit_post() { - test_post_is_success("/admin/post/edit_post_from_plaintext").await - } - - #[actix_web::test] - async fn comment_edit_post() { - test_post_is_success("/admin/comment/edit_post_from_plaintext").await - } - - async fn test_post_is_success(url: &str) { + async fn comment_create_and_edit() { let conn = super::create_tables_and_get_connection().await; - let actix_admin_builder = super::create_actix_admin_builder(); let actix_admin = actix_admin_builder.get_actix_admin(); - let app_state = super::AppState { db: conn, - actix_admin: actix_admin, + actix_admin, }; - + let app = test::init_service( App::new() .app_data(web::Data::new(app_state.clone())) @@ -49,12 +31,96 @@ mod tests { ) .await; + #[derive(Serialize)] + pub struct CommentModel { + id: &'static str, + insert_date: &'static str, + comment: &'static str, + user: &'static str, + is_visible: &'static str, + post_id: Option<&'static str>, + my_decimal: &'static str + } + + let model = CommentModel { + id: "0", + insert_date: "1977-04-01T14:00", + comment: "test", + user: "test", + is_visible: "true", + post_id: None, + my_decimal: "113.141" // must be larger than 100 + }; + let req = test::TestRequest::post() .insert_header(ContentType::form_url_encoded()) - .uri(url) + .uri("/admin/comment/create_post_from_plaintext") + .set_form(model) .to_request(); let resp = test::call_service(&app, req).await; - assert!(resp.status().is_success()); + assert!(resp.status().is_redirection()); + + let entities = super::test_setup::Comment::find() + .paginate(&app_state.db, 50) + .fetch_page(0) + .await + .expect("could not retrieve entities"); + + assert!(entities.len() == 1, "After post, db does not contain 1 model"); + + } + + #[actix_web::test] + async fn post_create_and_edit() { + let conn = super::create_tables_and_get_connection().await; + let actix_admin_builder = super::create_actix_admin_builder(); + let actix_admin = actix_admin_builder.get_actix_admin(); + let app_state = super::AppState { + db: conn, + actix_admin, + }; + + let app = test::init_service( + App::new() + .app_data(web::Data::new(app_state.clone())) + .service(actix_admin_builder.get_scope::()) + .wrap(middleware::Logger::default()), + ) + .await; + + #[derive(Serialize)] + pub struct PostModel { + id: &'static str, + title: &'static str, + text: &'static str, + tea_mandatory: &'static str, + insert_date: &'static str, + } + + let model = PostModel { + id: "0", + insert_date: "1977-04-01", + title: "test", + text: "test", + tea_mandatory: "EverydayTea" + }; + + let req = test::TestRequest::post() + .insert_header(ContentType::form_url_encoded()) + .uri("/admin/post/create_post_from_plaintext") + .set_form(model) + .to_request(); + let resp = test::call_service(&app, req).await; + + assert!(resp.status().is_redirection()); + + let entities = super::test_setup::Post::find() + .paginate(&app_state.db, 50) + .fetch_page(0) + .await + .expect("could not retrieve entities"); + + assert!(entities.len() == 1, "After post, db does not contain 1 model"); } } diff --git a/tests/test_setup/helper.rs b/tests/test_setup/helper.rs index 422fc6c..4327ebe 100644 --- a/tests/test_setup/helper.rs +++ b/tests/test_setup/helper.rs @@ -48,7 +48,7 @@ pub fn create_actix_admin_builder() -> ActixAdminBuilder { admin_builder.add_entity::(&comment_view_model); admin_builder.add_custom_handler_for_entity::( - "/create_comment_from_plaintext", + "/create_post_from_plaintext", web::post().to(create_post_from_plaintext::)); admin_builder.add_custom_handler_for_entity::( @@ -60,7 +60,7 @@ pub fn create_actix_admin_builder() -> ActixAdminBuilder { web::post().to(edit_post_from_plaintext::)); admin_builder.add_custom_handler_for_entity::( - "/edit_comment_from_plaintext/{id}", + "/edit_post_from_plaintext/{id}", web::post().to(edit_post_from_plaintext::)); admin_builder