diff --git a/server/src/models.rs b/server/src/models.rs index 18b1321..93b4610 100644 --- a/server/src/models.rs +++ b/server/src/models.rs @@ -18,7 +18,7 @@ pub struct User { } pub const USER_OFFLINE: i32 = 1; -pub const _USER_ONLINE: i32 = 2; +pub const USER_ONLINE: i32 = 2; pub const _USER_AWAY: i32 = 3; pub const _USER_DO_NOT_DISTRUB: i32 = 4; diff --git a/server/src/users.rs b/server/src/users.rs index 0226e34..db86ee5 100644 --- a/server/src/users.rs +++ b/server/src/users.rs @@ -1,7 +1,7 @@ use chrono::Utc; use rocket_contrib::json::{Json, JsonValue}; use crate::rand_utils::{new_user_id, new_key}; -use crate::models::{User, USER_OFFLINE}; +use crate::models::{User, USER_ONLINE, USER_OFFLINE}; use crate::payload::NewUserResponse; use crate::{DBConn, schema}; use diesel::{self, prelude::*}; @@ -16,7 +16,7 @@ pub fn create_new_user() -> User { username: uname, key: rstring, date: Utc::now().timestamp() as u64, - status: USER_OFFLINE, + status: USER_ONLINE, } } @@ -53,6 +53,8 @@ pub fn remove_user(hash: u64, conn: DBConn) -> JsonValue { } +// TODO: optional paramter where we can specify getting the offline users as well +// TODO: More optionally specify what kind of users we want online/offline/away etc. #[get("/list")] pub fn get_user_list(conn: DBConn) -> Json> { use schema::users::dsl::*; @@ -75,6 +77,39 @@ mod user_tests { use rocket::local::Client; use rocket::http::Status; + macro_rules! rocket_inst { + ($func:expr) => { + rocket::ignite() + .mount("/users", routes![$func]) + .attach(DBConn::fairing()) + } + } + + #[test] + fn generate_bs_users() { + use diesel::mysql::MysqlConnection; + // please don't ask me why this is here it just is + let conn = MysqlConnection::establish("mysql://freechat_dev:password@localhost:3306/freechat") + .unwrap(); + for _i in 0..100 { + let user = create_new_user(); + let _bs = diesel::insert_into(schema::users::table) + .values(&user) + .execute(&conn); + } + } + + #[test] + fn get_user_list() { + let app = rocket_inst!(get_user_list); + let client = Client::new(app).expect("Bad rocket instance"); + + let mut response = client.get("/users/list").dispatch(); + let body: String = response.body_string().unwrap(); + assert_eq!(response.status(), Status::Ok); + println!("{}", body); + } + #[test] fn user_struct() { @@ -86,9 +121,7 @@ mod user_tests { let user = create_new_user(); println!("Client to remove: {:?}", user); - let app = rocket::ignite() - .mount("/users", routes![remove_user]) - .attach(DBConn::fairing()); + let app = rocket_inst!(remove_user); let client = Client::new(app).expect("Bad rocket instance"); let mut response = client.delete(format!("/users/remove/{}", user.userid)).dispatch();