Skip to content

Commit

Permalink
fix: format
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Nov 30, 2022
1 parent 8b761c8 commit b400962
Show file tree
Hide file tree
Showing 22 changed files with 195 additions and 425 deletions.
2 changes: 1 addition & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
fn main() {
// trigger recompilation when a new migration is added
println!("cargo:rerun-if-changed=migrations");
}
}
81 changes: 21 additions & 60 deletions src/databases/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,12 @@ impl Database for SqliteDatabase {
.map_err(|_| DatabaseError::Error)?;

// add password hash for account
let insert_user_auth_result =
query("INSERT INTO torrust_user_authentication (user_id, password_hash) VALUES (?, ?)")
.bind(user_id)
.bind(password_hash)
.execute(&mut tx)
.await
.map_err(|_| DatabaseError::Error);
let insert_user_auth_result = query("INSERT INTO torrust_user_authentication (user_id, password_hash) VALUES (?, ?)")
.bind(user_id)
.bind(password_hash)
.execute(&mut tx)
.await
.map_err(|_| DatabaseError::Error);

// rollback transaction on error
if let Err(e) = insert_user_auth_result {
Expand Down Expand Up @@ -109,23 +108,15 @@ impl Database for SqliteDatabase {
.map_err(|_| DatabaseError::UserNotFound)
}

async fn get_user_authentication_from_id(
&self,
user_id: i64,
) -> Result<UserAuthentication, DatabaseError> {
query_as::<_, UserAuthentication>(
"SELECT * FROM torrust_user_authentication WHERE user_id = ?",
)
.bind(user_id)
.fetch_one(&self.pool)
.await
.map_err(|_| DatabaseError::UserNotFound)
async fn get_user_authentication_from_id(&self, user_id: i64) -> Result<UserAuthentication, DatabaseError> {
query_as::<_, UserAuthentication>("SELECT * FROM torrust_user_authentication WHERE user_id = ?")
.bind(user_id)
.fetch_one(&self.pool)
.await
.map_err(|_| DatabaseError::UserNotFound)
}

async fn get_user_profile_from_username(
&self,
username: &str,
) -> Result<UserProfile, DatabaseError> {
async fn get_user_profile_from_username(&self, username: &str) -> Result<UserProfile, DatabaseError> {
query_as::<_, UserProfile>("SELECT * FROM torrust_user_profiles WHERE username = ?")
.bind(username)
.fetch_one(&self.pool)
Expand Down Expand Up @@ -164,12 +155,7 @@ impl Database for SqliteDatabase {
.map_err(|_| DatabaseError::Error)
}

async fn ban_user(
&self,
user_id: i64,
reason: &str,
date_expiry: NaiveDateTime,
) -> Result<(), DatabaseError> {
async fn ban_user(&self, user_id: i64, reason: &str, date_expiry: NaiveDateTime) -> Result<(), DatabaseError> {
// date needs to be in ISO 8601 format
let date_expiry_string = date_expiry.format("%Y-%m-%d %H:%M:%S").to_string();

Expand Down Expand Up @@ -207,11 +193,7 @@ impl Database for SqliteDatabase {
.map_err(|_| DatabaseError::Error)
}

async fn add_tracker_key(
&self,
user_id: i64,
tracker_key: &TrackerKey,
) -> Result<(), DatabaseError> {
async fn add_tracker_key(&self, user_id: i64, tracker_key: &TrackerKey) -> Result<(), DatabaseError> {
let key = tracker_key.key.clone();

query("INSERT INTO torrust_tracker_keys (user_id, tracker_key, date_expiry) VALUES ($1, $2, $3)")
Expand Down Expand Up @@ -361,10 +343,7 @@ impl Database for SqliteDatabase {
category_filter_query
);

let count_query = format!(
"SELECT COUNT(*) as count FROM ({}) AS count_table",
query_string
);
let count_query = format!("SELECT COUNT(*) as count FROM ({}) AS count_table", query_string);

let count_result: Result<i64, DatabaseError> = query_as(&count_query)
.bind(title.clone())
Expand Down Expand Up @@ -411,11 +390,7 @@ impl Database for SqliteDatabase {
let (pieces, root_hash): (String, bool) = if let Some(pieces) = &torrent.info.pieces {
(bytes_to_hex(pieces.as_ref()), false)
} else {
let root_hash = torrent
.info
.root_hash
.as_ref()
.ok_or(DatabaseError::Error)?;
let root_hash = torrent.info.root_hash.as_ref().ok_or(DatabaseError::Error)?;
(root_hash.to_string(), true)
};

Expand Down Expand Up @@ -562,10 +537,7 @@ impl Database for SqliteDatabase {
))
}

async fn get_torrent_info_from_id(
&self,
torrent_id: i64,
) -> Result<DbTorrentInfo, DatabaseError> {
async fn get_torrent_info_from_id(&self, torrent_id: i64) -> Result<DbTorrentInfo, DatabaseError> {
query_as::<_, DbTorrentInfo>(
"SELECT name, pieces, piece_length, private, root_hash FROM torrust_torrents WHERE torrent_id = ?",
)
Expand Down Expand Up @@ -604,10 +576,7 @@ impl Database for SqliteDatabase {
.map_err(|_| DatabaseError::TorrentNotFound)
}

async fn get_torrent_listing_from_id(
&self,
torrent_id: i64,
) -> Result<TorrentListing, DatabaseError> {
async fn get_torrent_listing_from_id(&self, torrent_id: i64) -> Result<TorrentListing, DatabaseError> {
query_as::<_, TorrentListing>(
"SELECT tt.torrent_id, tp.username AS uploader, tt.info_hash, ti.title, ti.description, tt.category_id, tt.date_uploaded, tt.size AS file_size,
CAST(COALESCE(sum(ts.seeders),0) as signed) as seeders,
Expand All @@ -632,11 +601,7 @@ impl Database for SqliteDatabase {
.map_err(|_| DatabaseError::Error)
}

async fn update_torrent_title(
&self,
torrent_id: i64,
title: &str,
) -> Result<(), DatabaseError> {
async fn update_torrent_title(&self, torrent_id: i64, title: &str) -> Result<(), DatabaseError> {
query("UPDATE torrust_torrent_info SET title = $1 WHERE torrent_id = $2")
.bind(title)
.bind(torrent_id)
Expand All @@ -661,11 +626,7 @@ impl Database for SqliteDatabase {
})
}

async fn update_torrent_description(
&self,
torrent_id: i64,
description: &str,
) -> Result<(), DatabaseError> {
async fn update_torrent_description(&self, torrent_id: i64, description: &str) -> Result<(), DatabaseError> {
query("UPDATE torrust_torrent_info SET description = $1 WHERE torrent_id = $2")
.bind(description)
.bind(torrent_id)
Expand Down
2 changes: 1 addition & 1 deletion src/models/tracker_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ pub struct NewTrackerKey {
pub struct Duration {
pub secs: i64,
pub nanos: i64,
}
}
2 changes: 1 addition & 1 deletion src/models/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct User {
#[derive(Debug, Serialize, Deserialize, Clone, sqlx::FromRow)]
pub struct UserAuthentication {
pub user_id: i64,
pub password_hash: String
pub password_hash: String,
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone, sqlx::FromRow)]
Expand Down
16 changes: 7 additions & 9 deletions src/routes/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,17 +282,17 @@ pub async fn ban_user(req: HttpRequest, app_data: WebAppData) -> ServiceResult<i

#[cfg(test)]
mod tests {
use crate::models::user::UserAuthentication;

use super::verify_password;
use crate::models::user::UserAuthentication;

#[test]
fn password_hashed_with_pbkdf2_sha256_should_be_verified() {
let password = "12345678".as_bytes();
let password_hash = "$pbkdf2-sha256$i=10000,l=32$pZIh8nilm+cg6fk5Ubf2zQ$AngLuZ+sGUragqm4bIae/W+ior0TWxYFFaTx8CulqtY".to_string();
let password_hash =
"$pbkdf2-sha256$i=10000,l=32$pZIh8nilm+cg6fk5Ubf2zQ$AngLuZ+sGUragqm4bIae/W+ior0TWxYFFaTx8CulqtY".to_string();
let user_authentication = UserAuthentication {
user_id: 1i64,
password_hash
password_hash,
};

assert!(verify_password(password, &user_authentication).is_ok());
Expand All @@ -302,16 +302,14 @@ mod tests {
#[test]
fn password_hashed_with_argon2_should_be_verified() {
let password = "87654321".as_bytes();
let password_hash = "$argon2id$v=19$m=4096,t=3,p=1$ycK5lJ4xmFBnaJ51M1j1eA$kU3UlNiSc3JDbl48TCj7JBDKmrT92DOUAgo4Yq0+nMw".to_string();
let password_hash =
"$argon2id$v=19$m=4096,t=3,p=1$ycK5lJ4xmFBnaJ51M1j1eA$kU3UlNiSc3JDbl48TCj7JBDKmrT92DOUAgo4Yq0+nMw".to_string();
let user_authentication = UserAuthentication {
user_id: 1i64,
password_hash
password_hash,
};

assert!(verify_password(password, &user_authentication).is_ok());
assert!(verify_password("incorrect password".as_bytes(), &user_authentication).is_err());
}
}



3 changes: 2 additions & 1 deletion src/upgrades/from_v1_0_0_to_v2_0_0/databases/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::sync::Arc;

use self::sqlite_v1_0_0::SqliteDatabaseV1_0_0;
use self::sqlite_v2_0_0::SqliteDatabaseV2_0_0;
use std::sync::Arc;

pub mod sqlite_v1_0_0;
pub mod sqlite_v2_0_0;
Expand Down
18 changes: 7 additions & 11 deletions src/upgrades/from_v1_0_0_to_v2_0_0/databases/sqlite_v1_0_0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,10 @@ impl SqliteDatabaseV1_0_0 {
}

pub async fn get_categories_order_by_id(&self) -> Result<Vec<CategoryRecordV1>, DatabaseError> {
query_as::<_, CategoryRecordV1>(
"SELECT category_id, name FROM torrust_categories ORDER BY category_id ASC",
)
.fetch_all(&self.pool)
.await
.map_err(|_| DatabaseError::Error)
query_as::<_, CategoryRecordV1>("SELECT category_id, name FROM torrust_categories ORDER BY category_id ASC")
.fetch_all(&self.pool)
.await
.map_err(|_| DatabaseError::Error)
}

pub async fn get_users(&self) -> Result<Vec<UserRecordV1>, sqlx::Error> {
Expand Down Expand Up @@ -99,10 +97,8 @@ impl SqliteDatabaseV1_0_0 {
}

pub async fn get_torrent_files(&self) -> Result<Vec<TorrentFileRecordV1>, sqlx::Error> {
query_as::<_, TorrentFileRecordV1>(
"SELECT * FROM torrust_torrent_files ORDER BY file_id ASC",
)
.fetch_all(&self.pool)
.await
query_as::<_, TorrentFileRecordV1>("SELECT * FROM torrust_torrent_files ORDER BY file_id ASC")
.fetch_all(&self.pool)
.await
}
}
Loading

0 comments on commit b400962

Please sign in to comment.