From 8d74e6683c15b67674a652fc2df05d36a8dfe1fd Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Thu, 10 Nov 2022 12:28:56 +0000 Subject: [PATCH] tests: [#56] for users profile and auth tables in upgrader --- .../from_v1_0_0_to_v2_0_0/sqlite_v2_0_0.rs | 35 +++++++++++++++++ .../testers/user_data_tester.rs | 39 +++++++++++++++++++ .../from_v1_0_0_to_v2_0_0/upgrader.rs | 8 ++++ 3 files changed, 82 insertions(+) diff --git a/tests/upgrades/from_v1_0_0_to_v2_0_0/sqlite_v2_0_0.rs b/tests/upgrades/from_v1_0_0_to_v2_0_0/sqlite_v2_0_0.rs index ba6f4831..87363cea 100644 --- a/tests/upgrades/from_v1_0_0_to_v2_0_0/sqlite_v2_0_0.rs +++ b/tests/upgrades/from_v1_0_0_to_v2_0_0/sqlite_v2_0_0.rs @@ -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, + pub avatar: Option, +} + +#[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, } @@ -34,4 +50,23 @@ impl SqliteDatabaseV2_0_0 { .fetch_one(&self.pool) .await } + + pub async fn get_user_profile(&self, user_id: i64) -> Result { + 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 { + query_as::<_, UserAuthenticationRecordV2>( + "SELECT * FROM torrust_user_authentication WHERE user_id = ?", + ) + .bind(user_id) + .fetch_one(&self.pool) + .await + } } diff --git a/tests/upgrades/from_v1_0_0_to_v2_0_0/testers/user_data_tester.rs b/tests/upgrades/from_v1_0_0_to_v2_0_0/testers/user_data_tester.rs index a83b8077..3f70081c 100644 --- a/tests/upgrades/from_v1_0_0_to_v2_0_0/testers/user_data_tester.rs +++ b/tests/upgrades/from_v1_0_0_to_v2_0_0/testers/user_data_tester.rs @@ -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` @@ -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 { diff --git a/tests/upgrades/from_v1_0_0_to_v2_0_0/upgrader.rs b/tests/upgrades/from_v1_0_0_to_v2_0_0/upgrader.rs index 4e9d4228..b0976944 100644 --- a/tests/upgrades/from_v1_0_0_to_v2_0_0/upgrader.rs +++ b/tests/upgrades/from_v1_0_0_to_v2_0_0/upgrader.rs @@ -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;