aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIlion Beyst <ilion.beyst@gmail.com>2022-10-05 21:14:09 +0200
committerIlion Beyst <ilion.beyst@gmail.com>2022-10-05 21:14:09 +0200
commit9825d2c49efcadb8c6ead09d679449c1c06fad88 (patch)
treed4bdceee2089f307f91c544a773a227b16898c64
parentdf44ef055e671b9aae2d431f5bcb12d4f4f97e6c (diff)
downloadplanetwars.dev-9825d2c49efcadb8c6ead09d679449c1c06fad88.tar.xz
planetwars.dev-9825d2c49efcadb8c6ead09d679449c1c06fad88.zip
full integration test: sign up, create bot, play match
-rw-r--r--planetwars-server/tests/integration.rs102
1 files changed, 102 insertions, 0 deletions
diff --git a/planetwars-server/tests/integration.rs b/planetwars-server/tests/integration.rs
index bef6a44..ad63e0e 100644
--- a/planetwars-server/tests/integration.rs
+++ b/planetwars-server/tests/integration.rs
@@ -212,3 +212,105 @@ async fn test_submit_bot() -> io::Result<()> {
.expect("failed to get match result");
Ok(())
}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn test_sign_up_and_create_bot() -> io::Result<()> {
+ let test_app = TestApp::create().await.unwrap();
+ test_app
+ .with_db_conn(|db_conn| {
+ clear_database(db_conn);
+ setup_simple_fixture(db_conn, &test_app.config);
+ })
+ .await;
+
+ let mut app = create_pw_api(test_app.config, test_app.db_pool);
+
+ // Registration
+ let credentials = json!({
+ "username": "piepkonijn",
+ "password": "123geheim",
+ });
+ let response = app
+ .call(
+ Request::builder()
+ .method(http::Method::POST)
+ .header("Content-Type", "application/json")
+ .uri("/api/register")
+ .body(serde_json::to_vec(&credentials).unwrap().into())
+ .unwrap(),
+ )
+ .await
+ .unwrap();
+ assert_eq!(response.status(), StatusCode::OK);
+
+ // Login
+ let response = app
+ .call(
+ Request::builder()
+ .method(http::Method::POST)
+ .header("Content-Type", "application/json")
+ .uri("/api/login")
+ .body(serde_json::to_vec(&credentials).unwrap().into())
+ .unwrap(),
+ )
+ .await
+ .unwrap();
+ assert_eq!(response.status(), StatusCode::OK);
+ let session_token = response.headers()["Token"].to_str().unwrap().clone();
+
+ // save bot
+ let simplebot_code = std::fs::read_to_string("../simplebot/simplebot.py")
+ .expect("could not read simplebot code");
+
+ let payload = json!({
+ "bot_name": "testbot",
+ "code": simplebot_code,
+ });
+
+ let response = app
+ .call(
+ Request::builder()
+ .method(http::Method::POST)
+ .header("Content-Type", "application/json")
+ .header("Authorization", format!("Bearer {}", session_token))
+ .uri("/api/save_bot")
+ .body(serde_json::to_vec(&payload).unwrap().into())
+ .unwrap(),
+ )
+ .await
+ .unwrap();
+ assert_eq!(response.status(), StatusCode::OK);
+
+ // launch a match against the new bot
+ let payload = json!({
+ "code": simplebot_code,
+ // TODO: how can we test that this bot is acutally being selected?
+ "opponent_name": "testbot",
+ });
+ let response = app
+ .call(
+ Request::builder()
+ .method(http::Method::POST)
+ .header("Content-Type", "application/json")
+ .uri("/api/submit_bot")
+ .body(serde_json::to_vec(&payload).unwrap().into())
+ .unwrap(),
+ )
+ .await
+ .unwrap();
+
+ assert_eq!(response.status(), StatusCode::OK);
+ let body = hyper::body::to_bytes(response.into_body()).await.unwrap();
+ let resp: JsonValue = serde_json::from_slice(&body).unwrap();
+
+ let match_id = &resp["match"]["id"].as_i64().unwrap();
+ let _match_result = tokio::time::timeout(
+ Duration::from_secs(10),
+ poll_match_until_complete(&mut app, &match_id.to_string()),
+ )
+ .await
+ .expect("fetching match result timed out")
+ .expect("failed to get match result");
+
+ Ok(())
+}