-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
261 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
use actix_web::test::{init_service, read_body_json, TestRequest}; | ||
use actix_web::App; | ||
use serde_json::Value; | ||
|
||
use api_ittoku_tech::db::post; | ||
use api_ittoku_tech::routes; | ||
|
||
mod utils; | ||
|
||
use utils::prelude::*; | ||
|
||
#[actix_web::test] | ||
async fn post_list() { | ||
let app = init_service( | ||
App::new() | ||
.app_data(app_state().await) | ||
.configure(routes::init), | ||
) | ||
.await; | ||
let resp = TestRequest::get().uri("/posts").send_request(&app).await; | ||
assert!(resp.status().is_success()); | ||
} | ||
|
||
#[actix_web::test] | ||
async fn post_create() { | ||
let app = init_service( | ||
App::new() | ||
.app_data(app_state().await) | ||
.configure(routes::init), | ||
) | ||
.await; | ||
// Empty post data | ||
let post_data = NEW_POST_EMPTY.to_owned(); | ||
let resp = TestRequest::post() | ||
.uri("/posts") | ||
.set_json(post_data) | ||
.send_request(&app) | ||
.await; | ||
assert!(resp.status().is_client_error()); | ||
let resp: Value = read_body_json(resp).await; | ||
let resp_empty = RESP_POST_EMPTY.to_owned(); | ||
assert_eq!(resp, resp_empty); | ||
// Too long post data | ||
let post_data = NEW_POST_TOO_LONG.to_owned(); | ||
let resp = TestRequest::post() | ||
.uri("/posts") | ||
.set_json(post_data) | ||
.send_request(&app) | ||
.await; | ||
assert!(resp.status().is_client_error()); | ||
let resp: Value = read_body_json(resp).await; | ||
let resp_too_long = RESP_POST_TOO_LONG.to_owned(); | ||
assert_eq!(resp, resp_too_long); | ||
// success to create post | ||
let post_data = NEW_POST.to_owned(); | ||
let resp = TestRequest::post() | ||
.uri("/posts") | ||
.set_json(&post_data) | ||
.send_request(&app) | ||
.await; | ||
assert!(resp.status().is_success()); | ||
let resp: Value = read_body_json(resp).await; | ||
assert_eq!(resp["title"], post_data["title"]); | ||
delete_post(resp["id"].as_i64().unwrap() as i32).await; | ||
} | ||
|
||
#[actix_web::test] | ||
async fn post_detail() { | ||
let app = init_service( | ||
App::new() | ||
.app_data(app_state().await) | ||
.configure(routes::init), | ||
) | ||
.await; | ||
let post = create_post().await; | ||
// post not found | ||
let resp = TestRequest::get() | ||
.uri("/posts/hoge") | ||
.send_request(&app) | ||
.await; | ||
assert!(resp.status().is_client_error()); | ||
let resp_not_found = RESP_NOTFOUND.to_owned(); | ||
let resp: Value = read_body_json(resp).await; | ||
assert_eq!(resp, resp_not_found); | ||
// success to get post | ||
let resp = TestRequest::get() | ||
.uri(&format!("/posts/{}", post.id)) | ||
.send_request(&app) | ||
.await; | ||
assert!(resp.status().is_success()); | ||
let resp: post::Model = read_body_json(resp).await; | ||
assert_eq!(resp, post); | ||
delete_post(resp.id).await; | ||
} | ||
|
||
#[actix_web::test] | ||
async fn post_update() { | ||
let app = init_service( | ||
App::new() | ||
.app_data(app_state().await) | ||
.configure(routes::init), | ||
) | ||
.await; | ||
let post = create_post().await; | ||
// Empty post data | ||
let post_data = NEW_POST_EMPTY.to_owned(); | ||
let resp = TestRequest::patch() | ||
.uri(&format!("/posts/{}", post.id)) | ||
.set_json(post_data) | ||
.send_request(&app) | ||
.await; | ||
assert!(resp.status().is_client_error()); | ||
let resp: Value = read_body_json(resp).await; | ||
let resp_empty = RESP_POST_EMPTY.to_owned(); | ||
assert_eq!(resp, resp_empty); | ||
// Too long post data | ||
let post_data = NEW_POST_TOO_LONG.to_owned(); | ||
let resp = TestRequest::patch() | ||
.uri(&format!("/posts/{}", post.id)) | ||
.set_json(post_data) | ||
.send_request(&app) | ||
.await; | ||
assert!(resp.status().is_client_error()); | ||
let resp: Value = read_body_json(resp).await; | ||
let resp_too_long = RESP_POST_TOO_LONG.to_owned(); | ||
assert_eq!(resp, resp_too_long); | ||
// post not found | ||
let post_data = NEW_POST_UPDATED.to_owned(); | ||
let resp = TestRequest::patch() | ||
.uri("/posts/hoge") | ||
.set_json(&post_data) | ||
.send_request(&app) | ||
.await; | ||
assert!(resp.status().is_client_error()); | ||
let resp_not_found = RESP_NOTFOUND.to_owned(); | ||
let resp: Value = read_body_json(resp).await; | ||
assert_eq!(resp, resp_not_found); | ||
// success to update post | ||
let resp = TestRequest::patch() | ||
.uri(&format!("/posts/{}", post.id)) | ||
.set_json(&post_data) | ||
.send_request(&app) | ||
.await; | ||
assert!(resp.status().is_success()); | ||
let resp: Value = read_body_json(resp).await; | ||
assert_eq!(resp["title"], post_data["title"]); | ||
delete_post(resp["id"].as_i64().unwrap() as i32).await; | ||
} | ||
|
||
#[actix_web::test] | ||
async fn post_delete() { | ||
let app = init_service( | ||
App::new() | ||
.app_data(app_state().await) | ||
.configure(routes::init), | ||
) | ||
.await; | ||
let post = create_post().await; | ||
// post not found | ||
let resp = TestRequest::delete() | ||
.uri("/posts/hoge") | ||
.send_request(&app) | ||
.await; | ||
assert!(resp.status().is_client_error()); | ||
let resp_not_found = RESP_NOTFOUND.to_owned(); | ||
let resp: Value = read_body_json(resp).await; | ||
assert_eq!(resp, resp_not_found); | ||
// success to delete post | ||
let resp = TestRequest::delete() | ||
.uri(&format!("/posts/{}", post.id)) | ||
.send_request(&app) | ||
.await; | ||
assert!(resp.status().is_success()); | ||
let resp: post::Model = read_body_json(resp).await; | ||
assert_eq!(resp, post); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,82 @@ | ||
use once_cell::sync::Lazy; | ||
use serde_json::{json, Value}; | ||
|
||
#[allow(dead_code)] | ||
pub static RESP_ROOT: Lazy<Value> = Lazy::new(|| { | ||
json!({ | ||
"post_detail_url": "http://0.0.0.0:8080/posts/{id}", | ||
"post_list_url": "http://0.0.0.0:8080/posts" | ||
}) | ||
}); | ||
|
||
#[allow(dead_code)] | ||
pub static RESP_NOTFOUND: Lazy<Value> = | ||
Lazy::new(|| json!({"status": 404, "message": "Not Found".to_string()})); | ||
|
||
#[allow(dead_code)] | ||
pub static RESP_INVALID_JSON: Lazy<Value> = | ||
Lazy::new(|| json!({"status": 400, "message": "Invalid JSON Data".to_string()})); | ||
|
||
#[allow(dead_code)] | ||
pub static NEW_POST: Lazy<Value> = Lazy::new(|| { | ||
json!({ | ||
"title": "test title", | ||
"text": "test text", | ||
}) | ||
}); | ||
|
||
#[allow(dead_code)] | ||
pub static NEW_POST_UPDATED: Lazy<Value> = Lazy::new(|| { | ||
json!({ | ||
"title": "test title updated", | ||
"text": "test text updated", | ||
}) | ||
}); | ||
|
||
#[allow(dead_code)] | ||
pub static NEW_POST_EMPTY: Lazy<Value> = Lazy::new(|| { | ||
json!({ | ||
"title": "", | ||
"text": "", | ||
}) | ||
}); | ||
|
||
#[allow(dead_code)] | ||
pub static NEW_POST_TOO_LONG: Lazy<Value> = Lazy::new(|| { | ||
json!({ | ||
"title": "x".repeat(257), | ||
"text": "x".repeat(65537), | ||
}) | ||
}); | ||
|
||
#[allow(dead_code)] | ||
pub static RESP_POST_EMPTY: Lazy<Value> = Lazy::new(|| { | ||
json!({ | ||
"errors": [ | ||
{ | ||
"status": 422, | ||
"message": "title cannot be empty", | ||
}, | ||
{ | ||
"status": 422, | ||
"message": "text cannot be empty", | ||
}, | ||
] | ||
}) | ||
}); | ||
|
||
#[allow(dead_code)] | ||
pub static RESP_POST_TOO_LONG: Lazy<Value> = Lazy::new(|| { | ||
json!({ | ||
"errors": [ | ||
{ | ||
"status": 422, | ||
"message": "title cannot be longer than 256 characters", | ||
}, | ||
{ | ||
"status": 422, | ||
"message": "text cannot be longer than 65536 characters", | ||
}, | ||
] | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters