diff options
-rw-r--r-- | backend/tests/bots.rs | 98 | ||||
-rw-r--r-- | backend/tests/login.rs | 61 | ||||
-rw-r--r-- | backend/tests/util/mod.rs | 59 | ||||
-rw-r--r-- | planetwars-server/Cargo.toml (renamed from backend/Cargo.toml) | 2 | ||||
-rw-r--r-- | planetwars-server/Rocket.toml (renamed from backend/Rocket.toml) | 0 | ||||
-rw-r--r-- | planetwars-server/diesel.toml (renamed from backend/diesel.toml) | 0 | ||||
-rw-r--r-- | planetwars-server/migrations/.gitkeep (renamed from backend/migrations/.gitkeep) | 0 | ||||
-rw-r--r-- | planetwars-server/migrations/00000000000000_diesel_initial_setup/down.sql (renamed from backend/migrations/00000000000000_diesel_initial_setup/down.sql) | 0 | ||||
-rw-r--r-- | planetwars-server/migrations/00000000000000_diesel_initial_setup/up.sql (renamed from backend/migrations/00000000000000_diesel_initial_setup/up.sql) | 0 | ||||
-rw-r--r-- | planetwars-server/migrations/2021-12-13-145111_users/down.sql (renamed from backend/migrations/2021-12-13-145111_users/down.sql) | 0 | ||||
-rw-r--r-- | planetwars-server/migrations/2021-12-13-145111_users/up.sql (renamed from backend/migrations/2021-12-13-145111_users/up.sql) | 0 | ||||
-rw-r--r-- | planetwars-server/migrations/2021-12-13-151129_sessions/down.sql (renamed from backend/migrations/2021-12-13-151129_sessions/down.sql) | 0 | ||||
-rw-r--r-- | planetwars-server/migrations/2021-12-13-151129_sessions/up.sql (renamed from backend/migrations/2021-12-13-151129_sessions/up.sql) | 0 | ||||
-rw-r--r-- | planetwars-server/migrations/2021-12-18-130837_bots/down.sql (renamed from backend/migrations/2021-12-18-130837_bots/down.sql) | 0 | ||||
-rw-r--r-- | planetwars-server/migrations/2021-12-18-130837_bots/up.sql (renamed from backend/migrations/2021-12-18-130837_bots/up.sql) | 0 | ||||
-rw-r--r-- | planetwars-server/src/db/bots.rs (renamed from backend/src/db/bots.rs) | 0 | ||||
-rw-r--r-- | planetwars-server/src/db/mod.rs (renamed from backend/src/db/mod.rs) | 0 | ||||
-rw-r--r-- | planetwars-server/src/db/sessions.rs (renamed from backend/src/db/sessions.rs) | 0 | ||||
-rw-r--r-- | planetwars-server/src/db/users.rs (renamed from backend/src/db/users.rs) | 0 | ||||
-rw-r--r-- | planetwars-server/src/lib.rs (renamed from backend/src/lib.rs) | 0 | ||||
-rw-r--r-- | planetwars-server/src/main.rs (renamed from backend/src/main.rs) | 4 | ||||
-rw-r--r-- | planetwars-server/src/routes/bots.rs (renamed from backend/src/routes/bots.rs) | 0 | ||||
-rw-r--r-- | planetwars-server/src/routes/mod.rs (renamed from backend/src/routes/mod.rs) | 0 | ||||
-rw-r--r-- | planetwars-server/src/routes/users.rs (renamed from backend/src/routes/users.rs) | 0 | ||||
-rw-r--r-- | planetwars-server/src/schema.rs (renamed from backend/src/schema.rs) | 0 |
25 files changed, 3 insertions, 221 deletions
diff --git a/backend/tests/bots.rs b/backend/tests/bots.rs deleted file mode 100644 index fe81712..0000000 --- a/backend/tests/bots.rs +++ /dev/null @@ -1,98 +0,0 @@ -#![feature(async_closure)] -extern crate mozaic4_backend; -extern crate zip; - -use rocket::http::{ContentType, Status}; - -mod util; -use mozaic4_backend::db::{bots, sessions, users}; -use mozaic4_backend::DbConn; -use sessions::Session; -use users::{Credentials, User}; -use util::{run_test, BearerAuth}; - -async fn user_with_session(conn: &DbConn) -> (User, Session) { - conn.run(|conn| { - let credentials = Credentials { - username: "piepkonijn", - password: "geheim123", - }; - let user = users::create_user(&credentials, conn).unwrap(); - let session = sessions::create_session(&user, conn); - (user, session) - }) - .await -} - -#[rocket::async_test] -async fn test_bot_create() { - run_test(async move |client, conn| { - let (user, session) = user_with_session(&conn).await; - - let response = client - .post("/bots") - .header(BearerAuth::new(session.token.clone())) - .header(ContentType::JSON) - .body( - r#"{ - "name": "testbot" - }"#, - ) - .dispatch() - .await; - - assert_eq!(response.status(), Status::Created); - assert_eq!(response.content_type(), Some(ContentType::JSON)); - - let resp_text = response.into_string().await.unwrap(); - let json: serde_json::Value = serde_json::from_str(&resp_text).unwrap(); - assert_eq!(json["name"], "testbot"); - assert_eq!(json["owner_id"], user.id); - }) - .await -} - -// create an example zipfile for bot upload -fn create_zip() -> std::io::Result<Vec<u8>> { - use std::io::Write; - use zip::write::FileOptions; - - let cursor = std::io::Cursor::new(Vec::new()); - let mut zip = zip::ZipWriter::new(cursor); - - zip.start_file("test.txt", FileOptions::default())?; - zip.write_all(b"sup brudi")?; - let buf = zip.finish()?; - Ok(buf.into_inner()) -} - -#[rocket::async_test] -async fn test_bot_upload() { - run_test(async move |client, conn| { - let (user, session) = user_with_session(&conn).await; - - let owner_id = user.id; - let bot = conn - .run(move |conn| { - let new_bot = bots::NewBot { - name: "testbot", - owner_id: owner_id, - }; - bots::create_bot(&new_bot, conn).unwrap() - }) - .await; - - let zip_file = create_zip().unwrap(); - - let response = client - .post(format!("/bots/{}/upload", bot.id)) - .header(BearerAuth::new(session.token.clone())) - .header(ContentType::JSON) - .body(zip_file) - .dispatch() - .await; - - assert_eq!(response.status(), Status::Created); - }) - .await -} diff --git a/backend/tests/login.rs b/backend/tests/login.rs deleted file mode 100644 index 60c5d6f..0000000 --- a/backend/tests/login.rs +++ /dev/null @@ -1,61 +0,0 @@ -#![feature(async_closure)] -extern crate mozaic4_backend; - -use rocket::http::{ContentType, Status}; - -mod util; -use util::run_test; - -#[rocket::async_test] -async fn test_registration() { - run_test(async move |client, _conn| { - let response = client - .post("/register") - .header(ContentType::JSON) - .body(r#"{"username": "piepkonijn", "password": "geheim123"}"#) - .dispatch() - .await; - - assert_eq!(response.status(), Status::Ok); - assert_eq!(response.content_type(), Some(ContentType::JSON)); - - let response = client - .post("/login") - .header(ContentType::JSON) - .body(r#"{"username": "piepkonijn", "password": "geheim123"}"#) - .dispatch() - .await; - - assert_eq!(response.status(), Status::Ok); - let token = response.into_string().await.unwrap(); - - let response = client - .get("/users/me") - .header(util::BearerAuth::new(token)) - .dispatch() - .await; - - assert_eq!(response.status(), Status::Ok); - assert_eq!(response.content_type(), Some(ContentType::JSON)); - let resp = response.into_string().await.unwrap(); - let json: serde_json::Value = serde_json::from_str(&resp).unwrap(); - assert_eq!(json["username"], "piepkonijn"); - }) - .await -} - -#[rocket::async_test] -async fn test_reject_invalid_credentials() { - run_test(async move |client, _conn| { - let response = client - .post("/login") - .header(ContentType::JSON) - .body(r#"{"username": "piepkonijn", "password": "letmeinplease"}"#) - .dispatch() - .await; - - assert_eq!(response.status(), Status::Forbidden); - // assert_eq!(response.content_type(), Some(ContentType::JSON)); - }) - .await -} diff --git a/backend/tests/util/mod.rs b/backend/tests/util/mod.rs deleted file mode 100644 index f34e9f3..0000000 --- a/backend/tests/util/mod.rs +++ /dev/null @@ -1,59 +0,0 @@ -use std::future::Future; - -use diesel::RunQueryDsl; -use mozaic4_backend::DbConn; -use rocket::{http::Header, local::asynchronous::Client}; - -// We use a lock to synchronize between tests so DB operations don't collide. -// For now. In the future, we'll have a nice way to run each test in a DB -// transaction so we can regain concurrency. -static DB_LOCK: parking_lot::Mutex<()> = parking_lot::const_mutex(()); - -async fn reset_db(db: &DbConn) { - db.run(|conn| { - diesel::sql_query( - r#" - TRUNCATE TABLE users, sessions, - bots, code_bundles"#, - ) - .execute(conn) - .expect("drop all tables"); - }) - .await -} - -pub async fn run_test<F, R>(test_closure: F) -where - F: FnOnce(Client, DbConn) -> R, - R: Future<Output = ()>, -{ - let _lock = DB_LOCK.lock(); - - let client = Client::untracked(mozaic4_backend::rocket()) - .await - .expect("failed to create test client"); - let db = mozaic4_backend::DbConn::get_one(client.rocket()) - .await - .expect("failed to get db connection"); - - // make sure we start with a clean DB - reset_db(&db).await; - - test_closure(client, db).await; -} - -pub struct BearerAuth { - token: String, -} - -impl BearerAuth { - pub fn new(token: String) -> Self { - Self { token } - } -} - -impl<'a> Into<Header<'a>> for BearerAuth { - fn into(self) -> Header<'a> { - Header::new("Authorization", format!("Bearer {}", self.token)) - } -} diff --git a/backend/Cargo.toml b/planetwars-server/Cargo.toml index de98df7..8fb7693 100644 --- a/backend/Cargo.toml +++ b/planetwars-server/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "mozaic4-backend" +name = "planetwars-server" version = "0.0.0" edition = "2021" diff --git a/backend/Rocket.toml b/planetwars-server/Rocket.toml index 40635de..40635de 100644 --- a/backend/Rocket.toml +++ b/planetwars-server/Rocket.toml diff --git a/backend/diesel.toml b/planetwars-server/diesel.toml index 92267c8..92267c8 100644 --- a/backend/diesel.toml +++ b/planetwars-server/diesel.toml diff --git a/backend/migrations/.gitkeep b/planetwars-server/migrations/.gitkeep index e69de29..e69de29 100644 --- a/backend/migrations/.gitkeep +++ b/planetwars-server/migrations/.gitkeep diff --git a/backend/migrations/00000000000000_diesel_initial_setup/down.sql b/planetwars-server/migrations/00000000000000_diesel_initial_setup/down.sql index a9f5260..a9f5260 100644 --- a/backend/migrations/00000000000000_diesel_initial_setup/down.sql +++ b/planetwars-server/migrations/00000000000000_diesel_initial_setup/down.sql diff --git a/backend/migrations/00000000000000_diesel_initial_setup/up.sql b/planetwars-server/migrations/00000000000000_diesel_initial_setup/up.sql index d68895b..d68895b 100644 --- a/backend/migrations/00000000000000_diesel_initial_setup/up.sql +++ b/planetwars-server/migrations/00000000000000_diesel_initial_setup/up.sql diff --git a/backend/migrations/2021-12-13-145111_users/down.sql b/planetwars-server/migrations/2021-12-13-145111_users/down.sql index 49285a1..49285a1 100644 --- a/backend/migrations/2021-12-13-145111_users/down.sql +++ b/planetwars-server/migrations/2021-12-13-145111_users/down.sql diff --git a/backend/migrations/2021-12-13-145111_users/up.sql b/planetwars-server/migrations/2021-12-13-145111_users/up.sql index f35e718..f35e718 100644 --- a/backend/migrations/2021-12-13-145111_users/up.sql +++ b/planetwars-server/migrations/2021-12-13-145111_users/up.sql diff --git a/backend/migrations/2021-12-13-151129_sessions/down.sql b/planetwars-server/migrations/2021-12-13-151129_sessions/down.sql index 54d1e93..54d1e93 100644 --- a/backend/migrations/2021-12-13-151129_sessions/down.sql +++ b/planetwars-server/migrations/2021-12-13-151129_sessions/down.sql diff --git a/backend/migrations/2021-12-13-151129_sessions/up.sql b/planetwars-server/migrations/2021-12-13-151129_sessions/up.sql index f8ec21b..f8ec21b 100644 --- a/backend/migrations/2021-12-13-151129_sessions/up.sql +++ b/planetwars-server/migrations/2021-12-13-151129_sessions/up.sql diff --git a/backend/migrations/2021-12-18-130837_bots/down.sql b/planetwars-server/migrations/2021-12-18-130837_bots/down.sql index 3d14604..3d14604 100644 --- a/backend/migrations/2021-12-18-130837_bots/down.sql +++ b/planetwars-server/migrations/2021-12-18-130837_bots/down.sql diff --git a/backend/migrations/2021-12-18-130837_bots/up.sql b/planetwars-server/migrations/2021-12-18-130837_bots/up.sql index 27f3582..27f3582 100644 --- a/backend/migrations/2021-12-18-130837_bots/up.sql +++ b/planetwars-server/migrations/2021-12-18-130837_bots/up.sql diff --git a/backend/src/db/bots.rs b/planetwars-server/src/db/bots.rs index bc9cb11..bc9cb11 100644 --- a/backend/src/db/bots.rs +++ b/planetwars-server/src/db/bots.rs diff --git a/backend/src/db/mod.rs b/planetwars-server/src/db/mod.rs index 947b789..947b789 100644 --- a/backend/src/db/mod.rs +++ b/planetwars-server/src/db/mod.rs diff --git a/backend/src/db/sessions.rs b/planetwars-server/src/db/sessions.rs index 96f3926..96f3926 100644 --- a/backend/src/db/sessions.rs +++ b/planetwars-server/src/db/sessions.rs diff --git a/backend/src/db/users.rs b/planetwars-server/src/db/users.rs index 663f173..663f173 100644 --- a/backend/src/db/users.rs +++ b/planetwars-server/src/db/users.rs diff --git a/backend/src/lib.rs b/planetwars-server/src/lib.rs index 665523f..665523f 100644 --- a/backend/src/lib.rs +++ b/planetwars-server/src/lib.rs diff --git a/backend/src/main.rs b/planetwars-server/src/main.rs index c75aaf6..9bd283e 100644 --- a/backend/src/main.rs +++ b/planetwars-server/src/main.rs @@ -1,11 +1,11 @@ use std::net::SocketAddr; -extern crate mozaic4_backend; +extern crate planetwars_server; extern crate tokio; #[tokio::main] async fn main() { - let app = mozaic4_backend::app().await; + let app = planetwars_server::app().await; let addr = SocketAddr::from(([127, 0, 0, 1], 9000)); diff --git a/backend/src/routes/bots.rs b/planetwars-server/src/routes/bots.rs index da09669..da09669 100644 --- a/backend/src/routes/bots.rs +++ b/planetwars-server/src/routes/bots.rs diff --git a/backend/src/routes/mod.rs b/planetwars-server/src/routes/mod.rs index 718d7ef..718d7ef 100644 --- a/backend/src/routes/mod.rs +++ b/planetwars-server/src/routes/mod.rs diff --git a/backend/src/routes/users.rs b/planetwars-server/src/routes/users.rs index fc77d7b..fc77d7b 100644 --- a/backend/src/routes/users.rs +++ b/planetwars-server/src/routes/users.rs diff --git a/backend/src/schema.rs b/planetwars-server/src/schema.rs index bf58434..bf58434 100644 --- a/backend/src/schema.rs +++ b/planetwars-server/src/schema.rs |