write some tests (they fail)
This commit is contained in:
parent
b10a50be08
commit
f611c5378d
|
@ -0,0 +1,3 @@
|
||||||
|
SFRS_ENV=development
|
||||||
|
SFRS_JWT_SECRET=whatever_a_secret_is
|
||||||
|
DATABASE_URL=./db/database.test.db
|
|
@ -1,3 +1,4 @@
|
||||||
/target
|
/target
|
||||||
.env
|
.env
|
||||||
database.db
|
database.db
|
||||||
|
database.test.db
|
|
@ -1005,6 +1005,7 @@ dependencies = [
|
||||||
"diesel_migrations",
|
"diesel_migrations",
|
||||||
"dotenv",
|
"dotenv",
|
||||||
"jwt",
|
"jwt",
|
||||||
|
"lazy_static",
|
||||||
"rocket",
|
"rocket",
|
||||||
"rocket_contrib",
|
"rocket_contrib",
|
||||||
"rust-crypto",
|
"rust-crypto",
|
||||||
|
|
|
@ -11,6 +11,7 @@ jwt = "0.4.0"
|
||||||
diesel = { version = "1.4.3", features = ["sqlite"] }
|
diesel = { version = "1.4.3", features = ["sqlite"] }
|
||||||
diesel_migrations = "1.4.0"
|
diesel_migrations = "1.4.0"
|
||||||
dotenv = "0.9.0"
|
dotenv = "0.9.0"
|
||||||
|
lazy_static = "1.4.0"
|
||||||
serde = { version = "1.0.104", features = ["derive"] }
|
serde = { version = "1.0.104", features = ["derive"] }
|
||||||
scrypt = "0.2.0"
|
scrypt = "0.2.0"
|
||||||
rust-crypto = "0.2.36"
|
rust-crypto = "0.2.36"
|
24
src/main.rs
24
src/main.rs
|
@ -13,17 +13,22 @@ extern crate dotenv;
|
||||||
extern crate serde;
|
extern crate serde;
|
||||||
extern crate crypto;
|
extern crate crypto;
|
||||||
extern crate scrypt;
|
extern crate scrypt;
|
||||||
|
#[macro_use]
|
||||||
|
#[cfg(test)]
|
||||||
|
extern crate lazy_static;
|
||||||
|
|
||||||
mod schema;
|
mod schema;
|
||||||
mod api;
|
mod api;
|
||||||
mod user;
|
mod user;
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests;
|
||||||
|
|
||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
use diesel::sqlite::SqliteConnection;
|
use diesel::sqlite::SqliteConnection;
|
||||||
use dotenv::dotenv;
|
use dotenv::dotenv;
|
||||||
use rocket::Rocket;
|
use rocket::Rocket;
|
||||||
use rocket::config::{Config, Environment, Value};
|
use rocket::config::{Config, Environment, Value};
|
||||||
use rocket::fairing::AdHoc;
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
|
@ -69,10 +74,10 @@ fn build_config() -> Config {
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_db_migrations(rocket: Rocket) -> Result<Rocket, Rocket> {
|
fn run_db_migrations(rocket: Rocket) -> Rocket {
|
||||||
let db = DbConn::get_one(&rocket).expect("Could not connect to Database");
|
let db = DbConn::get_one(&rocket).expect("Could not connect to Database");
|
||||||
match embedded_migrations::run(&*db) {
|
match embedded_migrations::run(&*db) {
|
||||||
Ok(()) => Ok(rocket),
|
Ok(()) => rocket,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
// We should not do anything if database failed to migrate
|
// We should not do anything if database failed to migrate
|
||||||
panic!("Failed to run database migrations: {:?}", e);
|
panic!("Failed to run database migrations: {:?}", e);
|
||||||
|
@ -80,11 +85,14 @@ fn run_db_migrations(rocket: Rocket) -> Result<Rocket, Rocket> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn build_rocket() -> Rocket {
|
||||||
|
let r = rocket::custom(build_config())
|
||||||
|
.attach(DbConn::fairing())
|
||||||
|
.mount("/", api::routes());
|
||||||
|
run_db_migrations(r)
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
dotenv().ok();
|
dotenv().ok();
|
||||||
rocket::custom(build_config())
|
build_rocket().launch();
|
||||||
.attach(DbConn::fairing())
|
|
||||||
.attach(AdHoc::on_attach("Database Migrations", run_db_migrations))
|
|
||||||
.mount("/", api::routes())
|
|
||||||
.launch();
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,112 @@
|
||||||
|
use crate::build_rocket;
|
||||||
|
use rocket::local::Client;
|
||||||
|
use rocket::http::{ContentType, Status};
|
||||||
|
use lazy_static::*;
|
||||||
|
|
||||||
|
fn get_test_client() -> Client {
|
||||||
|
dotenv::from_filename(".env.test").unwrap();
|
||||||
|
Client::new(build_rocket())
|
||||||
|
.expect("valid rocket instance")
|
||||||
|
}
|
||||||
|
|
||||||
|
lazy_static! {
|
||||||
|
static ref CLIENT: Client = get_test_client();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn should_add_user() {
|
||||||
|
let mut resp = CLIENT
|
||||||
|
.post("/auth")
|
||||||
|
.header(ContentType::JSON)
|
||||||
|
.body(r#"{
|
||||||
|
"email": "test@example.com",
|
||||||
|
"password": "testpw",
|
||||||
|
"pw_cost": "100",
|
||||||
|
"pw_nonce": "whatever",
|
||||||
|
"version": "001"
|
||||||
|
}"#)
|
||||||
|
.dispatch();
|
||||||
|
assert_eq!(resp.status(), Status::Ok);
|
||||||
|
assert!(resp.body_string().unwrap().contains(r#"{"token":"#));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn should_not_add_user_twice() {
|
||||||
|
CLIENT.post("/auth")
|
||||||
|
.header(ContentType::JSON)
|
||||||
|
.body(r#"{
|
||||||
|
"email": "test1@example.com",
|
||||||
|
"password": "testpw",
|
||||||
|
"pw_cost": "100",
|
||||||
|
"pw_nonce": "whatever",
|
||||||
|
"version": "001"
|
||||||
|
}"#)
|
||||||
|
.dispatch()
|
||||||
|
.body_string()
|
||||||
|
.unwrap();
|
||||||
|
let resp = CLIENT
|
||||||
|
.post("/auth")
|
||||||
|
.header(ContentType::JSON)
|
||||||
|
.body(r#"{
|
||||||
|
"email": "test1@example.com",
|
||||||
|
"password": "does not matter",
|
||||||
|
"pw_cost": "100",
|
||||||
|
"pw_nonce": "whatever",
|
||||||
|
"version": "001"
|
||||||
|
}"#)
|
||||||
|
.dispatch();
|
||||||
|
assert_eq!(resp.status(), Status::InternalServerError);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn should_log_in_successfully() {
|
||||||
|
CLIENT.post("/auth")
|
||||||
|
.header(ContentType::JSON)
|
||||||
|
.body(r#"{
|
||||||
|
"email": "test2@example.com",
|
||||||
|
"password": "testpw",
|
||||||
|
"pw_cost": "100",
|
||||||
|
"pw_nonce": "whatever",
|
||||||
|
"version": "001"
|
||||||
|
}"#)
|
||||||
|
.dispatch()
|
||||||
|
.body_string()
|
||||||
|
.unwrap();
|
||||||
|
let mut resp = CLIENT
|
||||||
|
.post("/auth/sign_in")
|
||||||
|
.header(ContentType::JSON)
|
||||||
|
.body(r#"{
|
||||||
|
"email": "test2@example.com",
|
||||||
|
"password": "testpw"
|
||||||
|
}"#)
|
||||||
|
.dispatch();
|
||||||
|
assert_eq!(resp.status(), Status::Ok);
|
||||||
|
let body = resp.body_string().unwrap();
|
||||||
|
//println!("{}", body);
|
||||||
|
assert!(body.contains(r#"{"token":"#));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn should_log_in_fail() {
|
||||||
|
CLIENT.post("/auth")
|
||||||
|
.header(ContentType::JSON)
|
||||||
|
.body(r#"{
|
||||||
|
"email": "test3@example.com",
|
||||||
|
"password": "testpw",
|
||||||
|
"pw_cost": "100",
|
||||||
|
"pw_nonce": "whatever",
|
||||||
|
"version": "001"
|
||||||
|
}"#)
|
||||||
|
.dispatch()
|
||||||
|
.body_string()
|
||||||
|
.unwrap();
|
||||||
|
let resp = CLIENT
|
||||||
|
.post("/auth/sign_in")
|
||||||
|
.header(ContentType::JSON)
|
||||||
|
.body(r#"{
|
||||||
|
"email": "test3@example.com",
|
||||||
|
"password": "testpw1"
|
||||||
|
}"#)
|
||||||
|
.dispatch();
|
||||||
|
assert_eq!(resp.status(), Status::InternalServerError);
|
||||||
|
}
|
Loading…
Reference in New Issue