chore(database): update tests

This commit is contained in:
MedzikUser 2022-06-14 22:37:42 +02:00
parent 9ad2af716a
commit d9e87907ec
No known key found for this signature in database
GPG Key ID: A5FAC1E185C112DB
10 changed files with 87 additions and 133 deletions

76
Cargo.lock generated
View File

@ -505,7 +505,7 @@ checksum = "62007592ac46aa7c2b6416f7deb9a8a8f63a01e0f1d6e1787d5630170db2b63e"
dependencies = [
"futures-core",
"lock_api",
"parking_lot 0.11.2",
"parking_lot",
]
[[package]]
@ -567,13 +567,13 @@ dependencies = [
[[package]]
name = "getrandom"
version = "0.2.6"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9be70c98951c83b8d2f8f60d7065fa6d5146873094452a1008da8c2f1e4205ad"
checksum = "4eb1a864a501629691edf6c15a593b7a51eebaa1e8468e9ddc623de7c9b58ec6"
dependencies = [
"cfg-if",
"libc",
"wasi 0.10.0+wasi-snapshot-preview1",
"wasi 0.11.0+wasi-snapshot-preview1",
]
[[package]]
@ -828,9 +828,9 @@ checksum = "112c678d4050afce233f4f2852bb2eb519230b3cf12f33585275537d7e41578d"
[[package]]
name = "js-sys"
version = "0.3.57"
version = "0.3.58"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "671a26f820db17c2a2750743f1dd03bafd15b98c9f30c7c2628c024c05d73397"
checksum = "c3fac17f7123a73ca62df411b1bf727ccc805daa070338fda671c86dac1bdc27"
dependencies = [
"wasm-bindgen",
]
@ -1052,17 +1052,7 @@ checksum = "7d17b78036a60663b797adeaee46f5c9dfebb86948d1255007a1d6be0271ff99"
dependencies = [
"instant",
"lock_api",
"parking_lot_core 0.8.5",
]
[[package]]
name = "parking_lot"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f"
dependencies = [
"lock_api",
"parking_lot_core 0.9.3",
"parking_lot_core",
]
[[package]]
@ -1079,19 +1069,6 @@ dependencies = [
"winapi",
]
[[package]]
name = "parking_lot_core"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09a279cbf25cb0757810394fbc1e359949b59e348145c643a939a525692e6929"
dependencies = [
"cfg-if",
"libc",
"redox_syscall",
"smallvec",
"windows-sys",
]
[[package]]
name = "paste"
version = "1.0.7"
@ -1316,15 +1293,6 @@ dependencies = [
"digest",
]
[[package]]
name = "signal-hook-registry"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e51e73328dc4ac0c7ccbda3a494dfa03df1de2f46018127f60c693f2648455b0"
dependencies = [
"libc",
]
[[package]]
name = "simple_asn1"
version = "0.6.2"
@ -1615,9 +1583,7 @@ dependencies = [
"mio",
"num_cpus",
"once_cell",
"parking_lot 0.12.1",
"pin-project-lite",
"signal-hook-registry",
"socket2",
"tokio-macros",
"winapi",
@ -1787,9 +1753,9 @@ checksum = "099b7128301d285f79ddd55b9a83d5e6b9e97c92e0ea0daebee7263e932de992"
[[package]]
name = "unicode-ident"
version = "1.0.0"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d22af068fba1eb5edcb4aea19d382b2a3deb4c8f9d475c589b6ada9e0fd493ee"
checksum = "5bd2fe26506023ed7b5e1e315add59d6f584c621d037f9368fea9cfb988f368c"
[[package]]
name = "unicode-normalization"
@ -1888,9 +1854,9 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "wasm-bindgen"
version = "0.2.80"
version = "0.2.81"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "27370197c907c55e3f1a9fbe26f44e937fe6451368324e009cba39e139dc08ad"
checksum = "7c53b543413a17a202f4be280a7e5c62a1c69345f5de525ee64f8cfdbc954994"
dependencies = [
"cfg-if",
"wasm-bindgen-macro",
@ -1898,9 +1864,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.80"
version = "0.2.81"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53e04185bfa3a779273da532f5025e33398409573f348985af9a1cbf3774d3f4"
checksum = "5491a68ab4500fa6b4d726bd67408630c3dbe9c4fe7bda16d5c82a1fd8c7340a"
dependencies = [
"bumpalo",
"lazy_static",
@ -1913,9 +1879,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.80"
version = "0.2.81"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "17cae7ff784d7e83a2fe7611cfe766ecf034111b49deb850a3dc7699c08251f5"
checksum = "c441e177922bc58f1e12c022624b6216378e5febc2f0533e41ba443d505b80aa"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
@ -1923,9 +1889,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.80"
version = "0.2.81"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99ec0dc7a4756fffc231aab1b9f2f578d23cd391390ab27f952ae0c9b3ece20b"
checksum = "7d94ac45fcf608c1f45ef53e748d35660f168490c10b23704c7779ab8f5c3048"
dependencies = [
"proc-macro2",
"quote",
@ -1936,15 +1902,15 @@ dependencies = [
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.80"
version = "0.2.81"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d554b7f530dee5964d9a9468d95c1f8b8acae4f282807e7d27d4b03099a46744"
checksum = "6a89911bd99e5f3659ec4acf9c4d93b0a90fe4a2a11f15328472058edc5261be"
[[package]]
name = "web-sys"
version = "0.3.57"
version = "0.3.58"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b17e741662c70c8bd24ac5c5b18de314a2c26c32bf8346ee1e6f53de919c283"
checksum = "2fed94beee57daf8dd7d51f2b15dc2bcde92d7a72304cdf662a4371008b71b90"
dependencies = [
"js-sys",
"wasm-bindgen",

View File

@ -13,4 +13,4 @@ uuid = { version = "1.1.2", features = ["v5"] }
homedisk-types = { path = "../types", features = ["database"] }
[dev-dependencies]
tokio = { version = "1.19.2", features = ["full"] }
tokio = { version = "1.19.2", features = ["rt-multi-thread", "macros"] }

View File

@ -13,53 +13,75 @@ pub struct Database {
impl Database {
/// Open a SQLite database
/// ```ignore,rust
/// ```
/// use homedisk_database::Database;
///
/// // open database in memory
/// Database::open("sqlite::memory:").await?;
/// #[tokio::main]
/// async fn connect() {
/// // open database in memory
/// Database::open("sqlite::memory:").await.unwrap();
///
/// // open database from file
/// Database::open("path/to/file.db").await?;
/// // open database from file
/// Database::open("path/to/file.db").await.unwrap();
/// }
/// ```
pub async fn open(path: &str) -> Result<Self, Error> {
debug!("Opening SQLite database");
// create a database pool
let conn = SqlitePool::connect(path).await?;
// return `Database`
Ok(Self { conn })
}
/// Create a new User
/// ```ignore,rust
/// ```
/// use homedisk_database::{Database, User};
///
/// // create `User` type
/// let user = User::new("username", "password");
/// #[tokio::main]
/// async fn create_user() {
/// // open database in memory
/// let db = Database::open("sqlite::memory:").await.unwrap();
///
/// // create a user in database
/// db.create_user(&user).await?;
/// // create `User` type
/// let user = User::new("username", "password");
///
/// // create a user in database
/// db.create_user(&user).await.unwrap();
/// }
/// ```
pub async fn create_user(&self, user: &User) -> Result<SqliteQueryResult, Error> {
debug!("Creating user - {}", user.username);
// inster user to a database
let query = sqlx::query("INSERT INTO user (id, username, password) VALUES (?, ?, ?)")
.bind(&user.id)
.bind(&user.username)
.bind(&user.password);
// execute query and return output
Ok(self.conn.execute(query).await?)
}
/// Search for a user
/// ```ignore,rust
/// ```
/// use homedisk_database::{Database, User};
///
/// // create `User` type
/// let user = User::new("username", "password");
/// #[tokio::main]
/// async fn find_user() {
/// // open database in memory
/// let db = Database::open("sqlite::memory:").await.unwrap();
///
/// // search for a user in database
/// db.find_user(&user.username, &user.password).await?;
/// // create `User` type
/// let user = User::new("username", "password");
///
/// // create a user in database
/// db.create_user(&user).await.unwrap();
///
/// // search for a user
/// db.find_user(&user.username, &user.password).await.unwrap();
/// }
/// ```
pub async fn find_user(&self, username: &str, password: &str) -> Result<User, Error> {
debug!("Searching for a user - {}", username);
@ -83,6 +105,7 @@ impl Database {
// get `password` row
let password = row.try_get("password")?;
// return `User`
Ok(User {
id,
username,
@ -91,16 +114,25 @@ impl Database {
}
/// Search for a user by UUID
/// ```ignore,rust
/// ```
/// use homedisk_database::{Database, User};
///
/// // create `User` type
/// let user = User::new("username", "password");
/// #[tokio::main]
/// async fn find_user_by_id() {
/// // open database in memory
/// let db = Database::open("sqlite::memory:").await.unwrap();
///
/// // search for a user by UUID in database
/// db.find_user_by_id(&user.id).await?;
/// // create `User` type
/// let user = User::new("username", "password");
///
/// // create a user in database
/// db.create_user(&user).await.unwrap();
///
/// // search for a user using UUID
/// db.find_user_by_id(&user.id).await.unwrap();
/// }
/// ```
pub async fn find_user_by_id(&self, id: String) -> Result<User, Error> {
pub async fn find_user_by_id(&self, id: &str) -> Result<User, Error> {
debug!("Searching for a user by UUID - {}", id);
// create query request to database
@ -119,6 +151,7 @@ impl Database {
// get `password` row
let password = row.try_get("password")?;
// return `User`
Ok(User {
id,
username,
@ -155,38 +188,7 @@ mod tests {
db.create_user(&user).await.expect("create user");
}
/// Open database in memory
#[tokio::test]
async fn open_db_in_memory() {
open_db().await;
}
/// Create a new user
#[tokio::test]
async fn create_user() {
let db = open_db().await;
new_user(&db).await;
}
/// Search for a user
#[tokio::test]
async fn find_user() {
let db = open_db().await;
new_user(&db).await;
let user = User::new("medzik", "Qwerty1234!");
let res = db
.find_user(&user.username, &user.password)
.await
.expect("find user");
assert_eq!(res.password, user.password)
}
/// Search for a user with an invalid password to see if the user is returned (it shouldn't be)
/// Test a search for a user with an invalid password to see if the user is returned (it shouldn't be)
#[tokio::test]
async fn find_user_wrong_password() {
let db = open_db().await;
@ -203,7 +205,7 @@ mod tests {
assert_eq!(err.to_string(), "user not found")
}
/// Search for a user who does not exist
/// Test a search for a user who does not exist
#[tokio::test]
async fn find_user_wrong_username() {
let db = open_db().await;
@ -220,21 +222,7 @@ mod tests {
assert_eq!(err.to_string(), "user not found")
}
/// Search for a user by UUID
#[tokio::test]
async fn find_user_by_id() {
let db = open_db().await;
new_user(&db).await;
let user = User::new("medzik", "Qwerty1234!");
let res = db.find_user_by_id(user.id).await.expect("find user");
assert_eq!(res.password, user.password)
}
/// Search for a user by UUID who does not exist
/// Test a search for a user by UUID who does not exist
#[tokio::test]
async fn find_user_wrong_id() {
let db = open_db().await;
@ -243,7 +231,7 @@ mod tests {
let other_user = User::new("other_user", "my secret passphrase");
let err = db.find_user_by_id(other_user.id).await.unwrap_err();
let err = db.find_user_by_id(&other_user.id).await.unwrap_err();
assert_eq!(err.to_string(), "user not found")
}

View File

@ -19,7 +19,7 @@ pub async fn handle(
let token = validate_jwt(config.jwt.secret.as_bytes(), &token)?;
// search for a user in database
let response = match db.find_user_by_id(token.claims.sub).await {
let response = match db.find_user_by_id(&token.claims.sub).await {
Ok(res) => Response {
username: res.username,
},

View File

@ -29,7 +29,7 @@ pub async fn handle(
validate_path(&request.path)?;
// search for a user by UUID from a token
let user = find_user(db, token.claims.sub).await?;
let user = find_user(&db, &token.claims.sub).await?;
// directory where the file will be placed
let path = format!(

View File

@ -27,7 +27,7 @@ pub async fn handle(
validate_path(&query.path)?;
// search for a user by UUID from a token
let user = find_user(db, token.claims.sub).await?;
let user = find_user(&db, &token.claims.sub).await?;
// path to the file
let path = format!(

View File

@ -24,7 +24,7 @@ pub async fn handle(
validate_path(&query.path)?;
// search for a user by UUID from a token
let user = find_user(db, token.claims.sub).await?;
let user = find_user(&db, &token.claims.sub).await?;
// directory where the file will be placed
let path = format!(

View File

@ -48,7 +48,7 @@ pub async fn handle(
validate_path(&request.path)?;
// search for a user by UUID from a token
let user = find_user(db, token.claims.sub).await?;
let user = find_user(&db, &token.claims.sub).await?;
// directory where the file will be placed
let path = format!(

View File

@ -30,7 +30,7 @@ pub async fn handle(
validate_path(&query.path)?;
// search for a user by UUID from a token
let user = find_user(db, token.claims.sub).await?;
let user = find_user(&db, &token.claims.sub).await?;
// path to the file
let file_path = format!(

View File

@ -11,7 +11,7 @@ pub fn create_token(user: &User, secret: &[u8], expires: i64) -> Result<String,
}
/// Search for a user
pub async fn find_user(db: Database, user_id: String) -> Result<User, ServerError> {
pub async fn find_user(db: &Database, user_id: &str) -> Result<User, ServerError> {
match db.find_user_by_id(user_id).await {
// if success return user
Ok(user) => Ok(user),