forked from Ruderverein-Donau-Linz/rowt
		
	add tests for usre
This commit is contained in:
		@@ -30,30 +30,6 @@ pub enum LoginError {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl User {
 | 
			
		||||
    pub async fn update(&self, db: &SqlitePool, is_cox: bool, is_admin: bool, is_guest: bool) {
 | 
			
		||||
        sqlx::query!(
 | 
			
		||||
            "UPDATE user SET is_cox = ?, is_admin = ?, is_guest = ? where id = ?",
 | 
			
		||||
            is_cox,
 | 
			
		||||
            is_admin,
 | 
			
		||||
            is_guest,
 | 
			
		||||
            self.id
 | 
			
		||||
        )
 | 
			
		||||
        .execute(db)
 | 
			
		||||
        .await
 | 
			
		||||
        .unwrap(); //TODO: fixme
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub async fn create(db: &SqlitePool, name: String, is_guest: bool) -> bool {
 | 
			
		||||
        sqlx::query!(
 | 
			
		||||
            "INSERT INTO USER(name, is_guest) VALUES (?,?)",
 | 
			
		||||
            name,
 | 
			
		||||
            is_guest,
 | 
			
		||||
        )
 | 
			
		||||
        .execute(db)
 | 
			
		||||
        .await
 | 
			
		||||
        .is_ok()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub async fn find_by_id(db: &SqlitePool, id: i32) -> Option<Self> {
 | 
			
		||||
        sqlx::query_as!(
 | 
			
		||||
            User,
 | 
			
		||||
@@ -84,13 +60,42 @@ WHERE name like ?
 | 
			
		||||
        .ok()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn get_hashed_pw(pw: &str) -> String {
 | 
			
		||||
        let salt = SaltString::from_b64("dS/X5/sPEKTj4Rzs/CuvzQ").unwrap();
 | 
			
		||||
        let argon2 = Argon2::default();
 | 
			
		||||
        argon2
 | 
			
		||||
            .hash_password(pw.as_bytes(), &salt)
 | 
			
		||||
            .unwrap()
 | 
			
		||||
            .to_string()
 | 
			
		||||
    pub async fn all(db: &SqlitePool) -> Vec<Self> {
 | 
			
		||||
        sqlx::query_as!(
 | 
			
		||||
            User,
 | 
			
		||||
            "
 | 
			
		||||
SELECT id, name, pw, is_cox, is_admin, is_guest
 | 
			
		||||
FROM user
 | 
			
		||||
ORDER BY name
 | 
			
		||||
        "
 | 
			
		||||
        )
 | 
			
		||||
        .fetch_all(db)
 | 
			
		||||
        .await
 | 
			
		||||
        .unwrap() //TODO: fixme
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub async fn create(db: &SqlitePool, name: String, is_guest: bool) -> bool {
 | 
			
		||||
        sqlx::query!(
 | 
			
		||||
            "INSERT INTO USER(name, is_guest) VALUES (?,?)",
 | 
			
		||||
            name,
 | 
			
		||||
            is_guest,
 | 
			
		||||
        )
 | 
			
		||||
        .execute(db)
 | 
			
		||||
        .await
 | 
			
		||||
        .is_ok()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub async fn update(&self, db: &SqlitePool, is_cox: bool, is_admin: bool, is_guest: bool) {
 | 
			
		||||
        sqlx::query!(
 | 
			
		||||
            "UPDATE user SET is_cox = ?, is_admin = ?, is_guest = ? where id = ?",
 | 
			
		||||
            is_cox,
 | 
			
		||||
            is_admin,
 | 
			
		||||
            is_guest,
 | 
			
		||||
            self.id
 | 
			
		||||
        )
 | 
			
		||||
        .execute(db)
 | 
			
		||||
        .await
 | 
			
		||||
        .unwrap(); //Okay, because we can only create a User of a valid id
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub async fn login(db: &SqlitePool, name: String, pw: String) -> Result<Self, LoginError> {
 | 
			
		||||
@@ -114,25 +119,11 @@ WHERE name like ?
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub async fn all(db: &SqlitePool) -> Vec<Self> {
 | 
			
		||||
        sqlx::query_as!(
 | 
			
		||||
            User,
 | 
			
		||||
            "
 | 
			
		||||
SELECT id, name, pw, is_cox, is_admin, is_guest
 | 
			
		||||
FROM user
 | 
			
		||||
ORDER BY name
 | 
			
		||||
        "
 | 
			
		||||
        )
 | 
			
		||||
        .fetch_all(db)
 | 
			
		||||
        .await
 | 
			
		||||
        .unwrap() //TODO: fixme
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub async fn reset_pw(&self, db: &SqlitePool) {
 | 
			
		||||
        sqlx::query!("UPDATE user SET pw = null where id = ?", self.id)
 | 
			
		||||
            .execute(db)
 | 
			
		||||
            .await
 | 
			
		||||
            .unwrap(); //TODO: fixme
 | 
			
		||||
            .unwrap(); //Okay, because we can only create a User of a valid id
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub async fn update_pw(&self, db: &SqlitePool, pw: String) {
 | 
			
		||||
@@ -140,7 +131,16 @@ ORDER BY name
 | 
			
		||||
        sqlx::query!("UPDATE user SET pw = ? where id = ?", pw, self.id)
 | 
			
		||||
            .execute(db)
 | 
			
		||||
            .await
 | 
			
		||||
            .unwrap(); //TODO: fixme
 | 
			
		||||
            .unwrap(); //Okay, because we can only create a User of a valid id
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn get_hashed_pw(pw: &str) -> String {
 | 
			
		||||
        let salt = SaltString::from_b64("dS/X5/sPEKTj4Rzs/CuvzQ").unwrap();
 | 
			
		||||
        let argon2 = Argon2::default();
 | 
			
		||||
        argon2
 | 
			
		||||
            .hash_password(pw.as_bytes(), &salt)
 | 
			
		||||
            .unwrap()
 | 
			
		||||
            .to_string()
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -243,6 +243,69 @@ mod test {
 | 
			
		||||
    use super::User;
 | 
			
		||||
    use sqlx::SqlitePool;
 | 
			
		||||
 | 
			
		||||
    #[sqlx::test]
 | 
			
		||||
    fn test_find_correct_id() {
 | 
			
		||||
        let pool = testdb!();
 | 
			
		||||
        let user = User::find_by_id(&pool, 1).await.unwrap();
 | 
			
		||||
        assert_eq!(user.id, 1);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #[sqlx::test]
 | 
			
		||||
    fn test_find_wrong_id() {
 | 
			
		||||
        let pool = testdb!();
 | 
			
		||||
        let user = User::find_by_id(&pool, 1337).await;
 | 
			
		||||
        assert!(user.is_none());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #[sqlx::test]
 | 
			
		||||
    fn test_find_correct_name() {
 | 
			
		||||
        let pool = testdb!();
 | 
			
		||||
        let user = User::find_by_name(&pool, "admin".into()).await.unwrap();
 | 
			
		||||
        assert_eq!(user.id, 1);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #[sqlx::test]
 | 
			
		||||
    fn test_find_wrong_name() {
 | 
			
		||||
        let pool = testdb!();
 | 
			
		||||
        let user = User::find_by_name(&pool, "name-does-not-exist".into()).await;
 | 
			
		||||
        assert!(user.is_none());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #[sqlx::test]
 | 
			
		||||
    fn test_all() {
 | 
			
		||||
        let pool = testdb!();
 | 
			
		||||
        let res = User::all(&pool).await;
 | 
			
		||||
        assert!(res.len() > 3);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #[sqlx::test]
 | 
			
		||||
    fn test_succ_create() {
 | 
			
		||||
        let pool = testdb!();
 | 
			
		||||
 | 
			
		||||
        assert_eq!(
 | 
			
		||||
            User::create(&pool, "new-user-name".into(), false).await,
 | 
			
		||||
            true
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #[sqlx::test]
 | 
			
		||||
    fn test_duplicate_name_create() {
 | 
			
		||||
        let pool = testdb!();
 | 
			
		||||
 | 
			
		||||
        assert_eq!(User::create(&pool, "admin".into(), false).await, false);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #[sqlx::test]
 | 
			
		||||
    fn test_update() {
 | 
			
		||||
        let pool = testdb!();
 | 
			
		||||
 | 
			
		||||
        let user = User::find_by_id(&pool, 1).await.unwrap();
 | 
			
		||||
        user.update(&pool, false, false, false).await;
 | 
			
		||||
 | 
			
		||||
        let user = User::find_by_id(&pool, 1).await.unwrap();
 | 
			
		||||
        assert_eq!(user.is_admin, false);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #[sqlx::test]
 | 
			
		||||
    fn succ_login_with_test_db() {
 | 
			
		||||
        let pool = testdb!();
 | 
			
		||||
@@ -266,4 +329,31 @@ mod test {
 | 
			
		||||
            .await
 | 
			
		||||
            .is_err());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #[sqlx::test]
 | 
			
		||||
    fn reset() {
 | 
			
		||||
        let pool = testdb!();
 | 
			
		||||
        let user = User::find_by_id(&pool, 1).await.unwrap();
 | 
			
		||||
 | 
			
		||||
        user.reset_pw(&pool).await;
 | 
			
		||||
 | 
			
		||||
        let user = User::find_by_id(&pool, 1).await.unwrap();
 | 
			
		||||
        assert_eq!(user.pw, None);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #[sqlx::test]
 | 
			
		||||
    fn update_pw() {
 | 
			
		||||
        let pool = testdb!();
 | 
			
		||||
        let user = User::find_by_id(&pool, 1).await.unwrap();
 | 
			
		||||
 | 
			
		||||
        assert!(User::login(&pool, "admin".into(), "abc".into())
 | 
			
		||||
            .await
 | 
			
		||||
            .is_err());
 | 
			
		||||
 | 
			
		||||
        user.update_pw(&pool, "abc".into()).await;
 | 
			
		||||
 | 
			
		||||
        User::login(&pool, "admin".into(), "abc".into())
 | 
			
		||||
            .await
 | 
			
		||||
            .unwrap();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user