From 119d6abe535cde7284c4968cd32a0a90891d9254 Mon Sep 17 00:00:00 2001 From: "Nathan.fooo" <86001920+appflowy@users.noreply.github.com> Date: Tue, 20 Feb 2024 11:59:44 +0800 Subject: [PATCH] chore: enable collab ac (#333) --- libs/realtime/src/collaborate/metrics.rs | 4 ++-- src/biz/casbin/collab_ac.rs | 22 ++++++++++------------ tests/access_control/collab_ac_test.rs | 17 +++++++++++------ tests/access_control/mod.rs | 9 ++++----- tests/collab/edit_permission.rs | 3 +++ tests/collab/single_device_edit.rs | 2 +- 6 files changed, 31 insertions(+), 26 deletions(-) diff --git a/libs/realtime/src/collaborate/metrics.rs b/libs/realtime/src/collaborate/metrics.rs index dc3176ae..d4207044 100644 --- a/libs/realtime/src/collaborate/metrics.rs +++ b/libs/realtime/src/collaborate/metrics.rs @@ -46,8 +46,8 @@ impl RealtimeMetrics { } pub fn record_mem_cache_usage(&self, size_in_bytes: usize) { - let size_in_mb = size_in_bytes / (1024 * 1024); - trace!("[metrics]: mem_cache_usage: {} MB", size_in_mb); + let size_in_mb = size_in_bytes / 1024; + trace!("[metrics]: mem_cache_usage: {} KB", size_in_mb); self.mem_cache_usage.set(size_in_mb as i64); } diff --git a/src/biz/casbin/collab_ac.rs b/src/biz/casbin/collab_ac.rs index b26918c3..8ef76a9e 100644 --- a/src/biz/casbin/collab_ac.rs +++ b/src/biz/casbin/collab_ac.rs @@ -80,19 +80,17 @@ impl CollabAccessControl for CollabAccessControlImpl { .await } - async fn can_send_collab_update(&self, _uid: &i64, _oid: &str) -> Result { - Ok(true) - // self - // .access_control - // .enforce(uid, &ObjectType::Collab(oid), Action::Write) - // .await + async fn can_send_collab_update(&self, uid: &i64, oid: &str) -> Result { + self + .access_control + .enforce(uid, &ObjectType::Collab(oid), Action::Write) + .await } - async fn can_receive_collab_update(&self, _uid: &i64, _oid: &str) -> Result { - Ok(true) - // self - // .access_control - // .enforce(uid, &ObjectType::Collab(oid), Action::Read) - // .await + async fn can_receive_collab_update(&self, uid: &i64, oid: &str) -> Result { + self + .access_control + .enforce(uid, &ObjectType::Collab(oid), Action::Read) + .await } } diff --git a/tests/access_control/collab_ac_test.rs b/tests/access_control/collab_ac_test.rs index a828cb3a..4e840f04 100644 --- a/tests/access_control/collab_ac_test.rs +++ b/tests/access_control/collab_ac_test.rs @@ -123,7 +123,9 @@ async fn test_collab_access_control_when_obj_not_exist(pool: PgPool) -> anyhow:: let user = create_user(&pool).await?; for method in [Method::GET, Method::POST, Method::PUT, Method::DELETE] { - assert_can_access_http_method(&collab_access_control, &user.uid, "fake_id", method, true).await; + assert_can_access_http_method(&collab_access_control, &user.uid, "fake_id", method, true) + .await + .unwrap(); } Ok(()) @@ -155,7 +157,8 @@ async fn test_collab_access_control_access_http_method(pool: PgPool) -> anyhow:: }], ) .await - .context("adding users to workspace")?; + .context("adding users to workspace") + .unwrap(); for method in [Method::GET, Method::POST, Method::PUT, Method::DELETE] { assert_can_access_http_method( @@ -165,7 +168,8 @@ async fn test_collab_access_control_access_http_method(pool: PgPool) -> anyhow:: method, true, ) - .await; + .await + .unwrap(); } assert!( @@ -183,7 +187,8 @@ async fn test_collab_access_control_access_http_method(pool: PgPool) -> anyhow:: Method::GET, true, ) - .await; + .await + .unwrap(); // guest should not have write access assert_can_access_http_method( @@ -193,7 +198,8 @@ async fn test_collab_access_control_access_http_method(pool: PgPool) -> anyhow:: Method::POST, false, ) - .await; + .await + .unwrap(); assert!( !collab_access_control @@ -249,7 +255,6 @@ async fn test_collab_access_control_send_receive_collab_update(pool: PgPool) -> .context("adding users to workspace")?; // Need to wait for the listener(spawn_listen_on_workspace_member_change) to receive the event - // sleep(Duration::from_secs(2)).await; assert!( diff --git a/tests/access_control/mod.rs b/tests/access_control/mod.rs index ad0998ad..6d8d5b35 100644 --- a/tests/access_control/mod.rs +++ b/tests/access_control/mod.rs @@ -1,5 +1,5 @@ use actix_http::Method; -use anyhow::Context; +use anyhow::{Context, Error}; use app_error::ErrorCode; use appflowy_cloud::biz::casbin::{CollabAccessControlImpl, WorkspaceAccessControlImpl}; use appflowy_cloud::biz::workspace::access_control::WorkspaceAccessControl; @@ -273,7 +273,7 @@ pub async fn assert_can_access_http_method( object_id: &str, method: Method, expected: bool, -) { +) -> Result<(), Error> { let timeout_duration = Duration::from_secs(10); let retry_interval = Duration::from_millis(300); let mut retries = 0usize; @@ -304,7 +304,6 @@ pub async fn assert_can_access_http_method( } }; - timeout(timeout_duration, operation) - .await - .expect("Operation timed out"); + timeout(timeout_duration, operation).await?; + Ok(()) } diff --git a/tests/collab/edit_permission.rs b/tests/collab/edit_permission.rs index 277f1138..e853536d 100644 --- a/tests/collab/edit_permission.rs +++ b/tests/collab/edit_permission.rs @@ -434,6 +434,9 @@ async fn multiple_user_with_read_and_write_permission_edit_same_collab_test() { expected_json.insert(index.to_string(), s); } + // wait 5 seconds to make sure all the server broadcast the updates to all the clients + sleep(Duration::from_secs(5)).await; + // all the clients should have the same collab object assert_json_include!( actual: json!(expected_json), diff --git a/tests/collab/single_device_edit.rs b/tests/collab/single_device_edit.rs index c235ec40..cfc35be4 100644 --- a/tests/collab/single_device_edit.rs +++ b/tests/collab/single_device_edit.rs @@ -467,7 +467,7 @@ async fn post_realtime_message_test() { let task = tokio::spawn(async move { let mut new_user = TestClient::new_user().await; // sleep 2 secs to make sure it do not trigger register user too fast in gotrue - sleep(Duration::from_secs(i % 3)).await; + sleep(Duration::from_secs(i % 5)).await; let object_id = Uuid::new_v4().to_string(); let workspace_id = new_user.workspace_id().await;