Skip to content

Commit

Permalink
os(user): add test for setup
Browse files Browse the repository at this point in the history
  • Loading branch information
vnghia committed Jan 9, 2024
1 parent 9490c07 commit 19b05f2
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/open_subsonic/user/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,70 @@ pub async fn setup_handler(
.await?;
Ok(SetupBody::default().into())
}

#[cfg(test)]
mod tests {
use fake::{faker::internet::en::FreeEmail, Fake};

use super::*;
use crate::utils::test::{db::TemporaryDatabase, user::create_key_user_token};

#[tokio::test]
async fn test_setup_no_user() {
let db = TemporaryDatabase::new_from_env().await;
let (key, username, password, _, _) = create_key_user_token();

let state = State(ServerState {
conn: db.get_conn().clone(),
encryption_key: key,
});
let form = Form(SetupParams {
username,
password,
email: FreeEmail().fake(),
});

assert_eq!(
setup_handler(state, form).await.unwrap().0,
SetupBody::default().into()
);

db.async_drop().await;
}

#[tokio::test]
async fn test_setup_with_user() {
let db = TemporaryDatabase::new_from_env().await;
let (_, current_username, current_password, _, _) = create_key_user_token();
let (key, username, password, _, _) = create_key_user_token();

let state = State(ServerState {
conn: db.get_conn().clone(),
encryption_key: key,
});
let form = Form(SetupParams {
username,
password,
email: FreeEmail().fake(),
});

create_user(
&state.conn,
&state.encryption_key,
CreateUserParams {
username: current_username,
password: current_password,
..Default::default()
},
)
.await
.unwrap();

assert!(matches!(
setup_handler(state, form).await,
Err(OpenSubsonicError::Forbidden { message: _ })
));

db.async_drop().await;
}
}

0 comments on commit 19b05f2

Please sign in to comment.