test registration & login

This commit is contained in:
Ilion Beyst 2021-12-14 20:23:07 +01:00
parent eabeb7ed7b
commit 13cdbc7ff7
6 changed files with 121 additions and 33 deletions

View file

@ -21,3 +21,6 @@ base64 = "0.13.0"
[dependencies.rocket_sync_db_pools]
version = "0.1.0-rc.1"
features = ["diesel_postgres_pool"]
[dev-dependencies]
parking_lot = "0.11"

View file

@ -1 +1,2 @@
DROP INDEX users_username_index
DROP TABLE users;

View file

@ -4,3 +4,5 @@ CREATE TABLE users(
password_salt BYTEA NOT NULL,
password_hash BYTEA NOT NULL
);
CREATE UNIQUE INDEX users_username_index ON users(username);

35
backend/src/lib.rs Normal file
View file

@ -0,0 +1,35 @@
#![feature(proc_macro_hygiene, decl_macro)]
use rocket::{Build, Rocket};
use rocket_sync_db_pools::database;
#[macro_use]
extern crate rocket;
#[macro_use]
extern crate diesel;
mod db;
mod routes;
mod schema;
#[database("postgresql_database")]
pub struct DbConn(diesel::PgConnection);
#[get("/")]
fn index() -> &'static str {
"Hello, world!"
}
pub fn rocket() -> Rocket<Build> {
rocket::build()
.mount(
"/",
routes![
index,
routes::users::register,
routes::users::login,
routes::users::current_user,
],
)
.attach(DbConn::fairing())
}

View file

@ -1,36 +1,8 @@
#![feature(proc_macro_hygiene, decl_macro)]
use rocket::{Build, Rocket};
use rocket_sync_db_pools::database;
#[macro_use]
extern crate rocket;
#[macro_use]
extern crate diesel;
mod db;
mod routes;
mod schema;
#[database("postgresql_database")]
pub struct DbConn(diesel::PgConnection);
#[get("/")]
fn index() -> &'static str {
"Hello, world!"
}
extern crate mozaic4_backend;
#[launch]
fn rocket() -> Rocket<Build> {
rocket::build()
.mount(
"/",
routes![
index,
routes::users::register,
routes::users::login,
routes::users::current_user,
],
)
.attach(DbConn::fairing())
fn launch() -> _ {
mozaic4_backend::rocket()
}

View file

@ -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");
});
}