fix integration tests for post routes

This commit is contained in:
Manuel Gugger 2022-08-28 20:26:10 +02:00
parent a7352e5793
commit f37f3736a7
4 changed files with 161 additions and 30 deletions

65
.vscode/launch.json vendored Normal file
View File

@ -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}"
}
]
}

View File

@ -85,8 +85,8 @@ impl From<String> 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", ":"),
);
}
}

View File

@ -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::<super::AppState>())
.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");
}
}

View File

@ -48,7 +48,7 @@ pub fn create_actix_admin_builder() -> ActixAdminBuilder {
admin_builder.add_entity::<AppState, Comment>(&comment_view_model);
admin_builder.add_custom_handler_for_entity::<AppState, Comment>(
"/create_comment_from_plaintext",
"/create_post_from_plaintext",
web::post().to(create_post_from_plaintext::<AppState, Comment>));
admin_builder.add_custom_handler_for_entity::<AppState, Post>(
@ -60,7 +60,7 @@ pub fn create_actix_admin_builder() -> ActixAdminBuilder {
web::post().to(edit_post_from_plaintext::<AppState, Post>));
admin_builder.add_custom_handler_for_entity::<AppState, Comment>(
"/edit_comment_from_plaintext/{id}",
"/edit_post_from_plaintext/{id}",
web::post().to(edit_post_from_plaintext::<AppState, Comment>));
admin_builder