@@ -3,8 +3,11 @@ use std::sync::Arc;
33
44use argon2:: password_hash:: SaltString ;
55use argon2:: { Argon2 , PasswordHasher } ;
6+ use async_trait:: async_trait;
67use jsonwebtoken:: { decode, Algorithm , DecodingKey , Validation } ;
78use log:: { debug, info} ;
9+ #[ cfg( test) ]
10+ use mockall:: automock;
811use pbkdf2:: password_hash:: rand_core:: OsRng ;
912
1013use crate :: config:: { Configuration , EmailOnSignup } ;
@@ -26,7 +29,7 @@ fn no_email() -> String {
2629pub struct RegistrationService {
2730 configuration : Arc < Configuration > ,
2831 mailer : Arc < mailer:: Service > ,
29- user_repository : Arc < DbUserRepository > ,
32+ user_repository : Arc < Box < dyn Repository > > ,
3033 user_profile_repository : Arc < DbUserProfileRepository > ,
3134}
3235
@@ -35,7 +38,7 @@ impl RegistrationService {
3538 pub fn new (
3639 configuration : Arc < Configuration > ,
3740 mailer : Arc < mailer:: Service > ,
38- user_repository : Arc < DbUserRepository > ,
41+ user_repository : Arc < Box < dyn Repository > > ,
3942 user_profile_repository : Arc < DbUserProfileRepository > ,
4043 ) -> Self {
4144 Self {
@@ -184,15 +187,15 @@ impl RegistrationService {
184187}
185188
186189pub struct BanService {
187- user_repository : Arc < DbUserRepository > ,
190+ user_repository : Arc < Box < dyn Repository > > ,
188191 user_profile_repository : Arc < DbUserProfileRepository > ,
189192 banned_user_list : Arc < DbBannedUserList > ,
190193}
191194
192195impl BanService {
193196 #[ must_use]
194197 pub fn new (
195- user_repository : Arc < DbUserRepository > ,
198+ user_repository : Arc < Box < dyn Repository > > ,
196199 user_profile_repository : Arc < DbUserProfileRepository > ,
197200 banned_user_list : Arc < DbBannedUserList > ,
198201 ) -> Self {
@@ -233,6 +236,15 @@ impl BanService {
233236 }
234237}
235238
239+ #[ cfg_attr( test, automock) ]
240+ #[ async_trait]
241+ pub trait Repository : Sync + Send {
242+ async fn get_compact ( & self , user_id : & UserId ) -> Result < UserCompact , ServiceError > ;
243+ async fn grant_admin_role ( & self , user_id : & UserId ) -> Result < ( ) , Error > ;
244+ async fn delete ( & self , user_id : & UserId ) -> Result < ( ) , Error > ;
245+ async fn add ( & self , username : & str , email : & str , password_hash : & str ) -> Result < UserId , Error > ;
246+ }
247+
236248pub struct DbUserRepository {
237249 database : Arc < Box < dyn Database > > ,
238250}
@@ -242,13 +254,16 @@ impl DbUserRepository {
242254 pub fn new ( database : Arc < Box < dyn Database > > ) -> Self {
243255 Self { database }
244256 }
257+ }
245258
259+ #[ async_trait]
260+ impl Repository for DbUserRepository {
246261 /// It returns the compact user.
247262 ///
248263 /// # Errors
249264 ///
250265 /// It returns an error if there is a database error.
251- pub async fn get_compact ( & self , user_id : & UserId ) -> Result < UserCompact , ServiceError > {
266+ async fn get_compact ( & self , user_id : & UserId ) -> Result < UserCompact , ServiceError > {
252267 // todo: persistence layer should have its own errors instead of
253268 // returning a `ServiceError`.
254269 self . database
@@ -262,7 +277,7 @@ impl DbUserRepository {
262277 /// # Errors
263278 ///
264279 /// It returns an error if there is a database error.
265- pub async fn grant_admin_role ( & self , user_id : & UserId ) -> Result < ( ) , Error > {
280+ async fn grant_admin_role ( & self , user_id : & UserId ) -> Result < ( ) , Error > {
266281 self . database . grant_admin_role ( * user_id) . await
267282 }
268283
@@ -271,7 +286,7 @@ impl DbUserRepository {
271286 /// # Errors
272287 ///
273288 /// It returns an error if there is a database error.
274- pub async fn delete ( & self , user_id : & UserId ) -> Result < ( ) , Error > {
289+ async fn delete ( & self , user_id : & UserId ) -> Result < ( ) , Error > {
275290 self . database . delete_user ( * user_id) . await
276291 }
277292
@@ -280,7 +295,7 @@ impl DbUserRepository {
280295 /// # Errors
281296 ///
282297 /// It returns an error if there is a database error.
283- pub async fn add ( & self , username : & str , email : & str , password_hash : & str ) -> Result < UserId , Error > {
298+ async fn add ( & self , username : & str , email : & str , password_hash : & str ) -> Result < UserId , Error > {
284299 self . database . insert_user_and_get_id ( username, email, password_hash) . await
285300 }
286301}
0 commit comments