From eabeb7ed7b641dea0b8e71ab33ab97b4ed7a4cda Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Mon, 13 Dec 2021 22:41:20 +0100 Subject: start implementing basic login functionality --- backend/tests/common.rs | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 backend/tests/common.rs (limited to 'backend/tests') diff --git a/backend/tests/common.rs b/backend/tests/common.rs new file mode 100644 index 0000000..e69de29 -- cgit v1.2.3 From 13cdbc7ff760ae91ee3f62b2a2f62c7559ccaa3c Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Tue, 14 Dec 2021 20:23:07 +0100 Subject: test registration & login --- backend/tests/common.rs | 75 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) (limited to 'backend/tests') diff --git a/backend/tests/common.rs b/backend/tests/common.rs index e69de29..8ab68a1 100644 --- a/backend/tests/common.rs +++ b/backend/tests/common.rs @@ -0,0 +1,75 @@ +extern crate mozaic4_backend; + +use diesel; +use diesel::prelude::*; +use mozaic4_backend::DbConn; +use rocket::http::{ContentType, Header, Status}; +use rocket::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("TRUNCATE TABLE users, sessions") + .execute(conn) + .expect("drop all tables"); + }) + .await +} + +macro_rules! run_test { + (|$client:ident, $conn:ident| $block:expr) => {{ + let _lock = DB_LOCK.lock(); + + rocket::async_test(async move { + let $client = Client::tracked(mozaic4_backend::rocket()) + .await + .expect("Rocket client"); + let db = mozaic4_backend::DbConn::get_one($client.rocket()).await; + let $conn = db.expect("failed to get database connection for testing"); + reset_db(&$conn).await; + + $block + }) + }}; +} + +#[test] +fn test_registration() { + run_test!(|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(Header::new("Authorization", 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"); + }); +} -- cgit v1.2.3 From 6aa72b3c8717f32e62c772aeed327d3cd9a6fa65 Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Wed, 15 Dec 2021 22:40:55 +0100 Subject: gracefully handle invalid login credentials --- backend/tests/common.rs | 75 ---------------------------------- backend/tests/login.rs | 106 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 75 deletions(-) delete mode 100644 backend/tests/common.rs create mode 100644 backend/tests/login.rs (limited to 'backend/tests') diff --git a/backend/tests/common.rs b/backend/tests/common.rs deleted file mode 100644 index 8ab68a1..0000000 --- a/backend/tests/common.rs +++ /dev/null @@ -1,75 +0,0 @@ -extern crate mozaic4_backend; - -use diesel; -use diesel::prelude::*; -use mozaic4_backend::DbConn; -use rocket::http::{ContentType, Header, Status}; -use rocket::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("TRUNCATE TABLE users, sessions") - .execute(conn) - .expect("drop all tables"); - }) - .await -} - -macro_rules! run_test { - (|$client:ident, $conn:ident| $block:expr) => {{ - let _lock = DB_LOCK.lock(); - - rocket::async_test(async move { - let $client = Client::tracked(mozaic4_backend::rocket()) - .await - .expect("Rocket client"); - let db = mozaic4_backend::DbConn::get_one($client.rocket()).await; - let $conn = db.expect("failed to get database connection for testing"); - reset_db(&$conn).await; - - $block - }) - }}; -} - -#[test] -fn test_registration() { - run_test!(|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(Header::new("Authorization", 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"); - }); -} diff --git a/backend/tests/login.rs b/backend/tests/login.rs new file mode 100644 index 0000000..9c70af2 --- /dev/null +++ b/backend/tests/login.rs @@ -0,0 +1,106 @@ +extern crate mozaic4_backend; + +use diesel; +use diesel::prelude::*; +use mozaic4_backend::DbConn; +use rocket::http::{ContentType, Header, Status}; +use rocket::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("TRUNCATE TABLE users, sessions") + .execute(conn) + .expect("drop all tables"); + }) + .await +} + +macro_rules! run_test { + (|$client:ident, $conn:ident| $block:expr) => {{ + let _lock = DB_LOCK.lock(); + + rocket::async_test(async move { + let $client = Client::tracked(mozaic4_backend::rocket()) + .await + .expect("Rocket client"); + let db = mozaic4_backend::DbConn::get_one($client.rocket()).await; + let $conn = db.expect("failed to get database connection for testing"); + reset_db(&$conn).await; + + $block + }) + }}; +} + +pub struct BearerAuth { + token: String, +} + +impl BearerAuth { + pub fn new(token: String) -> Self { + Self { token } + } +} + +impl<'a> Into> for BearerAuth { + fn into(self) -> Header<'a> { + Header::new("Authorization", format!("Bearer {}", self.token)) + } +} + +#[test] +fn test_registration() { + run_test!(|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(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"); + }); +} + +#[test] +fn test_reject_invalid_credentials() { + run_test!(|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)); + }); +} -- cgit v1.2.3 From 2dbb085008f68ed56675cf23ea6e1c89af632ea9 Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Sat, 18 Dec 2021 15:39:05 +0100 Subject: use async closures for tests --- backend/tests/login.rs | 53 ++++++++++------------------------------------- backend/tests/util/mod.rs | 39 ++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 42 deletions(-) create mode 100644 backend/tests/util/mod.rs (limited to 'backend/tests') diff --git a/backend/tests/login.rs b/backend/tests/login.rs index 9c70af2..b4e07e3 100644 --- a/backend/tests/login.rs +++ b/backend/tests/login.rs @@ -1,41 +1,10 @@ +#![feature(async_closure)] extern crate mozaic4_backend; -use diesel; -use diesel::prelude::*; -use mozaic4_backend::DbConn; use rocket::http::{ContentType, Header, Status}; -use rocket::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("TRUNCATE TABLE users, sessions") - .execute(conn) - .expect("drop all tables"); - }) - .await -} - -macro_rules! run_test { - (|$client:ident, $conn:ident| $block:expr) => {{ - let _lock = DB_LOCK.lock(); - - rocket::async_test(async move { - let $client = Client::tracked(mozaic4_backend::rocket()) - .await - .expect("Rocket client"); - let db = mozaic4_backend::DbConn::get_one($client.rocket()).await; - let $conn = db.expect("failed to get database connection for testing"); - reset_db(&$conn).await; - - $block - }) - }}; -} +mod util; +use util::run_test; pub struct BearerAuth { token: String, @@ -53,9 +22,9 @@ impl<'a> Into> for BearerAuth { } } -#[test] -fn test_registration() { - run_test!(|client, _conn| { +#[rocket::async_test] +async fn test_registration() { + run_test(async move |client, _conn| { let response = client .post("/register") .header(ContentType::JSON) @@ -87,12 +56,12 @@ fn test_registration() { let resp = response.into_string().await.unwrap(); let json: serde_json::Value = serde_json::from_str(&resp).unwrap(); assert_eq!(json["username"], "piepkonijn"); - }); + }).await } -#[test] -fn test_reject_invalid_credentials() { - run_test!(|client, _conn| { +#[rocket::async_test] +async fn test_reject_invalid_credentials() { + run_test(async move |client, _conn| { let response = client .post("/login") .header(ContentType::JSON) @@ -102,5 +71,5 @@ fn test_reject_invalid_credentials() { 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 new file mode 100644 index 0000000..3502ddb --- /dev/null +++ b/backend/tests/util/mod.rs @@ -0,0 +1,39 @@ +use std::future::Future; + +use diesel::RunQueryDsl; +use mozaic4_backend::DbConn; +use rocket::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("TRUNCATE TABLE users, sessions") + .execute(conn) + .expect("drop all tables"); + }) + .await +} + +pub async fn run_test(test_closure: F) +where + F: FnOnce(Client, DbConn) -> R, + R: Future, +{ + 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; +} -- cgit v1.2.3 From 52242b03f1af7f73e73592c2e5ee2bc54813a64d Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Sun, 19 Dec 2021 00:16:46 +0100 Subject: simple bot uploads --- backend/tests/bots.rs | 98 +++++++++++++++++++++++++++++++++++++++++++++++ backend/tests/login.rs | 26 +++---------- backend/tests/util/mod.rs | 28 ++++++++++++-- 3 files changed, 128 insertions(+), 24 deletions(-) create mode 100644 backend/tests/bots.rs (limited to 'backend/tests') diff --git a/backend/tests/bots.rs b/backend/tests/bots.rs new file mode 100644 index 0000000..fe81712 --- /dev/null +++ b/backend/tests/bots.rs @@ -0,0 +1,98 @@ +#![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> { + 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 index b4e07e3..60c5d6f 100644 --- a/backend/tests/login.rs +++ b/backend/tests/login.rs @@ -1,27 +1,11 @@ #![feature(async_closure)] extern crate mozaic4_backend; -use rocket::http::{ContentType, Header, Status}; +use rocket::http::{ContentType, Status}; mod util; use util::run_test; -pub struct BearerAuth { - token: String, -} - -impl BearerAuth { - pub fn new(token: String) -> Self { - Self { token } - } -} - -impl<'a> Into> for BearerAuth { - fn into(self) -> Header<'a> { - Header::new("Authorization", format!("Bearer {}", self.token)) - } -} - #[rocket::async_test] async fn test_registration() { run_test(async move |client, _conn| { @@ -47,7 +31,7 @@ async fn test_registration() { let response = client .get("/users/me") - .header(BearerAuth::new(token)) + .header(util::BearerAuth::new(token)) .dispatch() .await; @@ -56,7 +40,8 @@ async fn test_registration() { let resp = response.into_string().await.unwrap(); let json: serde_json::Value = serde_json::from_str(&resp).unwrap(); assert_eq!(json["username"], "piepkonijn"); - }).await + }) + .await } #[rocket::async_test] @@ -71,5 +56,6 @@ async fn test_reject_invalid_credentials() { assert_eq!(response.status(), Status::Forbidden); // assert_eq!(response.content_type(), Some(ContentType::JSON)); - }).await + }) + .await } diff --git a/backend/tests/util/mod.rs b/backend/tests/util/mod.rs index 3502ddb..f34e9f3 100644 --- a/backend/tests/util/mod.rs +++ b/backend/tests/util/mod.rs @@ -2,7 +2,7 @@ use std::future::Future; use diesel::RunQueryDsl; use mozaic4_backend::DbConn; -use rocket::local::asynchronous::Client; +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 @@ -11,9 +11,13 @@ static DB_LOCK: parking_lot::Mutex<()> = parking_lot::const_mutex(()); async fn reset_db(db: &DbConn) { db.run(|conn| { - diesel::sql_query("TRUNCATE TABLE users, sessions") - .execute(conn) - .expect("drop all tables"); + diesel::sql_query( + r#" + TRUNCATE TABLE users, sessions, + bots, code_bundles"#, + ) + .execute(conn) + .expect("drop all tables"); }) .await } @@ -37,3 +41,19 @@ where test_closure(client, db).await; } + +pub struct BearerAuth { + token: String, +} + +impl BearerAuth { + pub fn new(token: String) -> Self { + Self { token } + } +} + +impl<'a> Into> for BearerAuth { + fn into(self) -> Header<'a> { + Header::new("Authorization", format!("Bearer {}", self.token)) + } +} -- cgit v1.2.3