Basic new user route with a sample test

Creating a payloads module as well
This commit is contained in:
shockrah 2020-02-07 14:57:44 -08:00
parent 6dd002171c
commit dd3acb5abe
4 changed files with 86 additions and 3 deletions

View File

@ -7,6 +7,7 @@
extern crate chrono; extern crate chrono;
extern crate dotenv; extern crate dotenv;
extern crate serde; extern crate serde;
extern crate rand;
use rocket_contrib::serve::StaticFiles; use rocket_contrib::serve::StaticFiles;
@ -15,9 +16,11 @@ use dotenv::dotenv;
use std::env::var; use std::env::var;
mod website; mod website;
mod users;
mod schema; mod schema;
mod invites;
mod models; mod models;
mod invites;
mod payload;
use website::*; use website::*;
use invites::*; use invites::*;
@ -25,7 +28,7 @@ use invites::*;
#[database("freechat_sample_db")] #[database("freechat_sample_db")]
pub struct DBConn(diesel::MysqlConnection); pub struct DBConn(diesel::MysqlConnection);
fn rocket() -> rocket::Rocket { pub fn rocket() -> rocket::Rocket {
rocket::ignite() rocket::ignite()
.mount("/static", StaticFiles::from("/static")) .mount("/static", StaticFiles::from("/static"))
.mount("/", routes![ .mount("/", routes![
@ -35,6 +38,9 @@ fn rocket() -> rocket::Rocket {
.mount("/invite", routes![ .mount("/invite", routes![
generate_invite, use_invite generate_invite, use_invite
]) ])
.mount("/user", routes![
users::create_user
])
.attach(Template::fairing()) .attach(Template::fairing())
.attach(DBConn::fairing()) .attach(DBConn::fairing())
} }

14
server/src/payload.rs Normal file
View File

@ -0,0 +1,14 @@
/* Module containg various structure which we use to pass back
* and forth from client/server as auth tokens
*/
// This structure allows us to provide some critical data for the client to reconnect to
// the server without having to go through a sign in process everytime
#[derive(Serialize)]
pub struct NewUserResponse {
pub userid: u64,
pub token: String,
pub username: String,
pub email: Option<String>,
}

55
server/src/users.rs Normal file
View File

@ -0,0 +1,55 @@
use rocket::request::Form;
use rocket_contrib::json::Json;
use crate::payload;
// TODO: authentication needs to get applied basically everywhere but right now its missing
#[derive(FromForm)]
pub struct NewUserForm {
pub username: String,
pub display: Option<String>,
pub email: Option<String>,
pub password: String // this part is generated for the user if an invite is used
}
#[post("/create", data = "<user_sign_up>")]
pub fn create_user(user_sign_up: Form<NewUserForm>) -> Json<payload::NewUserResponse> {
let email = match user_sign_up.email.clone() {
Some(val) => {
val
}
None => {
"None".to_string()
}
};
// Constructing the response to the user with stuff they'll need to reconnect to the server
let user_auth = payload::NewUserResponse {
userid: 1,
token: "random token".to_string(),
username: user_sign_up.username.clone(),
email: Some(email),
};
Json(user_auth)
}
#[cfg(test)]
mod user_tests {
use super::*;
use rocket;
use rocket::local::Client;
use rocket::http::{Status, ContentType};
#[test]
fn new_user() {
let rocket = rocket::ignite()
.mount("/user", routes![create_user]);
let client = Client::new(rocket).expect("Invalid rocket instance");
let response = client.post("/user/create")
.header(ContentType::Form)
.body("username=testuser&password=testpass")
.dispatch();
assert_eq!(response.status(), Status::Ok);
}
}

View File

@ -2,13 +2,21 @@
frontend js needs some testing if it's to be guaranteed to work at all frontend js needs some testing if it's to be guaranteed to work at all
endpoints need to start existsing so that we can actually start testing on per instance basis
# Creation of users # Creation of users
this part we'll probably use an sql db or something that couples easily with rocket this part we'll probably use an sql db or something that couples easily with rocket
keep the user data designi as stupid simple as possible to enforce both security by surface and haxor simplicity keep the user data designi as stupid simple as possible to enforce both security by surface and haxor simplicity
# Auth
* Modules should serve as a collection of authentication payloads and functions to verify that data from the client is correct
# Webpages # Webpages
Starting to think that we shouldn't even bother with this stuff tbh Starting to think that we shouldn't even bother with this stuff tbh
# Things to depracate
* webpages - as they're just not important to the core utility of freechat at the moment