@@ -216,6 +216,50 @@ async fn test_user_repo(pool: PgPool) {
216216 repo. save ( ) . await . unwrap ( ) ;
217217}
218218
219+ /// Test [`UserRepository::find_by_username`] with different casings.
220+ #[ sqlx:: test( migrator = "crate::MIGRATOR" ) ]
221+ async fn test_user_repo_find_by_username ( pool : PgPool ) {
222+ let mut repo = PgRepository :: from_pool ( & pool) . await . unwrap ( ) . boxed ( ) ;
223+ let mut rng = ChaChaRng :: seed_from_u64 ( 42 ) ;
224+ let clock = MockClock :: default ( ) ;
225+
226+ let alice = repo
227+ . user ( )
228+ . add ( & mut rng, & clock, "Alice" . to_owned ( ) )
229+ . await
230+ . unwrap ( ) ;
231+ let bob1 = repo
232+ . user ( )
233+ . add ( & mut rng, & clock, "Bob" . to_owned ( ) )
234+ . await
235+ . unwrap ( ) ;
236+ let bob2 = repo
237+ . user ( )
238+ . add ( & mut rng, & clock, "BOB" . to_owned ( ) )
239+ . await
240+ . unwrap ( ) ;
241+
242+ // This is fine, we can do a case-insensitive search
243+ assert_eq ! (
244+ repo. user( ) . find_by_username( "alice" ) . await . unwrap( ) ,
245+ Some ( alice)
246+ ) ;
247+
248+ // In case there are multiple users with the same username, we should return the
249+ // one that matches the exact casing
250+ assert_eq ! (
251+ repo. user( ) . find_by_username( "Bob" ) . await . unwrap( ) ,
252+ Some ( bob1)
253+ ) ;
254+ assert_eq ! (
255+ repo. user( ) . find_by_username( "BOB" ) . await . unwrap( ) ,
256+ Some ( bob2)
257+ ) ;
258+
259+ // If none match, we should return None
260+ assert ! ( repo. user( ) . find_by_username( "bob" ) . await . unwrap( ) . is_none( ) ) ;
261+ }
262+
219263/// Test the user email repository, by trying out most of its methods
220264#[ sqlx:: test( migrator = "crate::MIGRATOR" ) ]
221265async fn test_user_email_repo ( pool : PgPool ) {
0 commit comments