Skip to content

Commit

Permalink
tests: [torrust#56] for users profile and auth tables in upgrader
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Nov 30, 2022
1 parent 0a58b6c commit 8d74e66
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
35 changes: 35 additions & 0 deletions tests/upgrades/from_v1_0_0_to_v2_0_0/sqlite_v2_0_0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@ pub struct UserRecordV2 {
pub administrator: bool,
}

#[derive(Debug, Serialize, Deserialize, sqlx::FromRow)]
pub struct UserProfileRecordV2 {
pub user_id: i64,
pub username: String,
pub email: String,
pub email_verified: bool,
pub bio: Option<String>,
pub avatar: Option<String>,
}

#[derive(Debug, Serialize, Deserialize, sqlx::FromRow)]
pub struct UserAuthenticationRecordV2 {
pub user_id: i64,
pub password_hash: String,
}

pub struct SqliteDatabaseV2_0_0 {
pub pool: SqlitePool,
}
Expand All @@ -34,4 +50,23 @@ impl SqliteDatabaseV2_0_0 {
.fetch_one(&self.pool)
.await
}

pub async fn get_user_profile(&self, user_id: i64) -> Result<UserProfileRecordV2, sqlx::Error> {
query_as::<_, UserProfileRecordV2>("SELECT * FROM torrust_user_profiles WHERE user_id = ?")
.bind(user_id)
.fetch_one(&self.pool)
.await
}

pub async fn get_user_authentication(
&self,
user_id: i64,
) -> Result<UserAuthenticationRecordV2, sqlx::Error> {
query_as::<_, UserAuthenticationRecordV2>(
"SELECT * FROM torrust_user_authentication WHERE user_id = ?",
)
.bind(user_id)
.fetch_one(&self.pool)
.await
}
}
39 changes: 39 additions & 0 deletions tests/upgrades/from_v1_0_0_to_v2_0_0/testers/user_data_tester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ impl UserDataTester {

pub async fn assert(&self) {
self.assert_user().await;
self.assert_user_profile().await;
self.assert_user_authentication().await;
}

/// Table `torrust_users`
Expand All @@ -67,6 +69,43 @@ impl UserDataTester {
self.test_data.user.administrator
);
}

/// Table `torrust_user_profiles`
async fn assert_user_profile(&self) {
let imported_user_profile = self
.destiny_database
.get_user_profile(self.test_data.user.user_id)
.await
.unwrap();

assert_eq!(imported_user_profile.user_id, self.test_data.user.user_id);
assert_eq!(imported_user_profile.username, self.test_data.user.username);
assert_eq!(imported_user_profile.email, self.test_data.user.email);
assert_eq!(
imported_user_profile.email_verified,
self.test_data.user.email_verified
);
assert!(imported_user_profile.bio.is_none());
assert!(imported_user_profile.avatar.is_none());
}

/// Table `torrust_user_profiles`
async fn assert_user_authentication(&self) {
let imported_user_authentication = self
.destiny_database
.get_user_authentication(self.test_data.user.user_id)
.await
.unwrap();

assert_eq!(
imported_user_authentication.user_id,
self.test_data.user.user_id
);
assert_eq!(
imported_user_authentication.password_hash,
self.test_data.user.password
);
}
}

fn hashed_valid_password() -> String {
Expand Down
8 changes: 8 additions & 0 deletions tests/upgrades/from_v1_0_0_to_v2_0_0/upgrader.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
//! You can run this test with:
//!
//! //! ```text
//! cargo test upgrades_data_from_version_v1_0_0_to_v2_0_0
//! ```
//!
//! or:
//!
//! ```text
//! cargo test upgrades_data_from_version_v1_0_0_to_v2_0_0 -- --nocapture
//! ```
//!
//! to see the "upgrader" command output.
use crate::upgrades::from_v1_0_0_to_v2_0_0::sqlite_v1_0_0::SqliteDatabaseV1_0_0;
use crate::upgrades::from_v1_0_0_to_v2_0_0::sqlite_v2_0_0::SqliteDatabaseV2_0_0;
use crate::upgrades::from_v1_0_0_to_v2_0_0::testers::user_data_tester::UserDataTester;
Expand Down

0 comments on commit 8d74e66

Please sign in to comment.