diff options
author | Bryan Newbold <bnewbold@robocracy.org> | 2018-05-25 18:28:36 -0700 |
---|---|---|
committer | Bryan Newbold <bnewbold@robocracy.org> | 2018-05-25 18:28:51 -0700 |
commit | 6da398b730bbc269769066a8181f07d84f954f44 (patch) | |
tree | 4e816ce9f027722fb7d0cb9c54afeeea96930425 /rust/tests | |
parent | 0a637859a2e0f0b2015cf567af3bfeab7bbd58da (diff) | |
download | fatcat-6da398b730bbc269769066a8181f07d84f954f44.tar.gz fatcat-6da398b730bbc269769066a8181f07d84f954f44.zip |
refactor tests (much cleaner)
Diffstat (limited to 'rust/tests')
-rw-r--r-- | rust/tests/test_api_server.rs | 364 |
1 files changed, 193 insertions, 171 deletions
diff --git a/rust/tests/test_api_server.rs b/rust/tests/test_api_server.rs index ac9a8dcc..ccc5c371 100644 --- a/rust/tests/test_api_server.rs +++ b/rust/tests/test_api_server.rs @@ -12,179 +12,192 @@ use iron::mime::Mime; use iron::{status, Headers}; use iron_test::{request, response}; -#[test] -fn test_basics() { +fn setup() -> ( + Headers, + fatcat_api::router::Router, + diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>, +) { let server = fatcat::test_server().unwrap(); + let conn = server.db_pool.get().expect("db_pool error"); let router = fatcat_api::router(server); + let mut headers = Headers::new(); + let mime: Mime = "application/json".parse().unwrap(); + headers.set(ContentType(mime)); + (headers, router, conn) +} + +fn check_response( + resp: iron::IronResult<iron::response::Response>, + want_status: status::Status, + in_body: Option<&str>, +) { + let resp = resp.unwrap(); + let status = resp.status; + let body = response::extract_body_to_string(resp); + println!("{}", body); + assert_eq!(status, Some(want_status)); + if let Some(thing) = in_body { + assert!(body.contains(thing)); + } +} + +#[test] +fn test_basics() { + let (headers, router, _conn) = setup(); + + check_response( + request::get( + "http://localhost:9411/v0/creator/f1f046a3-45c9-4b99-adce-000000000001", + headers.clone(), + &router, + ), + status::Ok, + Some("Grace Hopper"), + ); - let response = request::get( - "http://localhost:9411/v0/creator/f1f046a3-45c9-4b99-adce-000000000001", - Headers::new(), - &router, - ).unwrap(); - assert_eq!(response.status, Some(status::Ok)); - let body = response::extract_body_to_string(response); - assert!(body.contains("Grace Hopper")); - - let response = request::get( - "http://localhost:9411/v0/creator/f1f046a3-45c9-4b99-adce-999999999999", - Headers::new(), - &router, - ).unwrap(); - assert_eq!(response.status, Some(status::NotFound)); + check_response( + request::get( + "http://localhost:9411/v0/creator/f1f046a3-45c9-4b99-adce-999999999999", + headers.clone(), + &router, + ), + status::NotFound, + None, + ); } #[test] fn test_lookups() { - let server = fatcat::test_server().unwrap(); - let router = fatcat_api::router(server); + let (headers, router, _conn) = setup(); - let response = request::get( - "http://localhost:9411/v0/container/lookup?issn=1234-5678", - Headers::new(), - &router, - ).unwrap(); - assert_eq!(response.status, Some(status::Ok)); - let body = response::extract_body_to_string(response); - assert!(body.contains("Journal of Trivial Results")); - - let response = request::get( - "http://localhost:9411/v0/creator/lookup?orcid=0000-0003-2088-7465", - Headers::new(), - &router, - ).unwrap(); - assert_eq!(response.status, Some(status::Ok)); - let body = response::extract_body_to_string(response); - assert!(body.contains("Christine Moran")); + check_response( + request::get( + "http://localhost:9411/v0/container/lookup?issn=1234-5678", + headers.clone(), + &router, + ), + status::Ok, + Some("Journal of Trivial Results"), + ); + + check_response( + request::get( + "http://localhost:9411/v0/creator/lookup?orcid=0000-0003-2088-7465", + headers.clone(), + &router, + ), + status::Ok, + Some("Christine Moran"), + ); } #[test] fn test_post_container() { - let server = fatcat::test_server().unwrap(); - let router = fatcat_api::router(server); - let mut headers = Headers::new(); - let mime: Mime = "application/json".parse().unwrap(); - headers.set(ContentType(mime)); + let (headers, router, _conn) = setup(); - let response = request::post( - "http://localhost:9411/v0/container", - headers, - r#"{"name": "test journal"}"#, - &router, - ).unwrap(); - assert_eq!(response.status, Some(status::Created)); - let body = response::extract_body_to_string(response); - println!("{}", body); - //assert!(body.contains("test journal")); + check_response( + request::post( + "http://localhost:9411/v0/container", + headers, + r#"{"name": "test journal"}"#, + &router, + ), + status::Created, + None, + ); // TODO: "test journal" } #[test] fn test_post_file() { - let server = fatcat::test_server().unwrap(); - let router = fatcat_api::router(server); - let mut headers = Headers::new(); - let mime: Mime = "application/json".parse().unwrap(); - headers.set(ContentType(mime)); + let (headers, router, _conn) = setup(); - let response = request::post( - "http://localhost:9411/v0/file", - headers.clone(), - r#"{ }"#, - &router, - ).unwrap(); - let status = response.status; - let body = response::extract_body_to_string(response); - println!("{}", body); - assert_eq!(status, Some(status::Created)); - - let response = request::post( - "http://localhost:9411/v0/file", - headers, - r#"{"size": 76543, - "sha1": "f013d66c7f6817d08b7eb2a93e6d0440c1f3e7f8", - "url": "http://archive.org/asdf.txt", - "releases": [ - "f1f046a3-45c9-4b99-4444-000000000001", - "f1f046a3-45c9-4b99-4444-000000000002" - ] - }"#, - &router, - ).unwrap(); - let status = response.status; - let body = response::extract_body_to_string(response); - println!("{}", body); - assert_eq!(status, Some(status::Created)); - //assert!(body.contains("secret paper")); + check_response( + request::post( + "http://localhost:9411/v0/file", + headers.clone(), + r#"{ }"#, + &router, + ), + status::Created, + None, + ); + + check_response( + request::post( + "http://localhost:9411/v0/file", + headers, + r#"{"size": 76543, + "sha1": "f013d66c7f6817d08b7eb2a93e6d0440c1f3e7f8", + "url": "http://archive.org/asdf.txt", + "releases": [ + "f1f046a3-45c9-4b99-4444-000000000001", + "f1f046a3-45c9-4b99-4444-000000000002" + ] + }"#, + &router, + ), + status::Created, + None, + ); // TODO: "secret paper" } #[test] fn test_post_release() { - let server = fatcat::test_server().unwrap(); - let router = fatcat_api::router(server); - let mut headers = Headers::new(); - let mime: Mime = "application/json".parse().unwrap(); - headers.set(ContentType(mime)); + let (headers, router, _conn) = setup(); - let response = request::post( - "http://localhost:9411/v0/release", - headers.clone(), - // TODO: target_release_id - r#"{"title": "secret minimal paper", - "release_type": "journal-article", - "work_id": "f1f046a3-45c9-4b99-3333-000000000001" - }"#, - &router, - ).unwrap(); - let status = response.status; - let body = response::extract_body_to_string(response); - println!("{}", body); - assert_eq!(status, Some(status::Created)); - //assert!(body.contains("secret paper")); - // - let response = request::post( - "http://localhost:9411/v0/release", - headers, - // TODO: target_release_id - r#"{"title": "secret paper", - "release_type": "journal-article", - "doi": "10.1234/abcde.781231231239", - "volume": "439", - "pages": "1-399", - "issue": "IV", - "work_id": "f1f046a3-45c9-4b99-3333-000000000002", - "container_id": "f1f046a3-45c9-4b99-cccc-000000000001", - "refs": [{ - "index": 3, - "stub": "just a string" - },{ - "stub": "just a string" - }], - "contribs": [{ - "index": 1, - "creator_stub": "textual description of contributor (aka, name)", - "creator_id": "f1f046a3-45c9-4b99-adce-000000000001", - "contrib_type": "author" - },{ - "creator_stub": "shorter" - }] - }"#, - &router, - ).unwrap(); - let status = response.status; - let body = response::extract_body_to_string(response); - println!("{}", body); - assert_eq!(status, Some(status::Created)); - //assert!(body.contains("secret paper")); + check_response( + request::post( + "http://localhost:9411/v0/release", + headers.clone(), + // TODO: target_release_id + r#"{"title": "secret minimal paper", + "release_type": "journal-article", + "work_id": "f1f046a3-45c9-4b99-3333-000000000001" + }"#, + &router, + ), + status::Created, + None, + ); // TODO: "secret paper" + + check_response( + request::post( + "http://localhost:9411/v0/release", + headers, + // TODO: target_release_id + r#"{"title": "secret paper", + "release_type": "journal-article", + "doi": "10.1234/abcde.781231231239", + "volume": "439", + "pages": "1-399", + "issue": "IV", + "work_id": "f1f046a3-45c9-4b99-3333-000000000002", + "container_id": "f1f046a3-45c9-4b99-cccc-000000000001", + "refs": [{ + "index": 3, + "stub": "just a string" + },{ + "stub": "just a string" + }], + "contribs": [{ + "index": 1, + "creator_stub": "textual description of contributor (aka, name)", + "creator_id": "f1f046a3-45c9-4b99-adce-000000000001", + "contrib_type": "author" + },{ + "creator_stub": "shorter" + }] + }"#, + &router, + ), + status::Created, + None, + ); // TODO: "secret paper" } #[test] fn test_accept_editgroup() { - let server = fatcat::test_server().unwrap(); - let conn = server.db_pool.get().expect("db_pool error"); - let router = fatcat_api::router(server); - let mut headers = Headers::new(); - let mime: Mime = "application/json".parse().unwrap(); - headers.set(ContentType(mime)); + let (headers, router, conn) = setup(); let editgroup_id = get_or_create_editgroup(1, &conn).unwrap(); @@ -201,26 +214,32 @@ fn test_accept_editgroup() { .unwrap(); assert_eq!(c, 0); - let response = request::post( - "http://localhost:9411/v0/container", - headers.clone(), - &format!( - "{{\"name\": \"test journal 1\", \"editgroup_id\": {}}}", - editgroup_id + check_response( + request::post( + "http://localhost:9411/v0/container", + headers.clone(), + &format!( + "{{\"name\": \"test journal 1\", \"editgroup_id\": {}}}", + editgroup_id + ), + &router, ), - &router, - ).unwrap(); - assert_eq!(response.status, Some(status::Created)); - let response = request::post( - "http://localhost:9411/v0/container", - headers.clone(), - &format!( - "{{\"name\": \"test journal 2\", \"editgroup_id\": {}}}", - editgroup_id + status::Created, + None, + ); + check_response( + request::post( + "http://localhost:9411/v0/container", + headers.clone(), + &format!( + "{{\"name\": \"test journal 2\", \"editgroup_id\": {}}}", + editgroup_id + ), + &router, ), - &router, - ).unwrap(); - assert_eq!(response.status, Some(status::Created)); + status::Created, + None, + ); let c: i64 = container_ident::table .filter(container_ident::is_live.eq(false)) @@ -229,13 +248,16 @@ fn test_accept_editgroup() { .unwrap(); assert_eq!(c, 2); - let response = request::post( - &format!("http://localhost:9411/v0/editgroup/{}/accept", editgroup_id), - headers.clone(), - "", - &router, - ).unwrap(); - assert_eq!(response.status, Some(status::Ok)); + check_response( + request::post( + &format!("http://localhost:9411/v0/editgroup/{}/accept", editgroup_id), + headers.clone(), + "", + &router, + ), + status::Ok, + None, + ); let c: i64 = container_ident::table .filter(container_ident::is_live.eq(false)) |