@@ -4,7 +4,7 @@ use matrix_sdk_test::async_test;
44use ruma:: {
55 api:: {
66 MatrixVersion ,
7- client:: profile:: { ProfileFieldName , ProfileFieldValue , TimeZone } ,
7+ client:: profile:: { AvatarUrl , DisplayName , ProfileFieldName , ProfileFieldValue , TimeZone } ,
88 } ,
99 mxc_uri,
1010} ;
@@ -220,3 +220,81 @@ async fn test_delete_profile_field() {
220220 account. delete_profile_field ( ProfileFieldName :: TimeZone ) . await . unwrap ( ) ;
221221 }
222222}
223+
224+ #[ async_test]
225+ async fn test_fetch_user_profile ( ) {
226+ let tz = "Africa/Bujumbura" ;
227+ let display_name = "Alice" ;
228+
229+ let server = MatrixMockServer :: new ( ) . await ;
230+ let client = server. client_builder ( ) . build ( ) . await ;
231+ let user_id = client. user_id ( ) . unwrap ( ) ;
232+
233+ server
234+ . mock_get_profile ( user_id)
235+ . ok_with_fields ( vec ! [
236+ ProfileFieldValue :: TimeZone ( tz. to_owned( ) ) ,
237+ ProfileFieldValue :: DisplayName ( display_name. to_owned( ) ) ,
238+ ] )
239+ . mock_once ( )
240+ . named ( "get profile" )
241+ . mount ( )
242+ . await ;
243+
244+ let profile = client. account ( ) . fetch_user_profile ( ) . await . unwrap ( ) ;
245+
246+ assert_eq ! ( profile. get_static:: <TimeZone >( ) . unwrap( ) . as_deref( ) , Some ( tz) ) ;
247+ assert_eq ! ( profile. get_static:: <DisplayName >( ) . unwrap( ) . as_deref( ) , Some ( display_name) ) ;
248+ assert_eq ! ( profile. get_static:: <AvatarUrl >( ) . unwrap( ) , None ) ;
249+ }
250+
251+ #[ async_test]
252+ async fn test_get_cached_avatar_url ( ) {
253+ let avatar_url = mxc_uri ! ( "mxc://localhost/1mA63" ) ;
254+
255+ let server = MatrixMockServer :: new ( ) . await ;
256+ let client = server. client_builder ( ) . build ( ) . await ;
257+ let user_id = client. user_id ( ) . unwrap ( ) ;
258+
259+ let account = client. account ( ) ;
260+
261+ // The cache is empty.
262+ let res_avatar_url = account. get_cached_avatar_url ( ) . await . unwrap ( ) ;
263+ assert_eq ! ( res_avatar_url, None ) ;
264+
265+ // Fetch it from the homeserver, it should fill the cache.
266+ {
267+ let _guard = server
268+ . mock_get_profile_field ( user_id, ProfileFieldName :: AvatarUrl )
269+ . ok_with_value ( Some ( avatar_url. as_str ( ) . into ( ) ) )
270+ . mock_once ( )
271+ . named ( "get avatar_url profile field with value" )
272+ . mount_as_scoped ( )
273+ . await ;
274+
275+ let res_avatar_url = account. get_avatar_url ( ) . await . unwrap ( ) ;
276+ assert_eq ! ( res_avatar_url. as_deref( ) , Some ( avatar_url) ) ;
277+ }
278+
279+ // The cache was filled.
280+ let res_avatar_url = account. get_cached_avatar_url ( ) . await . unwrap ( ) ;
281+ assert_eq ! ( res_avatar_url. as_deref( ) , Some ( avatar_url) ) ;
282+
283+ // Fetch it again from the homeserver, a missing value should empty the cache.
284+ {
285+ let _guard = server
286+ . mock_get_profile_field ( user_id, ProfileFieldName :: AvatarUrl )
287+ . ok_with_value ( None )
288+ . mock_once ( )
289+ . named ( "get avatar_url profile field without value" )
290+ . mount_as_scoped ( )
291+ . await ;
292+
293+ let res_avatar_url = account. get_avatar_url ( ) . await . unwrap ( ) ;
294+ assert_eq ! ( res_avatar_url, None ) ;
295+ }
296+
297+ // The cache was emptied.
298+ let res_avatar_url = account. get_cached_avatar_url ( ) . await . unwrap ( ) ;
299+ assert_eq ! ( res_avatar_url, None ) ;
300+ }
0 commit comments