Skip to content

Commit 547ab31

Browse files
zecakehpoljar
authored andcommitted
bonus(sdk): Add more profile tests
Signed-off-by: Kévin Commaille <[email protected]>
1 parent 3f5d51a commit 547ab31

File tree

2 files changed

+101
-2
lines changed

2 files changed

+101
-2
lines changed

crates/matrix-sdk/src/test_utils/mocks/mod.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use ruma::{
3535
DeviceId, EventId, MilliSecondsSinceUnixEpoch, MxcUri, OwnedDeviceId, OwnedEventId,
3636
OwnedOneTimeKeyId, OwnedRoomId, OwnedUserId, RoomId, ServerName, UserId,
3737
api::client::{
38-
profile::ProfileFieldName,
38+
profile::{ProfileFieldName, ProfileFieldValue},
3939
receipt::create_receipt::v3::ReceiptType,
4040
room::Visibility,
4141
sync::sync_events::v5,
@@ -1656,6 +1656,13 @@ impl MatrixMockServer {
16561656
.and(path(format!("/_matrix/client/v3/profile/{user_id}/{field}")));
16571657
self.mock_endpoint(mock, DeleteProfileFieldEndpoint).expect_default_access_token()
16581658
}
1659+
1660+
/// Create a prebuilt mock for the endpoint used to get a profile.
1661+
pub fn mock_get_profile(&self, user_id: &UserId) -> MockEndpoint<'_, GetProfileEndpoint> {
1662+
let mock =
1663+
Mock::given(method("GET")).and(path(format!("/_matrix/client/v3/profile/{user_id}")));
1664+
self.mock_endpoint(mock, GetProfileEndpoint)
1665+
}
16591666
}
16601667

16611668
/// A specification for a push rule ID.
@@ -4731,3 +4738,17 @@ impl<'a> MockEndpoint<'a, DeleteProfileFieldEndpoint> {
47314738
self.ok_empty_json()
47324739
}
47334740
}
4741+
4742+
/// A prebuilt mock for `GET /_matrix/client/*/profile/{user_id}`.
4743+
pub struct GetProfileEndpoint;
4744+
4745+
impl<'a> MockEndpoint<'a, GetProfileEndpoint> {
4746+
/// Returns a successful empty response.
4747+
pub fn ok_with_fields(self, fields: Vec<ProfileFieldValue>) -> MatrixMock<'a> {
4748+
let profile = fields
4749+
.iter()
4750+
.map(|field| (field.field_name(), field.value()))
4751+
.collect::<BTreeMap<_, _>>();
4752+
self.respond_with(ResponseTemplate::new(200).set_body_json(profile))
4753+
}
4754+
}

crates/matrix-sdk/tests/integration/account.rs

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use matrix_sdk_test::async_test;
44
use 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

Comments
 (0)