Skip to content

Commit 8d74e66

Browse files
committed
tests: [#56] for users profile and auth tables in upgrader
1 parent 0a58b6c commit 8d74e66

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

tests/upgrades/from_v1_0_0_to_v2_0_0/sqlite_v2_0_0.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,22 @@ pub struct UserRecordV2 {
1010
pub administrator: bool,
1111
}
1212

13+
#[derive(Debug, Serialize, Deserialize, sqlx::FromRow)]
14+
pub struct UserProfileRecordV2 {
15+
pub user_id: i64,
16+
pub username: String,
17+
pub email: String,
18+
pub email_verified: bool,
19+
pub bio: Option<String>,
20+
pub avatar: Option<String>,
21+
}
22+
23+
#[derive(Debug, Serialize, Deserialize, sqlx::FromRow)]
24+
pub struct UserAuthenticationRecordV2 {
25+
pub user_id: i64,
26+
pub password_hash: String,
27+
}
28+
1329
pub struct SqliteDatabaseV2_0_0 {
1430
pub pool: SqlitePool,
1531
}
@@ -34,4 +50,23 @@ impl SqliteDatabaseV2_0_0 {
3450
.fetch_one(&self.pool)
3551
.await
3652
}
53+
54+
pub async fn get_user_profile(&self, user_id: i64) -> Result<UserProfileRecordV2, sqlx::Error> {
55+
query_as::<_, UserProfileRecordV2>("SELECT * FROM torrust_user_profiles WHERE user_id = ?")
56+
.bind(user_id)
57+
.fetch_one(&self.pool)
58+
.await
59+
}
60+
61+
pub async fn get_user_authentication(
62+
&self,
63+
user_id: i64,
64+
) -> Result<UserAuthenticationRecordV2, sqlx::Error> {
65+
query_as::<_, UserAuthenticationRecordV2>(
66+
"SELECT * FROM torrust_user_authentication WHERE user_id = ?",
67+
)
68+
.bind(user_id)
69+
.fetch_one(&self.pool)
70+
.await
71+
}
3772
}

tests/upgrades/from_v1_0_0_to_v2_0_0/testers/user_data_tester.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ impl UserDataTester {
4949

5050
pub async fn assert(&self) {
5151
self.assert_user().await;
52+
self.assert_user_profile().await;
53+
self.assert_user_authentication().await;
5254
}
5355

5456
/// Table `torrust_users`
@@ -67,6 +69,43 @@ impl UserDataTester {
6769
self.test_data.user.administrator
6870
);
6971
}
72+
73+
/// Table `torrust_user_profiles`
74+
async fn assert_user_profile(&self) {
75+
let imported_user_profile = self
76+
.destiny_database
77+
.get_user_profile(self.test_data.user.user_id)
78+
.await
79+
.unwrap();
80+
81+
assert_eq!(imported_user_profile.user_id, self.test_data.user.user_id);
82+
assert_eq!(imported_user_profile.username, self.test_data.user.username);
83+
assert_eq!(imported_user_profile.email, self.test_data.user.email);
84+
assert_eq!(
85+
imported_user_profile.email_verified,
86+
self.test_data.user.email_verified
87+
);
88+
assert!(imported_user_profile.bio.is_none());
89+
assert!(imported_user_profile.avatar.is_none());
90+
}
91+
92+
/// Table `torrust_user_profiles`
93+
async fn assert_user_authentication(&self) {
94+
let imported_user_authentication = self
95+
.destiny_database
96+
.get_user_authentication(self.test_data.user.user_id)
97+
.await
98+
.unwrap();
99+
100+
assert_eq!(
101+
imported_user_authentication.user_id,
102+
self.test_data.user.user_id
103+
);
104+
assert_eq!(
105+
imported_user_authentication.password_hash,
106+
self.test_data.user.password
107+
);
108+
}
70109
}
71110

72111
fn hashed_valid_password() -> String {

tests/upgrades/from_v1_0_0_to_v2_0_0/upgrader.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
//! You can run this test with:
22
//!
3+
//! //! ```text
4+
//! cargo test upgrades_data_from_version_v1_0_0_to_v2_0_0
5+
//! ```
6+
//!
7+
//! or:
8+
//!
39
//! ```text
410
//! cargo test upgrades_data_from_version_v1_0_0_to_v2_0_0 -- --nocapture
511
//! ```
12+
//!
13+
//! to see the "upgrader" command output.
614
use crate::upgrades::from_v1_0_0_to_v2_0_0::sqlite_v1_0_0::SqliteDatabaseV1_0_0;
715
use crate::upgrades::from_v1_0_0_to_v2_0_0::sqlite_v2_0_0::SqliteDatabaseV2_0_0;
816
use crate::upgrades::from_v1_0_0_to_v2_0_0::testers::user_data_tester::UserDataTester;

0 commit comments

Comments
 (0)